Skip to content

Commit

Permalink
use flag to delete default escalation and schedule (#174)
Browse files Browse the repository at this point in the history
add a flag to delete default escalation schedule and change routing rule to None. 
Signed-off-by: Fahri YARDIMCI
  • Loading branch information
Fahri YARDIMCI authored Aug 26, 2020
1 parent 5c049d5 commit 9d7ce64
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.7 (August 26, 2020)

IMPROVEMENTS:
* **Team :** allow users to delete default resources while creating team.


## 0.4.6 (August 21, 2020)

BUGFIX:
Expand Down
116 changes: 116 additions & 0 deletions opsgenie/resource_opsgenie_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package opsgenie

import (
"context"
"errors"
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
"github.com/opsgenie/opsgenie-go-sdk-v2/escalation"
"github.com/opsgenie/opsgenie-go-sdk-v2/schedule"
"log"

"fmt"
Expand Down Expand Up @@ -34,6 +38,11 @@ func resourceOpsGenieTeam() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"delete_default_resources": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"member": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -92,6 +101,24 @@ func resourceOpsGenieTeamCreate(d *schema.ResourceData, meta interface{}) error

d.SetId(getResponse.Id)

shouldDeleteDefaultResources := d.Get("delete_default_resources").(bool)

if shouldDeleteDefaultResources {
err = findAndUpdateDefaultRoutingRule(name, meta.(*OpsgenieClient).client.Config)
if err != nil {
return err
}

err := findAndDeleteDefaultEscalation(name, meta.(*OpsgenieClient).client.Config)
if err != nil {
return err
}

err = findAndDeleteDefaultSchedule(name, meta.(*OpsgenieClient).client.Config)
if err != nil {
return err
}
}
return resourceOpsGenieTeamRead(d, meta)
}

Expand Down Expand Up @@ -222,3 +249,92 @@ func validateOpsGenieTeamName(v interface{}, k string) (ws []string, errors []er

return
}

func findAndDeleteDefaultSchedule(teamName string, config *client.Config) error {
scheduleClient, err := schedule.NewClient(config)
if err != nil {
return err
}
expand := true
res, err := scheduleClient.List(context.Background(), &schedule.ListRequest{
Expand: &expand,
})
if err != nil {
return err
}
for _, sched := range res.Schedule {
ownerTeam := sched.OwnerTeam
if ownerTeam != nil {
if ownerTeam.Name == teamName {
_, err = scheduleClient.Delete(context.Background(), &schedule.DeleteRequest{
IdentifierType: schedule.Id,
IdentifierValue: sched.Id,
})
if err != nil {
return err
}
return nil
}
}
}

return errors.New("Could not find any schedule name for this team")
}

func findAndDeleteDefaultEscalation(teamName string, config *client.Config) error {
escalationClient, err := escalation.NewClient(config)
if err != nil {
return err
}
res, err := escalationClient.List(context.Background())
if err != nil {
return err
}
for _, escal := range res.Escalations {
ownerTeam := escal.OwnerTeam
if ownerTeam != nil {
if ownerTeam.Name == teamName {
_, err = escalationClient.Delete(context.Background(), &escalation.DeleteRequest{
IdentifierType: escalation.Id,
Identifier: escal.Id,
})
if err != nil {
return err
}
return nil
}
}
}

return errors.New("Could not find any escalation for this team")
}

func findAndUpdateDefaultRoutingRule(teamName string, config *client.Config) error {
teamClient, err := team.NewClient(config)
if err != nil {
return err
}
rules, err := teamClient.ListRoutingRules(context.Background(), &team.ListRoutingRulesRequest{
TeamIdentifierType: team.Name,
TeamIdentifierValue: teamName,
})
if err != nil {
return err
}

for _, rule := range rules.RoutingRules {
_, err := teamClient.UpdateRoutingRule(context.Background(), &team.UpdateRoutingRuleRequest{
TeamIdentifierType: team.Name,
TeamIdentifierValue: teamName,
RoutingRuleId: rule.Id,
Notify: &team.Notify{
Type: team.None,
},
})

if err != nil {
return err
}
}
return nil
}
4 changes: 4 additions & 0 deletions website/docs/r/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ resource "opsgenie_team" "self-service" {
name = "Self Service"
description = "Membership in this team is managed via OpsGenie web UI only"
ignore_members = true
delete_default_resources = true
}
```

Expand All @@ -57,6 +58,9 @@ The following arguments are supported:

* `ignore_members` - (Optional) Set to true 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.

* `delete_default_resources` - (Optional) Set to true to remove default escalation and schedule for newly created team. **Be careful its also changes that team routing rule to None. That means you have to define routing rule as well**


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

`member` supports the following:
Expand Down

0 comments on commit 9d7ce64

Please sign in to comment.