Skip to content

Commit

Permalink
feat(notification): add auto destroy configuration triggers (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
notchairmk authored Jun 18, 2024
1 parent 660489c commit d981486
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Enhancements

* Adds the `IsUnified` field to `Project`, `Organization` and `Team` by @roncodingenthusiast [#915](https://github.com/hashicorp/go-tfe/pull/915)
* Adds Workspace auto-destroy notification types to `NotificationTriggerType` by @notchairmk [#918](https://github.com/hashicorp/go-tfe/pull/918)

# v1.56.0

Expand Down
22 changes: 13 additions & 9 deletions notification_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ type notificationConfigurations struct {
type NotificationTriggerType string

const (
NotificationTriggerCreated NotificationTriggerType = "run:created"
NotificationTriggerPlanning NotificationTriggerType = "run:planning"
NotificationTriggerNeedsAttention NotificationTriggerType = "run:needs_attention"
NotificationTriggerApplying NotificationTriggerType = "run:applying"
NotificationTriggerCompleted NotificationTriggerType = "run:completed"
NotificationTriggerErrored NotificationTriggerType = "run:errored"
NotificationTriggerAssessmentDrifted NotificationTriggerType = "assessment:drifted"
NotificationTriggerAssessmentFailed NotificationTriggerType = "assessment:failed"
NotificationTriggerAssessmentCheckFailed NotificationTriggerType = "assessment:check_failure"
NotificationTriggerCreated NotificationTriggerType = "run:created"
NotificationTriggerPlanning NotificationTriggerType = "run:planning"
NotificationTriggerNeedsAttention NotificationTriggerType = "run:needs_attention"
NotificationTriggerApplying NotificationTriggerType = "run:applying"
NotificationTriggerCompleted NotificationTriggerType = "run:completed"
NotificationTriggerErrored NotificationTriggerType = "run:errored"
NotificationTriggerAssessmentDrifted NotificationTriggerType = "assessment:drifted"
NotificationTriggerAssessmentFailed NotificationTriggerType = "assessment:failed"
NotificationTriggerAssessmentCheckFailed NotificationTriggerType = "assessment:check_failure"
NotificationTriggerWorkspaceAutoDestroyReminder NotificationTriggerType = "workspace:auto_destroy_reminder"
NotificationTriggerWorkspaceAutoDestroyRunResults NotificationTriggerType = "workspace:auto_destroy_run_results"
)

// NotificationDestinationType represents the destination type of the
Expand Down Expand Up @@ -359,6 +361,8 @@ func validNotificationTriggerType(triggers []NotificationTriggerType) bool {
NotificationTriggerPlanning,
NotificationTriggerAssessmentDrifted,
NotificationTriggerAssessmentFailed,
NotificationTriggerWorkspaceAutoDestroyReminder,
NotificationTriggerWorkspaceAutoDestroyRunResults,
NotificationTriggerAssessmentCheckFailed:
continue
default:
Expand Down
56 changes: 56 additions & 0 deletions notification_configuration_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tfe

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -199,6 +200,61 @@ func TestNotificationConfigurationCreate(t *testing.T) {
})
}

func TestNotificationConfigurationsCreate_byType(t *testing.T) {
t.Parallel()

client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)

upgradeOrganizationSubscription(t, client, orgTest)

wTest, wTestCleanup := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup)

// Create user to use when testing email destination type
orgMemberTest, orgMemberTestCleanup := createOrganizationMembership(t, client, orgTest)
t.Cleanup(orgMemberTestCleanup)

orgMemberTest.User = &User{ID: orgMemberTest.User.ID}

testCases := []NotificationTriggerType{
NotificationTriggerCreated,
NotificationTriggerPlanning,
NotificationTriggerNeedsAttention,
NotificationTriggerApplying,
NotificationTriggerCompleted,
NotificationTriggerErrored,
NotificationTriggerAssessmentDrifted,
NotificationTriggerAssessmentFailed,
NotificationTriggerAssessmentCheckFailed,
NotificationTriggerWorkspaceAutoDestroyReminder,
NotificationTriggerWorkspaceAutoDestroyRunResults,
}

for _, trigger := range testCases {
trigger := trigger
message := fmt.Sprintf("with trigger %s and all required values", trigger)

t.Run(message, func(t *testing.T) {
t.Parallel()
options := NotificationConfigurationCreateOptions{
DestinationType: NotificationDestination(NotificationDestinationTypeGeneric),
Enabled: Bool(false),
Name: String(randomString(t)),
Token: String(randomString(t)),
URL: String("http://example.com"),
Triggers: []NotificationTriggerType{trigger},
}

_, err := client.NotificationConfigurations.Create(ctx, wTest.ID, options)
require.NoError(t, err)
})
}
}

func TestNotificationConfigurationRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down

0 comments on commit d981486

Please sign in to comment.