From ab7c561dbe7b4ef9482f16d0bf02c23517be4756 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 28 Aug 2021 13:59:18 -0400 Subject: [PATCH] [#274] updating pagerduty addons and extensions. Fixing pagerduty services retry. --- pagerduty/resource_pagerduty_addon.go | 47 ++++----- pagerduty/resource_pagerduty_extension.go | 46 ++++----- pagerduty/resource_pagerduty_service.go | 111 +++++++++++----------- 3 files changed, 104 insertions(+), 100 deletions(-) diff --git a/pagerduty/resource_pagerduty_addon.go b/pagerduty/resource_pagerduty_addon.go index 3c2f5b314..1858260fd 100644 --- a/pagerduty/resource_pagerduty_addon.go +++ b/pagerduty/resource_pagerduty_addon.go @@ -41,32 +41,13 @@ func buildAddonStruct(d *schema.ResourceData) *pagerduty.Addon { return addon } -func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - - addon := buildAddonStruct(d) - - log.Printf("[INFO] Creating PagerDuty add-on %s", addon.Name) - - addon, _, err := client.Addons.Install(addon) - if err != nil { - return err - } - - d.SetId(addon.ID) - - return resourcePagerDutyAddonRead(d, meta) -} - -func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyAddon(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client := meta.(*pagerduty.Client) - - log.Printf("[INFO] Reading PagerDuty add-on %s", d.Id()) - return resource.Retry(2*time.Minute, func() *resource.RetryError { addon, _, err := client.Addons.Get(d.Id()) if err != nil { - errResp := handleNotFoundError(err, d) + log.Printf("[WARN] Service read error") + errResp := errCallback(err, d) if errResp != nil { time.Sleep(2 * time.Second) return resource.RetryableError(errResp) @@ -82,6 +63,28 @@ func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error }) } +func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + addon := buildAddonStruct(d) + + log.Printf("[INFO] Creating PagerDuty add-on %s", addon.Name) + + addon, _, err := client.Addons.Install(addon) + if err != nil { + return err + } + + d.SetId(addon.ID) + // Retrying on creates incase of eventual consistency on creation + return fetchPagerDutyAddon(d, meta, genError) +} + +func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[INFO] Reading PagerDuty add-on %s", d.Id()) + return fetchPagerDutyAddon(d, meta, handleNotFoundError) +} + func resourcePagerDutyAddonUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) diff --git a/pagerduty/resource_pagerduty_extension.go b/pagerduty/resource_pagerduty_extension.go index fb6a1cd60..1a287de38 100644 --- a/pagerduty/resource_pagerduty_extension.go +++ b/pagerduty/resource_pagerduty_extension.go @@ -82,32 +82,12 @@ func buildExtensionStruct(d *schema.ResourceData) *pagerduty.Extension { return Extension } -func resourcePagerDutyExtensionCreate(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyExtension(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client := meta.(*pagerduty.Client) - - extension := buildExtensionStruct(d) - - log.Printf("[INFO] Creating PagerDuty extension %s", extension.Name) - - extension, _, err := client.Extensions.Create(extension) - if err != nil { - return err - } - - d.SetId(extension.ID) - - return resourcePagerDutyExtensionRead(d, meta) -} - -func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - - log.Printf("[INFO] Reading PagerDuty extension %s", d.Id()) - return resource.Retry(2*time.Minute, func() *resource.RetryError { extension, _, err := client.Extensions.Get(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) @@ -133,6 +113,28 @@ func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) er }) } +func resourcePagerDutyExtensionCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + extension := buildExtensionStruct(d) + + log.Printf("[INFO] Creating PagerDuty extension %s", extension.Name) + + extension, _, err := client.Extensions.Create(extension) + if err != nil { + return err + } + + d.SetId(extension.ID) + + return fetchPagerDutyExtension(d, meta, genError) +} + +func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[INFO] Reading PagerDuty extension %s", d.Id()) + return fetchPagerDutyExtension(d, meta, handleNotFoundError) +} + func resourcePagerDutyExtensionUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index 1de521d96..8727b8718 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -328,59 +328,7 @@ func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) { return &service, nil } -func configureResource(d *schema.ResourceData, service *pagerduty.Service) *resource.RetryError { - d.Set("name", service.Name) - d.Set("html_url", service.HTMLURL) - d.Set("status", service.Status) - d.Set("created_at", service.CreatedAt) - d.Set("escalation_policy", service.EscalationPolicy.ID) - d.Set("description", service.Description) - if service.AutoResolveTimeout == nil { - d.Set("auto_resolve_timeout", "null") - } else { - d.Set("auto_resolve_timeout", strconv.Itoa(*service.AutoResolveTimeout)) - } - d.Set("last_incident_timestamp", service.LastIncidentTimestamp) - if service.AcknowledgementTimeout == nil { - d.Set("acknowledgement_timeout", "null") - } else { - d.Set("acknowledgement_timeout", strconv.Itoa(*service.AcknowledgementTimeout)) - } - d.Set("alert_creation", service.AlertCreation) - if service.AlertGrouping != nil && *service.AlertGrouping != "" { - d.Set("alert_grouping", *service.AlertGrouping) - } - if service.AlertGroupingTimeout == nil { - d.Set("alert_grouping_timeout", "null") - } else { - d.Set("alert_grouping_timeout", *service.AlertGroupingTimeout) - } - if service.AlertGroupingParameters != nil { - if err := d.Set("alert_grouping_parameters", flattenAlertGroupingParameters(service.AlertGroupingParameters)); err != nil { - return resource.NonRetryableError(err) - } - } - if service.IncidentUrgencyRule != nil { - if err := d.Set("incident_urgency_rule", flattenIncidentUrgencyRule(service.IncidentUrgencyRule)); err != nil { - return resource.NonRetryableError(err) - } - } - - if service.SupportHours != nil { - if err := d.Set("support_hours", flattenSupportHours(service.SupportHours)); err != nil { - return resource.NonRetryableError(err) - } - } - - if service.ScheduledActions != nil { - if err := d.Set("scheduled_actions", flattenScheduledActions(service.ScheduledActions)); err != nil { - return resource.NonRetryableError(err) - } - } - return nil -} - -func fetchResource(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { +func fetchService(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 { service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{}) @@ -394,12 +342,63 @@ func fetchResource(d *schema.ResourceData, meta interface{}, errCallback func(er return nil } - return configureResource(d, service) + + d.Set("name", service.Name) + d.Set("html_url", service.HTMLURL) + d.Set("status", service.Status) + d.Set("created_at", service.CreatedAt) + d.Set("escalation_policy", service.EscalationPolicy.ID) + d.Set("description", service.Description) + if service.AutoResolveTimeout == nil { + d.Set("auto_resolve_timeout", "null") + } else { + d.Set("auto_resolve_timeout", strconv.Itoa(*service.AutoResolveTimeout)) + } + d.Set("last_incident_timestamp", service.LastIncidentTimestamp) + if service.AcknowledgementTimeout == nil { + d.Set("acknowledgement_timeout", "null") + } else { + d.Set("acknowledgement_timeout", strconv.Itoa(*service.AcknowledgementTimeout)) + } + d.Set("alert_creation", service.AlertCreation) + if service.AlertGrouping != nil && *service.AlertGrouping != "" { + d.Set("alert_grouping", *service.AlertGrouping) + } + if service.AlertGroupingTimeout == nil { + d.Set("alert_grouping_timeout", "null") + } else { + d.Set("alert_grouping_timeout", *service.AlertGroupingTimeout) + } + if service.AlertGroupingParameters != nil { + if err := d.Set("alert_grouping_parameters", flattenAlertGroupingParameters(service.AlertGroupingParameters)); err != nil { + return resource.NonRetryableError(err) + } + } + if service.IncidentUrgencyRule != nil { + if err := d.Set("incident_urgency_rule", flattenIncidentUrgencyRule(service.IncidentUrgencyRule)); err != nil { + return resource.NonRetryableError(err) + } + } + + if service.SupportHours != nil { + if err := d.Set("support_hours", flattenSupportHours(service.SupportHours)); err != nil { + return resource.NonRetryableError(err) + } + } + + if service.ScheduledActions != nil { + if err := d.Set("scheduled_actions", flattenScheduledActions(service.ScheduledActions)); err != nil { + return resource.NonRetryableError(err) + } + } + return nil + }) } func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) + service, err := buildServiceStruct(d) if err != nil { return err @@ -414,12 +413,12 @@ func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) er d.SetId(service.ID) - return fetchResource(d, meta, genError) + return fetchService(d, meta, genError) } func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error { log.Printf("[INFO] Reading PagerDuty service %s", d.Id()) - return fetchResource(d, meta, handleNotFoundError) + return fetchService(d, meta, handleNotFoundError) } func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error {