diff --git a/go.sum b/go.sum index 51b6f6372..930fcb56d 100644 --- a/go.sum +++ b/go.sum @@ -141,6 +141,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/pagerduty/provider.go b/pagerduty/provider.go index a626510cc..c5a5ff9a6 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -105,14 +105,17 @@ func isErrCode(err error, code int) bool { return false } +func genError(err error, d *schema.ResourceData) error { + return fmt.Errorf("Error reading: %s: %s", d.Id(), err) +} + func handleNotFoundError(err error, d *schema.ResourceData) error { if isErrCode(err, 404) { log.Printf("[WARN] Removing %s because it's gone", d.Id()) d.SetId("") return nil } - - return fmt.Errorf("Error reading: %s: %s", d.Id(), err) + return genError(err, d) } func providerConfigure(data *schema.ResourceData, terraformVersion string) (interface{}, error) { diff --git a/pagerduty/resource_pagerduty_addon.go b/pagerduty/resource_pagerduty_addon.go index 9ae0e0225..55215a032 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.(*Config).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.(*Config).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.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_extension.go b/pagerduty/resource_pagerduty_extension.go index 77935772c..3b2eb4ffd 100644 --- a/pagerduty/resource_pagerduty_extension.go +++ b/pagerduty/resource_pagerduty_extension.go @@ -86,32 +86,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.(*Config).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.(*Config).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) @@ -137,6 +117,28 @@ func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) er }) } +func resourcePagerDutyExtensionCreate(d *schema.ResourceData, meta interface{}) error { + client, _ := meta.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_extension_servicenow.go b/pagerduty/resource_pagerduty_extension_servicenow.go index 9dec22b61..9bb2b3fde 100644 --- a/pagerduty/resource_pagerduty_extension_servicenow.go +++ b/pagerduty/resource_pagerduty_extension_servicenow.go @@ -121,32 +121,12 @@ func buildExtensionServiceNowStruct(d *schema.ResourceData) *pagerduty.Extension return Extension } -func resourcePagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}) error { - client, _ := meta.(*Config).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.(*Config).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) @@ -178,6 +158,27 @@ func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta inter }) } +func resourcePagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}) error { + client, _ := meta.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_ruleset.go b/pagerduty/resource_pagerduty_ruleset.go index 55fdc1f36..6618814d3 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.(*Config).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.(*Config).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.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index bbadbbb14..152b73305 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -350,6 +350,29 @@ func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) { return &service, nil } +func fetchService(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { + client, _ := meta.(*Config).Client() + 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 := errCallback(err, d) + if errResp != nil { + time.Sleep(2 * time.Second) + return resource.RetryableError(errResp) + } + + return nil + } + + if err := flattenService(d, service); err != nil { + return resource.NonRetryableError(err) + } + return nil + + }) +} + func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error { client, _ := meta.(*Config).Client() @@ -360,39 +383,19 @@ func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) er log.Printf("[INFO] Creating PagerDuty service %s", service.Name) - createdService, _, err := client.Services.Create(service) + service, _, err = client.Services.Create(service) if err != nil { return err } - d.SetId(createdService.ID) + d.SetId(service.ID) - return flattenService(d, createdService) + return fetchService(d, meta, genError) } func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error { - client, _ := meta.(*Config).Client() - log.Printf("[INFO] Reading PagerDuty service %s", d.Id()) - - 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 { - time.Sleep(2 * time.Second) - return resource.RetryableError(errResp) - } - - return nil - } - - if err := flattenService(d, service); err != nil { - return resource.NonRetryableError(err) - } - return nil - }) + return fetchService(d, meta, handleNotFoundError) } 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 bce14f5e0..abf37bad5 100644 --- a/pagerduty/resource_pagerduty_service_integration.go +++ b/pagerduty/resource_pagerduty_service_integration.go @@ -114,41 +114,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.(*Config).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.(*Config).Client() - - log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id()) - service := d.Get("service").(string) o := &pagerduty.GetIntegrationOptions{} @@ -157,7 +124,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) @@ -193,6 +160,41 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf }) } +func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error { + client, _ := meta.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_team_membership.go b/pagerduty/resource_pagerduty_team_membership.go index aaa0f42c7..482ef2300 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.(*Config).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.(*Config).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.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_user_contact_method.go b/pagerduty/resource_pagerduty_user_contact_method.go index 2330a9872..085146a05 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.(*Config).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.(*Config).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.(*Config).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.(*Config).Client() diff --git a/pagerduty/resource_pagerduty_user_notification_rule.go b/pagerduty/resource_pagerduty_user_notification_rule.go index 37663f065..768217c48 100644 --- a/pagerduty/resource_pagerduty_user_notification_rule.go +++ b/pagerduty/resource_pagerduty_user_notification_rule.go @@ -71,35 +71,14 @@ func buildUserNotificationRuleStruct(d *schema.ResourceData) (*pagerduty.Notific return notificationRule, nil } -func resourcePagerDutyUserNotificationRuleCreate(d *schema.ResourceData, meta interface{}) error { - client, _ := meta.(*Config).Client() - - userID := d.Get("user_id").(string) - - notificationRule, err := buildUserNotificationRuleStruct(d) - if err != nil { - return err - } - - 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 { +func fetchPagerDutyUserNotificationRule(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client, _ := meta.(*Config).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) @@ -116,6 +95,30 @@ func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta inte }) } +func resourcePagerDutyUserNotificationRuleCreate(d *schema.ResourceData, meta interface{}) error { + client, _ := meta.(*Config).Client() + + userID := d.Get("user_id").(string) + + notificationRule, err := buildUserNotificationRuleStruct(d) + if err != nil { + return err + } + + 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.(*Config).Client()