Skip to content

Commit

Permalink
Made Notification Destination resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyansh-db committed Jul 25, 2024
1 parent 5735002 commit 209487e
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/acceptance/notification_destination_test.go
Original file line number Diff line number Diff line change
@@ -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() + `"]
}
}
}
`,
})
}
38 changes: 38 additions & 0 deletions settings/resource_notification_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
183 changes: 183 additions & 0 deletions settings/resource_notification_destination_test.go
Original file line number Diff line number Diff line change
@@ -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")
// }

0 comments on commit 209487e

Please sign in to comment.