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

Commit

Permalink
Allow customizing timeouts (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeviaVir authored Oct 16, 2019
1 parent f61efe0 commit 2a2c0f2
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ GOVERSION := 1.12
PROJECT := github.com/DeviaVir/terraform-provider-gsuite
OWNER := $(notdir $(patsubst %/,%,$(dir $(PROJECT))))
NAME := $(notdir $(PROJECT))
VERSION := 0.1.31
VERSION := 0.1.32
EXTERNAL_TOOLS = \
github.com/golang/dep/cmd/dep

Expand Down
2 changes: 2 additions & 0 deletions gsuite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Config struct {

CustomerId string

TimeoutMinutes int

OauthScopes []string

directory *directory.Service
Expand Down
2 changes: 1 addition & 1 deletion gsuite/data_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func dataGroupRead(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
group, err = config.directory.Groups.Get(d.Get("email").(string)).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Group %q", d.Get("name").(string)))
Expand Down
2 changes: 1 addition & 1 deletion gsuite/data_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func dataUserRead(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
user, err = config.directory.Users.Get(d.Get("primary_email").(string)).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("User %q", d.Id()))
Expand Down
8 changes: 8 additions & 0 deletions gsuite/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func Provider() *schema.Provider {
Type: schema.TypeString,
Optional: true,
},
"timeout_minutes": {
Type: schema.TypeInt,
Optional: true,
Default: 1, // 1 + (n*2) roof 16 = 1+2+4+8+16 = 31 seconds, 1 min should be "normal" operations
},
},
DataSourcesMap: map[string]*schema.Resource{
"gsuite_group": dataGroup(),
Expand Down Expand Up @@ -103,12 +108,15 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
customerID = "my_customer"
}

timeoutMinutes := d.Get("timeout_minutes").(int)

oauthScopes := oauthScopesFromConfigOrDefault(d.Get("oauth_scopes").(*schema.Set))
config := Config{
Credentials: credentials,
ImpersonatedUserEmail: impersonatedUserEmail,
OauthScopes: oauthScopes,
CustomerId: customerID,
TimeoutMinutes: timeoutMinutes,
}

if err := config.loadAndValidate(terraformVersion); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions gsuite/resource_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func resourceDomainCreate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
createdDomain, err = config.directory.Domains.Insert(customerId, domain).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error creating domain: %s", err)
Expand Down Expand Up @@ -91,7 +91,7 @@ func resourceDomainRead(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
domain, err = config.directory.Domains.Get(customerId, domainName).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Domain %q", d.Get("domain_name").(string)))
Expand Down Expand Up @@ -120,7 +120,7 @@ func resourceDomainDelete(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
err = config.directory.Domains.Delete(customerId, domainName).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error deleting domain: %s", err)
Expand Down
16 changes: 8 additions & 8 deletions gsuite/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func resourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
createdGroup, err = config.directory.Groups.Insert(group).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error creating group: %s", err)
Expand All @@ -101,7 +101,7 @@ func resourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
}
_, err = config.directory.Groups.Aliases.Insert(d.Id(), alias).Do()
return err
})
}, config.TimeoutMinutes)
}

if err != nil {
Expand All @@ -112,7 +112,7 @@ func resourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
err = retryNotFound(func() error {
group, err = config.directory.Groups.Get(createdGroup.Id).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Taking too long to create this group: %s", err)
Expand Down Expand Up @@ -165,7 +165,7 @@ func resourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
updatedGroup, err = config.directory.Groups.Patch(d.Id(), group).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error updating group: %s", err)
Expand All @@ -176,7 +176,7 @@ func resourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
aliasesResponse, err = config.directory.Groups.Aliases.List(d.Id()).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Could not list group aliases: %s", err)
Expand Down Expand Up @@ -204,7 +204,7 @@ func resourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
}
_, err = config.directory.Groups.Aliases.Insert(d.Id(), alias).Do()
return err
})
}, config.TimeoutMinutes)
}

