Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New resource & data source 'azuread_user' #18

Merged
merged 21 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions azuread/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ArmClient struct {
// azure AD clients
applicationsClient graphrbac.ApplicationsClient
servicePrincipalsClient graphrbac.ServicePrincipalsClient
usersClient graphrbac.UsersClient
}

// getArmClient is a helper method which returns a fully instantiated *ArmClient based on the auth Config's current settings.
Expand Down Expand Up @@ -75,6 +76,9 @@ func (c *ArmClient) registerGraphRBACClients(endpoint, tenantID string, authoriz

c.servicePrincipalsClient = graphrbac.NewServicePrincipalsClientWithBaseURI(endpoint, tenantID)
configureClient(&c.servicePrincipalsClient.Client, authorizer)

c.usersClient = graphrbac.NewUsersClientWithBaseURI(endpoint, tenantID)
configureClient(&c.usersClient.Client, authorizer)
}

func configureClient(client *autorest.Client, auth autorest.Authorizer) {
Expand Down
60 changes: 60 additions & 0 deletions azuread/data_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package azuread

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"
)

func dataSourceUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"user_principal_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"mail": {
Type: schema.TypeString,
Computed: true,
},
},
tiwood marked this conversation as resolved.
Show resolved Hide resolved
}
}

func dataSourceUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).usersClient
ctx := meta.(*ArmClient).StopContext

var user graphrbac.User
var queryString string

queryString = d.Get("user_principal_name").(string)

log.Printf("[DEBUG] Using Get with the following query string: %q", queryString)
resp, err := client.Get(ctx, queryString)
if err != nil {
if ar.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: No AzureAD User found with the following query string: %q", queryString)
}
return fmt.Errorf("Error making Read request on AzureAD User the following query string: %q: %+v", queryString, err)
}

user = resp
tiwood marked this conversation as resolved.
Show resolved Hide resolved

d.SetId(*user.ObjectID)
tiwood marked this conversation as resolved.
Show resolved Hide resolved
d.Set("user_principal_name", user.UserPrincipalName)
d.Set("mail", user.Mail)

return nil
}
2 changes: 2 additions & 0 deletions azuread/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"azuread_application": dataApplication(),
"azuread_service_principal": dataServicePrincipal(),
"azuread_user": dataSourceUser(),
},

ResourcesMap: map[string]*schema.Resource{
"azuread_application": resourceApplication(),
"azuread_service_principal": resourceServicePrincipal(),
"azuread_service_principal_password": resourceServicePrincipalPassword(),
"azuread_user": resourceUser(),
},
}

Expand Down
188 changes: 188 additions & 0 deletions azuread/resource_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package azuread

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/p"
)

func resourceUser() *schema.Resource {
return &schema.Resource{
Create: resourceUserCreate,
Read: resourceUserRead,
Update: resourceUserUpdate,
Delete: resourceUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"user_principal_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
tiwood marked this conversation as resolved.
Show resolved Hide resolved

"display_name": {
Type: schema.TypeString,
Required: true,
},
tiwood marked this conversation as resolved.
Show resolved Hide resolved

"mail_nickname": {
Type: schema.TypeString,
Required: true,
},
tiwood marked this conversation as resolved.
Show resolved Hide resolved

"account_enabled": {
Type: schema.TypeBool,
Required: true,
},

"password_profile": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"force_password_change": {
Type: schema.TypeBool,
Required: true,
},
tiwood marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
}
}

func resourceUserCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).usersClient
ctx := meta.(*ArmClient).StopContext

userPrincipalName := d.Get("user_principal_name").(string)
displayName := d.Get("display_name").(string)
mailNickName := d.Get("mail_nickname").(string)
accountEnabled := d.Get("account_enabled").(bool)

log.Print("[DEBUG] expandingPasswordProfile start")
passwordProfile, err := expandPasswordProfile(d)
if err != nil {
return fmt.Errorf("Failed to expand passwordProfile: %+v", err)
}
log.Print("[DEBUG] expandingPasswordProfile done")

userCreateParameters := graphrbac.UserCreateParameters{
AccountEnabled: &accountEnabled,
DisplayName: &displayName,
MailNickname: &mailNickName,
PasswordProfile: &*passwordProfile,
UserPrincipalName: &userPrincipalName,
}

user, err := client.Create(ctx, userCreateParameters)
if err != nil {
return fmt.Errorf("Error creating User %q: %+v", userPrincipalName, err)
}

objectId := user.ObjectID

resp, err := client.Get(ctx, *objectId)
if err != nil {
return fmt.Errorf("Error retrieving User (%q) with ObjectID %q: %+v", userPrincipalName, *objectId, err)
}

d.SetId(*resp.ObjectID)
tiwood marked this conversation as resolved.
Show resolved Hide resolved

return resourceUserRead(d, meta)
}

func resourceUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).usersClient
ctx := meta.(*ArmClient).StopContext

objectId := d.Id()

user, err := client.Get(ctx, objectId)
if err != nil {
if ar.ResponseWasNotFound(user.Response) {
log.Printf("[DEBUG] User with Object ID %q was not found - removing from state!", objectId)
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving User with ID %q: %+v", objectId, err)
}

d.Set("user_principal_name", user.UserPrincipalName)
d.Set("display_name", user.DisplayName)
d.Set("mail_nickname", user.MailNickname)
d.Set("account_enabled", user.AccountEnabled)

return nil
}

func resourceUserUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).usersClient
ctx := meta.(*ArmClient).StopContext

var userUpdateParameters graphrbac.UserUpdateParameters

if d.HasChange("display_name") {
displayName := d.Get("display_name").(string)
userUpdateParameters.DisplayName = p.String(displayName)
}

if d.HasChange("mail_nickname") {
mailNickName := d.Get("mail_nickname").(string)
userUpdateParameters.MailNickname = p.String(mailNickName)
}

if d.HasChange("account_enabled") {
accountEnabled := d.Get("account_enabled").(bool)
userUpdateParameters.AccountEnabled = p.Bool(accountEnabled)
}

if _, err := client.Update(ctx, d.Id(), userUpdateParameters); err != nil {
return fmt.Errorf("Error updating User with ID %q: %+v", d.Id(), err)
}

return resourceUserRead(d, meta)
}

func resourceUserDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).usersClient
ctx := meta.(*ArmClient).StopContext

resp, err := client.Delete(ctx, d.Id())
if err != nil {
if !ar.ResponseWasNotFound(resp) {
return fmt.Errorf("Error Deleting User with ID %q: %+v", d.Id(), err)
}
}

return nil
}

func expandPasswordProfile(d *schema.ResourceData) (*graphrbac.PasswordProfile, error) {

passwordProfiles := d.Get("password_profile").(*schema.Set).List()
passwordProfile := passwordProfiles[0].(map[string]interface{})

forcePasswordChange := passwordProfile["force_password_change"].(bool)
password := passwordProfile["password"].(string)

passwordProfileProperties := &graphrbac.PasswordProfile{
ForceChangePasswordNextLogin: &forcePasswordChange,
Password: &password,
}

return passwordProfileProperties, nil
}