Skip to content

Commit d0527de

Browse files
authored
feat(monitor): support different regions for opsgenie notification channel (#251)
1 parent 3a70cbd commit d0527de

8 files changed

+72
-25
lines changed

sysdig/internal/client/monitor/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ type NotificationChannelOptions struct {
194194
ServiceKey string `json:"serviceKey,omitempty"` // Type: PagerDuty
195195
ServiceName string `json:"serviceName,omitempty"` // Type: PagerDuty
196196
AdditionalHeaders map[string]interface{} `json:"additionalHeaders,omitempty"` // Type: Webhook
197+
Region string `json:"region,omitempty"` // Type: OpsGenie
197198

198199
NotifyOnOk bool `json:"notifyOnOk"`
199200
NotifyOnResolve bool `json:"notifyOnResolve"`

sysdig/internal/client/secure/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type NotificationChannelOptions struct {
7777
Account string `json:"account,omitempty"` // Type: PagerDuty
7878
ServiceKey string `json:"serviceKey,omitempty"` // Type: PagerDuty
7979
ServiceName string `json:"serviceName,omitempty"` // Type: PagerDuty
80+
Region string `json:"region,omitempty"` // Type: OpsGenie
8081

8182
NotifyOnOk bool `json:"notifyOnOk"`
8283
NotifyOnResolve bool `json:"notifyOnResolve"`

sysdig/resource_sysdig_monitor_notification_channel_opsgenie.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package sysdig
22

33
import (
44
"context"
5-
"fmt"
6-
"regexp"
75
"strconv"
86
"time"
97

108
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
119

1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1312

1413
"github.com/draios/terraform-provider-sysdig/sysdig/internal/client/monitor"
1514
)
@@ -38,6 +37,12 @@ func resourceSysdigMonitorNotificationChannelOpsGenie() *schema.Resource {
3837
Type: schema.TypeString,
3938
Required: true,
4039
},
40+
"region": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Default: "US",
44+
ValidateFunc: validation.StringInSlice([]string{"US", "EU"}, false),
45+
},
4146
}),
4247
}
4348
}
@@ -134,8 +139,8 @@ func monitorNotificationChannelOpsGenieFromResourceData(d *schema.ResourceData)
134139

135140
nc.Type = NOTIFICATION_CHANNEL_TYPE_OPSGENIE
136141
apiKey := d.Get("api_key").(string)
137-
nc.Options.Url = fmt.Sprintf("https://api.opsgenie.com/v1/json/sysdigcloud?apiKey=%s", apiKey)
138142
nc.Options.APIKey = apiKey
143+
nc.Options.Region = d.Get("region").(string)
139144
return
140145
}
141146

@@ -145,13 +150,8 @@ func monitorNotificationChannelOpsGenieToResourceData(nc *monitor.NotificationCh
145150
return
146151
}
147152

148-
regex, err := regexp.Compile("apiKey=(.*)?$")
149-
if err != nil {
150-
return
151-
}
152-
153-
key := regex.FindStringSubmatch(nc.Options.Url)[1]
154-
_ = d.Set("api_key", key)
153+
_ = d.Set("api_key", nc.Options.APIKey)
154+
_ = d.Set("region", nc.Options.Region)
155155

156156
return
157157
}

