From 3a65abb6255eb791d96bef20231d59ddaa630574 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 29 Aug 2021 11:14:03 -0400 Subject: [PATCH] [#273] Updating ruleset, service integration, team membership, user contact, and notifications rules to have similar safeguards against eventual consistent reads. --- pagerduty/resource_pagerduty_ruleset.go | 55 +++++++------- .../resource_pagerduty_service_integration.go | 72 ++++++++++--------- .../resource_pagerduty_team_membership.go | 67 ++++++++--------- .../resource_pagerduty_user_contact_method.go | 40 ++++++----- ...source_pagerduty_user_notification_rule.go | 43 +++++------ 5 files changed, 145 insertions(+), 132 deletions(-) diff --git a/pagerduty/resource_pagerduty_ruleset.go b/pagerduty/resource_pagerduty_ruleset.go index f8210b84f..d054702fe 100644 --- a/pagerduty/resource_pagerduty_ruleset.go +++ b/pagerduty/resource_pagerduty_ruleset.go @@ -99,6 +99,33 @@ func flattenTeam(v *pagerduty.RulesetObject) []interface{} { return []interface{}{team} } + +func fetchPagerDutyRuleset(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { + client := meta.(*pagerduty.Client) + return resource.Retry(2*time.Minute, func() *resource.RetryError { + ruleset, _, err := client.Rulesets.Get(d.Id()) + if err != nil { + errResp := errCallback(err, d) + if errResp != nil { + time.Sleep(2 * time.Second) + return resource.RetryableError(errResp) + } + + return nil + } + d.Set("name", ruleset.Name) + d.Set("type", ruleset.Type) + + // if ruleset is found set to ResourceData + if ruleset.Team != nil { + d.Set("team", flattenTeam(ruleset.Team)) + } + d.Set("routing_keys", ruleset.RoutingKeys) + + return nil + }) +} + func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) @@ -122,37 +149,13 @@ func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) er if retryErr != nil { return retryErr } - - return resourcePagerDutyRulesetRead(d, meta) + return fetchPagerDutyRuleset(d, meta, genError) } func resourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - log.Printf("[INFO] Reading PagerDuty ruleset: %s", d.Id()) + return fetchPagerDutyRuleset(d, meta, handleNotFoundError) - return resource.Retry(2*time.Minute, func() *resource.RetryError { - ruleset, _, err := client.Rulesets.Get(d.Id()) - if err != nil { - errResp := handleNotFoundError(err, d) - if errResp != nil { - time.Sleep(2 * time.Second) - return resource.RetryableError(errResp) - } - - return nil - } - d.Set("name", ruleset.Name) - d.Set("type", ruleset.Type) - - // if ruleset is found set to ResourceData - if ruleset.Team != nil { - d.Set("team", flattenTeam(ruleset.Team)) - } - d.Set("routing_keys", ruleset.RoutingKeys) - - return nil - }) } func resourcePagerDutyRulesetUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) diff --git a/pagerduty/resource_pagerduty_service_integration.go b/pagerduty/resource_pagerduty_service_integration.go index 55e374311..0bdef5795 100644 --- a/pagerduty/resource_pagerduty_service_integration.go +++ b/pagerduty/resource_pagerduty_service_integration.go @@ -106,41 +106,8 @@ func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integratio return serviceIntegration } -func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyServiceIntegration(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client := meta.(*pagerduty.Client) - - serviceIntegration := buildServiceIntegrationStruct(d) - - log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name) - - service := d.Get("service").(string) - - retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError { - if serviceIntegration, _, err := client.Services.CreateIntegration(service, serviceIntegration); err != nil { - if isErrCode(err, 400) { - time.Sleep(2 * time.Second) - return resource.RetryableError(err) - } - - return resource.NonRetryableError(err) - } else if serviceIntegration != nil { - d.SetId(serviceIntegration.ID) - } - return nil - }) - - if retryErr != nil { - return retryErr - } - - return resourcePagerDutyServiceIntegrationRead(d, meta) -} - -func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - - log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id()) - service := d.Get("service").(string) o := &pagerduty.GetIntegrationOptions{} @@ -149,7 +116,7 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf serviceIntegration, _, err := client.Services.GetIntegration(service, d.Id(), o) if err != nil { log.Printf("[WARN] Service integration read error") - errResp := handleNotFoundError(err, d) + errResp := errCallback(err, d) if errResp != nil { time.Sleep(2 * time.Second) return resource.RetryableError(errResp) @@ -185,6 +152,41 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf }) } +func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + serviceIntegration := buildServiceIntegrationStruct(d) + + log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name) + + service := d.Get("service").(string) + + retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError { + if serviceIntegration, _, err := client.Services.CreateIntegration(service, serviceIntegration); err != nil { + if isErrCode(err, 400) { + time.Sleep(2 * time.Second) + return resource.RetryableError(err) + } + + return resource.NonRetryableError(err) + } else if serviceIntegration != nil { + d.SetId(serviceIntegration.ID) + } + return nil + }) + + if retryErr != nil { + return retryErr + } + + return fetchPagerDutyServiceIntegration(d, meta, genError) +} + +func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id()) + return fetchPagerDutyServiceIntegration(d, meta, handleNotFoundError) +} + func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) diff --git a/pagerduty/resource_pagerduty_team_membership.go b/pagerduty/resource_pagerduty_team_membership.go index 1db825b30..85fcd45ef 100644 --- a/pagerduty/resource_pagerduty_team_membership.go +++ b/pagerduty/resource_pagerduty_team_membership.go @@ -44,46 +44,15 @@ func resourcePagerDutyTeamMembership() *schema.Resource { }, } } -func resourcePagerDutyTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - - userID := d.Get("user_id").(string) - teamID := d.Get("team_id").(string) - role := d.Get("role").(string) - - log.Printf("[DEBUG] Adding user: %s to team: %s with role: %s", userID, teamID, role) - retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { - if _, err := client.Teams.AddUserWithRole(teamID, userID, role); err != nil { - if isErrCode(err, 500) { - return resource.RetryableError(err) - } - - return resource.NonRetryableError(err) - } - - return nil - }) - if retryErr != nil { - return retryErr - } - - d.SetId(fmt.Sprintf("%s:%s", userID, teamID)) - - return resourcePagerDutyTeamMembershipRead(d, meta) -} - -func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyTeamMembership(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client := meta.(*pagerduty.Client) - userID, teamID := resourcePagerDutyTeamMembershipParseID(d.Id()) - log.Printf("[DEBUG] Reading user: %s from team: %s", userID, teamID) - return resource.Retry(2*time.Minute, func() *resource.RetryError { resp, _, err := client.Teams.GetMembers(teamID, &pagerduty.GetMembersOptions{}) if err != nil { - errResp := handleNotFoundError(err, d) + errResp := errCallback(err, d) if errResp != nil { time.Sleep(2 * time.Second) return resource.RetryableError(errResp) @@ -108,6 +77,38 @@ func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{ return nil }) } +func resourcePagerDutyTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + userID := d.Get("user_id").(string) + teamID := d.Get("team_id").(string) + role := d.Get("role").(string) + + log.Printf("[DEBUG] Adding user: %s to team: %s with role: %s", userID, teamID, role) + + retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { + if _, err := client.Teams.AddUserWithRole(teamID, userID, role); err != nil { + if isErrCode(err, 500) { + return resource.RetryableError(err) + } + + return resource.NonRetryableError(err) + } + + return nil + }) + if retryErr != nil { + return retryErr + } + + d.SetId(fmt.Sprintf("%s:%s", userID, teamID)) + + return fetchPagerDutyTeamMembership(d, meta, genError) +} + +func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{}) error { + return fetchPagerDutyTeamMembership(d, meta, handleNotFoundError) +} func resourcePagerDutyTeamMembershipUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) diff --git a/pagerduty/resource_pagerduty_user_contact_method.go b/pagerduty/resource_pagerduty_user_contact_method.go index 3b0f1d52a..57c7b05bb 100644 --- a/pagerduty/resource_pagerduty_user_contact_method.go +++ b/pagerduty/resource_pagerduty_user_contact_method.go @@ -92,26 +92,9 @@ func buildUserContactMethodStruct(d *schema.ResourceData) *pagerduty.ContactMeth return contactMethod } -func resourcePagerDutyUserContactMethodCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - - userID := d.Get("user_id").(string) - - contactMethod := buildUserContactMethodStruct(d) - resp, _, err := client.Users.CreateContactMethod(userID, contactMethod) - if err != nil { - return err - } - - d.SetId(resp.ID) - - return resourcePagerDutyUserContactMethodRead(d, meta) -} - -func resourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyUserContactMethod(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client := meta.(*pagerduty.Client) - userID := d.Get("user_id").(string) return resource.Retry(2*time.Minute, func() *resource.RetryError { @@ -138,6 +121,27 @@ func resourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interfa }) } +func resourcePagerDutyUserContactMethodCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + userID := d.Get("user_id").(string) + + contactMethod := buildUserContactMethodStruct(d) + + resp, _, err := client.Users.CreateContactMethod(userID, contactMethod) + if err != nil { + return err + } + + d.SetId(resp.ID) + + return fetchPagerDutyUserContactMethod(d, meta, genError) +} + +func resourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interface{}) error { + return fetchPagerDutyUserContactMethod(d, meta, handleNotFoundError) +} + func resourcePagerDutyUserContactMethodUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) diff --git a/pagerduty/resource_pagerduty_user_notification_rule.go b/pagerduty/resource_pagerduty_user_notification_rule.go index c5f1402bc..d73a09c2b 100644 --- a/pagerduty/resource_pagerduty_user_notification_rule.go +++ b/pagerduty/resource_pagerduty_user_notification_rule.go @@ -76,32 +76,14 @@ func buildUserNotificationRuleStruct(d *schema.ResourceData) *pagerduty.Notifica return notificationRule } -func resourcePagerDutyUserNotificationRuleCreate(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyUserNotificationRule(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client := meta.(*pagerduty.Client) - - userID := d.Get("user_id").(string) - - notificationRule := buildUserNotificationRuleStruct(d) - - resp, _, err := client.Users.CreateNotificationRule(userID, notificationRule) - if err != nil { - return err - } - - d.SetId(resp.ID) - - return resourcePagerDutyUserNotificationRuleRead(d, meta) -} - -func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - userID := d.Get("user_id").(string) return resource.Retry(2*time.Minute, func() *resource.RetryError { resp, _, err := client.Users.GetNotificationRule(userID, d.Id()) if err != nil { - errResp := handleNotFoundError(err, d) + errResp := errCallback(err, d) if errResp != nil { time.Sleep(2 * time.Second) return resource.RetryableError(errResp) @@ -119,6 +101,27 @@ func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta inte }) } +func resourcePagerDutyUserNotificationRuleCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + userID := d.Get("user_id").(string) + + notificationRule := buildUserNotificationRuleStruct(d) + + resp, _, err := client.Users.CreateNotificationRule(userID, notificationRule) + if err != nil { + return err + } + + d.SetId(resp.ID) + + return fetchPagerDutyUserNotificationRule(d, meta, genError) +} + +func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta interface{}) error { + return fetchPagerDutyUserNotificationRule(d, meta, handleNotFoundError) +} + func resourcePagerDutyUserNotificationRuleUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client)