Skip to content

Commit 0e97651

Browse files
authored
email notification enhancements and test (#112)
* email notification enhancements and test * remove redundant printing
1 parent 427e7f1 commit 0e97651

File tree

4 files changed

+487
-108
lines changed

4 files changed

+487
-108
lines changed

client/notification.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
)
10+
11+
func (c *Client) CreateSpaceEmailNotification(space_name string, notification_name string, environment_launched bool,
12+
environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool,
13+
drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool,
14+
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+
Description: "",
23+
},
24+
}
25+
26+
if environment_launched {
27+
data.Events = append(data.Events, "EnvironmentLaunched")
28+
}
29+
if environment_deployed {
30+
data.Events = append(data.Events, "EnvironmentDeployed")
31+
}
32+
if environment_force_ended {
33+
data.Events = append(data.Events, "EnvironmentForceEnded")
34+
}
35+
36+
if environment_idle {
37+
data.Events = append(data.Events, "EnvironmentIdle")
38+
data.IdleReminder = []ReminderRequest{}
39+
for _, idleNumber := range idle_reminder {
40+
data.IdleReminder = append(data.IdleReminder, ReminderRequest{TimeInHours: idleNumber})
41+
}
42+
}
43+
44+
if environment_extended {
45+
data.Events = append(data.Events, "EnvironmentExtended")
46+
}
47+
if drift_detected {
48+
data.Events = append(data.Events, "DriftDetected")
49+
}
50+
if workflow_failed {
51+
data.Events = append(data.Events, "WorkflowFailed")
52+
}
53+
if workflow_started {
54+
data.Events = append(data.Events, "WorkflowStarted")
55+
data.WorkflowStartReminder = workflow_start_reminder
56+
}
57+
if updates_detected {
58+
data.Events = append(data.Events, "UpdatesDetected")
59+
}
60+
if collaborator_added {
61+
data.Events = append(data.Events, "CollaboratorAdded")
62+
}
63+
if action_failed {
64+
data.Events = append(data.Events, "ActionFailed")
65+
}
66+
if environment_ending_failed {
67+
data.Events = append(data.Events, "EnvironmentEndingFailed")
68+
}
69+
if environment_ended {
70+
data.Events = append(data.Events, "EnvironmentEnded")
71+
data.EndThreshold = end_threshold
72+
}
73+
if environment_active_with_error {
74+
data.Events = append(data.Events, "EnvironmentActiveWithError")
75+
}
76+
77+
payload, err := json.Marshal(data)
78+
if err != nil {
79+
log.Fatalf("impossible to marshall update space request: %s", err)
80+
}
81+
82+
req, err := http.NewRequest("POST", fmt.Sprintf("%sapi/spaces/%s/subscriptions", c.HostURL, space_name), bytes.NewReader(payload))
83+
if err != nil {
84+
return "", err
85+
}
86+
87+
req.Header.Add("Content-Type", "application/json")
88+
req.Header.Add("Accept", "application/json")
89+
90+
body, err := c.doRequest(req, &c.Token)
91+
if err != nil {
92+
return "", err
93+
}
94+
95+
return string(body), nil
96+
}
97+
98+
func (c *Client) UpdateSpaceEmailNotification(notification_id string, space_name string, notification_name string, environment_launched bool,
99+
environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool,
100+
drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool,
101+
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) {
103+
104+
data := SubscriptionsRequest{
105+
Name: notification_name,
106+
Description: "",
107+
Target: SubscriptionsTargetRequest{
108+
Type: "Email",
109+
Description: "",
110+
},
111+
}
112+
113+
if environment_launched {
114+
data.Events = append(data.Events, "EnvironmentLaunched")
115+
}
116+
if environment_deployed {
117+
data.Events = append(data.Events, "EnvironmentDeployed")
118+
}
119+
if environment_force_ended {
120+
data.Events = append(data.Events, "EnvironmentForceEnded")
121+
}
122+
123+
if environment_idle {
124+
data.Events = append(data.Events, "EnvironmentIdle")
125+
data.IdleReminder = []ReminderRequest{}
126+
for _, idleNumber := range idle_reminder {
127+
data.IdleReminder = append(data.IdleReminder, ReminderRequest{TimeInHours: idleNumber})
128+
}
129+
}
130+
131+
if environment_extended {
132+
data.Events = append(data.Events, "EnvironmentExtended")
133+
}
134+
if drift_detected {
135+
data.Events = append(data.Events, "DriftDetected")
136+
}
137+
if workflow_failed {
138+
data.Events = append(data.Events, "WorkflowFailed")
139+
}
140+
if workflow_started {
141+
data.Events = append(data.Events, "WorkflowStarted")
142+
data.WorkflowStartReminder = workflow_start_reminder
143+
}
144+
if updates_detected {
145+
data.Events = append(data.Events, "UpdatesDetected")
146+
}
147+
if collaborator_added {
148+
data.Events = append(data.Events, "CollaboratorAdded")
149+
}
150+
if action_failed {
151+
data.Events = append(data.Events, "ActionFailed")
152+
}
153+
if environment_ending_failed {
154+
data.Events = append(data.Events, "EnvironmentEndingFailed")
155+
}
156+
if environment_ended {
157+
data.Events = append(data.Events, "EnvironmentEnded")
158+
data.EndThreshold = end_threshold
159+
}
160+
if environment_active_with_error {
161+
data.Events = append(data.Events, "EnvironmentActiveWithError")
162+
}
163+
164+
payload, err := json.Marshal(data)
165+
if err != nil {
166+
log.Fatalf("impossible to marshall update space request: %s", err)
167+
}
168+
169+
req, err := http.NewRequest("PUT", fmt.Sprintf("%sapi/spaces/%s/subscriptions?subscriptionId=%s", c.HostURL, space_name, notification_id), bytes.NewReader(payload))
170+
if err != nil {
171+
return "", err
172+
}
173+
174+
req.Header.Add("Content-Type", "application/json")
175+
req.Header.Add("Accept", "application/json")
176+
177+
body, err := c.doRequest(req, &c.Token)
178+
if err != nil {
179+
return "", err
180+
}
181+
182+
return string(body), nil
183+
}
184+
185+
func (c *Client) DeleteSpaceNotification(space_name string, notification_id string) error {
186+
req, err := http.NewRequest("DELETE", fmt.Sprintf("%sapi/spaces/%s/subscriptions?subscriptionId=%s", c.HostURL, space_name, notification_id), nil)
187+
if err != nil {
188+
return err
189+
}
190+
191+
req.Header.Add("Content-Type", "application/json")
192+
req.Header.Add("Accept", "application/json")
193+
194+
_, err = c.doRequest(req, &c.Token)
195+
if err != nil {
196+
return err
197+
}
198+
199+
return nil
200+
}

