Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add support to webhookSecret in notification API #851

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions client/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type Notification struct {
}

type NotificationCreatePayload struct {
Name string `json:"name"`
Type NotificationType `json:"type"`
Value string `json:"value"`
Name string `json:"name"`
Type NotificationType `json:"type"`
Value string `json:"value"`
WebhookSecret string `json:"webhookSecret,omitempty"`
}

type NotificationCreatePayloadWith struct {
Expand All @@ -30,9 +31,10 @@ type NotificationCreatePayloadWith struct {
}

type NotificationUpdatePayload struct {
Name string `json:"name,omitempty"`
Type NotificationType `json:"type,omitempty"`
Value string `json:"value,omitempty"`
Name string `json:"name,omitempty"`
Type NotificationType `json:"type,omitempty"`
Value string `json:"value,omitempty"`
WebhookSecret **string `json:"webhookSecret,omitempty" tfschema:"-"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pointer to pointer allows me to support 3 modes:

  1. passing a string "....". (modify the secret).
  2. passing a null. (remove the secret)
  3. omitting the field (no change)

}

func (client *ApiClient) Notifications() ([]Notification, error) {
Expand Down
22 changes: 20 additions & 2 deletions env0/resource_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func resourceNotification() *schema.Resource {
Required: true,
ValidateDiagFunc: ValidateNotEmptyString,
},
"webhook_secret": {
Type: schema.TypeString,
Description: "the webhook secret to use for signing the webhook payload",
Optional: true,
Sensitive: true,
},
},
}
}
Expand Down Expand Up @@ -136,8 +142,20 @@ func resourceNotificationUpdate(ctx context.Context, d *schema.ResourceData, met
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

_, err := apiClient.NotificationUpdate(d.Id(), payload)
if err != nil {
if d.HasChanges("webhook_secret") {
webhookSecret := d.Get("webhook_secret").(string)
var strPtr *string
if webhookSecret == "" {
// webhook secret was removed - pass 'null pointer' (will pass 'null' in json).
// see https://docs.env0.com/reference/notifications-update-notification-endpoint
payload.WebhookSecret = &strPtr
} else {
strPtr = &webhookSecret
payload.WebhookSecret = &strPtr
}
}

if _, err := apiClient.NotificationUpdate(d.Id(), payload); err != nil {
return diag.Errorf("could not update notification: %v", err)
}

Expand Down
25 changes: 16 additions & 9 deletions env0/resource_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ func TestUnitNotificationResource(t *testing.T) {
CreatedByUser: notification.CreatedByUser,
}

webhookSecret := "my-little-secret"
var nullString *string

t.Run("Success", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": notification.Name,
"type": notification.Type,
"value": notification.Value,
"name": notification.Name,
"type": notification.Type,
"value": notification.Value,
"webhook_secret": webhookSecret,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", notification.Id),
resource.TestCheckResourceAttr(accessor, "name", notification.Name),
resource.TestCheckResourceAttr(accessor, "value", notification.Value),
resource.TestCheckResourceAttr(accessor, "type", string(notification.Type)),
resource.TestCheckResourceAttr(accessor, "webhook_secret", webhookSecret),
),
},
{
Expand All @@ -85,15 +90,17 @@ func TestUnitNotificationResource(t *testing.T) {

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().NotificationCreate(client.NotificationCreatePayload{
Name: notification.Name,
Type: notification.Type,
Value: notification.Value,
Name: notification.Name,
Type: notification.Type,
Value: notification.Value,
WebhookSecret: webhookSecret,
}).Times(1).Return(&notification, nil)

mock.EXPECT().NotificationUpdate(updatedNotification.Id, client.NotificationUpdatePayload{
Name: updatedNotification.Name,
Type: updatedNotification.Type,
Value: updatedNotification.Value,
Name: updatedNotification.Name,
Type: updatedNotification.Type,
Value: updatedNotification.Value,
WebhookSecret: &nullString,
}).Times(1).Return(&updatedNotification, nil)

gomock.InOrder(
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/025_notifications/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ locals {
}

resource "env0_notification" "test_notification_1" {
name = "${local.notification_name_prefix}-1-${random_string.random.result}"
type = "Slack"
value = "https://someurl1.com"
name = "${local.notification_name_prefix}-1-${random_string.random.result}"
type = "Slack"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be type = "Webhook"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aligned with type body param here

value = "https://someurl1.com"
webhook_secret = "my_little_secret"
}

resource "env0_notification" "test_notification_2" {
Expand Down
Loading