Skip to content

Commit

Permalink
Merge branch 'master' into add-support-immediate-notification
Browse files Browse the repository at this point in the history
  • Loading branch information
koushik-swaminathan authored Oct 17, 2023
2 parents e7b4dae + b640450 commit da0e354
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 78 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/hashicorp/go-retryablehttp v0.6.6
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.0
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.20
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.21
github.com/pkg/errors v0.9.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.20 h1:twA4Kiaw+Vi4x5Ei3qDO1TtQf7oSuJQ+CqJB9j9lEmQ=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.20/go.mod h1:4OjcxgwdXzezqytxN534MooNmrxRD50geWZxTD7845s=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.21 h1:LaUWNfxQJdJL5wGPolEhZaKzfFYKBkDABPRl2FsFpe0=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.21/go.mod h1:4OjcxgwdXzezqytxN534MooNmrxRD50geWZxTD7845s=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
4 changes: 4 additions & 0 deletions opsgenie/data_source_opsgenie_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func dataSourceOpsGenieTeam() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"username": {
Type: schema.TypeString,
Optional: true,
},
"role": {
Type: schema.TypeString,
Optional: true,
Expand Down
56 changes: 21 additions & 35 deletions opsgenie/resource_opsgenie_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package opsgenie
import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
"github.com/opsgenie/opsgenie-go-sdk-v2/og"
"strconv"

"log"
"strings"
Expand Down Expand Up @@ -70,7 +71,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"match-all", "match-any-condition", "match-all-conditions"}, false),
},
"conditions": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -128,7 +129,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"time-of-day", "weekday-and-time-of-day"}, false),
},
"restrictions": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -160,7 +161,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
},
},
"restriction": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -238,7 +239,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
Default: false,
},
"responders": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -315,10 +316,10 @@ func resourceOpsGenieAlertPolicyCreate(ctx context.Context, d *schema.ResourceDa
IgnoreOriginalTags: &ignore_original_tags,
Priority: alert.Priority(priority),
Actions: flattenOpsgenieAlertPolicyActions(d),
Tags: flattenOpsgenieAlertPolicyTags(d),
Tags: flattenTags(d, "tags"),
}

if len(d.Get("responders").([]interface{})) > 0 {
if d.Get("responders").(*schema.Set).Len() > 0 {
createRequest.Responders = expandOpsGenieAlertPolicyResponders(d)
}

Expand Down Expand Up @@ -434,10 +435,10 @@ func resourceOpsGenieAlertPolicyUpdate(d *schema.ResourceData, meta interface{})
IgnoreOriginalTags: &ignore_original_tags,
Priority: alert.Priority(priority),
Actions: flattenOpsgenieAlertPolicyActions(d),
Tags: flattenOpsgenieAlertPolicyTags(d),
Tags: flattenTags(d, "tags"),
}

if len(d.Get("responders").([]interface{})) > 0 {
if d.Get("responders").(*schema.Set).Len() > 0 {
updateRequest.Responders = expandOpsGenieAlertPolicyResponders(d)
}

Expand Down Expand Up @@ -499,14 +500,14 @@ func expandOpsGenieAlertPolicyRequestMainFields(d *schema.ResourceData) *policy.
}