client/space.go

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -427,109 +427,3 @@ func (c *Client) UpdateSpace(current_space string, name string, color string, ic
427427

428428
return nil
429429
}
430-
431-
func (c *Client) CreateSpaceEmailNotification(space_name string, notification_name string, environment_launched bool,
432-
environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool,
433-
drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool,
434-
collaborator_added bool, action_failed bool, environment_ending_failed bool, environment_ended bool,
435-
environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, idle_reminder []int64) (string, error) {
436-
437-
data := SubscriptionsRequest{
438-
Name: notification_name,
439-
Description: "",
440-
Target: SubscriptionsTargetRequest{
441-
Type: "Email",
442-
Description: "",
443-
},
444-
}
445-
446-
if environment_launched {
447-
data.Events = append(data.Events, "EnvironmentLaunched")
448-
}
449-
if environment_deployed {
450-
data.Events = append(data.Events, "EnvironmentDeployed")
451-
}
452-
if environment_force_ended {
453-
data.Events = append(data.Events, "EnvironmentForceEnded")
454-
}
455-
456-
if environment_idle {
457-
data.Events = append(data.Events, "EnvironmentIdle")
458-
data.IdleReminder = []ReminderRequest{}
459-
for _, idleNumber := range idle_reminder {
460-
data.IdleReminder = append(data.IdleReminder, ReminderRequest{TimeInHours: idleNumber})
461-
}
462-
}
463-
464-
if environment_extended {
465-
data.Events = append(data.Events, "EnvironmentExtended")
466-
}
467-
if drift_detected {
468-
data.Events = append(data.Events, "DriftDetected")
469-
}
470-
if workflow_failed {
471-
data.Events = append(data.Events, "WorkflowFailed")
472-
}
473-
if workflow_started {
474-
data.Events = append(data.Events, "WorkflowStarted")
475-
data.WorkflowStartReminder = workflow_start_reminder
476-
}
477-
if updates_detected {
478-
data.Events = append(data.Events, "UpdatesDetected")
479-
}
480-
if collaborator_added {
481-
data.Events = append(data.Events, "CollaboratorAdded")
482-
}
483-
if action_failed {
484-
data.Events = append(data.Events, "ActionFailed")
485-
}
486-
if environment_ending_failed {
487-
data.Events = append(data.Events, "EnvironmentEndingFailed")
488-
}
489-
if environment_ended {
490-
data.Events = append(data.Events, "EnvironmentEnded")
491-
data.EndThreshold = end_threshold
492-
}
493-
if environment_active_with_error {
494-
data.Events = append(data.Events, "EnvironmentActiveWithError")
495-
}
496-
497-
payload, err := json.Marshal(data)
498-
if err != nil {
499-
log.Fatalf("impossible to marshall update space request: %s", err)
500-
}
501-
502-
req, err := http.NewRequest("POST", fmt.Sprintf("%sapi/spaces/%s/subscriptions", c.HostURL, space_name), bytes.NewReader(payload))
503-
if err != nil {
504-
return "", err
505-
}
506-
507-
req.Header.Add("Content-Type", "application/json")
508-
req.Header.Add("Accept", "application/json")
509-
510-
body, err := c.doRequest(req, &c.Token)
511-
if err != nil {
512-
return "", err
513-
}
514-
515-
return string(body), nil
516-
}
517-
518-
func (c *Client) DeleteSpaceNotification(space_name string, notification_id string) error {
519-
fmt.Println(c.HostURL + "api/spaces")
520-
521-
req, err := http.NewRequest("DELETE", fmt.Sprintf("%sapi/spaces/%s/subscriptions?subscriptionId=%s", c.HostURL, space_name, notification_id), nil)
522-
if err != nil {
523-
return err
524-
}
525-
526-
req.Header.Add("Content-Type", "application/json")
527-
req.Header.Add("Accept", "application/json")
528-
529-
_, err = c.doRequest(req, &c.Token)
530-
if err != nil {
531-
return err
532-
}
533-
534-
return nil
535-
}

