Skip to content

Commit

Permalink
Set tags when creating a new service (#149)
Browse files Browse the repository at this point in the history
* Support tags when creating and updating services

* update sdk
  • Loading branch information
multani authored Oct 16, 2023
1 parent d4378be commit fac4479
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 36 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
19 changes: 2 additions & 17 deletions opsgenie/resource_opsgenie_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ 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 d.Get("responders").(*schema.Set).Len() > 0 {
Expand Down Expand Up @@ -435,7 +435,7 @@ 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 d.Get("responders").(*schema.Set).Len() > 0 {
Expand Down Expand Up @@ -682,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
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
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

0 comments on commit fac4479

Please sign in to comment.