Skip to content

Commit ca9cd44

Browse files
committed
Adds DeliveryResponses parsing in NotificationConfiguration
1 parent 5aecfdd commit ca9cd44

File tree

2 files changed

+147
-10
lines changed

2 files changed

+147
-10
lines changed

notification_configuration.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package tfe
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910
"net/url"
1011
"time"
@@ -91,16 +92,17 @@ type NotificationConfigurationSubscribableChoice struct {
9192

9293
// NotificationConfiguration represents a Notification Configuration.
9394
type NotificationConfiguration struct {
94-
ID string `jsonapi:"primary,notification-configurations"`
95-
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
96-
DeliveryResponses []*DeliveryResponse `jsonapi:"attr,delivery-responses"`
97-
DestinationType NotificationDestinationType `jsonapi:"attr,destination-type"`
98-
Enabled bool `jsonapi:"attr,enabled"`
99-
Name string `jsonapi:"attr,name"`
100-
Token string `jsonapi:"attr,token"`
101-
Triggers []string `jsonapi:"attr,triggers"`
102-
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
103-
URL string `jsonapi:"attr,url"`
95+
ID string `jsonapi:"primary,notification-configurations"`
96+
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
97+
DeliveryResponsesRaw []interface{} `jsonapi:"attr,delivery-responses"`
98+
DeliveryResponses []*DeliveryResponse `json:"-"`
99+
DestinationType NotificationDestinationType `jsonapi:"attr,destination-type"`
100+
Enabled bool `jsonapi:"attr,enabled"`
101+
Name string `jsonapi:"attr,name"`
102+
Token string `jsonapi:"attr,token"`
103+
Triggers []string `jsonapi:"attr,triggers"`
104+
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
105+
URL string `jsonapi:"attr,url"`
104106

105107
// EmailAddresses is only available for TFE users. It is not available in HCP Terraform.
106108
EmailAddresses []string `jsonapi:"attr,email-addresses"`
@@ -241,6 +243,8 @@ func (s *notificationConfigurations) List(ctx context.Context, subscribableID st
241243

242244
for i := range ncl.Items {
243245
backfillDeprecatedSubscribable(ncl.Items[i])
246+
247+
parseDeliveryResponses(ncl.Items[i])
244248
}
245249

246250
return ncl, nil
@@ -276,6 +280,8 @@ func (s *notificationConfigurations) Create(ctx context.Context, subscribableID
276280

277281
backfillDeprecatedSubscribable(nc)
278282

283+
parseDeliveryResponses(nc)
284+
279285
return nc, nil
280286
}
281287

@@ -299,6 +305,8 @@ func (s *notificationConfigurations) Read(ctx context.Context, notificationConfi
299305

300306
backfillDeprecatedSubscribable(nc)
301307

308+
parseDeliveryResponses(nc)
309+
302310
return nc, nil
303311
}
304312

@@ -326,6 +334,8 @@ func (s *notificationConfigurations) Update(ctx context.Context, notificationCon
326334

327335
backfillDeprecatedSubscribable(nc)
328336

337+
parseDeliveryResponses(nc)
338+
329339
return nc, nil
330340
}
331341

@@ -364,6 +374,8 @@ func (s *notificationConfigurations) Verify(ctx context.Context, notificationCon
364374
return nil, err
365375
}
366376

377+
parseDeliveryResponses(nc)
378+
367379
return nc, nil
368380
}
369381

@@ -447,3 +459,21 @@ func validNotificationTriggerType(triggers []NotificationTriggerType) bool {
447459

448460
return true
449461
}
462+
463+
func parseDeliveryResponses(nc *NotificationConfiguration) {
464+
for _, raw := range nc.DeliveryResponsesRaw {
465+
data, ok := raw.(map[string]interface{})
466+
if !ok {
467+
continue
468+
}
469+
byteData, err := json.Marshal(data)
470+
if err != nil {
471+
continue
472+
}
473+
deliveryResponse := &DeliveryResponse{}
474+
if err := json.Unmarshal(byteData, deliveryResponse); err != nil {
475+
continue
476+
}
477+
nc.DeliveryResponses = append(nc.DeliveryResponses, deliveryResponse)
478+
}
479+
}

notification_configuration_integration_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tfe
66
import (
77
"context"
88
"fmt"
9+
"os"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
@@ -514,6 +515,112 @@ func TestNotificationConfigurationRead_forTeams(t *testing.T) {
514515
})
515516
}
516517

518+
func TestNotificationConfigurationReadDeliveryResponses(t *testing.T) {
519+
client := testClient(t)
520+
ctx := context.Background()
521+
522+
runTaskURL := os.Getenv("TFC_RUN_TASK_URL")
523+
if runTaskURL == "" {
524+
t.Skip("Cannot create a notification configuration with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
525+
}
526+
527+
options := &NotificationConfigurationCreateOptions{
528+
DestinationType: NotificationDestination(NotificationDestinationTypeGeneric),
529+
Enabled: Bool(true),
530+
Name: String(randomString(t)),
531+
Token: String(randomString(t)),
532+
URL: String(runTaskURL),
533+
Triggers: []NotificationTriggerType{NotificationTriggerCreated},
534+
}
535+
536+
t.Run("with notification configuration create", func(t *testing.T) {
537+
ncTest, ncTestCleanup := createNotificationConfiguration(t, client, nil, options)
538+
defer ncTestCleanup()
539+
540+
assert.Equal(t, 1, len(ncTest.DeliveryResponses))
541+
assert.NotNil(t, ncTest.DeliveryResponses[0])
542+
assert.NotEmpty(t, ncTest.DeliveryResponses[0].Code)
543+
})
544+
545+
t.Run("with notification configuration list", func(t *testing.T) {
546+
wTest, wTestCleanup := createWorkspace(t, client, nil)
547+
defer wTestCleanup()
548+
549+
ncTest1, ncTestCleanup1 := createNotificationConfiguration(t, client, wTest, options)
550+
defer ncTestCleanup1()
551+
ncTest2, ncTestCleanup2 := createNotificationConfiguration(t, client, wTest, options)
552+
defer ncTestCleanup2()
553+
554+
ncl, err := client.NotificationConfigurations.List(
555+
ctx,
556+
wTest.ID,
557+
nil,
558+
)
559+
require.NoError(t, err)
560+
assert.Contains(t, ncl.Items, ncTest1)
561+
assert.Contains(t, ncl.Items, ncTest2)
562+
563+
assert.NotNil(t, ncl.Items[0].DeliveryResponses)
564+
assert.NotEmpty(t, ncl.Items[0].DeliveryResponses)
565+
assert.NotNil(t, ncl.Items[1].DeliveryResponses)
566+
assert.NotEmpty(t, ncl.Items[1].DeliveryResponses)
567+
})
568+
569+
t.Run("with notification configuration read", func(t *testing.T) {
570+
ncTest, ncTestCleanup := createNotificationConfiguration(t, client, nil, options)
571+
defer ncTestCleanup()
572+
573+
nc, err := client.NotificationConfigurations.Read(ctx, ncTest.ID)
574+
require.NoError(t, err)
575+
assert.Equal(t, ncTest.ID, nc.ID)
576+
assert.Equal(t, 1, len(nc.DeliveryResponses))
577+
assert.NotNil(t, nc.DeliveryResponses[0])
578+
assert.NotEmpty(t, nc.DeliveryResponses[0].Code)
579+
})
580+
581+
t.Run("with notification configuration update", func(t *testing.T) {
582+
ncTest, ncTestCleanup := createNotificationConfiguration(t, client, nil, options)
583+
defer ncTestCleanup()
584+
585+
optionsUpdate := NotificationConfigurationUpdateOptions{
586+
Enabled: Bool(true),
587+
Name: String("newName"),
588+
}
589+
590+
nc, err := client.NotificationConfigurations.Update(ctx, ncTest.ID, optionsUpdate)
591+
require.NoError(t, err)
592+
593+
assert.Equal(t, nc.Name, "newName")
594+
assert.Equal(t, ncTest.ID, nc.ID)
595+
assert.Equal(t, 1, len(nc.DeliveryResponses))
596+
assert.NotNil(t, nc.DeliveryResponses[0])
597+
assert.NotEmpty(t, nc.DeliveryResponses[0].Code)
598+
})
599+
600+
t.Run("with notification configuration verify", func(t *testing.T) {
601+
ncTest, ncTestCleanup := createNotificationConfiguration(t, client, nil, options)
602+
defer ncTestCleanup()
603+
604+
ncVerifyTest, err := client.NotificationConfigurations.Verify(ctx, ncTest.ID)
605+
require.NoError(t, err)
606+
607+
assert.Equal(t, ncTest.ID, ncVerifyTest.ID)
608+
assert.Equal(t, 1, len(ncVerifyTest.DeliveryResponses))
609+
assert.NotNil(t, ncVerifyTest.DeliveryResponses[0])
610+
assert.NotEmpty(t, ncVerifyTest.DeliveryResponses[0].Code)
611+
})
612+
613+
t.Run("with notification configuration disabled", func(t *testing.T) {
614+
ncTest, ncTestCleanup := createNotificationConfiguration(t, client, nil, nil)
615+
defer ncTestCleanup()
616+
617+
nc, err := client.NotificationConfigurations.Read(ctx, ncTest.ID)
618+
require.NoError(t, err)
619+
assert.Equal(t, ncTest.ID, nc.ID)
620+
assert.Equal(t, 0, len(nc.DeliveryResponses))
621+
})
622+
}
623+
517624
func TestNotificationConfigurationUpdate_forTeams(t *testing.T) {
518625
skipUnlessBeta(t)
519626
client := testClient(t)

0 commit comments

Comments
 (0)