internal/provider/resources/space_email_notification_resource.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/path"
99
"github.com/hashicorp/terraform-plugin-framework/resource"
1010
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
12+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1113
"github.com/hashicorp/terraform-plugin-framework/types"
1214
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1315
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -64,6 +66,9 @@ func (r *TorqueSpaceEmailNotificationResource) Schema(ctx context.Context, req r
6466
"space_name": schema.StringAttribute{
6567
MarkdownDescription: "Space name to add the notification to",
6668
Required: true,
69+
PlanModifiers: []planmodifier.String{
70+
stringplanmodifier.RequiresReplace(),
71+
},
6772
},
6873
"notification_name": schema.StringAttribute{
6974
MarkdownDescription: "The notification cofngiuration name in the space",
@@ -234,14 +239,33 @@ func (r *TorqueSpaceEmailNotificationResource) Read(ctx context.Context, req res
234239

235240
func (r *TorqueSpaceEmailNotificationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
236241
var data TorqueSpaceEmailNotificationResourceModel
242+
var state TorqueSpaceEmailNotificationResourceModel
237243

238-
// Read Terraform plan data into the model
239244
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
240-
245+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
241246
if resp.Diagnostics.HasError() {
242247
return
243248
}
244249

250+
var idle []int64
251+
if len(data.IdleReminder) > 0 {
252+
for _, reminder := range data.IdleReminder {
253+
idle = append(idle, reminder.ValueInt64())
254+
}
255+
}
256+
257+
_, err := r.client.UpdateSpaceEmailNotification(state.NotificationId.ValueString(), data.SpaceName.ValueString(), data.NotificationName.ValueString(), data.EnvironmentLaunched.ValueBool(),
258+
data.EnvironmentDeployed.ValueBool(), data.EnvironmentForceEnded.ValueBool(), data.EnvironmentIdle.ValueBool(), data.EnvironmentExtended.ValueBool(), data.DriftDetected.ValueBool(),
259+
data.WorkflowFailed.ValueBool(), data.WorkflowStarted.ValueBool(), data.UpdatesDetected.ValueBool(), data.CollaboratorAdded.ValueBool(), data.ActionFailed.ValueBool(),
260+
data.EnvironmentEndingFailed.ValueBool(), data.EnvironmentEnded.ValueBool(), data.EnvironmentActiveWithError.ValueBool(), data.WorkflowStartReminder.ValueInt64(), data.EndThreashold.ValueInt64(),
261+
idle)
262+
263+
if err != nil {
264+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update notification in space, got error: %s", err))
265+
return
266+
}
267+
data.NotificationId = state.NotificationId
268+
245269
// Save updated data into Terraform state
246270
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
247271
}

0 commit comments

Comments
 (0)