diff options
| author | Rob van der Linde <rob@catalyst.net.nz> | 2023-06-23 12:52:58 +1200 |
|---|---|---|
| committer | Andrew Bartlett <abartlet@samba.org> | 2023-06-25 23:29:32 +0000 |
| commit | 0cfa7f6cff978041665d8688567077a71fb32cc6 (patch) | |
| tree | dc824bc2450e3f5aa57e9362c708d9b507f97225 /python | |
| parent | 76ca95db6bce16d8b01a5f9b9be84e1061953060 (diff) | |
| download | samba-0cfa7f6cff978041665d8688567077a71fb32cc6.tar.gz samba-0cfa7f6cff978041665d8688567077a71fb32cc6.tar.bz2 samba-0cfa7f6cff978041665d8688567077a71fb32cc6.zip | |
netcmd: domain: add error handling to domain claims commands
Similar to the auth commands commit prior to this.
Where we wre catching LdbError before we now catch ModelError, all
exceptions that are known and handled in the model layer will have a
user-friendly error message.
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Diffstat (limited to 'python')
| -rw-r--r-- | python/samba/netcmd/domain/claim/claim_type.py | 43 | ||||
| -rw-r--r-- | python/samba/netcmd/domain/claim/value_type.py | 14 |
2 files changed, 42 insertions, 15 deletions
diff --git a/python/samba/netcmd/domain/claim/claim_type.py b/python/samba/netcmd/domain/claim/claim_type.py index b4a114c8c7d..e80c4ca82fa 100644 --- a/python/samba/netcmd/domain/claim/claim_type.py +++ b/python/samba/netcmd/domain/claim/claim_type.py @@ -24,10 +24,10 @@ import binascii import os import samba.getopt as options -from ldb import LdbError from samba.netcmd import Command, CommandError, Option, SuperCommand from samba.netcmd.domain.models import AttributeSchema, ClassSchema,\ ClaimType, ValueType +from samba.netcmd.domain.models.exceptions import ModelError class cmd_domain_claim_claim_type_create(Command): @@ -82,10 +82,14 @@ class cmd_domain_claim_claim_type_create(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) + display_name = name or attribute_name + try: + claim_type = ClaimType.get(ldb, display_name=display_name) + except ModelError as e: + raise CommandError(e) + # Check if a claim type with this display name already exists. # Note: you can register the same claim type under another display name. - display_name = name or attribute_name - claim_type = ClaimType.get(ldb, display_name=display_name) if claim_type: raise CommandError(f"Claim type {display_name} already exists, " "but you can use --name to use another name.") @@ -95,7 +99,7 @@ class cmd_domain_claim_claim_type_create(Command): applies_to = [ClassSchema.lookup(ldb, name) for name in class_names] attribute = AttributeSchema.lookup(ldb, attribute_name) value_type = ValueType.lookup(ldb, attribute) - except (LookupError, ValueError) as e: + except (LookupError, ModelError, ValueError) as e: raise CommandError(e) # Generate the new Claim Type cn. @@ -136,7 +140,7 @@ class cmd_domain_claim_claim_type_create(Command): if protect: claim_type.protect(ldb) - except LdbError as e: + except ModelError as e: raise CommandError(e) # Claim type created successfully. @@ -193,8 +197,12 @@ class cmd_domain_claim_claim_type_modify(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) + try: + claim_type = ClaimType.get(ldb, display_name=name) + except ModelError as e: + raise CommandError(e) + # Check if claim type exists. - claim_type = ClaimType.get(ldb, display_name=name) if not claim_type: raise CommandError(f"Claim type {name} not found.") @@ -226,7 +234,7 @@ class cmd_domain_claim_claim_type_modify(Command): claim_type.protect(ldb) elif unprotect: claim_type.unprotect(ldb) - except LdbError as e: + except ModelError as e: raise CommandError(e) # Claim type updated successfully. @@ -260,8 +268,12 @@ class cmd_domain_claim_claim_type_delete(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) + try: + claim_type = ClaimType.get(ldb, display_name=name) + except ModelError as e: + raise CommandError(e) + # Check if claim type exists first. - claim_type = ClaimType.get(ldb, display_name=name) if claim_type is None: raise CommandError(f"Claim type {name} not found.") @@ -271,7 +283,7 @@ class cmd_domain_claim_claim_type_delete(Command): claim_type.unprotect(ldb) claim_type.delete(ldb) - except LdbError as e: + except ModelError as e: if not force: raise CommandError( f"{e}\nTry --force to delete protected claim types.") @@ -305,8 +317,11 @@ class cmd_domain_claim_claim_type_list(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) # Claim types grouped by displayName. - claim_types = {claim_type.display_name: claim_type.as_dict() - for claim_type in ClaimType.query(ldb)} + try: + claim_types = {claim_type.display_name: claim_type.as_dict() + for claim_type in ClaimType.query(ldb)} + except ModelError as e: + raise CommandError(e) # Using json output format gives more detail. if output_format == "json": @@ -340,8 +355,12 @@ class cmd_domain_claim_claim_type_view(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) + try: + claim_type = ClaimType.get(ldb, display_name=name) + except ModelError as e: + raise CommandError(e) + # Check if claim type exists first. - claim_type = ClaimType.get(ldb, display_name=name) if claim_type is None: raise CommandError(f"Claim type {name} not found.") diff --git a/python/samba/netcmd/domain/claim/value_type.py b/python/samba/netcmd/domain/claim/value_type.py index 3118e20c387..307bd6cc07c 100644 --- a/python/samba/netcmd/domain/claim/value_type.py +++ b/python/samba/netcmd/domain/claim/value_type.py @@ -23,6 +23,7 @@ import samba.getopt as options from samba.netcmd import Command, CommandError, Option, SuperCommand from samba.netcmd.domain.models import ValueType +from samba.netcmd.domain.models.exceptions import ModelError class cmd_domain_claim_value_type_list(Command): @@ -48,8 +49,11 @@ class cmd_domain_claim_value_type_list(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) # Value types grouped by display name. - value_types = {value_type.display_name: value_type.as_dict() - for value_type in ValueType.query(ldb)} + try: + value_types = {value_type.display_name: value_type.as_dict() + for value_type in ValueType.query(ldb)} + except ModelError as e: + raise CommandError(e) # Using json output format gives more detail. if output_format == "json": @@ -84,8 +88,12 @@ class cmd_domain_claim_value_type_view(Command): ldb = self.ldb_connect(ldap_url, sambaopts, credopts) + try: + value_type = ValueType.get(ldb, display_name=name) + except ModelError as e: + raise CommandError(e) + # Check if value type exists first. - value_type = ValueType.get(ldb, display_name=name) if value_type is None: raise CommandError(f"Value type {name} not found.") |
