Skip to content

Commit

Permalink
Merge pull request #2 from netresearch/username
Browse files Browse the repository at this point in the history
feat: add possiblity to set username
  • Loading branch information
nela-rese authored Sep 12, 2024
2 parents 97879bb + 23d7523 commit 90deee3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
root = true

[*]
indent_style = space
indent_style = tab
indent_size = 2
end_of_line = lf
charset = utf-8
Expand Down
6 changes: 6 additions & 0 deletions ad/data_source_ad_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
13 changes: 10 additions & 3 deletions ad/internal/winrmhelper/winrm_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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]
}
Expand Down Expand Up @@ -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]
}
Expand Down
7 changes: 7 additions & 0 deletions ad/resource_ad_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ad/resource_ad_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 90deee3

Please sign in to comment.