Skip to content

Commit b2ca52f

Browse files
authored
Notification enhancements (#118)
* add blueprint publishing events to email notification * make notification client generic to support slack, teams and webhook notification channels * generic webhook, slack and teams notifications resources * update example and docs
1 parent a365aaa commit b2ca52f

26 files changed

+2723
-34
lines changed

client/models.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ type SubscriptionsRequest struct {
178178
}
179179

180180
type SubscriptionsTargetRequest struct {
181-
Type string `json:"type"`
182-
Description string `json:"description"`
181+
Type string `json:"type"`
182+
Description string `json:"description"`
183+
WebHook *string `json:"web_hook,omitempty"`
184+
Token *string `json:"token,omitempty"`
183185
}
184186

185187
type SubscriptionsWorkflowEventNotifierRequest struct {

client/notification.go

Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,61 @@ import (
88
"net/http"
99
)
1010

11-
func (c *Client) CreateSpaceEmailNotification(space_name string, notification_name string, environment_launched bool,
11+
const (
12+
email_notification_type = "Email"
13+
slack_notification_type = "Slack"
14+
teams_notification_type = "Teams"
15+
webhook_notification_type = "GenericWebhook"
16+
)
17+
18+
func (c *Client) CreateSpaceNotification(notification_type string, space_name string, notification_name string, environment_launched bool,
1219
environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool,
1320
drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool,
1421
collaborator_added bool, action_failed bool, environment_ending_failed bool, environment_ended bool,
15-
environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, idle_reminder []int64) (string, error) {
16-
17-
data := SubscriptionsRequest{
18-
Name: notification_name,
19-
Description: "",
20-
Target: SubscriptionsTargetRequest{
21-
Type: "Email",
22+
environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, blueprint_published bool, blueprint_unpublished bool, idle_reminder []int64, webhook *string, token *string) (string, error) {
23+
var data SubscriptionsRequest
24+
switch notification_type {
25+
case email_notification_type:
26+
data = SubscriptionsRequest{
27+
Name: notification_name,
28+
Description: "",
29+
Target: SubscriptionsTargetRequest{
30+
Type: email_notification_type,
31+
Description: "",
32+
},
33+
}
34+
case slack_notification_type:
35+
data = SubscriptionsRequest{
36+
Name: notification_name,
37+
Description: "",
38+
Target: SubscriptionsTargetRequest{
39+
Type: slack_notification_type,
40+
Description: "",
41+
WebHook: webhook,
42+
},
43+
}
44+
case teams_notification_type:
45+
data = SubscriptionsRequest{
46+
Name: notification_name,
2247
Description: "",
23-
},
48+
Target: SubscriptionsTargetRequest{
49+
Type: teams_notification_type,
50+
Description: "",
51+
WebHook: webhook,
52+
},
53+
}
54+
case webhook_notification_type:
55+
data = SubscriptionsRequest{
56+
Name: notification_name,
57+
Description: "",
58+
Target: SubscriptionsTargetRequest{
59+
Type: webhook_notification_type,
60+
Description: "",
61+
WebHook: webhook,
62+
Token: token,
63+
},
64+
}
2465
}
25-
2666
if environment_launched {
2767
data.Events = append(data.Events, "EnvironmentLaunched")
2868
}
@@ -73,10 +113,15 @@ func (c *Client) CreateSpaceEmailNotification(space_name string, notification_na
73113
if environment_active_with_error {
74114
data.Events = append(data.Events, "EnvironmentActiveWithError")
75115
}
76-
116+
if blueprint_published {
117+
data.Events = append(data.Events, "BlueprintPublished")
118+
}
119+
if blueprint_unpublished {
120+
data.Events = append(data.Events, "BlueprintUnpublished")
121+
}
77122
payload, err := json.Marshal(data)
78123
if err != nil {
79-
log.Fatalf("impossible to marshall update space request: %s", err)
124+
log.Fatalf("impossible to marshall space notification request: %s", err)
80125
}
81126

82127
req, err := http.NewRequest("POST", fmt.Sprintf("%sapi/spaces/%s/subscriptions", c.HostURL, space_name), bytes.NewReader(payload))
@@ -95,19 +140,54 @@ func (c *Client) CreateSpaceEmailNotification(space_name string, notification_na
95140
return string(body), nil
96141
}
97142

98-
func (c *Client) UpdateSpaceEmailNotification(notification_id string, space_name string, notification_name string, environment_launched bool,
143+
func (c *Client) UpdateSpaceNotification(notification_id string, notification_type string, space_name string, notification_name string, environment_launched bool,
99144
environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool,
100145
drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool,
101146
collaborator_added bool, action_failed bool, environment_ending_failed bool, environment_ended bool,
102-
environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, idle_reminder []int64) (string, error) {
147+
environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, blueprint_published bool, blueprint_unpublished bool, idle_reminder []int64, webhook *string, token *string) (string, error) {
103148

104-
data := SubscriptionsRequest{
105-
Name: notification_name,
106-
Description: "",
107-
Target: SubscriptionsTargetRequest{
108-
Type: "Email",
149+
var data SubscriptionsRequest
150+
switch notification_type {
151+
case email_notification_type:
152+
data = SubscriptionsRequest{
153+
Name: notification_name,
154+
Description: "",
155+
Target: SubscriptionsTargetRequest{
156+
Type: email_notification_type,
157+
Description: "",
158+
},
159+
}
160+
case slack_notification_type:
161+
data = SubscriptionsRequest{
162+
Name: notification_name,
163+
Description: "",
164+
Target: SubscriptionsTargetRequest{
165+
Type: slack_notification_type,
166+
Description: "",
167+
WebHook: webhook,
168+
},
169+
}
170+
case teams_notification_type:
171+
data = SubscriptionsRequest{
172+
Name: notification_name,
109173
Description: "",
110-
},
174+
Target: SubscriptionsTargetRequest{
175+
Type: teams_notification_type,
176+
Description: "",
177+
WebHook: webhook,
178+
},
179+
}
180+
case webhook_notification_type:
181+
data = SubscriptionsRequest{
182+
Name: notification_name,
183+
Description: "",
184+
Target: SubscriptionsTargetRequest{
185+
Type: webhook_notification_type,
186+
Description: "",
187+
WebHook: webhook,
188+
Token: token,
189+
},
190+
}
111191
}
112192

113193
if environment_launched {
@@ -160,7 +240,12 @@ func (c *Client) UpdateSpaceEmailNotification(notification_id string, space_name
160240
if environment_active_with_error {
161241
data.Events = append(data.Events, "EnvironmentActiveWithError")
162242
}
163-
243+
if blueprint_published {
244+
data.Events = append(data.Events, "BlueprintPublished")
245+
}
246+
if blueprint_unpublished {
247+
data.Events = append(data.Events, "BlueprintUnpublished")
248+
}
164249
payload, err := json.Marshal(data)
165250
if err != nil {
166251
log.Fatalf("impossible to marshall update space request: %s", err)

docs/resources/space_email_notification.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ resource "torque_space_email_notification" "new_notification" {
4545
environment_ending_failed = true
4646
environment_ended = true
4747
environment_active_with_error = true
48+
blueprint_published = true
49+
blueprint_unpublished = true
4850
idle_reminders = [1, 2, 3]
4951
}
5052
```
@@ -60,6 +62,8 @@ resource "torque_space_email_notification" "new_notification" {
6062
### Optional
6163

6264
- `action_failed` (Boolean) Configure notification for the "Action Failed" event
65+
- `blueprint_published` (Boolean) Configure notification for the "Blueprint Published" event
66+
- `blueprint_unpublished` (Boolean) Configure notification for the "Blueprint Unpublished" event
6367
- `collaborator_added` (Boolean) Configure notification for the "Collaborator Added" event
6468
- `drift_detected` (Boolean) Configure notification for the "Drift Detected" event
6569
- `end_threshold` (Number) Time in minutes to send notification environment end event reminder notification before an environment ends. For example, 10
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "torque_space_generic_webhook_notification Resource - terraform-provider-torque"
4+
subcategory: ""
5+
description: |-
6+
Creation of a new Generic Webhook notification is a Torque space
7+
---
8+
9+
# torque_space_generic_webhook_notification (Resource)
10+
11+
Creation of a new Generic Webhook notification is a Torque space
12+
13+
## Example Usage
14+
15+
```terraform
16+
terraform {
17+
required_providers {
18+
torque = {
19+
source = "qualitorque/torque"
20+
}
21+
}
22+
}
23+
24+
provider "torque" {
25+
host = "https://portal.qtorque.io/"
26+
space = "space"
27+
token = "111111111111"
28+
}
29+
30+
31+
resource "torque_space_generic_webhook_notification" "new_notification" {
32+
space_name = "space"
33+
notification_name = "notification_name"
34+
webhook = "https://webhook.com"
35+
environment_launched = false
36+
environment_deployed = false
37+
environment_force_ended = false
38+
environment_idle = false
39+
environment_extended = false
40+
drift_detected = false
41+
workflow_failed = true
42+
workflow_started = true
43+
updates_detected = true
44+
collaborator_added = true
45+
action_failed = false
46+
environment_ending_failed = true
47+
environment_ended = true
48+
environment_active_with_error = true
49+
blueprint_published = true
50+
blueprint_unpublished = true
51+
idle_reminders = [1, 2, 3]
52+
}
53+
```
54+
55+
<!-- schema generated by tfplugindocs -->
56+
## Schema
57+
58+
### Required
59+
60+
- `notification_name` (String) The notification cofngiuration name in the space
61+
- `space_name` (String) Space name to add the notification to
62+
- `token` (String) The token of the generic webhook to send the notification to
63+
- `web_hook` (String) The webhook to send the notification to
64+
65+
### Optional
66+
67+
- `action_failed` (Boolean) Configure notification for the "Action Failed" event
68+
- `blueprint_published` (Boolean) Configure notification for the "Blueprint Published" event
69+
- `blueprint_unpublished` (Boolean) Configure notification for the "Blueprint Unpublished" event
70+
- `collaborator_added` (Boolean) Configure notification for the "Collaborator Added" event
71+
- `drift_detected` (Boolean) Configure notification for the "Drift Detected" event
72+
- `end_threshold` (Number) Time in minutes to send notification environment end event reminder notification before an environment ends. For example, 10
73+
- `environment_active_with_error` (Boolean) Configure notification for the "Environment Active With Error" event
74+
- `environment_deployed` (Boolean) Configure notification for the "Environment Deployed" event
75+
- `environment_ended` (Boolean) Configure notification for the "Environment Ended" event
76+
- `environment_ending_failed` (Boolean) Configure notification for the "Environment Ending Failed" event
77+
- `environment_extended` (Boolean) Configure notification for the "Environment Extended" event
78+
- `environment_force_ended` (Boolean) Configure notification for the "Environment Force Ended" event
79+
- `environment_idle` (Boolean) Configure notification for the "Environment Idle" event
80+
- `environment_launched` (Boolean) Configure notification for the "Environment Launched" event
81+
- `idle_reminders` (List of Number) Array of time in hours to send notification for environment idle reminder
82+
- `updates_detected` (Boolean) Configure notification for the "Updates Detected" event
83+
- `workflow_failed` (Boolean) Configure notification for the "Workflow Failed" event
84+
- `workflow_start_reminder` (Number) Configure notification for the "Drift Detected" event
85+
- `workflow_started` (Boolean) Configure notification for the "Workflow Started" event
86+
87+
### Read-Only
88+
89+
- `notification_id` (String) The id of the newly added notification
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "torque_space_slack_notification Resource - terraform-provider-torque"
4+
subcategory: ""
5+
description: |-
6+
Creation of a new Slack notification is a Torque space
7+
---
8+
9+
# torque_space_slack_notification (Resource)
10+
11+
Creation of a new Slack notification is a Torque space
12+
13+
## Example Usage
14+
15+
```terraform
16+
terraform {
17+
required_providers {
18+
torque = {
19+
source = "qualitorque/torque"
20+
}
21+
}
22+
}
23+
24+
provider "torque" {
25+
host = "https://portal.qtorque.io/"
26+
space = "space"
27+
token = "111111111111"
28+
}
29+
30+
31+
resource "torque_space_slack_notification" "new_notification" {
32+
space_name = "space"
33+
notification_name = "notification_name"
34+
webhook = "https://webhook.com"
35+
environment_launched = false
36+
environment_deployed = false
37+
environment_force_ended = false
38+
environment_idle = false
39+
environment_extended = false
40+
drift_detected = false
41+
workflow_failed = true
42+
workflow_started = true
43+
updates_detected = true
44+
collaborator_added = true
45+
action_failed = false
46+
environment_ending_failed = true
47+
environment_ended = true
48+
environment_active_with_error = true
49+
blueprint_published = true
50+
blueprint_unpublished = true
51+
idle_reminders = [1, 2, 3]
52+
}
53+
```
54+
55+
<!-- schema generated by tfplugindocs -->
56+
## Schema
57+
58+
### Required
59+
60+
- `notification_name` (String) The notification cofngiuration name in the space
61+
- `space_name` (String) Space name to add the notification to
62+
- `web_hook` (String) The webhook to send the notification to
63+
64+
### Optional
65+
66+
- `action_failed` (Boolean) Configure notification for the "Action Failed" event
67+
- `blueprint_published` (Boolean) Configure notification for the "Blueprint Published" event
68+
- `blueprint_unpublished` (Boolean) Configure notification for the "Blueprint Unpublished" event
69+
- `collaborator_added` (Boolean) Configure notification for the "Collaborator Added" event
70+
- `drift_detected` (Boolean) Configure notification for the "Drift Detected" event
71+
- `end_threshold` (Number) Time in minutes to send notification environment end event reminder notification before an environment ends. For example, 10
72+
- `environment_active_with_error` (Boolean) Configure notification for the "Environment Active With Error" event
73+
- `environment_deployed` (Boolean) Configure notification for the "Environment Deployed" event
74+
- `environment_ended` (Boolean) Configure notification for the "Environment Ended" event
75+
- `environment_ending_failed` (Boolean) Configure notification for the "Environment Ending Failed" event
76+
- `environment_extended` (Boolean) Configure notification for the "Environment Extended" event
77+
- `environment_force_ended` (Boolean) Configure notification for the "Environment Force Ended" event
78+
- `environment_idle` (Boolean) Configure notification for the "Environment Idle" event
79+
- `environment_launched` (Boolean) Configure notification for the "Environment Launched" event
80+
- `idle_reminders` (List of Number) Array of time in hours to send notification for environment idle reminder
81+
- `updates_detected` (Boolean) Configure notification for the "Updates Detected" event
82+
- `workflow_failed` (Boolean) Configure notification for the "Workflow Failed" event
83+
- `workflow_start_reminder` (Number) Configure notification for the "Drift Detected" event
84+
- `workflow_started` (Boolean) Configure notification for the "Workflow Started" event
85+
86+
### Read-Only
87+
88+
- `notification_id` (String) The id of the newly added notification

0 commit comments

Comments
 (0)