diff --git a/ethz_iam_webservice/group.py b/ethz_iam_webservice/group.py index ae5e92d5cdf93f27f849c8df4e2faa2689b4d0ee..e85d0aa9ecf982563346bf79f33c854513af2aa3 100644 --- a/ethz_iam_webservice/group.py +++ b/ethz_iam_webservice/group.py @@ -202,19 +202,19 @@ class Group(IAMApi): payload = {} if new_name and new_name != self.name: payload["newName"] = new_name - if description is not None and description != self.description: + if description: payload["newDescription"] = description - if managers != self.managers: + if managers: payload["newGroupManager"] = managers if group_ad_ou: payload["newGroupADOU"] = group_ad_ou - if certification_period and certification_period != self.certification_period: + if certification_period: payload["newCertPeriod"] = certification_period if certification_period == RecertificationPeriod.NONE.value: payload["newCertNote"] = ( certification_note or "no recertification needed" ) - if certification_note and certification_note != self.certification_note: + if certification_note: payload["newCertNote"] = certification_note payload["newCertPeriod"] = RecertificationPeriod.NONE.value if not payload: @@ -295,7 +295,7 @@ class Group(IAMApi): else: target_string = targets[0].upper() endpoint = f"/groups/{self.name}/targetsystems/{target_string}" - data = self.put_request(endpoint, {}) + self.put_request(endpoint, {}) if target_string == "ALL": self.targets = ["AD", "LDAP"] else: diff --git a/ethz_iam_webservice/main.py b/ethz_iam_webservice/main.py index ef717e2190c88d6b622fe8599e7a9d43ed08b84f..dd2c235d224c576103d731ef15abc3f22189cf76 100644 --- a/ethz_iam_webservice/main.py +++ b/ethz_iam_webservice/main.py @@ -345,12 +345,20 @@ def manage_group( followed by a dash, e.g. agrl-xxxx """ iam = login(credentials) - try: - group = iam.get_group(name) - except ValueError: - group = None - except ConnError as exc: - raise ClickException(exc) from exc + + def get_group(name): + try: + group = iam.get_group(name) + except ValueError as exc: + raise ClickException( + f"No group found with name {name}. Use --new if you want to create a new group." + ) from exc + except ConnError as exc: + raise ClickException(exc) from exc + return group + + group = iam.group + group.name = name if certification_period: if certification_period.upper() not in recertification_period_map: @@ -363,8 +371,6 @@ def manage_group( if new: if certification_period is None: certification_period = RecertificationPeriod.NONE.value - if group: - raise ClickException(f"A group named {name} already exists.") if not agroup: raise ClickException("Please provide an admingroup with --agroup") if not description: @@ -390,17 +396,15 @@ def manage_group( ) from exc elif delete: group.delete() - click.echo(f"Successfully deleted {name}") + click.echo(f"Successfully deleted group {name}") return elif recertify: group.recertify() click.echo(f"Group {name} successfully recertified.") elif update: - if not group: - raise ClickException(f"No group {name} exists.") try: new_group = group.update( - current_name=group.name, + current_name=name, new_name=new_name, description=description, group_ad_ou=organizational_unit, @@ -412,11 +416,6 @@ def manage_group( except ValueError as exc: raise ClickException(exc) from exc - if not group: - raise ClickException( - f"No group found with name {name}. Use --new if you want to create a new group." - ) - if add or add_subgroup: group.add_members(users=add, subgroups=add_subgroup) if remove or remove_subgroup: @@ -439,6 +438,8 @@ def manage_group( pass else: raise ClickException(exc) from exc + if not group.mod_date: + group = get_group(name=name) print(json.dumps(asdict(group), indent=4, sort_keys=True))