Skip to content

Commit

Permalink
added force send fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyansh-db committed Jul 31, 2024
1 parent 1cd8f71 commit ea6ceb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 66 deletions.
32 changes: 7 additions & 25 deletions settings/resource_notification_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,7 @@ func Read(ctx context.Context, d *schema.ResourceData, w *databricks.WorkspaceCl
return common.StructToData(readND, ndSchema, d)
}

func detectConfigTypeChange(d *schema.ResourceData) bool {
switch d.Get("destination_type").(string) {
case string(settings.DestinationTypeSlack):
_, ok := d.GetOk("config.0.slack")
return !ok
case string(settings.DestinationTypePagerduty):
_, ok := d.GetOk("config.0.pagerduty")
return !ok
case string(settings.DestinationTypeMicrosoftTeams):
_, ok := d.GetOk("config.0.microsoft_teams")
return !ok
case string(settings.DestinationTypeWebhook):
_, ok := d.GetOk("config.0.generic_webhook")
return !ok
}
return false
}

func Update(ctx context.Context, d *schema.ResourceData, w *databricks.WorkspaceClient) error {
if detectConfigTypeChange(d) {
err := Delete(ctx, d, w)
if err != nil {
return err
}
return Create(ctx, d, w)
}
var updateNDRequest settings.UpdateNotificationDestinationRequest
common.DataToStructPointer(d, ndSchema, &updateNDRequest)
updateNDRequest.Id = d.Id()
Expand Down Expand Up @@ -125,6 +100,13 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab
s.SchemaPath("config", "generic_webhook", "password_set").SetComputed()
s.SchemaPath("config", "generic_webhook", "username_set").SetComputed()

// ForceNew fields
s.SchemaPath("config", "slack").SetForceNew()
s.SchemaPath("config", "pagerduty").SetForceNew()
s.SchemaPath("config", "microsoft_teams").SetForceNew()
s.SchemaPath("config", "generic_webhook").SetForceNew()
s.SchemaPath("config", "email").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)
Expand Down
48 changes: 7 additions & 41 deletions settings/resource_notification_destination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestNDRead(t *testing.T) {
DestinationType: "EMAIL",
Config: &settings.Config{
Email: &settings.EmailConfig{
Addresses: []string{"abc@gmail.com"},
Addresses: []string{"abc@email.com"},
},
},
}, nil)
Expand All @@ -87,7 +87,7 @@ func TestNDRead(t *testing.T) {
display_name = "Notification Destination"
config {
email {
addresses = ["abc@gmail.com"]
addresses = ["abc@email.com"]
}
}
`,
Expand All @@ -106,7 +106,7 @@ func TestNDUpdate(t *testing.T) {
DisplayName: "Notification Destination",
Config: &settings.Config{
Email: &settings.EmailConfig{
Addresses: []string{"pqr@gmail.com"},
Addresses: []string{"pqr@email.com"},
},
},
}).Return(&settings.NotificationDestination{
Expand All @@ -115,7 +115,7 @@ func TestNDUpdate(t *testing.T) {
DestinationType: "EMAIL",
Config: &settings.Config{
Email: &settings.EmailConfig{
Addresses: []string{"pqr@gmail.com"},
Addresses: []string{"pqr@email.com"},
},
},
}, nil)
Expand All @@ -127,7 +127,7 @@ func TestNDUpdate(t *testing.T) {
DestinationType: "EMAIL",
Config: &settings.Config{
Email: &settings.EmailConfig{
Addresses: []string{"pqr@gmail.com"},
Addresses: []string{"pqr@email.com"},
},
},
}, nil)
Expand All @@ -139,14 +139,14 @@ func TestNDUpdate(t *testing.T) {
display_name = "Notification Destination"
config {
email {
addresses = ["pqr@gmail.com"]
addresses = ["pqr@email.com"]
}
}
`,
InstanceState: map[string]string{
"id": "xyz",
"display_name": "Notification Destination",
"config": `{"email":{"addresses":["abc@gmail.com"]}}`,
"config": `{"email":{"addresses":["abc@email.com"]}}`,
},
}.ApplyAndExpectData(t, map[string]any{
"id": "xyz",
Expand Down Expand Up @@ -175,37 +175,3 @@ func TestNDDelete(t *testing.T) {
`,
}.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{"[email protected]"},
// },
// },
// })
// },
// Resource: ResourceNotificationDestination(),
// Create: true,
// HCL: `
// display_name = "Notification Destination"
// config {
// generic_webhook {
// url = "https://webhook.site/abc"
// password = "password"
// }
// email {
// addresses = ["[email protected]"]
// }
// }
// `,
// }.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 ea6ceb5

Please sign in to comment.