sysdig/resource_sysdig_monitor_notification_channel_opsgenie_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ func TestAccMonitorNotificationChannelOpsGenie(t *testing.T) {
3838
ImportState: true,
3939
ImportStateVerify: true,
4040
},
41+
{
42+
Config: monitorNotificationChannelOpsGenieWithNameAndRegion(rText()),
43+
},
44+
{
45+
ResourceName: "sysdig_monitor_notification_channel_opsgenie.sample-opsgenie-2",
46+
ImportState: true,
47+
ImportStateVerify: true,
48+
},
4149
},
4250
})
4351
}
@@ -52,3 +60,15 @@ resource "sysdig_monitor_notification_channel_opsgenie" "sample-opsgenie" {
5260
notify_when_resolved = false
5361
}`, name)
5462
}
63+
64+
func monitorNotificationChannelOpsGenieWithNameAndRegion(name string) string {
65+
return fmt.Sprintf(`
66+
resource "sysdig_monitor_notification_channel_opsgenie" "sample-opsgenie-2" {
67+
name = "Example Channel %s - OpsGenie - 2"
68+
enabled = true
69+
api_key = "2349324-342354353-5324-23"
70+
notify_when_ok = false
71+
notify_when_resolved = false
72+
region = "EU"
73+
}`, name)
74+
}

sysdig/resource_sysdig_secure_notification_channel_opsgenie.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package sysdig
22

33
import (
44
"context"
5-
"fmt"
6-
"regexp"
75
"strconv"
86
"time"
97

108
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
119

1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1312

1413
"github.com/draios/terraform-provider-sysdig/sysdig/internal/client/secure"
1514
)
@@ -38,6 +37,12 @@ func resourceSysdigSecureNotificationChannelOpsGenie() *schema.Resource {
3837
Type: schema.TypeString,
3938
Required: true,
4039
},
40+
"region": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Default: "US",
44+
ValidateFunc: validation.StringInSlice([]string{"US", "EU"}, false),
45+
},
4146
}),
4247
}
4348
}
@@ -133,8 +138,8 @@ func secureNotificationChannelOpsGenieFromResourceData(d *schema.ResourceData) (
133138

134139
nc.Type = NOTIFICATION_CHANNEL_TYPE_OPSGENIE
135140
apiKey := d.Get("api_key").(string)
136-
nc.Options.Url = fmt.Sprintf("https://api.opsgenie.com/v1/json/sysdigcloud?apiKey=%s", apiKey)
137141
nc.Options.APIKey = apiKey
142+
nc.Options.Region = d.Get("region").(string)
138143
return
139144
}
140145

@@ -144,12 +149,8 @@ func secureNotificationChannelOpsGenieToResourceData(nc *secure.NotificationChan
144149
return
145150
}
146151

147-
regex, err := regexp.Compile("apiKey=(.*)?$")
148-
if err != nil {
149-
return
150-
}
151-
key := regex.FindStringSubmatch(nc.Options.Url)[1]
152-
_ = d.Set("api_key", key)
152+
_ = d.Set("api_key", nc.Options.APIKey)
153+
_ = d.Set("region", nc.Options.Region)
153154

154155
return
155156
}

sysdig/resource_sysdig_secure_notification_channel_opsgenie_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ func TestAccSecureNotificationChannelOpsGenie(t *testing.T) {
3838
ImportState: true,
3939
ImportStateVerify: true,
4040
},
41+
{
42+
Config: secureNotificationChannelOpsGenieWithNameAndRegion(rText()),
43+
},
44+
{
45+
ResourceName: "sysdig_secure_notification_channel_opsgenie.sample-opsgenie-2",
46+
ImportState: true,
47+
ImportStateVerify: true,
48+
},
4149
},
4250
})
4351
}
@@ -52,3 +60,15 @@ resource "sysdig_secure_notification_channel_opsgenie" "sample-opsgenie" {
5260
notify_when_resolved = false
5361
}`, name)
5462
}
63+
64+
func secureNotificationChannelOpsGenieWithNameAndRegion(name string) string {
65+
return fmt.Sprintf(`
66+
resource "sysdig_secure_notification_channel_opsgenie" "sample-opsgenie-2" {
67+
name = "Example Channel %s - OpsGenie - 2"
68+
enabled = true
69+
api_key = "2349324-342354353-5324-23"
70+
notify_when_ok = false
71+
notify_when_resolved = false
72+
region = "EU"
73+
}`, name)
74+
}

website/docs/r/monitor_notification_channel_opsgenie.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ resource "sysdig_monitor_notification_channel_opsgenie" "sample-opsgenie" {
3030

3131
* `api_key` - (Required) Key for the API.
3232

33+
* `region` - (Optional) Opsgenie Region. Can be `US` or `EU`. Default is `US`.
34+
3335
* `enabled` - (Optional) If false, the channel will not emit notifications. Default is true.
3436

35-
* `notify_when_ok` - (Optional) Send a new notification when the alert condition is
37+
* `notify_when_ok` - (Optional) Send a new notification when the alert condition is
3638
no longer triggered. Default is false.
3739

38-
* `notify_when_resolved` - (Optional) Send a new notification when the alert is manually
40+
* `notify_when_resolved` - (Optional) Send a new notification when the alert is manually
3941
acknowledged by a user. Default is false.
4042

4143
* `send_test_notification` - (Optional) Send an initial test notification to check
@@ -55,4 +57,4 @@ Opsgenie notification channels for Monitor can be imported using the ID, e.g.
5557

5658
```
5759
$ terraform import sysdig_monitor_notification_channel_opsgenie.example 12345
58-
```
60+
```

website/docs/r/secure_notification_channel_opsgenie.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ resource "sysdig_secure_notification_channel_opsgenie" "sample-opsgenie" {
3030

3131
* `api_key` - (Required) Key for the API.
3232

33+
* `region` - (Optional) Opsgenie Region. Can be `US` or `EU`. Default is `US`.
34+
3335
* `enabled` - (Optional) If false, the channel will not emit notifications. Default is true.
3436

35-
* `notify_when_ok` - (Optional) Send a new notification when the alert condition is
37+
* `notify_when_ok` - (Optional) Send a new notification when the alert condition is
3638
no longer triggered. Default is false.
3739

38-
* `notify_when_resolved` - (Optional) Send a new notification when the alert is manually
40+
* `notify_when_resolved` - (Optional) Send a new notification when the alert is manually
3941
acknowledged by a user. Default is false.
4042

4143
* `send_test_notification` - (Optional) Send an initial test notification to check
@@ -55,4 +57,4 @@ Opsgenie notification channels for Secure can be imported using the ID, e.g.
5557

5658
```
5759
$ terraform import sysdig_secure_notification_channel_opsgenie.example 12345
58-
```
60+
```

0 commit comments

Comments
 (0)