From ab529e3493f8b27b2a0d07415f668d724b577d07 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 28 Aug 2021 17:19:21 -0400 Subject: [PATCH] [#274] adding servince now extension --- ...resource_pagerduty_extension_servicenow.go | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/pagerduty/resource_pagerduty_extension_servicenow.go b/pagerduty/resource_pagerduty_extension_servicenow.go index c375eb356..cb8ac3e52 100644 --- a/pagerduty/resource_pagerduty_extension_servicenow.go +++ b/pagerduty/resource_pagerduty_extension_servicenow.go @@ -116,32 +116,12 @@ func buildExtensionServiceNowStruct(d *schema.ResourceData) *pagerduty.Extension return Extension } -func resourcePagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*pagerduty.Client) - - extension := buildExtensionServiceNowStruct(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 resourcePagerDutyExtensionServiceNowRead(d, meta) -} - -func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta interface{}) error { +func fetchPagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) 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) @@ -173,6 +153,27 @@ func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta inter }) } +func resourcePagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + extension := buildExtensionServiceNowStruct(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 fetchPagerDutyExtensionServiceNowCreate(d, meta, genError) +} + +func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[INFO] Reading PagerDuty extension %s", d.Id()) + return fetchPagerDutyExtensionServiceNowCreate(d, meta, handleNotFoundError) +} + func resourcePagerDutyExtensionServiceNowUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client)