diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cb7a880..23189493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/opsgenie/resource_opsgenie_team.go b/opsgenie/resource_opsgenie_team.go index 0b760bbc..31abe659 100644 --- a/opsgenie/resource_opsgenie_team.go +++ b/opsgenie/resource_opsgenie_team.go @@ -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" @@ -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, @@ -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) } @@ -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 +} diff --git a/website/docs/r/team.html.markdown b/website/docs/r/team.html.markdown index 7a8c835f..ee46bd96 100644 --- a/website/docs/r/team.html.markdown +++ b/website/docs/r/team.html.markdown @@ -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 } ``` @@ -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: