From 8352f75539aeb604af04ff7c88eff17e6dd5b0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wilczyn=CC=81ski?= Date: Fri, 20 Mar 2020 12:13:14 +0100 Subject: [PATCH 1/2] Implement retry logic in read requests for services and service integrations --- go.mod | 2 +- go.sum | 2 + main.go | 2 +- pagerduty/resource_pagerduty_service.go | 98 ++++++++++--------- .../resource_pagerduty_service_integration.go | 53 +++++----- 5 files changed, 88 insertions(+), 69 deletions(-) diff --git a/go.mod b/go.mod index 8478f776c..838a9143e 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/terraform-providers/terraform-provider-pagerduty +module github.com/mLupine/terraform-provider-pagerduty require ( github.com/google/go-querystring v1.0.0 // indirect diff --git a/go.sum b/go.sum index 9afc9446c..6f75ae317 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,7 @@ github.com/hashicorp/terraform-json v0.4.0 h1:KNh29iNxozP5adfUFBJ4/fWd0Cu3taGgjH github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU= github.com/hashicorp/terraform-plugin-sdk v1.7.0 h1:B//oq0ZORG+EkVrIJy0uPGSonvmXqxSzXe8+GhknoW0= github.com/hashicorp/terraform-plugin-sdk v1.7.0/go.mod h1:OjgQmey5VxnPej/buEhe+YqKm0KNvV3QqU4hkqHqPCY= +github.com/hashicorp/terraform-plugin-sdk v1.8.0 h1:HE1p52nzcgz88hIJmapUnkmM9noEjV3QhTOLaua5XUA= github.com/hashicorp/terraform-plugin-test v1.2.0 h1:AWFdqyfnOj04sxTdaAF57QqvW7XXrT8PseUHkbKsE8I= github.com/hashicorp/terraform-plugin-test v1.2.0/go.mod h1:QIJHYz8j+xJtdtLrFTlzQVC0ocr3rf/OjIpgZLK56Hs= github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg= @@ -141,6 +142,7 @@ github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3v github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mLupine/terraform-provider-pagerduty v1.5.1 h1:m1BIqG6pdjGyhBt2opuq1hM92HodrDNLcLWfWxqIF8A= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= diff --git a/main.go b/main.go index eb76b5bb5..85a4e71cc 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "github.com/hashicorp/terraform-plugin-sdk/plugin" - "github.com/terraform-providers/terraform-provider-pagerduty/pagerduty" + "github.com/mLupine/terraform-provider-pagerduty/pagerduty" ) func main() { diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index dfd3967b3..c02a8a884 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -3,7 +3,9 @@ package pagerduty import ( "log" "strconv" + "time" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/heimweh/go-pagerduty/pagerduty" ) @@ -295,57 +297,63 @@ func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) erro log.Printf("[INFO] Reading PagerDuty service %s", d.Id()) - service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{}) - if err != nil { - return handleNotFoundError(err, d) - } - - 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 != "" { - 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.IncidentUrgencyRule != nil { - if err := d.Set("incident_urgency_rule", flattenIncidentUrgencyRule(service.IncidentUrgencyRule)); err != nil { - return err + return resource.Retry(2*time.Minute, func() *resource.RetryError { + service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{}) + if err != nil { + log.Printf("[WARN] Service read error") + errResp := handleNotFoundError(err, d) + if errResp != nil { + log.Printf("[WARN] Returning retryable error") + return resource.RetryableError(errResp) + } } - } - if service.SupportHours != nil { - if err := d.Set("support_hours", flattenSupportHours(service.SupportHours)); err != nil { - return err + 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 != "" { + 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.ScheduledActions != nil { - if err := d.Set("scheduled_actions", flattenScheduledActions(service.ScheduledActions)); err != nil { - return err + if service.IncidentUrgencyRule != nil { + if err := d.Set("incident_urgency_rule", flattenIncidentUrgencyRule(service.IncidentUrgencyRule)); err != nil { + return resource.NonRetryableError(err) + } } - } - return nil + 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 resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error { diff --git a/pagerduty/resource_pagerduty_service_integration.go b/pagerduty/resource_pagerduty_service_integration.go index 4b140412b..84c8cb480 100644 --- a/pagerduty/resource_pagerduty_service_integration.go +++ b/pagerduty/resource_pagerduty_service_integration.go @@ -4,7 +4,9 @@ import ( "fmt" "log" "strings" + "time" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/heimweh/go-pagerduty/pagerduty" ) @@ -131,35 +133,42 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf o := &pagerduty.GetIntegrationOptions{} - serviceIntegration, _, err := client.Services.GetIntegration(service, d.Id(), o) - if err != nil { - return handleNotFoundError(err, d) - } + return resource.Retry(2*time.Minute, func() *resource.RetryError { + serviceIntegration, _, err := client.Services.GetIntegration(service, d.Id(), o) + if err != nil { + log.Printf("[WARN] Service integration read error") + errResp := handleNotFoundError(err, d) + if errResp != nil { + log.Printf("[WARN] Returning retryable error") + return resource.RetryableError(errResp) + } + } - d.Set("name", serviceIntegration.Name) - d.Set("type", serviceIntegration.Type) + d.Set("name", serviceIntegration.Name) + d.Set("type", serviceIntegration.Type) - if serviceIntegration.Service != nil { - d.Set("service", serviceIntegration.Service.ID) - } + if serviceIntegration.Service != nil { + d.Set("service", serviceIntegration.Service.ID) + } - if serviceIntegration.Vendor != nil { - d.Set("vendor", serviceIntegration.Vendor.ID) - } + if serviceIntegration.Vendor != nil { + d.Set("vendor", serviceIntegration.Vendor.ID) + } - if serviceIntegration.IntegrationKey != "" { - d.Set("integration_key", serviceIntegration.IntegrationKey) - } + if serviceIntegration.IntegrationKey != "" { + d.Set("integration_key", serviceIntegration.IntegrationKey) + } - if serviceIntegration.IntegrationEmail != "" { - d.Set("integration_email", serviceIntegration.IntegrationEmail) - } + if serviceIntegration.IntegrationEmail != "" { + d.Set("integration_email", serviceIntegration.IntegrationEmail) + } - if serviceIntegration.HTMLURL != "" { - d.Set("html_url", serviceIntegration.HTMLURL) - } + if serviceIntegration.HTMLURL != "" { + d.Set("html_url", serviceIntegration.HTMLURL) + } - return nil + return nil + }) } func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error { From 09aec9c6d35ec6d196e697cf6db186fd1179a169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wilczyn=CC=81ski?= Date: Fri, 20 Mar 2020 13:17:45 +0100 Subject: [PATCH 2/2] Revert to the original package name for a PR --- go.mod | 2 +- go.sum | 2 -- main.go | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 838a9143e..8478f776c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/mLupine/terraform-provider-pagerduty +module github.com/terraform-providers/terraform-provider-pagerduty require ( github.com/google/go-querystring v1.0.0 // indirect diff --git a/go.sum b/go.sum index 6f75ae317..9afc9446c 100644 --- a/go.sum +++ b/go.sum @@ -114,7 +114,6 @@ github.com/hashicorp/terraform-json v0.4.0 h1:KNh29iNxozP5adfUFBJ4/fWd0Cu3taGgjH github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU= github.com/hashicorp/terraform-plugin-sdk v1.7.0 h1:B//oq0ZORG+EkVrIJy0uPGSonvmXqxSzXe8+GhknoW0= github.com/hashicorp/terraform-plugin-sdk v1.7.0/go.mod h1:OjgQmey5VxnPej/buEhe+YqKm0KNvV3QqU4hkqHqPCY= -github.com/hashicorp/terraform-plugin-sdk v1.8.0 h1:HE1p52nzcgz88hIJmapUnkmM9noEjV3QhTOLaua5XUA= github.com/hashicorp/terraform-plugin-test v1.2.0 h1:AWFdqyfnOj04sxTdaAF57QqvW7XXrT8PseUHkbKsE8I= github.com/hashicorp/terraform-plugin-test v1.2.0/go.mod h1:QIJHYz8j+xJtdtLrFTlzQVC0ocr3rf/OjIpgZLK56Hs= github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg= @@ -142,7 +141,6 @@ github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3v github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/mLupine/terraform-provider-pagerduty v1.5.1 h1:m1BIqG6pdjGyhBt2opuq1hM92HodrDNLcLWfWxqIF8A= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= diff --git a/main.go b/main.go index 85a4e71cc..eb76b5bb5 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "github.com/hashicorp/terraform-plugin-sdk/plugin" - "github.com/mLupine/terraform-provider-pagerduty/pagerduty" + "github.com/terraform-providers/terraform-provider-pagerduty/pagerduty" ) func main() {