From 36c03c2fb6c1519f3f48e57a4b07612745a30469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Marten?= Date: Thu, 15 Aug 2024 09:37:27 +0000 Subject: [PATCH 1/2] feat: add possiblity to set username (equivalent to -Name argument in PS) --- .editorconfig | 2 +- ad/data_source_ad_user.go | 6 ++++++ ad/internal/winrmhelper/winrm_user.go | 13 ++++++++++--- ad/resource_ad_user.go | 7 +++++++ ad/resource_ad_user_test.go | 1 + 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.editorconfig b/.editorconfig index ebe51d3b..2593d1ad 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,7 +4,7 @@ root = true [*] -indent_style = space +indent_style = tab indent_size = 2 end_of_line = lf charset = utf-8 diff --git a/ad/data_source_ad_user.go b/ad/data_source_ad_user.go index 4c4bcd96..c868eac3 100644 --- a/ad/data_source_ad_user.go +++ b/ad/data_source_ad_user.go @@ -185,6 +185,11 @@ func dataSourceADUser() *schema.Resource { Computed: true, Description: "Check if user is trusted for delegation", }, + "username": { + Type: schema.TypeString, + Computed: true, + Description: "Username of the user object.", + }, "dn": { Type: schema.TypeString, Computed: true, @@ -239,6 +244,7 @@ func dataSourceADUserRead(d *schema.ResourceData, meta interface{}) error { _ = d.Set("title", u.Title) _ = d.Set("smart_card_logon_required", u.SmartcardLogonRequired) _ = d.Set("trusted_for_delegation", u.TrustedForDelegation) + _ = d.Set("username", u.Username) _ = d.Set("user_id", userID) d.SetId(u.GUID) diff --git a/ad/internal/winrmhelper/winrm_user.go b/ad/internal/winrmhelper/winrm_user.go index 6d68c71a..498cfd56 100644 --- a/ad/internal/winrmhelper/winrm_user.go +++ b/ad/internal/winrmhelper/winrm_user.go @@ -56,7 +56,7 @@ type User struct { Password string Container string Domain string - Username string + Username string `json:"Name"` PasswordNeverExpires bool CannotChangePassword bool CustomAttributes map[string]interface{} @@ -278,6 +278,7 @@ func (u *User) ModifyUser(d *schema.ResourceData, conf *config.ProviderConf) err "street_address": "StreetAddress", "surname": "Surname", "title": "Title", + "username": "Username", } cmds := []string{fmt.Sprintf("Set-ADUser -Identity %q", u.GUID)} @@ -540,10 +541,14 @@ func GetUserFromResource(d *schema.ResourceData) (*User, error) { Surname: SanitiseTFInput(d, "surname"), Title: SanitiseTFInput(d, "title"), TrustedForDelegation: d.Get("trusted_for_delegation").(bool), + Username: SanitiseTFInput(d, "username"), } if user.PrincipalName != "" { tokens := strings.Split(user.PrincipalName, "@") - user.Username = tokens[0] + // set user.Username to the first token in the split if it user.Username is empty + if user.Username == "" { + user.Username = tokens[0] + } if len(tokens) > 1 { user.Domain = tokens[1] } @@ -608,7 +613,9 @@ func unmarshallUser(input []byte, customAttributes []string) (*User, error) { } if user.PrincipalName != "" { tokens := strings.Split(user.PrincipalName, "@") - user.Username = tokens[0] + if user.Username == "" { + user.Username = tokens[0] + } if len(tokens) > 1 { user.Domain = tokens[1] } diff --git a/ad/resource_ad_user.go b/ad/resource_ad_user.go index 49612b21..204c9e05 100644 --- a/ad/resource_ad_user.go +++ b/ad/resource_ad_user.go @@ -219,6 +219,12 @@ func resourceADUser() *schema.Resource { Default: false, Description: "If set to true, the user account is trusted for Kerberos delegation. A service that runs under an account that is trusted for Kerberos delegation can assume the identity of a client requesting the service. This parameter sets the TrustedForDelegation property of an account object.", }, + "username": { + Type: schema.TypeString, + Optional: true, + Default: false, + Description: "Set a username for the user. This parameter is the equivalent to the -Name parameter of New-ADUser cmdlet.", + }, "custom_attributes": { Type: schema.TypeString, Optional: true, @@ -342,6 +348,7 @@ func resourceADUserRead(d *schema.ResourceData, meta interface{}) error { _ = d.Set("title", u.Title) _ = d.Set("smart_card_logon_required", u.SmartcardLogonRequired) _ = d.Set("trusted_for_delegation", u.TrustedForDelegation) + _ = d.Set("username", u.Username) if u.CustomAttributes != nil { ca, err := structure.FlattenJsonToString(u.CustomAttributes) diff --git a/ad/resource_ad_user_test.go b/ad/resource_ad_user_test.go index b3232d57..7cef0606 100644 --- a/ad/resource_ad_user_test.go +++ b/ad/resource_ad_user_test.go @@ -306,6 +306,7 @@ func testAccResourceADUserConfigAttributes() string { street_address = "StreetAddress" surname = "Surname" title = "Title" + username = "Username" smart_card_logon_required = false trusted_for_delegation = true }`, defaultVariablesSection(), defaultUserSection("", fmt.Sprintf("%q", From 23d75239f90510394c13befa4fe688cf6094c390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Marten?= Date: Wed, 11 Sep 2024 14:04:01 +0200 Subject: [PATCH 2/2] fix: CS --- ad/internal/winrmhelper/winrm_user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ad/internal/winrmhelper/winrm_user.go b/ad/internal/winrmhelper/winrm_user.go index 498cfd56..fa3acde1 100644 --- a/ad/internal/winrmhelper/winrm_user.go +++ b/ad/internal/winrmhelper/winrm_user.go @@ -615,7 +615,7 @@ func unmarshallUser(input []byte, customAttributes []string) (*User, error) { tokens := strings.Split(user.PrincipalName, "@") if user.Username == "" { user.Username = tokens[0] - } + } if len(tokens) > 1 { user.Domain = tokens[1] }