Skip to content

Commit

Permalink
add optional argument to stop maintaining team members, refs #65 (#79)
Browse files Browse the repository at this point in the history
* add optional setting to stop maintaing team members, refs #65

Signed-off-by: Arnold Bechtoldt <[email protected]>
  • Loading branch information
arnisoph authored Mar 19, 2020
1 parent 8765153 commit a1764cf
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
IMPROVEMENTS:

* **Show warning if Opsgenie username (email addr) contains uppercase characters. This lead to unexpected behaviour in the past.**
* **Updated Resource opsgenie_team:** New optional argument *ignore_members* added to change team membership management behaviour (#65). The provider will add this argument to every new/existing opsgenie_team resource state with the default value (false).

## 0.2.8 (February 07, 2020)

Expand Down
23 changes: 19 additions & 4 deletions opsgenie/resource_opsgenie_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func resourceOpsGenieTeam() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"ignore_members": {
Type: schema.TypeBool,
Optional: true,
},
"member": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -63,10 +67,13 @@ func resourceOpsGenieTeamCreate(d *schema.ResourceData, meta interface{}) error
createRequest := &team.CreateTeamRequest{
Name: name,
Description: description,
Members: expandOpsGenieTeamMembers(d),
}

log.Printf("[INFO] Creating OpsGenie team '%s'", name)
if len(d.Get("member").([]interface{})) > 0 && !d.Get("ignore_members").(bool) {
createRequest.Members = expandOpsGenieTeamMembers(d)
}

log.Printf("[INFO] Creating OpsGenie team %q", name)

_, err = client.Create(context.Background(), createRequest)
if err != nil {
Expand Down Expand Up @@ -99,14 +106,19 @@ func resourceOpsGenieTeamRead(d *schema.ResourceData, meta interface{}) error {
IdentifierValue: d.Id(),
}

log.Printf("[INFO] Retrieving state of OpsGenie team '%s'", d.Get("name"))

getResponse, err := client.Get(context.Background(), getRequest)
if err != nil {
return err
}

d.Set("name", getResponse.Name)
d.Set("description", getResponse.Description)
d.Set("member", flattenOpsGenieTeamMembers(getResponse.Members))

if !d.Get("ignore_members").(bool) {
d.Set("member", flattenOpsGenieTeamMembers(getResponse.Members))
}

return nil
}
Expand All @@ -123,7 +135,10 @@ func resourceOpsGenieTeamUpdate(d *schema.ResourceData, meta interface{}) error
Id: d.Id(),
Name: name,
Description: description,
Members: expandOpsGenieTeamMembers(d),
}

if len(d.Get("member").([]interface{})) > 0 && !d.Get("ignore_members").(bool) {
updateRequest.Members = expandOpsGenieTeamMembers(d)
}

log.Printf("[INFO] Updating OpsGenie team '%s'", name)
Expand Down
118 changes: 118 additions & 0 deletions opsgenie/resource_opsgenie_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,108 @@ func TestAccOpsGenieTeam_basic(t *testing.T) {
})
}

func TestAccOpsGenieTeam_basicNoMember(t *testing.T) {
randomTeam := acctest.RandString(6)
randomUser := acctest.RandString(6)
config := testAccOpsGenieTeam_basicNoMember(randomUser, randomTeam)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testCheckOpsGenieTeamDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckOpsGenieTeamExists("opsgenie_team.test"),
testEmptyMemberList("opsgenie_team.test"),
addTeamMembers("opsgenie_team.test", "opsgenie_user.test"),
),
},
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testEmptyMemberList("opsgenie_team.test"),
removeAllTeamMembers("opsgenie_team.test"),
),
},
},
})
}

func testEmptyMemberList(teamName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
teamResource, ok := s.RootModule().Resources[teamName]
if !ok {
return fmt.Errorf("Not found: %s", teamName)
}

if len(teamResource.Primary.Attributes["member"]) > 0 {
return fmt.Errorf("member list in state of resource %q is unexpectely not empty", teamName)
}

return nil
}
}

func addTeamMembers(teamName string, userName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
teamResource, ok := s.RootModule().Resources[teamName]
if !ok {
return fmt.Errorf("Not found: %s", teamName)
}

userResource, ok := s.RootModule().Resources[userName]
if !ok {
return fmt.Errorf("Not found: %s", userName)
}

updateRequest := team.UpdateTeamRequest{
Id: teamResource.Primary.Attributes["id"],
Name: teamResource.Primary.Attributes["name"],
Description: teamResource.Primary.Attributes["description"],
Members: []team.Member{
{
User: team.User{ID: userResource.Primary.ID},
},
},
}

return updateTeamMembers(s, updateRequest)
}
}

func removeAllTeamMembers(teamName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
teamResource, ok := s.RootModule().Resources[teamName]
if !ok {
return fmt.Errorf("Not found: %s", teamName)
}

updateRequest := team.UpdateTeamRequest{
Id: teamResource.Primary.Attributes["id"],
Name: teamResource.Primary.Attributes["name"],
Description: teamResource.Primary.Attributes["description"],
Members: []team.Member{},
}

return updateTeamMembers(s, updateRequest)
}
}

func updateTeamMembers(s *terraform.State, updateRequest team.UpdateTeamRequest) error {
client, err := team.NewClient(testAccProvider.Meta().(*OpsgenieClient).client.Config)
if err != nil {
return err
}

_, err = client.Update(context.Background(), &updateRequest)
if err != nil {
return fmt.Errorf("error updating team %q with members %q", updateRequest.Name, updateRequest.Members)
}

return nil
}

func TestAccOpsGenieTeam_complete(t *testing.T) {
randomTeam := acctest.RandString(6)
randomUser := acctest.RandString(6)
Expand Down Expand Up @@ -156,6 +258,22 @@ resource "opsgenie_team" "test" {
`, rString)
}

func testAccOpsGenieTeam_basicNoMember(randomUser, rString string) string {
return fmt.Sprintf(`
resource "opsgenie_team" "test" {
name = "genieteam-%s"
description = "This team deals with all the things"
ignore_members = true
depends_on = [opsgenie_user.test] # Just a hack for the test to destroy resources in the right order
}
resource "opsgenie_user" "test" {
username = "genietest-%[email protected]"
full_name = "Acceptance Test User"
role = "User"
}
`, randomUser, rString)
}

func testAccOpsGenieTeam_complete(randomUser, randomTeam string) string {
return fmt.Sprintf(`
resource "opsgenie_user" "test" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ resource "opsgenie_team" "test" {
role = "user"
}
}
resource "opsgenie_team" "self-service" {
name = "Self Service"
description = "Membership in this team is managed via OpsGenie web UI only"
ignore_members = false
}
```

## Argument Reference
Expand All @@ -49,6 +55,8 @@ The following arguments are supported:

* `description` - (Optional) A description for this team.

* `ignore_members` - (Optional) Set to false to ignore any configured member blocks and any team member added/updated/removed via OpsGenie web UI. Use this option e.g. to maintain membership via web UI only and use it only for new teams. Changing the value for existing teams might lead to strange behaviour. Defaults to false.

* `member` - (Optional) A Member block as documented below.

`member` supports the following:
Expand Down

0 comments on commit a1764cf

Please sign in to comment.