Skip to content

Commit

Permalink
[PagerDuty#273] updating pagerduty addons and extensions. Fixing page…
Browse files Browse the repository at this point in the history
…rduty services retry.
  • Loading branch information
eric-spence-code committed Aug 29, 2021
1 parent e5fd91a commit 7819cf8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 100 deletions.
47 changes: 25 additions & 22 deletions pagerduty/resource_pagerduty_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
46 changes: 24 additions & 22 deletions pagerduty/resource_pagerduty_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
111 changes: 55 additions & 56 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 7819cf8

Please sign in to comment.