Skip to content

Commit

Permalink
Merge branch 'main' into chore-add-environment-import-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Alon Noga authored and Alon Noga committed May 21, 2024
2 parents a92d9d9 + 4c5ff4b commit c708bec
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
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:"-"`
}

func (client *ApiClient) Notifications() ([]Notification, error) {
Expand Down
6 changes: 5 additions & 1 deletion docs/resources/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ resource "env0_notification_project_assignment" "test_assignment" {

- `name` (String) the name of the notification
- `type` (String) 'Slack', 'Teams' or 'Email'
- `value` (String) the target url of the notification
- `value` (String) URL for Slack, Teams or Webhooks endpoint. Coma separated list of email addresses for email endpoint, you can use `$ENVIRONMENT_CREATOR$`, and `$DEPLOYER$` to resolve emails dynamically.

### Optional

- `webhook_secret` (String, Sensitive) the webhook secret to use for signing the webhook payload

### Read-Only

Expand Down
24 changes: 21 additions & 3 deletions env0/resource_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ func resourceNotification() *schema.Resource {
},
"value": {
Type: schema.TypeString,
Description: "the target url of the notification",
Description: "URL for Slack, Teams or Webhooks endpoint. Coma separated list of email addresses for email endpoint, you can use `$ENVIRONMENT_CREATOR$`, and `$DEPLOYER$` to resolve emails dynamically.",
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"
value = "https://someurl1.com"
webhook_secret = "my_little_secret"
}

resource "env0_notification" "test_notification_2" {
Expand Down

0 comments on commit c708bec

Please sign in to comment.