Skip to content

Commit

Permalink
Use consts for notification types instead of string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston committed Mar 27, 2023
1 parent d53d81b commit bb64091
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
17 changes: 9 additions & 8 deletions api/v1alpha2/workspace_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package v1alpha2
import (
"fmt"

"github.com/hashicorp/go-tfe"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -78,13 +79,13 @@ func (w *Workspace) validateSpecNotifications() field.ErrorList {
}
nn[n.Name] = i
switch n.Type {
case "email":
case tfe.NotificationDestinationTypeEmail:
allErrs = append(allErrs, w.validateSpecNotificationsEmail(n, f)...)
case "generic":
case tfe.NotificationDestinationTypeGeneric:
allErrs = append(allErrs, w.validateSpecNotificationsGeneric(n, f)...)
case "microsoft-teams":
case tfe.NotificationDestinationTypeMicrosoftTeams:
allErrs = append(allErrs, w.validateSpecNotificationsMicrosoftTeams(n, f)...)
case "slack":
case tfe.NotificationDestinationTypeSlack:
allErrs = append(allErrs, w.validateSpecNotificationsSlack(n, f)...)
}
}
Expand All @@ -94,7 +95,7 @@ func (w *Workspace) validateSpecNotifications() field.ErrorList {

func (w *Workspace) validateSpecNotificationsEmail(n Notification, f *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
t := "email"
t := tfe.NotificationDestinationTypeEmail

if n.Token != "" {
allErrs = append(allErrs, field.Required(
Expand All @@ -121,7 +122,7 @@ func (w *Workspace) validateSpecNotificationsEmail(n Notification, f *field.Path

func (w *Workspace) validateSpecNotificationsGeneric(n Notification, f *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
t := "generic"
t := tfe.NotificationDestinationTypeGeneric

if n.Token == "" {
allErrs = append(allErrs, field.Required(
Expand Down Expand Up @@ -155,7 +156,7 @@ func (w *Workspace) validateSpecNotificationsGeneric(n Notification, f *field.Pa

func (w *Workspace) validateSpecNotificationsMicrosoftTeams(n Notification, f *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
t := "microsoft-teams"
t := tfe.NotificationDestinationTypeMicrosoftTeams

if n.URL == "" {
allErrs = append(allErrs, field.Required(
Expand Down Expand Up @@ -190,7 +191,7 @@ func (w *Workspace) validateSpecNotificationsMicrosoftTeams(n Notification, f *f

func (w *Workspace) validateSpecNotificationsSlack(n Notification, f *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
t := "slack"
t := tfe.NotificationDestinationTypeSlack

if n.URL == "" {
allErrs = append(allErrs, field.Required(
Expand Down
56 changes: 29 additions & 27 deletions api/v1alpha2/workspace_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package v1alpha2

import (
"testing"

"github.com/hashicorp/go-tfe"
)

func TestValidateWorkspaceSpecAgentPool(t *testing.T) {
Expand Down Expand Up @@ -67,7 +69,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailAddresses: []string{
"user@mail.com",
},
Expand All @@ -80,7 +82,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailUsers: []string{
"user@mail.com",
},
Expand All @@ -93,7 +95,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailAddresses: []string{
"user@mail.com",
},
Expand All @@ -109,7 +111,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "generic",
Type: tfe.NotificationDestinationTypeGeneric,
Token: token,
URL: url,
},
Expand All @@ -121,7 +123,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
URL: url,
},
},
Expand All @@ -132,7 +134,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "slack",
Type: tfe.NotificationDestinationTypeSlack,
URL: url,
},
},
Expand All @@ -143,21 +145,21 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "thisA",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailAddresses: []string{
"user@mail.com",
},
},
{
Name: "thisB",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailUsers: []string{
"user@mail.com",
},
},
{
Name: "thisC",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailAddresses: []string{
"user@mail.com",
},
Expand All @@ -167,18 +169,18 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
},
{
Name: "thisD",
Type: "generic",
Type: tfe.NotificationDestinationTypeGeneric,
Token: token,
URL: url,
},
{
Name: "thisE",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
URL: url,
},
{
Name: "thisF",
Type: "slack",
Type: tfe.NotificationDestinationTypeSlack,
URL: url,
},
},
Expand All @@ -200,7 +202,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
},
},
},
Expand All @@ -210,7 +212,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
URL: url,
},
},
Expand All @@ -221,7 +223,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
Token: token,
},
},
Expand All @@ -232,7 +234,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "generic",
Type: tfe.NotificationDestinationTypeGeneric,
URL: url,
},
},
Expand All @@ -243,7 +245,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "generic",
Type: tfe.NotificationDestinationTypeGeneric,
Token: token,
},
},
Expand All @@ -254,7 +256,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "generic",
Type: tfe.NotificationDestinationTypeGeneric,
Token: token,
URL: url,
EmailAddresses: []string{
Expand All @@ -269,7 +271,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "generic",
Type: tfe.NotificationDestinationTypeGeneric,
Token: token,
URL: url,
EmailUsers: []string{
Expand All @@ -284,7 +286,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
URL: url,
Token: token,
},
Expand All @@ -296,7 +298,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
},
},
},
Expand All @@ -306,7 +308,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
Token: token,
URL: url,
EmailAddresses: []string{
Expand All @@ -321,7 +323,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
Token: token,
URL: url,
EmailUsers: []string{
Expand All @@ -336,7 +338,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
URL: url,
Token: token,
},
Expand All @@ -348,7 +350,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "slack",
Type: tfe.NotificationDestinationTypeSlack,
},
},
},
Expand All @@ -358,7 +360,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
Token: token,
URL: url,
EmailAddresses: []string{
Expand All @@ -373,7 +375,7 @@ func TestValidateWorkspaceSpecNotifications(t *testing.T) {
Notifications: []Notification{
{
Name: "this",
Type: "microsoft-teams",
Type: tfe.NotificationDestinationTypeMicrosoftTeams,
Token: token,
URL: url,
EmailUsers: []string{
Expand Down
15 changes: 8 additions & 7 deletions controllers/workspace_controller_notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/hashicorp/go-tfe"
tfc "github.com/hashicorp/go-tfe"
appv1alpha2 "github.com/hashicorp/terraform-cloud-operator/api/v1alpha2"
)
Expand Down Expand Up @@ -82,7 +83,7 @@ var _ = Describe("Workspace controller", Label("Notifications"), Ordered, func()
It("can create single notification", func() {
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "slack",
Type: "slack",
Type: tfe.NotificationDestinationTypeSlack,
URL: "https://example.com",
})
// Create a new Kubernetes workspace object and wait until the controller finishes the reconciliation
Expand All @@ -94,12 +95,12 @@ var _ = Describe("Workspace controller", Label("Notifications"), Ordered, func()
It("can create multiple notifications", func() {
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "slack",
Type: "slack",
Type: tfe.NotificationDestinationTypeSlack,
URL: "https://example.com",
})
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "email",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailUsers: []string{memberEmail},
})
// Create a new Kubernetes workspace object and wait until the controller finishes the reconciliation
Expand All @@ -111,7 +112,7 @@ var _ = Describe("Workspace controller", Label("Notifications"), Ordered, func()
It("can re-create notifications", func() {
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "slack",
Type: "slack",
Type: tfe.NotificationDestinationTypeSlack,
URL: "https://example.com",
})
// Create a new Kubernetes workspace object and wait until the controller finishes the reconciliation
Expand All @@ -134,7 +135,7 @@ var _ = Describe("Workspace controller", Label("Notifications"), Ordered, func()
It("can update notifications", func() {
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "email",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailUsers: []string{memberEmail},
})
// Create a new Kubernetes workspace object and wait until the controller finishes the reconciliation
Expand All @@ -152,7 +153,7 @@ var _ = Describe("Workspace controller", Label("Notifications"), Ordered, func()
It("can delete notifications", func() {
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "email",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailUsers: []string{memberEmail},
})
// Create a new Kubernetes workspace object and wait until the controller finishes the reconciliation
Expand All @@ -173,7 +174,7 @@ var _ = Describe("Workspace controller", Label("Notifications"), Ordered, func()
}
instance.Spec.Notifications = append(instance.Spec.Notifications, appv1alpha2.Notification{
Name: "email",
Type: "email",
Type: tfe.NotificationDestinationTypeEmail,
EmailAddresses: []string{"user@example.com"},
})
// Create a new Kubernetes workspace object and wait until the controller finishes the reconciliation
Expand Down

0 comments on commit bb64091

Please sign in to comment.