func expandOpsGenieAlertPolicyResponders(d *schema.ResourceData) *[]alert.Responder {
input := d.Get("responders").([]interface{})
responders := make([]alert.Responder, 0, len(input))
input := d.Get("responders").(*schema.Set)
responders := make([]alert.Responder, 0, input.Len())

if input == nil {
return &responders
}

for _, v := range input {
for _, v := range input.List() {
config := v.(map[string]interface{})
responderID := config["id"].(string)
name := config["name"].(string)
Expand Down Expand Up @@ -535,19 +536,19 @@ func expandOpsGenieAlertPolicyFilter(input []interface{}) *og.Filter {
for _, v := range input {
config := v.(map[string]interface{})
filter.ConditionMatchType = og.ConditionMatchType(config["type"].(string))
filter.Conditions = expandOpsGenieAlertPolicyFilterConditions(config["conditions"].([]interface{}))
filter.Conditions = expandOpsGenieAlertPolicyFilterConditions(config["conditions"].(*schema.Set))
}
return &filter
}

func expandOpsGenieAlertPolicyFilterConditions(input []interface{}) []og.Condition {
conditions := make([]og.Condition, 0, len(input))
func expandOpsGenieAlertPolicyFilterConditions(input *schema.Set) []og.Condition {
conditions := make([]og.Condition, 0, input.Len())
condition := og.Condition{}
if input == nil {
return conditions
}

for _, v := range input {
for _, v := range input.List() {
config := v.(map[string]interface{})
not_value := config["not"].(bool)
order := config["order"].(int)
Expand All @@ -567,9 +568,9 @@ func expandOpsGenieAlertPolicyTimeRestriction(d []interface{}) *og.TimeRestricti
for _, v := range d {
config := v.(map[string]interface{})
timeRestriction.Type = og.RestrictionType(config["type"].(string))
if len(config["restrictions"].([]interface{})) > 0 {
restrictionList := make([]og.Restriction, 0, len(config["restrictions"].([]interface{})))
for _, v := range config["restrictions"].([]interface{}) {
if config["restrictions"].(*schema.Set).Len() > 0 {
restrictionList := make([]og.Restriction, 0, config["restrictions"].(*schema.Set).Len())
for _, v := range config["restrictions"].(*schema.Set).List() {
config := v.(map[string]interface{})
startHour := uint32(config["start_hour"].(int))
startMin := uint32(config["start_min"].(int))
Expand All @@ -588,7 +589,7 @@ func expandOpsGenieAlertPolicyTimeRestriction(d []interface{}) *og.TimeRestricti
timeRestriction.RestrictionList = restrictionList
} else {
restriction := og.Restriction{}
for _, v := range config["restriction"].([]interface{}) {
for _, v := range config["restriction"].(*schema.Set).List() {
config := v.(map[string]interface{})
startHour := uint32(config["start_hour"].(int))
startMin := uint32(config["start_min"].(int))
Expand Down Expand Up @@ -681,21 +682,6 @@ func flattenOpsgenieAlertPolicyTimeRestriction(input *og.TimeRestriction) []map[
return output
}

func flattenOpsgenieAlertPolicyTags(d *schema.ResourceData) []string {
input := d.Get("tags").(*schema.Set)
tags := make([]string, len(input.List()))

if input == nil {
return tags
}

for k, v := range input.List() {
tags[k] = v.(string)
}

return tags
}

func flattenOpsgenieAlertPolicyActions(d *schema.ResourceData) []string {
input := d.Get("actions").(*schema.Set)
actions := make([]string, len(input.List()))
Expand Down
19 changes: 2 additions & 17 deletions opsgenie/resource_opsgenie_heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func resourceOpsgenieHeartbeatCreate(d *schema.ResourceData, meta interface{}) e
IntervalUnit: heartbeat.Unit(intervalUnit),
Enabled: &enabled,
AlertMessage: alertMessage,
AlertTag: flattenTags(d),
AlertTag: flattenTags(d, "alert_tags"),
AlertPriority: alertPriority,
}
if ownerTeamId != "" {
Expand Down Expand Up @@ -151,7 +151,7 @@ func resourceOpsgenieHeartbeatUpdate(d *schema.ResourceData, meta interface{}) e
IntervalUnit: heartbeat.Unit(intervalUnit),
Enabled: &enabled,
AlertMessage: alertMessage,
AlertTag: flattenTags(d),
AlertTag: flattenTags(d, "alert_tags"),
AlertPriority: alertPriority,
}
if ownerTeamId != "" {
Expand Down Expand Up @@ -182,21 +182,6 @@ func resourceOpsgenieHeartbeatDelete(d *schema.ResourceData, meta interface{}) e
return nil
}

func flattenTags(d *schema.ResourceData) []string {
input := d.Get("alert_tags").(*schema.Set)
tags := make([]string, len(input.List()))

if input == nil {
return tags
}

for k, v := range input.List() {
tags[k] = v.(string)
}

return tags
}

func validateOpsgenieHeartbeat(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-zA-Z0-9._-]+$`).MatchString(value) {
Expand Down
9 changes: 8 additions & 1 deletion opsgenie/resource_opsgenie_integration_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,14 @@ func expandOpsgenieIntegrationActions(input interface{}) []integration.Integrati
action.CustomPriority = customPriority.(string)
}
filters := expandOpsgenieFilter(inputMap["filter"].([]interface{}))
action.Filter = &filters

// If a filter is not included in the resource, this will still set an empty filter. `omitempty` will
// only leave out the key when marshaling the Json if its value is `nil`
if filters.ConditionMatchType == "" && len(filters.Conditions) == 0 {
action.Filter = nil
} else {
action.Filter = &filters
}

if action.Type == integration.Create {
action.Source = inputMap["source"].(string)
Expand Down
13 changes: 13 additions & 0 deletions opsgenie/resource_opsgenie_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func resourceOpsGenieService() *schema.Resource {
Optional: true,
ValidateFunc: validateOpsGenieServiceDescription,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 20,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand All @@ -48,11 +56,13 @@ func resourceOpsGenieServiceCreate(d *schema.ResourceData, meta interface{}) err
name := d.Get("name").(string)
teamId := d.Get("team_id").(string)
description := d.Get("description").(string)
tags := flattenTags(d, "tags")

createRequest := &service.CreateRequest{
Name: name,
TeamId: teamId,
Description: description,
Tags: tags,
}

log.Printf("[INFO] Creating OpsGenie service '%s'", name)
Expand Down Expand Up @@ -85,6 +95,7 @@ func resourceOpsGenieServiceRead(d *schema.ResourceData, meta interface{}) error
d.Set("name", res.Service.Name)
d.Set("team_id", res.Service.TeamId)
d.Set("description", res.Service.Description)
d.Set("tags", res.Service.Tags)

return nil
}
Expand All @@ -96,13 +107,15 @@ func resourceOpsGenieServiceUpdate(d *schema.ResourceData, meta interface{}) err
}
name := d.Get("name").(string)
description := d.Get("description").(string)
tags := flattenTags(d, "tags")

log.Printf("[INFO] Updating OpsGenie service '%s'", name)

updateRequest := &service.UpdateRequest{
Id: d.Id(),
Name: name,
Description: description,
Tags: tags,
}

_, err = client.Update(context.Background(), updateRequest)
Expand Down
7 changes: 6 additions & 1 deletion opsgenie/resource_opsgenie_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func resourceOpsGenieTeam() *schema.Resource {
Type: schema.TypeString,
Required: true,
},

"username": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"role": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -202,6 +206,7 @@ func flattenOpsGenieTeamMembers(input []team.Member) []map[string]interface{} {
for _, inputMember := range input {
outputMember := make(map[string]interface{})
outputMember["id"] = inputMember.User.ID
outputMember["username"] = inputMember.User.Username
outputMember["role"] = inputMember.Role
members = append(members, outputMember)
}
Expand Down
16 changes: 16 additions & 0 deletions opsgenie/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,26 @@ func convertStringMapToInterfaceMap(old map[string]string) map[string]interface{
}
return new
}

func convertStringSliceToInterfaceSlice(old []string) []interface{} {
new := make([]interface{}, len(old))
for k, v := range old {
new[k] = v
}
return new
}

func flattenTags(d *schema.ResourceData, fieldName string) []string {
input := d.Get(fieldName).(*schema.Set)
tags := make([]string, len(input.List()))

if input == nil {
return tags
}

for k, v := range input.List() {
tags[k] = v.(string)
}

return tags
}

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 @@ -173,7 +173,7 @@ github.com/mitchellh/reflectwalk
# github.com/oklog/run v1.0.0
## explicit
github.com/oklog/run
# github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.20
# github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.21
## explicit; go 1.12
github.com/opsgenie/opsgenie-go-sdk-v2/alert
github.com/opsgenie/opsgenie-go-sdk-v2/client
Expand Down
Loading

0 comments on commit da0e354

Please sign in to comment.