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

Add Parent Field to Teams Resource #319

Merged
merged 2 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
cloud.google.com/go v0.71.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3
github.com/heimweh/go-pagerduty v0.0.0-20210401200608-e772e426d1d0
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd // indirect
google.golang.org/api v0.35.0 // indirect
google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ github.com/heimweh/go-pagerduty v0.0.0-20210226020252-e256912df9d4 h1:SdP0fGf1bS
github.com/heimweh/go-pagerduty v0.0.0-20210226020252-e256912df9d4/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3 h1:W26FTjH1Sg3ngrdGuep+t6a0fpoELVEGbZh62ykkw7k=
github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20210401200608-e772e426d1d0 h1:fF/STDApEmPMx5pxXOrliPnWim3K1w0f9Ma06OqrKeI=
github.com/heimweh/go-pagerduty v0.0.0-20210401200608-e772e426d1d0/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
5 changes: 5 additions & 0 deletions pagerduty/data_source_pagerduty_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func dataSourcePagerDutyTeam() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"parent": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -70,6 +74,7 @@ func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error
d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("description", found.Description)
d.Set("parent", found.Parent)

return nil
})
Expand Down
14 changes: 10 additions & 4 deletions pagerduty/data_source_pagerduty_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (

func TestAccDataSourcePagerDutyTeam_Basic(t *testing.T) {
name := fmt.Sprintf("tf-%s", acctest.RandString(5))
parent := fmt.Sprintf("tf-%s", acctest.RandString(5))
description := "team description"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyTeamConfig(name, description),
Config: testAccDataSourcePagerDutyTeamConfig(name, parent, description),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyTeam("pagerduty_team.test", "data.pagerduty_team.by_name"),
),
Expand All @@ -40,7 +41,7 @@ func testAccDataSourcePagerDutyTeam(src, n string) resource.TestCheckFunc {
return fmt.Errorf("Expected to get a user ID from PagerDuty")
}

testAtts := []string{"id", "name", "description"}
testAtts := []string{"id", "name", "description", "parent"}

for _, att := range testAtts {
if a[att] != srcA[att] {
Expand All @@ -52,8 +53,13 @@ func testAccDataSourcePagerDutyTeam(src, n string) resource.TestCheckFunc {
}
}

func testAccDataSourcePagerDutyTeamConfig(name, description string) string {
func testAccDataSourcePagerDutyTeamConfig(name, parent, description string) string {
return fmt.Sprintf(`
resource "pagerduty_team" "parent" {
name = "%s"
description = "parent team"
}

resource "pagerduty_team" "test" {
name = "%s"
description = "%s"
Expand All @@ -62,5 +68,5 @@ resource "pagerduty_team" "test" {
data "pagerduty_team" "by_name" {
name = pagerduty_team.test.name
}
`, name, description)
`, parent, name, description)
}
3 changes: 1 addition & 2 deletions pagerduty/resource_pagerduty_response_play.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ func resourcePagerDutyResponsePlayCreate(d *schema.ResourceData, meta interface{
d.SetId(responsePlay.ID)
d.Set("from", responsePlay.FromEmail)
log.Printf("[INFO] Created PagerDuty response play: %s (from: %s)", d.Id(), responsePlay.FromEmail)

}
return nil
})
Expand Down Expand Up @@ -305,7 +304,7 @@ func resourcePagerDutyResponsePlayUpdate(d *schema.ResourceData, meta interface{
time.Sleep(2 * time.Second)
return retryErr
}
return nil
return resourcePagerDutyResponsePlayRead(d, meta)
}

func resourcePagerDutyResponsePlayDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
39 changes: 30 additions & 9 deletions pagerduty/resource_pagerduty_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func resourcePagerDutyTeam() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"parent": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
Expand All @@ -44,7 +48,12 @@ func buildTeamStruct(d *schema.ResourceData) *pagerduty.Team {
if attr, ok := d.GetOk("description"); ok {
team.Description = attr.(string)
}

if attr, ok := d.GetOk("parent"); ok {
team.Parent = &pagerduty.TeamReference{
ID: attr.(string),
Type: "team_reference",
}
}
return team
}

Expand All @@ -55,13 +64,19 @@ func resourcePagerDutyTeamCreate(d *schema.ResourceData, meta interface{}) error

log.Printf("[INFO] Creating PagerDuty team %s", team.Name)

team, _, err := client.Teams.Create(team)
if err != nil {
return err
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if team, _, err := client.Teams.Create(team); err != nil {
return resource.RetryableError(err)
} else if team != nil {
d.SetId(team.ID)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

d.SetId(team.ID)

return resourcePagerDutyTeamRead(d, meta)

}
Expand Down Expand Up @@ -91,10 +106,16 @@ func resourcePagerDutyTeamUpdate(d *schema.ResourceData, meta interface{}) error

log.Printf("[INFO] Updating PagerDuty team %s", d.Id())

if _, _, err := client.Teams.Update(d.Id(), team); err != nil {
return err
retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if _, _, err := client.Teams.Update(d.Id(), team); err != nil {
return resource.RetryableError(err)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

return resourcePagerDutyTeamRead(d, meta)
}

Expand Down
48 changes: 46 additions & 2 deletions pagerduty/resource_pagerduty_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ func TestAccPagerDutyTeam_Basic(t *testing.T) {
})
}

func TestAccPagerDutyTeam_Parent(t *testing.T) {
team := fmt.Sprintf("tf-%s", acctest.RandString(5))
parent := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyTeamDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyTeamWithParentConfig(team, parent),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyTeamExists("pagerduty_team.foo"),
testAccCheckPagerDutyTeamExists("pagerduty_team.parent"),
resource.TestCheckResourceAttr(
"pagerduty_team.foo", "name", team),
resource.TestCheckResourceAttr(
"pagerduty_team.foo", "description", "foo"),
resource.TestCheckResourceAttrSet(
"pagerduty_team.foo", "html_url"),
resource.TestCheckResourceAttrSet(
"pagerduty_team.foo", "parent"),
resource.TestCheckResourceAttr(
"pagerduty_team.parent", "name", parent),
),
},
},
})
}

func testAccCheckPagerDutyTeamDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
Expand Down Expand Up @@ -116,6 +146,7 @@ func testAccCheckPagerDutyTeamExists(n string) resource.TestCheckFunc {

func testAccCheckPagerDutyTeamConfig(team string) string {
return fmt.Sprintf(`

resource "pagerduty_team" "foo" {
name = "%s"
description = "foo"
Expand All @@ -125,7 +156,20 @@ resource "pagerduty_team" "foo" {
func testAccCheckPagerDutyTeamConfigUpdated(team string) string {
return fmt.Sprintf(`
resource "pagerduty_team" "foo" {
name = "%s"
description = "bar"
name = "%s"
description = "bar"
}`, team)
}

func testAccCheckPagerDutyTeamWithParentConfig(team, parent string) string {
return fmt.Sprintf(`
resource "pagerduty_team" "parent" {
name = "%s"
description = "parent"
}
resource "pagerduty_team" "foo" {
name = "%s"
description = "foo"
parent = pagerduty_team.parent.id
}`, parent, team)
}
17 changes: 9 additions & 8 deletions vendor/github.com/heimweh/go-pagerduty/pagerduty/team.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ github.com/hashicorp/terraform-svchost/auth
github.com/hashicorp/terraform-svchost/disco
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3
# github.com/heimweh/go-pagerduty v0.0.0-20210401200608-e772e426d1d0
## explicit
github.com/heimweh/go-pagerduty/pagerduty
# github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ The following arguments are supported:
* `id` - The ID of the found team.
* `name` - The name of the found team.
* `description` - A description of the found team.
* `parent` - ID of the parent team. This is available to accounts with the Team Hierarchy feature enabled. Please contact your account manager for more information.

[1]: https://v1.developer.pagerduty.com/documentation/rest/teams/list
7 changes: 7 additions & 0 deletions website/docs/r/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ The account must have the `teams` ability to use the following resource.
## Example Usage

```hcl
resource "pagerduty_team" "parent" {
name = "Product Development"
description = "Product and Engineering
}

resource "pagerduty_team" "example" {
name = "Engineering"
description = "All engineering"
parent = pagerduty.team.id
}
```

Expand All @@ -28,6 +34,7 @@ The following arguments are supported:
* `name` - (Required) The name of the group.
* `description` - (Optional) A human-friendly description of the team.
If not set, a placeholder of "Managed by Terraform" will be set.
* `parent` - (Optional) ID of the parent team. This is available to accounts with the Team Hierarchy feature enabled. Please contact your account manager for more information.

## Attributes Reference

Expand Down