Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added team to pagerduty_business_service resource #246

Merged
merged 3 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions pagerduty/resource_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package pagerduty

import (
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)
Expand Down Expand Up @@ -51,6 +53,10 @@ func resourcePagerDutyBusinessService() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"team": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
Expand All @@ -77,27 +83,37 @@ func buildBusinessServiceStruct(d *schema.ResourceData) (*pagerduty.BusinessServ
if attr, ok := d.GetOk("html_url"); ok {
businessService.HTMLUrl = attr.(string)
}
if attr, ok := d.GetOk("team"); ok {
businessService.Team = &pagerduty.BusinessServiceTeam{
ID: attr.(string),
}
}

return &businessService, nil
}

func resourcePagerDutyBusinessServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

businessService, err := buildBusinessServiceStruct(d)
if err != nil {
return err
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {

businessService, err := buildBusinessServiceStruct(d)
if err != nil {
return resource.NonRetryableError(err)
}
log.Printf("[INFO] Creating PagerDuty business service %s", businessService.Name)
if businessService, _, err = client.BusinessServices.Create(businessService); err != nil {
return resource.RetryableError(err)
} else if businessService != nil {
d.SetId(businessService.ID)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

log.Printf("[INFO] Creating PagerDuty business service %s", businessService.Name)

businessService, _, err = client.BusinessServices.Create(businessService)
if err != nil {
return err
}

d.SetId(businessService.ID)

return resourcePagerDutyBusinessServiceRead(d, meta)
}

Expand All @@ -106,19 +122,29 @@ func resourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface

log.Printf("[INFO] Reading PagerDuty business service %s", d.Id())

businessService, _, err := client.BusinessServices.Get(d.Id())
if err != nil {
return handleNotFoundError(err, d)
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if businessService, _, err := client.BusinessServices.Get(d.Id()); err != nil {
return resource.RetryableError(err)
} else if businessService != nil {
d.Set("name", businessService.Name)
d.Set("html_url", businessService.HTMLUrl)
d.Set("description", businessService.Description)
d.Set("type", businessService.Type)
d.Set("point_of_contact", businessService.PointOfContact)
d.Set("summary", businessService.Summary)
d.Set("self", businessService.Self)
if businessService.Team != nil {
d.Set("team", businessService.Team.ID)
}
}
return nil
})

if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

d.Set("name", businessService.Name)
d.Set("html_url", businessService.HTMLUrl)
d.Set("description", businessService.Description)
d.Set("type", businessService.Type)
d.Set("point_of_contact", businessService.PointOfContact)
d.Set("summary", businessService.Summary)
d.Set("self", businessService.Self)

return nil
}

Expand Down
46 changes: 46 additions & 0 deletions pagerduty/resource_pagerduty_business_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ func TestAccPagerDutyBusinessService_Basic(t *testing.T) {
},
})
}

func TestAccPagerDutyBusinessService_WithTeam(t *testing.T) {
businessService := fmt.Sprintf("tf-%s", acctest.RandString(5))
teamName := fmt.Sprintf("tf-%s", acctest.RandString(5))
description := fmt.Sprintf("tf-%s", acctest.RandString(5))
pointOfContact := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyBusinessServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyBusinessServiceWithTeamConfig(businessService, teamName, description, pointOfContact),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyBusinessServiceExists("pagerduty_business_service.bar"),
resource.TestCheckResourceAttr(
"pagerduty_business_service.bar", "name", businessService),
resource.TestCheckResourceAttr(
"pagerduty_business_service.bar", "description", description),
resource.TestCheckResourceAttr(
"pagerduty_business_service.bar", "point_of_contact", pointOfContact),
resource.TestCheckResourceAttrSet(
"pagerduty_business_service.bar", "self"),
),
},
},
})
}

func testAccCheckPagerDutyBusinessServiceExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -150,3 +180,19 @@ resource "pagerduty_business_service" "foo" {
}
`, name, description, poc)
}

func testAccCheckPagerDutyBusinessServiceWithTeamConfig(businessServiceName, teamName, description, poc string) string {
return fmt.Sprintf(`

resource "pagerduty_team" "bar" {
name = "%s"
}

resource "pagerduty_business_service" "bar" {
name = "%s"
description = "%s"
point_of_contact = "%s"
team = pagerduty_team.bar.id
}
`, teamName, businessServiceName, description, poc)
}
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_ruleset_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func resourcePagerDutyRulesetRuleCreate(d *schema.ResourceData, meta interface{}

log.Printf("[INFO] Creating PagerDuty ruleset rule for ruleset: %s", rule.Ruleset.ID)

retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if rule, _, err := client.Rulesets.CreateRule(rule.Ruleset.ID, rule); err != nil {
return resource.RetryableError(err)
} else if rule != nil {
Expand All @@ -621,7 +621,7 @@ func resourcePagerDutyRulesetRuleRead(d *schema.ResourceData, meta interface{})
log.Printf("[INFO] Reading PagerDuty ruleset rule: %s", d.Id())
rulesetID := d.Get("ruleset").(string)

return resource.Retry(30*time.Second, func() *resource.RetryError {
return resource.Retry(2*time.Minute, func() *resource.RetryError {
if rule, _, err := client.Rulesets.GetRule(rulesetID, d.Id()); err != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/business_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ resource "pagerduty_business_service" "example" {
name = "My Web App"
description = "A very descriptive description of this business service"
point_of_contact = "PagerDuty Admin"
team = "P37RSRS"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we run a terraform fmt against this snippet? 🙂

}
```

Expand All @@ -30,7 +31,8 @@ The following arguments are supported:
If not set, a placeholder of "Managed by Terraform" will be set.
* `point_of_contact` - (Optional) The owner of the business service.
* `type` - (Optional) Default value is `business_service`. Can also be set as `business_service_reference`.

* `team` - (Optional) ID of the team that owns the business service.

## Attributes Reference

The following attributes are exported:
Expand Down