Skip to content

Commit

Permalink
Add support for a few additional users attributes (#597)
Browse files Browse the repository at this point in the history
* Add suppor for a new user attributes - employeeType, costCenter and division

* code review fixes

* fix tf formatting

* Tidy up duplicate fields

Co-authored-by: Tom Bamford <tom@bamford.io>
  • Loading branch information
michaljirman and manicminer authored Sep 30, 2021
1 parent 5b3f5b0 commit ebb686b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 32 deletions.
3 changes: 3 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ The following attributes are exported:
* `company_name` - The company name which the user is associated. This property can be useful for describing the company that an external user comes from.
* `consent_provided_for_minor` - Whether consent has been obtained for minors. Supported values are `Granted`, `Denied` and `NotRequired`.
* `country` - The country/region in which the user is located, e.g. `US` or `UK`.
* `cost_center` - The cost center associated with the user.
* `creation_type` - Indicates whether the user account was created as a regular school or work account (`null`), an external account (`Invitation`), a local account for an Azure Active Directory B2C tenant (`LocalAccount`) or self-service sign-up using email verification (`EmailVerified`).
* `department` - The name for the department in which the user works.
* `display_name` - The display name of the user.
* `division` - The name of the division in which the user works.
* `employee_id` - The employee identifier assigned to the user by the organisation.
* `employee_type` - Captures enterprise worker type. For example, Employee, Contractor, Consultant, or Vendor.
* `external_user_state` - For an external user invited to the tenant, this property represents the invited user's invitation status. Possible values are `PendingAcceptance` or `Accepted`.
* `fax_number` - The fax number of the user.
* `given_name` - The given name (first name) of the user.
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ The following arguments are supported:
* `city` - (Optional) The city in which the user is located.
* `company_name` - (Optional) The company name which the user is associated. This property can be useful for describing the company that an external user comes from.
* `consent_provided_for_minor` - (Optional) Whether consent has been obtained for minors. Supported values are `Granted`, `Denied` and `NotRequired`. Omit this property or specify a blank string to unset.
* `cost_center` - (Optional) The cost center associated with the user.
* `country` - (Optional) The country/region in which the user is located, e.g. `US` or `UK`.
* `department` - (Optional) The name for the department in which the user works.
* `disable_password_expiration` - (Optional) Whether the users password is exempt from expiring. Defaults to `false`.
* `disable_strong_password` - (Optional) Whether the user is allowed weaker passwords than the default policy to be specified. Defaults to `false`.
* `display_name` - (Required) The name to display in the address book for the user.
* `division` - (Optional) The name of the division in which the user works.
* `employee_id` - (Optional) The employee identifier assigned to the user by the organisation.
* `employee_type` - (Optional) Captures enterprise worker type. For example, Employee, Contractor, Consultant, or Vendor.
* `fax_number` - (Optional) The fax number of the user.
* `force_password_change` - (Optional) Whether the user is forced to change the password during the next sign-in. Only takes effect when also changing the password. Defaults to `false`.
* `given_name` - (Optional) The given name (first name) of the user.
Expand Down
24 changes: 24 additions & 0 deletions internal/services/users/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func userDataSource() *schema.Resource {
Computed: true,
},

"cost_center": {
Description: "The cost center associated with the user.",
Type: schema.TypeString,
Computed: true,
},

"country": {
Description: "The country/region in which the user is located, e.g. `US` or `UK`",
Type: schema.TypeString,
Expand All @@ -116,12 +122,24 @@ func userDataSource() *schema.Resource {
Computed: true,
},

"division": {
Description: "The name of the division in which the user works.",
Type: schema.TypeString,
Computed: true,
},

"employee_id": {
Description: "The employee identifier assigned to the user by the organisation",
Type: schema.TypeString,
Computed: true,
},

"employee_type": {
Description: "Captures enterprise worker type. For example, Employee, Contractor, Consultant, or Vendor.",
Type: schema.TypeString,
Computed: true,
},

"external_user_state": {
Description: "For an external user invited to the tenant, this property represents the invited user's invitation status",
Type: schema.TypeString,
Expand Down Expand Up @@ -359,6 +377,7 @@ func userDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interf
tf.Set(d, "department", user.Department)
tf.Set(d, "display_name", user.DisplayName)
tf.Set(d, "employee_id", user.EmployeeId)
tf.Set(d, "employee_type", user.EmployeeType)
tf.Set(d, "external_user_state", user.ExternalUserState)
tf.Set(d, "fax_number", user.FaxNumber)
tf.Set(d, "given_name", user.GivenName)
Expand Down Expand Up @@ -388,5 +407,10 @@ func userDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interf
tf.Set(d, "user_principal_name", user.UserPrincipalName)
tf.Set(d, "user_type", user.UserType)

if user.EmployeeOrgData != nil {
tf.Set(d, "cost_center", user.EmployeeOrgData.CostCenter)
tf.Set(d, "division", user.EmployeeOrgData.Division)
}

return nil
}
99 changes: 67 additions & 32 deletions internal/services/users/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func userResource() *schema.Resource {
}, false),
},

