summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorRob van der Linde <rob@catalyst.net.nz>2024-03-06 16:47:29 +1300
committerAndrew Bartlett <abartlet@samba.org>2024-03-20 03:49:34 +0000
commit200948c172d20de75a3598d244de3f47d91d7bc0 (patch)
tree43bc2706c3d227e34b007ba23bd21e12f5ac3dbc /python
parentbd79c074e2ddbf434c70f6d2692dd702917309ce (diff)
downloadsamba-200948c172d20de75a3598d244de3f47d91d7bc0.tar.gz
samba-200948c172d20de75a3598d244de3f47d91d7bc0.tar.bz2
samba-200948c172d20de75a3598d244de3f47d91d7bc0.zip
netcmd: models: improve Computer constructor adding "$" handling
In some cases the previous code would end up creating computers where the account name ended on double "$" Rewrote constructor to handle more cases, for example only an account name is provided, only a name is provided, or both. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'python')
-rw-r--r--python/samba/netcmd/domain/models/computer.py37
1 files changed, 29 insertions, 8 deletions
diff --git a/python/samba/netcmd/domain/models/computer.py b/python/samba/netcmd/domain/models/computer.py
index c9e034a530f..84dddb16a9b 100644
--- a/python/samba/netcmd/domain/models/computer.py
+++ b/python/samba/netcmd/domain/models/computer.py
@@ -33,18 +33,39 @@ class Computer(User):
def __init__(self, **kwargs):
"""Computer constructor automatically adds "$" to account_name.
- Also applies to GroupManagedServiceAccount subclass.
- """
- name = kwargs.get("name", kwargs.get("cn"))
+ The various ways a Computer can be constructed:
+
+ >>> Computer(name="pc")
+ >>> Computer(account_name="pc$")
+ >>> Computer(cn="pc")
+ >>> Computer(account_name="pc$", name="pc")
+
+ In each case the constructor does its best to ensure the
+ account name ends with a "$" and the name doesn't.
+
+ Also applies to GroupManagedServiceAccount subclass."""
+ name = kwargs.get("name", kwargs.pop("cn", None))
account_name = kwargs.get("account_name")
- # If account_name is missing, use name or cn and add a "$".
- # If account_name is present but lacking "$", add it automatically.
+ # First make sure the account_name always has a "$".
+ if account_name and not account_name.endswith("$"):
+ account_name += "$"
+
+ # The name is present but not account name.
+ # If the name already has a "$" don't add two.
if name and not account_name:
- kwargs["account_name"] = name + "$"
- elif account_name and not account_name.endswith("$"):
- kwargs["account_name"] = account_name + "$"
+ if name.endswith("$"):
+ account_name = name
+ else:
+ account_name = name + "$"
+
+ # The account name is present but not the name.
+ # Use the account name, stripping the "$" character.
+ elif account_name and not name:
+ name = account_name.rstrip("$")
+ kwargs["name"] = name
+ kwargs["account_name"] = account_name
super().__init__(**kwargs)
@staticmethod