if err != nil {
Expand All @@ -223,7 +223,7 @@ func resourceGroupRead(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
group, err = config.directory.Groups.Get(d.Id()).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Group %q", d.Get("name").(string)))
Expand All @@ -247,7 +247,7 @@ func resourceGroupDelete(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
err = config.directory.Groups.Delete(d.Id()).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error deleting group: %s", err)
}
Expand Down
14 changes: 7 additions & 7 deletions gsuite/resource_group_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func resourceGroupMemberCreate(d *schema.ResourceData, meta interface{}) error {
err = retryPassDuplicate(func() error {
createdGroupMember, err = config.directory.Members.Insert(group, groupMember).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
if !strings.Contains(err.Error(), "Member already exists") {
Expand All @@ -102,7 +102,7 @@ func resourceGroupMemberCreate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
existingGroupMembers, err = config.directory.Members.List(group).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error listing existing group members: %s", err)
}
Expand All @@ -122,7 +122,7 @@ func resourceGroupMemberCreate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
_, err = config.directory.Members.Patch(group, locatedGroupMember.Id, groupMember).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error updating existing group member: %s", err)
Expand All @@ -138,7 +138,7 @@ func resourceGroupMemberCreate(d *schema.ResourceData, meta interface{}) error {
err = retryNotFound(func() error {
groupMember, err = config.directory.Members.Get(group, d.Id()).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Taking too long to create this group member: %s", err)
Expand Down Expand Up @@ -172,7 +172,7 @@ func resourceGroupMemberUpdate(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
updatedGroupMember, err = config.directory.Members.Patch(strings.ToLower(d.Get("group").(string)), d.Id(), groupMember).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error updating group member: %s", err)
Expand All @@ -190,7 +190,7 @@ func resourceGroupMemberRead(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
groupMember, err = config.directory.Members.Get(strings.ToLower(d.Get("group").(string)), d.Id()).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Group member %q", d.Get("email").(string)))
Expand All @@ -214,7 +214,7 @@ func resourceGroupMemberDelete(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
err = config.directory.Members.Delete(strings.ToLower(d.Get("group").(string)), d.Id()).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error deleting group member: %s", err)
}
Expand Down
28 changes: 14 additions & 14 deletions gsuite/resource_group_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ func reconcileMembers(d *schema.ResourceData, cfgMembers, apiMembers []map[strin

var updatedGroupMember *directory.Member
var err error
err = retry(func() error {
err = retryNotFound(func() error {
updatedGroupMember, err = config.directory.Members.Patch(
strings.ToLower(d.Get("group_email").(string)),
strings.ToLower(cfgMember["email"].(string)),
groupMember).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error updating groupMember: %s", err)
Expand Down Expand Up @@ -245,7 +245,7 @@ func getAPIMembers(groupEmail string, config *Config) ([]*directory.Member, erro
err = retry(func() error {
membersResponse, err = config.directory.Members.List(groupEmail).PageToken(token).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return groupMembers, err
Expand Down Expand Up @@ -277,7 +277,7 @@ func upsertMember(email, groupEmail, role string, config *Config) error {
return nil
}
return err
})
}, config.TimeoutMinutes)

if isGroup == true {
if role != "MEMBER" {
Expand All @@ -297,25 +297,25 @@ func upsertMember(email, groupEmail, role string, config *Config) error {
}

return err
})
}, config.TimeoutMinutes)

// Based on the err return, either add as a new member, or update
if isGroupMember == false {
var createdGroupMember *directory.Member
err = retry(func() error {
createdGroupMember, err = config.directory.Members.Insert(groupEmail, groupMember).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error creating groupMember: %s, %s", err, email)
}
log.Printf("[INFO] Created groupMember: %s", createdGroupMember.Email)
} else {
var updatedGroupMember *directory.Member
err = retry(func() error {
err = retryNotFound(func() error {
updatedGroupMember, err = config.directory.Members.Update(groupEmail, email, groupMember).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error updating groupMember: %s, %s", err, email)
}
Expand All @@ -340,17 +340,17 @@ func upsertMember(email, groupEmail, role string, config *Config) error {
return fmt.Errorf("[ERROR] Error adding groupMember %s, please make sure the user exists beforehand", email)
}
return err
})
}, config.TimeoutMinutes)
if err != nil {
return createGroupMember(groupMember, groupEmail, config)
}

if hasMemberResponse.IsMember == true {
var updatedGroupMember *directory.Member
err = retry(func() error {
err = retryNotFound(func() error {
updatedGroupMember, err = config.directory.Members.Update(groupEmail, email, groupMember).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error updating groupMember: %s, %s", err, email)
}
Expand All @@ -368,7 +368,7 @@ func createGroupMember(groupMember *directory.Member, groupEmail string, config
err = retry(func() error {
createdGroupMember, err = config.directory.Members.Insert(groupEmail, groupMember).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Error creating groupMember: %s, %s", err, groupMember.Email)
}
Expand All @@ -381,7 +381,7 @@ func deleteMember(email, groupEmail string, config *Config) (err error) {
err = retry(func() error {
err = config.directory.Members.Delete(groupEmail, email).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error deleting member: %s", err)
Expand All @@ -399,7 +399,7 @@ func resourceGroupMembersImporter(d *schema.ResourceData, meta interface{}) ([]*
err = retry(func() error {
group, err = config.directory.Groups.Get(strings.ToLower(d.Id())).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return nil, fmt.Errorf("[ERROR] Error fetching group. Make sure the group exists: %s ", err)
Expand Down
6 changes: 3 additions & 3 deletions gsuite/resource_group_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func resourceGroupSettingsCreate(d *schema.ResourceData, meta interface{}) error
err = retry(func() error {
_, err = config.groupSettings.Groups.Update(d.Get("email").(string), groupSetting).Do()
return err
})
}, config.TimeoutMinutes)
if err != nil {
return fmt.Errorf("[ERROR] Something went wrong while updating group settings: %s", err)
}
Expand Down Expand Up @@ -1164,7 +1164,7 @@ func resourceGroupSettingsUpdate(d *schema.ResourceData, meta interface{}) error
err = retry(func() error {
_, err = config.groupSettings.Groups.Update(d.Get("email").(string), groupSetting).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return fmt.Errorf("[ERROR] Error updating group settings: %s", err)
Expand All @@ -1181,7 +1181,7 @@ func resourceGroupSettingsRead(d *schema.ResourceData, meta interface{}) error {
err = retry(func() error {
groupSetting, err = config.groupSettings.Groups.Get(d.Get("email").(string)).Do()
return err
})
}, config.TimeoutMinutes)

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Group Settings for %q", d.Get("name").(string)))
Expand Down
Loading

0 comments on commit 2a2c0f2

Please sign in to comment.