"cost_center": {
Description: "The cost center associated with the user.",
Type: schema.TypeString,
Optional: true,
},

"country": {
Description: "The country/region in which the user is located, e.g. `US` or `UK`",
Type: schema.TypeString,
Expand All @@ -126,13 +132,26 @@ func userResource() *schema.Resource {
Optional: true,
},

"division": {
Description: "The name of the division in which the user works.",
Type: schema.TypeString,
Optional: true,
},

"employee_id": {
Description: "The employee identifier assigned to the user by the organisation",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 16),
},

"employee_type": {
Description: "Captures enterprise worker type. For example, Employee, Contractor, Consultant, or Vendor.",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"Employee", "Contractor", "Consultant", "Vendor"}, false),
},

"force_password_change": {
Description: "Whether the user is forced to change the password during the next sign-in. Only takes effect when also changing the password",
Type: schema.TypeBool,
Expand Down Expand Up @@ -402,23 +421,28 @@ func userResourceCreate(ctx context.Context, d *schema.ResourceData, meta interf
Department: utils.NullableString(d.Get("department").(string)),
DisplayName: utils.String(d.Get("display_name").(string)),
EmployeeId: utils.NullableString(d.Get("employee_id").(string)),
FaxNumber: utils.NullableString(d.Get("fax_number").(string)),
GivenName: utils.NullableString(d.Get("given_name").(string)),
JobTitle: utils.NullableString(d.Get("job_title").(string)),
Mail: utils.NullableString(d.Get("mail").(string)),
MailNickname: utils.String(mailNickName),
MobilePhone: utils.NullableString(d.Get("mobile_phone").(string)),
OfficeLocation: utils.NullableString(d.Get("office_location").(string)),
OtherMails: tf.ExpandStringSlicePtr(d.Get("other_mails").(*schema.Set).List()),
PasswordPolicies: utils.NullableString(passwordPolicies),
PostalCode: utils.NullableString(d.Get("postal_code").(string)),
PreferredLanguage: utils.NullableString(d.Get("preferred_language").(string)),
ShowInAddressList: utils.Bool(d.Get("show_in_address_list").(bool)),
State: utils.NullableString(d.Get("state").(string)),
StreetAddress: utils.NullableString(d.Get("street_address").(string)),
Surname: utils.NullableString(d.Get("surname").(string)),
UsageLocation: utils.NullableString(d.Get("usage_location").(string)),
UserPrincipalName: utils.String(upn),
EmployeeOrgData: &msgraph.EmployeeOrgData{
CostCenter: utils.String(d.Get("cost_center").(string)),
Division: utils.String(d.Get("division").(string)),
},
EmployeeType: utils.NullableString(d.Get("employee_type").(string)),
FaxNumber: utils.NullableString(d.Get("fax_number").(string)),
GivenName: utils.NullableString(d.Get("given_name").(string)),
JobTitle: utils.NullableString(d.Get("job_title").(string)),
Mail: utils.NullableString(d.Get("mail").(string)),
MailNickname: utils.String(mailNickName),
MobilePhone: utils.NullableString(d.Get("mobile_phone").(string)),
OfficeLocation: utils.NullableString(d.Get("office_location").(string)),
OtherMails: tf.ExpandStringSlicePtr(d.Get("other_mails").(*schema.Set).List()),
PasswordPolicies: utils.NullableString(passwordPolicies),
PostalCode: utils.NullableString(d.Get("postal_code").(string)),
PreferredLanguage: utils.NullableString(d.Get("preferred_language").(string)),
ShowInAddressList: utils.Bool(d.Get("show_in_address_list").(bool)),
State: utils.NullableString(d.Get("state").(string)),
StreetAddress: utils.NullableString(d.Get("street_address").(string)),
Surname: utils.NullableString(d.Get("surname").(string)),
UsageLocation: utils.NullableString(d.Get("usage_location").(string)),
UserPrincipalName: utils.String(upn),

PasswordProfile: &msgraph.UserPasswordProfile{
ForceChangePasswordNextSignIn: utils.Bool(d.Get("force_password_change").(bool)),
Expand Down Expand Up @@ -476,21 +500,26 @@ func userResourceUpdate(ctx context.Context, d *schema.ResourceData, meta interf
Department: utils.NullableString(d.Get("department").(string)),
DisplayName: utils.String(d.Get("display_name").(string)),
EmployeeId: utils.NullableString(d.Get("employee_id").(string)),
FaxNumber: utils.NullableString(d.Get("fax_number").(string)),
GivenName: utils.NullableString(d.Get("given_name").(string)),
JobTitle: utils.NullableString(d.Get("job_title").(string)),
MailNickname: utils.String(d.Get("mail_nickname").(string)),
MobilePhone: utils.NullableString(d.Get("mobile_phone").(string)),
OfficeLocation: utils.NullableString(d.Get("office_location").(string)),
OtherMails: tf.ExpandStringSlicePtr(d.Get("other_mails").(*schema.Set).List()),
PasswordPolicies: utils.NullableString(passwordPolicies),
PostalCode: utils.NullableString(d.Get("postal_code").(string)),
PreferredLanguage: utils.NullableString(d.Get("preferred_language").(string)),
ShowInAddressList: utils.Bool(d.Get("show_in_address_list").(bool)),
State: utils.NullableString(d.Get("state").(string)),
StreetAddress: utils.NullableString(d.Get("street_address").(string)),
Surname: utils.NullableString(d.Get("surname").(string)),
UsageLocation: utils.NullableString(d.Get("usage_location").(string)),
EmployeeOrgData: &msgraph.EmployeeOrgData{
CostCenter: utils.String(d.Get("cost_center").(string)),
Division: utils.String(d.Get("division").(string)),
},
EmployeeType: utils.NullableString(d.Get("employee_type").(string)),
FaxNumber: utils.NullableString(d.Get("fax_number").(string)),
GivenName: utils.NullableString(d.Get("given_name").(string)),
JobTitle: utils.NullableString(d.Get("job_title").(string)),
MailNickname: utils.String(d.Get("mail_nickname").(string)),
MobilePhone: utils.NullableString(d.Get("mobile_phone").(string)),
OfficeLocation: utils.NullableString(d.Get("office_location").(string)),
OtherMails: tf.ExpandStringSlicePtr(d.Get("other_mails").(*schema.Set).List()),
PasswordPolicies: utils.NullableString(passwordPolicies),
PostalCode: utils.NullableString(d.Get("postal_code").(string)),
PreferredLanguage: utils.NullableString(d.Get("preferred_language").(string)),
ShowInAddressList: utils.Bool(d.Get("show_in_address_list").(bool)),
State: utils.NullableString(d.Get("state").(string)),
StreetAddress: utils.NullableString(d.Get("street_address").(string)),
Surname: utils.NullableString(d.Get("surname").(string)),
UsageLocation: utils.NullableString(d.Get("usage_location").(string)),
}

if password := d.Get("password").(string); d.HasChange("password") && password != "" {
Expand Down Expand Up @@ -548,6 +577,7 @@ func userResourceRead(ctx context.Context, d *schema.ResourceData, meta interfac
tf.Set(d, "department", user.Department)
tf.Set(d, "display_name", user.DisplayName)
tf.Set(d, "employee_id", user.EmployeeId)
tf.Set(d, "employee_type", user.EmployeeType)
tf.Set(d, "external_user_state", user.ExternalUserState)
tf.Set(d, "fax_number", user.FaxNumber)
tf.Set(d, "given_name", user.GivenName)
Expand Down Expand Up @@ -594,6 +624,11 @@ func userResourceRead(ctx context.Context, d *schema.ResourceData, meta interfac
tf.Set(d, "disable_strong_password", disableStrongPassword)
tf.Set(d, "disable_password_expiration", disablePasswordExpiration)

if user.EmployeeOrgData != nil {
tf.Set(d, "cost_center", user.EmployeeOrgData.CostCenter)
tf.Set(d, "division", user.EmployeeOrgData.Division)
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/services/users/user_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ resource "azuread_user" "test" {
business_phones = ["12345678901"]
company_name = "acctestUser-%[1]d-Company"
consent_provided_for_minor = "NotRequired"
cost_center = "acctestUser-%[1]d-CostCenter"
department = "acctestUser-%[1]d-Dept"
display_name = "acctestUser-%[1]d-DisplayName"
division = "acctestUser-%[1]d-Division"
employee_id = "%[3]s%[3]s"
employee_type = "Contractor"
fax_number = "(555) 555-5555"
given_name = "acctestUser-%[1]d-GivenName"
job_title = "acctestUser-%[1]d-Job"
Expand Down

0 comments on commit ebb686b

Please sign in to comment.