@@ -20,10 +20,10 @@ var _ NotificationConfigurations = (*notificationConfigurations)(nil)
20
20
// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/notification-configurations
21
21
type NotificationConfigurations interface {
22
22
// List all the notification configurations within a workspace.
23
- List (ctx context.Context , workspaceID string , options * NotificationConfigurationListOptions ) (* NotificationConfigurationList , error )
23
+ List (ctx context.Context , subscribableID string , options * NotificationConfigurationListOptions ) (* NotificationConfigurationList , error )
24
24
25
25
// Create a new notification configuration with the given options.
26
- Create (ctx context.Context , workspaceID string , options NotificationConfigurationCreateOptions ) (* NotificationConfiguration , error )
26
+ Create (ctx context.Context , subscribableID string , options NotificationConfigurationCreateOptions ) (* NotificationConfiguration , error )
27
27
28
28
// Read a notification configuration by its ID.
29
29
Read (ctx context.Context , notificationConfigurationID string ) (* NotificationConfiguration , error )
@@ -59,6 +59,7 @@ const (
59
59
NotificationTriggerAssessmentCheckFailed NotificationTriggerType = "assessment:check_failure"
60
60
NotificationTriggerWorkspaceAutoDestroyReminder NotificationTriggerType = "workspace:auto_destroy_reminder"
61
61
NotificationTriggerWorkspaceAutoDestroyRunResults NotificationTriggerType = "workspace:auto_destroy_run_results"
62
+ NotificationTriggerChangeRequestCreated NotificationTriggerType = "change_request:created"
62
63
)
63
64
64
65
// NotificationDestinationType represents the destination type of the
@@ -80,6 +81,14 @@ type NotificationConfigurationList struct {
80
81
Items []* NotificationConfiguration
81
82
}
82
83
84
+ // NotificationConfigurationSubscribableChoice is a choice type struct that represents the possible values
85
+ // within a polymorphic relation. If a value is available, exactly one field
86
+ // will be non-nil.
87
+ type NotificationConfigurationSubscribableChoice struct {
88
+ Team * Team
89
+ Workspace * Workspace
90
+ }
91
+
83
92
// NotificationConfiguration represents a Notification Configuration.
84
93
type NotificationConfiguration struct {
85
94
ID string `jsonapi:"primary,notification-configurations"`
@@ -97,8 +106,11 @@ type NotificationConfiguration struct {
97
106
EmailAddresses []string `jsonapi:"attr,email-addresses"`
98
107
99
108
// Relations
100
- Subscribable * Workspace `jsonapi:"relation,subscribable"`
101
- EmailUsers []* User `jsonapi:"relation,users"`
109
+ // DEPRECATED. The subscribable field is polymorphic. Use NotificationConfigurationSubscribableChoice instead.
110
+ Subscribable * Workspace `jsonapi:"relation,subscribable,omitempty"`
111
+ SubscribableChoice * NotificationConfigurationSubscribableChoice `jsonapi:"polyrelation,subscribable"`
112
+
113
+ EmailUsers []* User `jsonapi:"relation,users"`
102
114
}
103
115
104
116
// DeliveryResponse represents a notification configuration delivery response.
@@ -115,6 +127,8 @@ type DeliveryResponse struct {
115
127
// notification configurations.
116
128
type NotificationConfigurationListOptions struct {
117
129
ListOptions
130
+
131
+ SubscribableChoice * NotificationConfigurationSubscribableChoice
118
132
}
119
133
120
134
// NotificationConfigurationCreateOptions represents the options for
@@ -150,6 +164,9 @@ type NotificationConfigurationCreateOptions struct {
150
164
151
165
// Optional: The list of users belonging to the organization that will receive notification emails.
152
166
EmailUsers []* User `jsonapi:"relation,users,omitempty"`
167
+
168
+ // Required: The workspace or team that the notification configuration is associated with.
169
+ SubscribableChoice * NotificationConfigurationSubscribableChoice `jsonapi:"polyrelation,subscribable,omitempty"`
153
170
}
154
171
155
172
// NotificationConfigurationUpdateOptions represents the options for
@@ -185,12 +202,32 @@ type NotificationConfigurationUpdateOptions struct {
185
202
}
186
203
187
204
// List all the notification configurations associated with a workspace.
188
- func (s * notificationConfigurations ) List (ctx context.Context , workspaceID string , options * NotificationConfigurationListOptions ) (* NotificationConfigurationList , error ) {
189
- if ! validStringID (& workspaceID ) {
190
- return nil , ErrInvalidWorkspaceID
205
+ func (s * notificationConfigurations ) List (ctx context.Context , subscribableID string , options * NotificationConfigurationListOptions ) (* NotificationConfigurationList , error ) {
206
+ var u string
207
+ if options == nil {
208
+ options = & NotificationConfigurationListOptions {
209
+ SubscribableChoice : & NotificationConfigurationSubscribableChoice {
210
+ Workspace : & Workspace {ID : subscribableID },
211
+ },
212
+ }
213
+ } else if options .SubscribableChoice == nil {
214
+ options .SubscribableChoice = & NotificationConfigurationSubscribableChoice {
215
+ Workspace : & Workspace {ID : subscribableID },
216
+ }
217
+ }
218
+
219
+ if options .SubscribableChoice .Team != nil {
220
+ if ! validStringID (& subscribableID ) {
221
+ return nil , ErrInvalidTeamID
222
+ }
223
+ u = fmt .Sprintf ("teams/%s/notification-configurations" , url .PathEscape (subscribableID ))
224
+ } else {
225
+ if ! validStringID (& subscribableID ) {
226
+ return nil , ErrInvalidWorkspaceID
227
+ }
228
+ u = fmt .Sprintf ("workspaces/%s/notification-configurations" , url .PathEscape (subscribableID ))
191
229
}
192
230
193
- u := fmt .Sprintf ("workspaces/%s/notification-configurations" , url .PathEscape (workspaceID ))
194
231
req , err := s .client .NewRequest ("GET" , u , options )
195
232
if err != nil {
196
233
return nil , err
@@ -202,30 +239,43 @@ func (s *notificationConfigurations) List(ctx context.Context, workspaceID strin
202
239
return nil , err
203
240
}
204
241
242
+ for i := range ncl .Items {
243
+ backfillDeprecatedSubscribable (ncl .Items [i ])
244
+ }
245
+
205
246
return ncl , nil
206
247
}
207
248
208
249
// Create a notification configuration with the given options.
209
- func (s * notificationConfigurations ) Create (ctx context.Context , workspaceID string , options NotificationConfigurationCreateOptions ) (* NotificationConfiguration , error ) {
210
- if ! validStringID (& workspaceID ) {
211
- return nil , ErrInvalidWorkspaceID
250
+ func (s * notificationConfigurations ) Create (ctx context.Context , subscribableID string , options NotificationConfigurationCreateOptions ) (* NotificationConfiguration , error ) {
251
+ var u string
252
+ var subscribableChoice * NotificationConfigurationSubscribableChoice
253
+ if options .SubscribableChoice == nil || options .SubscribableChoice .Team == nil {
254
+ u = fmt .Sprintf ("workspaces/%s/notification-configurations" , url .PathEscape (subscribableID ))
255
+ options .SubscribableChoice = & NotificationConfigurationSubscribableChoice {Workspace : & Workspace {ID : subscribableID }}
256
+ } else {
257
+ u = fmt .Sprintf ("teams/%s/notification-configurations" , url .PathEscape (subscribableID ))
258
+ options .SubscribableChoice = & NotificationConfigurationSubscribableChoice {Team : & Team {ID : subscribableID }}
212
259
}
260
+
213
261
if err := options .valid (); err != nil {
214
262
return nil , err
215
263
}
216
264
217
- u := fmt .Sprintf ("workspaces/%s/notification-configurations" , url .PathEscape (workspaceID ))
218
265
req , err := s .client .NewRequest ("POST" , u , & options )
219
266
if err != nil {
220
267
return nil , err
221
268
}
222
269
223
- nc := & NotificationConfiguration {}
270
+ nc := & NotificationConfiguration {SubscribableChoice : subscribableChoice }
224
271
err = req .Do (ctx , nc )
272
+
225
273
if err != nil {
226
274
return nil , err
227
275
}
228
276
277
+ backfillDeprecatedSubscribable (nc )
278
+
229
279
return nc , nil
230
280
}
231
281
@@ -247,6 +297,8 @@ func (s *notificationConfigurations) Read(ctx context.Context, notificationConfi
247
297
return nil , err
248
298
}
249
299
300
+ backfillDeprecatedSubscribable (nc )
301
+
250
302
return nc , nil
251
303
}
252
304
@@ -272,6 +324,8 @@ func (s *notificationConfigurations) Update(ctx context.Context, notificationCon
272
324
return nil , err
273
325
}
274
326
327
+ backfillDeprecatedSubscribable (nc )
328
+
275
329
return nc , nil
276
330
}
277
331
@@ -314,6 +368,16 @@ func (s *notificationConfigurations) Verify(ctx context.Context, notificationCon
314
368
}
315
369
316
370
func (o NotificationConfigurationCreateOptions ) valid () error {
371
+ if o .SubscribableChoice == nil || o .SubscribableChoice .Workspace != nil {
372
+ if ! validStringID (& o .SubscribableChoice .Workspace .ID ) {
373
+ return ErrInvalidWorkspaceID
374
+ }
375
+ } else {
376
+ if ! validStringID (& o .SubscribableChoice .Team .ID ) {
377
+ return ErrInvalidTeamID
378
+ }
379
+ }
380
+
317
381
if o .DestinationType == nil {
318
382
return ErrRequiredDestinationType
319
383
}
@@ -350,6 +414,16 @@ func (o NotificationConfigurationUpdateOptions) valid() error {
350
414
return nil
351
415
}
352
416
417
+ func backfillDeprecatedSubscribable (notification * NotificationConfiguration ) {
418
+ if notification .Subscribable != nil || notification .SubscribableChoice == nil {
419
+ return
420
+ }
421
+
422
+ if notification .SubscribableChoice .Workspace != nil {
423
+ notification .Subscribable = notification .SubscribableChoice .Workspace
424
+ }
425
+ }
426
+
353
427
func validNotificationTriggerType (triggers []NotificationTriggerType ) bool {
354
428
for _ , t := range triggers {
355
429
switch t {
@@ -363,6 +437,7 @@ func validNotificationTriggerType(triggers []NotificationTriggerType) bool {
363
437
NotificationTriggerAssessmentFailed ,
364
438
NotificationTriggerWorkspaceAutoDestroyReminder ,
365
439
NotificationTriggerWorkspaceAutoDestroyRunResults ,
440
+ NotificationTriggerChangeRequestCreated ,
366
441
NotificationTriggerAssessmentCheckFailed :
367
442
continue
368
443
default :
0 commit comments