diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go new file mode 100644 index 0000000000..a1df5d3376 --- /dev/null +++ b/internal/acceptance/notification_destination_test.go @@ -0,0 +1,22 @@ +package acceptance + +import ( + "testing" + + "github.com/databricks/terraform-provider-databricks/qa" +) + +func TestAccNDEmail(t *testing.T) { + workspaceLevel(t, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "Email Notification Destination" + config { + email { + addresses = ["` + qa.RandomEmail() + `"] + } + } + } + `, + }) +} diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index 69796e1c3e..2b344863ea 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -14,6 +14,43 @@ type NDStruct struct { func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema { // Required fields + s.SchemaPath("display_name").SetRequired() + // s.SchemaPath("config").SetRequired() + + // Computed fields + s.SchemaPath("id").SetComputed() + s.SchemaPath("destination_type").SetComputed() + s.SchemaPath("config", "slack", "url_set").SetComputed() + s.SchemaPath("config", "pagerduty", "integration_key_set").SetComputed() + s.SchemaPath("config", "microsoft_teams", "url_set").SetComputed() + s.SchemaPath("config", "generic_webhook", "url_set").SetComputed() + s.SchemaPath("config", "generic_webhook", "password_set").SetComputed() + s.SchemaPath("config", "generic_webhook", "username_set").SetComputed() + + // ForceNew fields + s.SchemaPath("destination_type").SetForceNew() + + // ConflictsWith fields + config_eoo := []string{"config.0.slack", "config.0.pagerduty", "config.0.microsoft_teams", "config.0.generic_webhook", "config.0.email"} + s.SchemaPath("config", "slack").SetExactlyOneOf(config_eoo) + // s.SchemaPath("config", "pagerduty").SetExactlyOneOf(config_eoo) + // s.SchemaPath("config", "microsoft_teams").SetExactlyOneOf(config_eoo) + // s.SchemaPath("config", "generic_webhook").SetExactlyOneOf(config_eoo) + // s.SchemaPath("config", "email").SetExactlyOneOf(config_eoo) + + // RequiredWith fields + s.SchemaPath("config", "slack").SetRequiredWith([]string{"config.0.slack.0.url"}) + s.SchemaPath("config", "pagerduty").SetRequiredWith([]string{"config.0.pagerduty.0.integration_key"}) + s.SchemaPath("config", "microsoft_teams").SetRequiredWith([]string{"config.0.microsoft_teams.0.url"}) + s.SchemaPath("config", "generic_webhook").SetRequiredWith([]string{"config.0.generic_webhook.0.url"}) + s.SchemaPath("config", "email").SetRequiredWith([]string{"config.0.email.0.addresses"}) + + // s.SchemaPath("config", "slack", "url").SetRequiredWith([]string{"config.0.slack"}) + // s.SchemaPath("config", "pagerduty", "integration_key").SetRequiredWith([]string{"config.0.pagerduty"}) + // s.SchemaPath("config", "microsoft_teams", "url").SetRequiredWith([]string{"config.0.microsoft_teams"}) + // s.SchemaPath("config", "generic_webhook", "url").SetRequiredWith([]string{"config.0.generic_webhook"}) + // s.SchemaPath("config", "generic_webhook", "password").SetRequiredWith([]string{"config.0.generic_webhook"}) + // s.SchemaPath("config", "email", "addresses").SetRequiredWith([]string{"config.0.email"}) return s } @@ -57,6 +94,7 @@ func ResourceNotificationDestination() common.Resource { } var updateNDRequest settings.UpdateNotificationDestinationRequest common.DataToStructPointer(d, ndSchema, &updateNDRequest) + updateNDRequest.Id = d.Id() _, err = w.NotificationDestinations.Update(ctx, updateNDRequest) if err != nil { return err diff --git a/settings/resource_notification_destination_test.go b/settings/resource_notification_destination_test.go index 0e66fdf1e5..1a0c16684e 100644 --- a/settings/resource_notification_destination_test.go +++ b/settings/resource_notification_destination_test.go @@ -1 +1,184 @@ package settings + +import ( + "testing" + + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/settings" + "github.com/databricks/terraform-provider-databricks/qa" + + "github.com/stretchr/testify/mock" +) + +func TestNDCreate(t *testing.T) { + qa.ResourceFixture{ + MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { + e := w.GetMockNotificationDestinationsAPI().EXPECT() + e.Create(mock.Anything, settings.CreateNotificationDestinationRequest{ + DisplayName: "Notification Destination", + Config: &settings.Config{ + GenericWebhook: &settings.GenericWebhookConfig{ + Url: "https://webhook.site/abc", + Password: "password", + }, + }, + }).Return(&settings.NotificationDestination{ + Id: "xyz", + DisplayName: "Notification Destination", + DestinationType: "EMAIL", + }, nil) + e.Get(mock.Anything, settings.GetNotificationDestinationRequest{ + Id: "xyz", + }).Return(&settings.NotificationDestination{ + Id: "xyz", + DisplayName: "Notification Destination", + DestinationType: "EMAIL", + }, nil) + }, + Resource: ResourceNotificationDestination(), + Create: true, + HCL: ` + display_name = "Notification Destination" + config { + generic_webhook { + url = "https://webhook.site/abc" + password = "password" + } + } + `, + }.ApplyAndExpectData(t, map[string]any{ + "id": "xyz", + "display_name": "Notification Destination", + }) +} + +func TestNDRead(t *testing.T) { + qa.ResourceFixture{ + MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { + w.GetMockNotificationDestinationsAPI().EXPECT().Get(mock.Anything, settings.GetNotificationDestinationRequest{ + Id: "xyz", + }).Return(&settings.NotificationDestination{ + Id: "xyz", + DisplayName: "Notification Destination", + DestinationType: "EMAIL", + }, nil) + }, + Resource: ResourceNotificationDestination(), + Read: true, + ID: "xyz", + HCL: ` + display_name = "Notification Destination" + config { + email { + addresses = ["abc@gmail.com"] + } + } + `, + }.ApplyAndExpectData(t, map[string]any{ + "id": "xyz", + "display_name": "Notification Destination", + }) +} + +func TestNDUpdate(t *testing.T) { + qa.ResourceFixture{ + MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { + e := w.GetMockNotificationDestinationsAPI().EXPECT() + e.Update(mock.Anything, settings.UpdateNotificationDestinationRequest{ + Id: "xyz", + DisplayName: "Notification Destination", + Config: &settings.Config{ + Email: &settings.EmailConfig{ + Addresses: []string{"pqr@gmail.com"}, + }, + }, + }).Return(&settings.NotificationDestination{ + Id: "xyz", + DisplayName: "Notification Destination", + DestinationType: "EMAIL", + }, nil) + e.Get(mock.Anything, settings.GetNotificationDestinationRequest{ + Id: "xyz", + }).Return(&settings.NotificationDestination{ + Id: "xyz", + DisplayName: "Notification Destination", + DestinationType: "EMAIL", + }, nil) + }, + Resource: ResourceNotificationDestination(), + Update: true, + ID: "xyz", + HCL: ` + display_name = "Notification Destination" + config { + email { + addresses = ["pqr@gmail.com"] + } + } + `, + InstanceState: map[string]string{ + "id": "xyz", + "display_name": "Notification Destination", + "config": `{"email":{"addresses":["abc@gmail.com"]}}`, + }, + }.ApplyAndExpectData(t, map[string]any{ + "id": "xyz", + "display_name": "Notification Destination", + }) +} + +func TestNDDelete(t *testing.T) { + qa.ResourceFixture{ + MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { + w.GetMockNotificationDestinationsAPI().EXPECT().Delete(mock.Anything, settings.DeleteNotificationDestinationRequest{ + Id: "xyz", + }).Return(nil) + }, + Resource: ResourceNotificationDestination(), + Delete: true, + ID: "xyz", + HCL: ` + display_name = "Notification Destination" + config { + generic_webhook { + url = "https://webhook.site/abc" + password = "password" + } + } + `, + }.ApplyNoError(t) +} + +// func TestNDConflictingFields(t *testing.T) { +// qa.ResourceFixture{ +// MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { +// e := w.GetMockNotificationDestinationsAPI().EXPECT() +// e.Create(mock.Anything, settings.CreateNotificationDestinationRequest{ +// DisplayName: "Notification Destination", +// Config: &settings.Config{ +// GenericWebhook: &settings.GenericWebhookConfig{ +// Url: "https://webhook.site/abc", +// Password: "password", +// }, +// Email: &settings.EmailConfig{ +// Addresses: []string{"abc@gmail.com"}, +// }, +// }, +// }) +// }, +// Resource: ResourceNotificationDestination(), +// Create: true, +// HCL: ` +// display_name = "Notification Destination" +// config { +// generic_webhook { +// url = "https://webhook.site/abc" +// password = "password" +// } +// email { +// addresses = ["abc@gmail.com"] +// } +// } +// `, +// }.ExpectError(t, "invalid config supplied. [config.#.email] Invalid combination of arguments. [config.#.generic_webhook] Invalid combination of arguments. [config.#.microsoft_teams] Invalid combination of arguments. [config.#.pagerduty] Invalid combination of arguments. [config.#.slack] Invalid combination of ars") +// }