Skip to content
This repository has been archived by the owner on Jun 27, 2021. It is now read-only.

Commit

Permalink
add user aliases resource (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Rutherford authored and DeviaVir committed Jan 8, 2020
1 parent e74cb1c commit 524a2de
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/user/devteam.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
resource "gsuite_user" "developer" {

aliases = [
"[email protected]"
]

# advise to set this field to true on creation, then false afterwards
change_password_next_login = true

Expand Down
73 changes: 68 additions & 5 deletions gsuite/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func resourceUser() *schema.Resource {

Schema: map[string]*schema.Schema{
"aliases": {
Type: schema.TypeList,
Computed: true,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

Expand Down Expand Up @@ -334,7 +334,14 @@ func resourceUserCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

user := &directory.User{}
aliases := []string{}

if v, ok := d.GetOk("aliases"); ok {
for _, alias := range v.(*schema.Set).List() {
aliases = append(aliases, alias.(string))
}
log.Printf("[DEBUG] Setting %s: %v", "aliases", aliases)
}
if v, ok := d.GetOk("deletion_time"); ok {
log.Printf("[DEBUG] Setting %s: %s", "deletion_time", v.(string))
user.DeletionTime = v.(string)
Expand Down Expand Up @@ -442,7 +449,7 @@ func resourceUserCreate(d *schema.ResourceData, meta interface{}) error {
var err error
var existingUsers *directory.Users
err = retry(func() error {
existingUsers, err = config.directory.Users.List().Customer(config.CustomerId).Query("email:" + user.PrimaryEmail).Do()
existingUsers, err = config.directory.Users.List().Customer(config.CustomerId).Query("email:" + user.PrimaryEmail).Do()
return err
}, config.TimeoutMinutes)

Expand All @@ -456,16 +463,22 @@ func resourceUserCreate(d *schema.ResourceData, meta interface{}) error {

if locatedUser != nil {
log.Printf("[INFO] found existing user %s", locatedUser.PrimaryEmail)

err = retry(func() error {
_, err = config.directory.Users.Update(locatedUser.Id, user).Do()
return err
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error updating existing user: %s", err)
}

err = userAliasesUpdate(config, locatedUser, aliases)

if err != nil {
return err
}

log.Printf("[INFO] Updated user: %s", user.PrimaryEmail)
d.SetId(locatedUser.Id)
return resourceUserRead(d, meta)
Expand Down Expand Up @@ -504,11 +517,46 @@ func resourceUserCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[ERROR] Not failing on this operation, your POSIX data has not been set. A next apply will retry.")
}

err = userAliasesUpdate(config, createdUser, aliases)

if err != nil {
return err
}

d.SetId(createdUser.Id)
log.Printf("[INFO] Created user: %s", createdUser.PrimaryEmail)
return resourceUserRead(d, meta)
}

func userAliasesUpdate(config *Config, user *directory.User, aliases []string) error {

createdAliases := stringSliceDifference(aliases, user.Aliases)
deletedAliases := stringSliceDifference(user.Aliases, aliases)

for _, alias := range createdAliases {
err := retry(func() error {
_, err := config.directory.Users.Aliases.Insert(user.Id, &directory.Alias{Alias: alias}).Do()
return err
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error adding alias to existing user: %s", err)
}
}

for _, alias := range deletedAliases {
err := retry(func() error {
return config.directory.Users.Aliases.Delete(user.Id, alias).Do()
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error deleting alias from existing user: %s", err)
}
}

return nil
}

func userPosixCreate(d *schema.ResourceData, userID string, meta interface{}) error {
config := meta.(*Config)

Expand Down Expand Up @@ -827,6 +875,21 @@ func resourceUserUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("[ERROR] Error updating user: %s", err)
}

if d.HasChange("aliases") {

aliases := []string{}
if v, ok := d.GetOk("aliases"); ok {
for _, alias := range v.(*schema.Set).List() {
aliases = append(aliases, alias.(string))
}
}

err = userAliasesUpdate(config, updatedUser, aliases)
if err != nil {
return err
}
}

log.Printf("[INFO] Updated user: %s", updatedUser.PrimaryEmail)
return resourceUserRead(d, meta)
}
Expand Down
17 changes: 17 additions & 0 deletions gsuite/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,20 @@ func convertStringSet(set *schema.Set) []string {
}
return s
}

func stringSliceDifference(left []string, right []string) []string {
var d []string
for _, l := range left {
f := false
for _, r := range right {
if r == l {
f = true
break
}
}
if !f {
d = append(d, l)
}
}
return d
}

0 comments on commit 524a2de

Please sign in to comment.