Skip to content

Commit

Permalink
Merge pull request #346 from hashicorp/sebasslash/notif-conf-triggers…
Browse files Browse the repository at this point in the history
…-enum

Notification Triggers are now a type string
  • Loading branch information
sebasslash committed Mar 4, 2022
2 parents 6d75cd6 + be225a3 commit 7345694
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
2 changes: 2 additions & 0 deletions errors.go
Expand Up @@ -143,6 +143,8 @@ var (
ErrInvalidPolicies = errors.New("must provide at least one policy")

ErrInvalidVariableID = errors.New("invalid value for variable ID")

ErrInvalidNotificationTrigger = errors.New("invalid value for notification trigger")
)

// Missing values for required field/option
Expand Down
2 changes: 1 addition & 1 deletion helper_test.go
Expand Up @@ -164,7 +164,7 @@ func createNotificationConfiguration(t *testing.T, client *Client, w *Workspace,
Name: String(randomString(t)),
Token: String(randomString(t)),
URL: String("http://example.com"),
Triggers: []string{NotificationTriggerCreated},
Triggers: []NotificationTriggerType{NotificationTriggerCreated},
}
}

Expand Down
58 changes: 50 additions & 8 deletions notification_configuration.go
Expand Up @@ -40,13 +40,17 @@ type notificationConfigurations struct {
client *Client
}

// NotificationTriggerType represents the different TFE notifications that can be sent
// as a run's progress transitions between different states
type NotificationTriggerType string

const (
NotificationTriggerCreated string = "run:created"
NotificationTriggerPlanning string = "run:planning"
NotificationTriggerNeedsAttention string = "run:needs_attention"
NotificationTriggerApplying string = "run:applying"
NotificationTriggerCompleted string = "run:completed"
NotificationTriggerErrored string = "run:errored"
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"
)

// NotificationDestinationType represents the destination type of the
Expand Down Expand Up @@ -126,7 +130,7 @@ type NotificationConfigurationCreateOptions struct {
Token *string `jsonapi:"attr,token,omitempty"`

// Optional: The list of run events that will trigger notifications.
Triggers []string `jsonapi:"attr,triggers,omitempty"`
Triggers []NotificationTriggerType `jsonapi:"attr,triggers,omitempty"`

// Optional: The url of the notification configuration
URL *string `jsonapi:"attr,url,omitempty"`
Expand Down Expand Up @@ -158,7 +162,7 @@ type NotificationConfigurationUpdateOptions struct {
Token *string `jsonapi:"attr,token,omitempty"`

// Optional: The list of run events that will trigger notifications.
Triggers []string `jsonapi:"attr,triggers,omitempty"`
Triggers []NotificationTriggerType `jsonapi:"attr,triggers,omitempty"`

// Optional: The url of the notification configuration
URL *string `jsonapi:"attr,url,omitempty"`
Expand Down Expand Up @@ -243,6 +247,10 @@ func (s *notificationConfigurations) Update(ctx context.Context, notificationCon
return nil, ErrInvalidNotificationConfigID
}

if err := options.valid(); err != nil {
return nil, err
}

u := fmt.Sprintf("notification-configurations/%s", url.QueryEscape(notificationConfigurationID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
Expand Down Expand Up @@ -307,10 +315,44 @@ func (o NotificationConfigurationCreateOptions) valid() error {
return ErrRequiredName
}

if !validNotificationTriggerType(o.Triggers) {
return ErrInvalidNotificationTrigger
}

if *o.DestinationType == NotificationDestinationTypeGeneric || *o.DestinationType == NotificationDestinationTypeSlack {
if o.URL == nil {
return ErrRequiredURL
}
}
return nil
}

func (o NotificationConfigurationUpdateOptions) valid() error {
if o.Name != nil && !validString(o.Name) {
return ErrRequiredName
}

if !validNotificationTriggerType(o.Triggers) {
return ErrInvalidNotificationTrigger
}

return nil
}

func validNotificationTriggerType(triggers []NotificationTriggerType) bool {
for _, t := range triggers {
switch t {
case NotificationTriggerApplying,
NotificationTriggerNeedsAttention,
NotificationTriggerCompleted,
NotificationTriggerCreated,
NotificationTriggerErrored,
NotificationTriggerPlanning:
continue
default:
return false
}
}

return true
}
31 changes: 28 additions & 3 deletions notification_configuration_integration_test.go
Expand Up @@ -93,7 +93,7 @@ func TestNotificationConfigurationCreate(t *testing.T) {
Name: String(randomString(t)),
Token: String(randomString(t)),
URL: String("http://example.com"),
Triggers: []string{NotificationTriggerCreated},
Triggers: []NotificationTriggerType{NotificationTriggerCreated},
}

_, err := client.NotificationConfigurations.Create(ctx, wTest.ID, options)
Expand All @@ -106,7 +106,7 @@ func TestNotificationConfigurationCreate(t *testing.T) {
Enabled: Bool(false),
Token: String(randomString(t)),
URL: String("http://example.com"),
Triggers: []string{NotificationTriggerCreated},
Triggers: []NotificationTriggerType{NotificationTriggerCreated},
}

nc, err := client.NotificationConfigurations.Create(ctx, wTest.ID, options)
Expand All @@ -120,7 +120,7 @@ func TestNotificationConfigurationCreate(t *testing.T) {
Enabled: Bool(false),
Name: String(randomString(t)),
Token: String(randomString(t)),
Triggers: []string{NotificationTriggerCreated},
Triggers: []NotificationTriggerType{NotificationTriggerCreated},
}

nc, err := client.NotificationConfigurations.Create(ctx, wTest.ID, options)
Expand All @@ -134,6 +134,21 @@ func TestNotificationConfigurationCreate(t *testing.T) {
assert.EqualError(t, err, ErrInvalidWorkspaceID.Error())
})

t.Run("with an invalid notification trigger", func(t *testing.T) {
options := NotificationConfigurationCreateOptions{
DestinationType: NotificationDestination(NotificationDestinationTypeGeneric),
Enabled: Bool(false),
Name: String(randomString(t)),
Token: String(randomString(t)),
URL: String("http://example.com"),
Triggers: []NotificationTriggerType{"the beacons of gondor are lit"},
}

nc, err := client.NotificationConfigurations.Create(ctx, wTest.ID, options)
assert.Nil(t, nc)
assert.EqualError(t, err, ErrInvalidNotificationTrigger.Error())
})

t.Run("with email users when destination type is email", func(t *testing.T) {
options := NotificationConfigurationCreateOptions{
DestinationType: NotificationDestination(NotificationDestinationTypeEmail),
Expand Down Expand Up @@ -225,6 +240,16 @@ func TestNotificationConfigurationUpdate(t *testing.T) {
assert.Equal(t, nc.Name, "newName")
})

t.Run("with invalid notification trigger", func(t *testing.T) {
options := NotificationConfigurationUpdateOptions{
Triggers: []NotificationTriggerType{"fly you fools!"},
}

nc, err := client.NotificationConfigurations.Update(ctx, ncTest.ID, options)
assert.Nil(t, nc)
assert.EqualError(t, err, ErrInvalidNotificationTrigger.Error())
})

t.Run("with email users when destination type is email", func(t *testing.T) {
options := NotificationConfigurationUpdateOptions{
Enabled: Bool(true),
Expand Down

0 comments on commit 7345694

Please sign in to comment.