From ff7fdd17e422fc92e560deffcd51109c143c4ced Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Wed, 24 Jul 2024 17:33:31 +0200 Subject: [PATCH 01/15] WIP --- go.mod | 17 +++-- go.sum | 32 ++++---- provider/provider.go | 1 + settings/resource_notification_destination.go | 76 +++++++++++++++++++ .../resource_notification_destination_test.go | 1 + 5 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 settings/resource_notification_destination.go create mode 100644 settings/resource_notification_destination_test.go diff --git a/go.mod b/go.mod index 1947785570..6355e62ddc 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/databricks/terraform-provider-databricks go 1.22 require ( - github.com/databricks/databricks-sdk-go v0.43.2 + github.com/databricks/databricks-sdk-go v0.43.3-0.20240722114738-7650ccc7998a github.com/golang-jwt/jwt/v4 v4.5.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/hcl v1.0.0 @@ -13,7 +13,6 @@ require ( github.com/stretchr/testify v1.9.0 github.com/zclconf/go-cty v1.14.4 golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 - golang.org/x/mod v0.19.0 ) require ( @@ -70,17 +69,19 @@ require ( go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/metric v1.24.0 // indirect go.opentelemetry.io/otel/trace v1.24.0 // indirect - golang.org/x/crypto v0.23.0 // indirect - golang.org/x/net v0.25.0 // indirect + golang.org/x/crypto v0.24.0 // indirect + golang.org/x/mod v0.19.0 // indirect + golang.org/x/net v0.26.0 // indirect golang.org/x/oauth2 v0.20.0 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect golang.org/x/time v0.5.0 // indirect - golang.org/x/tools v0.18.0 // indirect + golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/api v0.182.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect - google.golang.org/grpc v1.64.0 // indirect + google.golang.org/grpc v1.64.1 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 6571a14507..9e185a86dd 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,8 @@ github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBS github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= -github.com/databricks/databricks-sdk-go v0.43.2 h1:4B+sHAYO5kFqwZNQRmsF70eecqsFX6i/0KfXoDFQT/E= -github.com/databricks/databricks-sdk-go v0.43.2/go.mod h1:nlzeOEgJ1Tmb5HyknBJ3GEorCZKWqEBoHprvPmTSNq8= +github.com/databricks/databricks-sdk-go v0.43.3-0.20240722114738-7650ccc7998a h1:ZeQihrvQqGf3CWs8gTZhAMmwCnh+RQThp/joeHJssWQ= +github.com/databricks/databricks-sdk-go v0.43.3-0.20240722114738-7650ccc7998a/go.mod h1:ds+zbv5mlQG7nFEU5ojLtgN/u0/9YzZmKQES/CfedzU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -223,8 +223,8 @@ go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= +golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ= golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= @@ -243,8 +243,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= @@ -269,19 +269,19 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= +golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -291,8 +291,8 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= -golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.182.0 h1:if5fPvudRQ78GeRx3RayIoiuV7modtErPIZC/T2bIvE= @@ -311,8 +311,8 @@ google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= -google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= +google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= +google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/provider/provider.go b/provider/provider.go index 32eee067c5..c33cf9330a 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -173,6 +173,7 @@ func DatabricksProvider() *schema.Provider { "databricks_mws_vpc_endpoint": mws.ResourceMwsVpcEndpoint().ToResource(), "databricks_mws_workspaces": mws.ResourceMwsWorkspaces().ToResource(), "databricks_notebook": workspace.ResourceNotebook().ToResource(), + "databricks_notification_destination": settings.ResourceNotificationDestination().ToResource(), "databricks_obo_token": tokens.ResourceOboToken().ToResource(), "databricks_online_table": catalog.ResourceOnlineTable().ToResource(), "databricks_permission_assignment": access.ResourcePermissionAssignment().ToResource(), diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go new file mode 100644 index 0000000000..69796e1c3e --- /dev/null +++ b/settings/resource_notification_destination.go @@ -0,0 +1,76 @@ +package settings + +import ( + "context" + + "github.com/databricks/databricks-sdk-go/service/settings" + "github.com/databricks/terraform-provider-databricks/common" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +type NDStruct struct { + settings.NotificationDestination +} + +func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema { + // Required fields + + return s +} + +var ndSchema = common.StructToSchema(NDStruct{}, nil) + +func ResourceNotificationDestination() common.Resource { + return common.Resource{ + Schema: ndSchema, + Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { + w, err := c.WorkspaceClient() + if err != nil { + return err + } + var newNDrequest settings.CreateNotificationDestinationRequest + common.DataToStructPointer(d, ndSchema, &newNDrequest) + createdND, err := w.NotificationDestinations.Create(ctx, newNDrequest) + if err != nil { + return err + } + d.SetId(createdND.Id) + return nil + }, + Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { + w, err := c.WorkspaceClient() + if err != nil { + return err + } + readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: d.Id(), + }) + if err != nil { + return err + } + return common.StructToData(readND, ndSchema, d) + }, + Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { + w, err := c.WorkspaceClient() + if err != nil { + return err + } + var updateNDRequest settings.UpdateNotificationDestinationRequest + common.DataToStructPointer(d, ndSchema, &updateNDRequest) + _, err = w.NotificationDestinations.Update(ctx, updateNDRequest) + if err != nil { + return err + } + return nil + }, + Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { + w, err := c.WorkspaceClient() + if err != nil { + return err + } + return w.NotificationDestinations.Delete(ctx, settings.DeleteNotificationDestinationRequest{ + Id: d.Id(), + }) + }, + } +} diff --git a/settings/resource_notification_destination_test.go b/settings/resource_notification_destination_test.go new file mode 100644 index 0000000000..0e66fdf1e5 --- /dev/null +++ b/settings/resource_notification_destination_test.go @@ -0,0 +1 @@ +package settings From 209487e543fc3cfeafc9093796b65c80afdbd570 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Thu, 25 Jul 2024 10:46:18 +0200 Subject: [PATCH 02/15] Made Notification Destination resource --- .../notification_destination_test.go | 22 +++ settings/resource_notification_destination.go | 38 ++++ .../resource_notification_destination_test.go | 183 ++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 internal/acceptance/notification_destination_test.go 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") +// } From 36f845e84206a61419edf427833d52e7276e75c1 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Thu, 25 Jul 2024 11:19:27 +0200 Subject: [PATCH 03/15] added some sensitive fields --- settings/resource_notification_destination.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index 2b344863ea..cebb5967ef 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -52,6 +52,9 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab // s.SchemaPath("config", "generic_webhook", "password").SetRequiredWith([]string{"config.0.generic_webhook"}) // s.SchemaPath("config", "email", "addresses").SetRequiredWith([]string{"config.0.email"}) + // Sensitive fields + s.SchemaPath("config", "generic_webhook", "password").SetSensitive() + return s } From e7b73811fd4fe54c7f3cb191f65cfdbe3b99a7b7 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Thu, 25 Jul 2024 14:48:27 +0200 Subject: [PATCH 04/15] added documentation and improved integration test --- docs/resources/notification_destination.md | 99 +++++++++++++++++++ .../notification_destination_test.go | 24 ++++- settings/resource_notification_destination.go | 8 -- 3 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 docs/resources/notification_destination.md diff --git a/docs/resources/notification_destination.md b/docs/resources/notification_destination.md new file mode 100644 index 0000000000..85f17fffa8 --- /dev/null +++ b/docs/resources/notification_destination.md @@ -0,0 +1,99 @@ +--- +subcategory: "Workspace" +--- +# databricks_notification_destination Resource + +This resource allows you to manage [Notification Destinations](https://docs.databricks.com/api/workspace/notificationdestinations). Notification destinations are used to send notifications for query alerts and jobs to destinations outside of Databricks. Only workspace admins can create, update, and delete notification destinations. + +## Example Usage + +`Email` notification destination: + +```hcl +resource "databricks_notification_destination" "ndresource" { + display_name = "Notification Destination" + config { + email { + addresses = ["abc@gmail.com"] + } + } +} +``` +`Slack` notification destination: + +```hcl +resource "databricks_notification_destination" "ndresource" { + display_name = "Notification Destination" + config { + slack { + url = "https://hooks.slack.com/services/..." + } + } +} +``` +`PagerDuty` notification destination: + +```hcl +resource "databricks_notification_destination" "ndresource" { + display_name = "Notification Destination" + config { + pagerduty { + integration_key = "xxxxxx" + } + } +} +``` +`Microsoft Teams` notification destination: + +```hcl +resource "databricks_notification_destination" "ndresource" { + display_name = "Notification Destination" + config { + microsoft_teams { + url = "https://outlook.office.com/webhook/..." + } + } +} +``` +`Generic Webhook` notification destination: + +```hcl +resource "databricks_notification_destination" "ndresource" { + display_name = "Notification Destination" + config { + generic_webhook { + url = "https://example.com/webhook" + username = "username" // Optional + password = "password" // Optional + } + } +} +``` + + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) The display name of the Notification Destination. +* `config` - (Required) The configuration of the Notification Destination. It must be one of the following: + * `email` - The email configuration of the Notification Destination. It must contain the following: + * `addresses` - (Required) The list of email addresses to send notifications to. + * `slack` - The Slack configuration of the Notification Destination. It must contain the following: + * `url` - (Required) The Slack webhook URL. + * `pagerduty` - The PagerDuty configuration of the Notification Destination. It must contain the following: + * `integration_key` - (Required) The PagerDuty integration key. + * `microsoft_teams` - The Microsoft Teams configuration of the Notification Destination. It must contain the following: + * `url` - (Required) The Microsoft Teams webhook URL. + * `generic_webhook` - The Generic Webhook configuration of the Notification Destination. It must contain the following: + * `url` - (Required) The Generic Webhook URL. + * `username` - (Optional) The username for basic authentication. + * `password` - (Optional) The password for basic authentication. + + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The unique ID of the Notification Destination. + diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index a1df5d3376..c466a833e7 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -1,16 +1,22 @@ package acceptance import ( + "context" "testing" + "github.com/databricks/databricks-sdk-go/service/settings" + "github.com/databricks/terraform-provider-databricks/common" "github.com/databricks/terraform-provider-databricks/qa" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAccNDEmail(t *testing.T) { + display_name := "Email Notification Destination - " + qa.RandomName() workspaceLevel(t, step{ Template: ` resource "databricks_notification_destination" "this" { - display_name = "Email Notification Destination" + display_name = "` + display_name + `" config { email { addresses = ["` + qa.RandomEmail() + `"] @@ -18,5 +24,21 @@ func TestAccNDEmail(t *testing.T) { } } `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("EMAIL"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), }) } diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index cebb5967ef..8137c003f8 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -15,7 +15,6 @@ 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() @@ -45,13 +44,6 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab 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"}) - // Sensitive fields s.SchemaPath("config", "generic_webhook", "password").SetSensitive() From 3a2efbd74d22c93d25bc0612529ead69bd0529e2 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Fri, 26 Jul 2024 10:00:02 +0200 Subject: [PATCH 05/15] wip --- .../notification_destination_test.go | 32 +++++++++++++++++++ settings/resource_notification_destination.go | 24 ++++++++++++-- .../resource_notification_destination_test.go | 31 ++++++++++++++++-- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index c466a833e7..736b000384 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -42,3 +42,35 @@ func TestAccNDEmail(t *testing.T) { }), }) } + +func TestAccNDSlack(t *testing.T) { + display_name := "Notification Destination - " + qa.RandomName() + workspaceLevel(t, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + slack { + url = "https://hooks.slack.com/services/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" + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + // assert.Equal(t, settings.DestinationType("EMAIL"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }) +} diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index 8137c003f8..a34d3ca583 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -2,6 +2,7 @@ package settings import ( "context" + "fmt" "github.com/databricks/databricks-sdk-go/service/settings" "github.com/databricks/terraform-provider-databricks/common" @@ -45,7 +46,12 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab s.SchemaPath("config", "email").SetRequiredWith([]string{"config.0.email.0.addresses"}) // Sensitive fields - s.SchemaPath("config", "generic_webhook", "password").SetSensitive() + // s.SchemaPath("config", "generic_webhook", "password").SetSensitive() + // s.SchemaPath("config", "generic_webhook", "username").SetSensitive() + // s.SchemaPath("config", "generic_webhook", "url").SetSensitive() + // s.SchemaPath("config", "microsoft_teams", "url").SetSensitive() + // s.SchemaPath("config", "pagerduty", "integration_key").SetSensitive() + // s.SchemaPath("config", "slack", "url").SetSensitive() return s } @@ -56,6 +62,7 @@ func ResourceNotificationDestination() common.Resource { return common.Resource{ Schema: ndSchema, Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { + w, err := c.WorkspaceClient() if err != nil { return err @@ -74,13 +81,26 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } + var s settings.NotificationDestination + common.DataToStructPointer(d, ndSchema, &s) + readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ Id: d.Id(), }) if err != nil { return err } - return common.StructToData(readND, ndSchema, d) + + err = common.StructToData(readND, ndSchema, d) + if err != nil { + return err + } + // fmt.Println(s.Config.Slack.Url) + // common.StructToData(s.Config, ndSchema, d) + // d.Get("config.0.slack.0.url") + d.Set("config.0.slack.0.url", s.Config.Slack.Url) + fmt.Println(d.Get("config.0.slack.0.url")) + return nil }, Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { w, err := c.WorkspaceClient() diff --git a/settings/resource_notification_destination_test.go b/settings/resource_notification_destination_test.go index 1a0c16684e..67e1e86e0f 100644 --- a/settings/resource_notification_destination_test.go +++ b/settings/resource_notification_destination_test.go @@ -25,14 +25,26 @@ func TestNDCreate(t *testing.T) { }).Return(&settings.NotificationDestination{ Id: "xyz", DisplayName: "Notification Destination", - DestinationType: "EMAIL", + DestinationType: "WEBHOOK", + Config: &settings.Config{ + GenericWebhook: &settings.GenericWebhookConfig{ + UrlSet: true, + PasswordSet: true, + }, + }, }, nil) e.Get(mock.Anything, settings.GetNotificationDestinationRequest{ Id: "xyz", }).Return(&settings.NotificationDestination{ Id: "xyz", DisplayName: "Notification Destination", - DestinationType: "EMAIL", + DestinationType: "WEBHOOK", + Config: &settings.Config{ + GenericWebhook: &settings.GenericWebhookConfig{ + UrlSet: true, + PasswordSet: true, + }, + }, }, nil) }, Resource: ResourceNotificationDestination(), @@ -61,6 +73,11 @@ func TestNDRead(t *testing.T) { Id: "xyz", DisplayName: "Notification Destination", DestinationType: "EMAIL", + Config: &settings.Config{ + Email: &settings.EmailConfig{ + Addresses: []string{"abc@gmail.com"}, + }, + }, }, nil) }, Resource: ResourceNotificationDestination(), @@ -96,6 +113,11 @@ func TestNDUpdate(t *testing.T) { Id: "xyz", DisplayName: "Notification Destination", DestinationType: "EMAIL", + Config: &settings.Config{ + Email: &settings.EmailConfig{ + Addresses: []string{"pqr@gmail.com"}, + }, + }, }, nil) e.Get(mock.Anything, settings.GetNotificationDestinationRequest{ Id: "xyz", @@ -103,6 +125,11 @@ func TestNDUpdate(t *testing.T) { Id: "xyz", DisplayName: "Notification Destination", DestinationType: "EMAIL", + Config: &settings.Config{ + Email: &settings.EmailConfig{ + Addresses: []string{"pqr@gmail.com"}, + }, + }, }, nil) }, Resource: ResourceNotificationDestination(), From 2a9d9f3495019433ff5025fd46e93af2996dd8c0 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia <171924202+Divyansh-db@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:01:53 +0200 Subject: [PATCH 06/15] Update docs/resources/notification_destination.md Co-authored-by: vuong-nguyen <44292934+nkvuong@users.noreply.github.com> --- docs/resources/notification_destination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/notification_destination.md b/docs/resources/notification_destination.md index 85f17fffa8..ba9c78d4c5 100644 --- a/docs/resources/notification_destination.md +++ b/docs/resources/notification_destination.md @@ -76,7 +76,7 @@ resource "databricks_notification_destination" "ndresource" { The following arguments are supported: * `display_name` - (Required) The display name of the Notification Destination. -* `config` - (Required) The configuration of the Notification Destination. It must be one of the following: +* `config` - (Required) The configuration of the Notification Destination. It must contain exactly one of the following blocks: * `email` - The email configuration of the Notification Destination. It must contain the following: * `addresses` - (Required) The list of email addresses to send notifications to. * `slack` - The Slack configuration of the Notification Destination. It must contain the following: From 2eb0d15f4a697ff4b9d7a7022f421a546d5c6f25 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Mon, 29 Jul 2024 09:22:42 +0200 Subject: [PATCH 07/15] wip --- .../notification_destination_test.go | 101 +++++++++++++++++- settings/resource_notification_destination.go | 6 +- 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index 736b000384..bed097e1a0 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -51,7 +51,7 @@ func TestAccNDSlack(t *testing.T) { display_name = "` + display_name + `" config { slack { - url = "https://hooks.slack.com/services/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" + url = "https://hooks.slack.com/services/..." } } } @@ -67,7 +67,104 @@ func TestAccNDSlack(t *testing.T) { if err != nil { return err } - // assert.Equal(t, settings.DestinationType("EMAIL"), ndResource.DestinationType) + assert.Equal(t, settings.DestinationType("SLACK"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }) +} + +func TestAccNDMicrosoftTeams(t *testing.T) { + display_name := "Notification Destination - " + qa.RandomName() + workspaceLevel(t, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + microsoft_teams { + url = "https://outlook.office.com/webhook/..." + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("MICROSOFT_TEAMS"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }) +} + +func TestAccNDPagerduty(t *testing.T) { + display_name := "Notification Destination - " + qa.RandomName() + workspaceLevel(t, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + pagerduty { + integration_key = "..." + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("PAGERDUTY"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }) +} + +func TestAccNDGenericWebhook(t *testing.T) { + display_name := "Notification Destination - " + qa.RandomName() + workspaceLevel(t, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + generic_webhook { + url = "https://webhook.site/abc" + password = "password" + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("WEBHOOK"), ndResource.DestinationType) assert.Equal(t, display_name, ndResource.DisplayName) require.NoError(t, err) return nil diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index a34d3ca583..ff4dbe8ae5 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -2,7 +2,6 @@ package settings import ( "context" - "fmt" "github.com/databricks/databricks-sdk-go/service/settings" "github.com/databricks/terraform-provider-databricks/common" @@ -95,11 +94,8 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } - // fmt.Println(s.Config.Slack.Url) - // common.StructToData(s.Config, ndSchema, d) - // d.Get("config.0.slack.0.url") d.Set("config.0.slack.0.url", s.Config.Slack.Url) - fmt.Println(d.Get("config.0.slack.0.url")) + // fmt.Println(d.Get("config.0.slack.0.url")) return nil }, Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { From 7f5de75266f22091dc8d0b37ac68d5ab5e832a28 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Mon, 29 Jul 2024 13:25:55 +0200 Subject: [PATCH 08/15] Fixed the resource and added integration tests for all fields --- .../notification_destination_test.go | 80 ++++++++++++++++++- settings/resource_notification_destination.go | 78 ++++++++++++++---- 2 files changed, 139 insertions(+), 19 deletions(-) diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index bed097e1a0..5a9fe3f7cc 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -24,6 +24,17 @@ func TestAccNDEmail(t *testing.T) { } } `, + }, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + email { + addresses = ["` + qa.RandomEmail() + `", "` + qa.RandomEmail() + `"] + } + } + } + `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { w, err := client.WorkspaceClient() if err != nil { @@ -51,7 +62,34 @@ func TestAccNDSlack(t *testing.T) { display_name = "` + display_name + `" config { slack { - url = "https://hooks.slack.com/services/..." + url = "https://hooks.slack.com/services/{var.RANDOM}" + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("SLACK"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + slack { + url = "https://hooks.slack.com/services/{var.RANDOM}" } } } @@ -83,7 +121,18 @@ func TestAccNDMicrosoftTeams(t *testing.T) { display_name = "` + display_name + `" config { microsoft_teams { - url = "https://outlook.office.com/webhook/..." + url = "https://outlook.office.com/webhook/{var.RANDOM}" + } + } + } + `, + }, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + microsoft_teams { + url = "https://outlook.office.com/webhook/{var.RANDOM}" } } } @@ -115,7 +164,18 @@ func TestAccNDPagerduty(t *testing.T) { display_name = "` + display_name + `" config { pagerduty { - integration_key = "..." + integration_key = "{var.RANDOM}" + } + } + } + `, + }, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + pagerduty { + integration_key = "{var.RANDOM}" } } } @@ -147,12 +207,24 @@ func TestAccNDGenericWebhook(t *testing.T) { display_name = "` + display_name + `" config { generic_webhook { - url = "https://webhook.site/abc" + url = "https://webhook.site/{var.RANDOM}" password = "password" } } } `, + }, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + generic_webhook { + url = "https://webhook.site/{var.RANDOM}" + username = "username2" + } + } + } + `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { w, err := client.WorkspaceClient() if err != nil { diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index ff4dbe8ae5..cbff8b3125 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -8,10 +8,49 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) +func setState(d *schema.ResourceData) { + d.Set("slack_url", d.Get("config.0.slack.0.url")) + d.Set("microsoft_teams_url", d.Get("config.0.microsoft_teams.0.url")) + d.Set("pagerduty_integration_key", d.Get("config.0.pagerduty.0.integration_key")) + d.Set("generic_webhook_url", d.Get("config.0.generic_webhook.0.url")) + d.Set("generic_webhook_password", d.Get("config.0.generic_webhook.0.password")) + d.Set("generic_webhook_username", d.Get("config.0.generic_webhook.0.username")) +} + type NDStruct struct { + SlackUrl string `json:"slack_url,omitempty"` + MicrosoftTeamsUrl string `json:"microsoft_teams_url,omitempty"` + PagerDutyIntegrationKey string `json:"pagerduty_integration_key,omitempty"` + GenericWebhookUrl string `json:"generic_webhook_url,omitempty"` + GenericWebhookPassword string `json:"generic_webhook_password,omitempty"` + GenericWebhookUsername string `json:"generic_webhook_username,omitempty"` settings.NotificationDestination } +func customDiffSlackUrl(k, old, new string, d *schema.ResourceData) bool { + return d.Get("slack_url") == new +} + +func customDiffMicrosoftTeamsUrl(k, old, new string, d *schema.ResourceData) bool { + return d.Get("microsoft_teams_url") == new +} + +func customDiffPagerdutyIntegrationKey(k, old, new string, d *schema.ResourceData) bool { + return d.Get("pagerduty_integration_key") == new +} + +func customDiffGenericWebhookUrl(k, old, new string, d *schema.ResourceData) bool { + return d.Get("generic_webhook_url") == new +} + +func customDiffGenericWebhookUsername(k, old, new string, d *schema.ResourceData) bool { + return d.Get("generic_webhook_username") == new +} + +func customDiffGenericWebhookPassword(k, old, new string, d *schema.ResourceData) bool { + return d.Get("generic_webhook_password") == new +} + func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema { // Required fields s.SchemaPath("display_name").SetRequired() @@ -26,6 +65,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() + s.SchemaPath("slack_url").SetComputed() + s.SchemaPath("microsoft_teams_url").SetComputed() + s.SchemaPath("pagerduty_integration_key").SetComputed() + s.SchemaPath("generic_webhook_url").SetComputed() + s.SchemaPath("generic_webhook_password").SetComputed() + s.SchemaPath("generic_webhook_username").SetComputed() + // ForceNew fields s.SchemaPath("destination_type").SetForceNew() @@ -44,13 +90,21 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab s.SchemaPath("config", "generic_webhook").SetRequiredWith([]string{"config.0.generic_webhook.0.url"}) s.SchemaPath("config", "email").SetRequiredWith([]string{"config.0.email.0.addresses"}) + // CustomSuppressDiff fields + s.SchemaPath("config", "slack", "url").SetCustomSuppressDiff(customDiffSlackUrl) + s.SchemaPath("config", "microsoft_teams", "url").SetCustomSuppressDiff(customDiffMicrosoftTeamsUrl) + s.SchemaPath("config", "pagerduty", "integration_key").SetCustomSuppressDiff(customDiffPagerdutyIntegrationKey) + s.SchemaPath("config", "generic_webhook", "url").SetCustomSuppressDiff(customDiffGenericWebhookUrl) + s.SchemaPath("config", "generic_webhook", "username").SetCustomSuppressDiff(customDiffGenericWebhookUsername) + s.SchemaPath("config", "generic_webhook", "password").SetCustomSuppressDiff(customDiffGenericWebhookPassword) + // Sensitive fields - // s.SchemaPath("config", "generic_webhook", "password").SetSensitive() - // s.SchemaPath("config", "generic_webhook", "username").SetSensitive() - // s.SchemaPath("config", "generic_webhook", "url").SetSensitive() - // s.SchemaPath("config", "microsoft_teams", "url").SetSensitive() - // s.SchemaPath("config", "pagerduty", "integration_key").SetSensitive() - // s.SchemaPath("config", "slack", "url").SetSensitive() + s.SchemaPath("config", "generic_webhook", "password").SetSensitive() + s.SchemaPath("config", "generic_webhook", "username").SetSensitive() + s.SchemaPath("config", "generic_webhook", "url").SetSensitive() + s.SchemaPath("config", "microsoft_teams", "url").SetSensitive() + s.SchemaPath("config", "pagerduty", "integration_key").SetSensitive() + s.SchemaPath("config", "slack", "url").SetSensitive() return s } @@ -73,6 +127,7 @@ func ResourceNotificationDestination() common.Resource { return err } d.SetId(createdND.Id) + setState(d) return nil }, Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { @@ -80,8 +135,6 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } - var s settings.NotificationDestination - common.DataToStructPointer(d, ndSchema, &s) readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ Id: d.Id(), @@ -90,13 +143,7 @@ func ResourceNotificationDestination() common.Resource { return err } - err = common.StructToData(readND, ndSchema, d) - if err != nil { - return err - } - d.Set("config.0.slack.0.url", s.Config.Slack.Url) - // fmt.Println(d.Get("config.0.slack.0.url")) - return nil + return common.StructToData(readND, ndSchema, d) }, Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { w, err := c.WorkspaceClient() @@ -110,6 +157,7 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } + setState(d) return nil }, Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { From 17b0463934e0aacefd3ab66856c14b19945b27b3 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Mon, 29 Jul 2024 14:06:29 +0200 Subject: [PATCH 09/15] fixed the resource code --- settings/resource_notification_destination.go | 81 +++++-------------- 1 file changed, 22 insertions(+), 59 deletions(-) diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index cbff8b3125..67729e244b 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -8,49 +8,31 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func setState(d *schema.ResourceData) { - d.Set("slack_url", d.Get("config.0.slack.0.url")) - d.Set("microsoft_teams_url", d.Get("config.0.microsoft_teams.0.url")) - d.Set("pagerduty_integration_key", d.Get("config.0.pagerduty.0.integration_key")) - d.Set("generic_webhook_url", d.Get("config.0.generic_webhook.0.url")) - d.Set("generic_webhook_password", d.Get("config.0.generic_webhook.0.password")) - d.Set("generic_webhook_username", d.Get("config.0.generic_webhook.0.username")) +func setStruct(s *settings.NotificationDestination, readND *settings.NotificationDestination) { + switch readND.DestinationType { + case settings.DestinationTypeSlack: + readND.Config.Slack.Url = s.Config.Slack.Url + case settings.DestinationTypePagerduty: + readND.Config.Pagerduty.IntegrationKey = s.Config.Pagerduty.IntegrationKey + case settings.DestinationTypeMicrosoftTeams: + readND.Config.MicrosoftTeams.Url = s.Config.MicrosoftTeams.Url + case settings.DestinationTypeWebhook: + if readND.Config.GenericWebhook.UrlSet { + readND.Config.GenericWebhook.Url = s.Config.GenericWebhook.Url + } + if readND.Config.GenericWebhook.PasswordSet { + readND.Config.GenericWebhook.Password = s.Config.GenericWebhook.Password + } + if readND.Config.GenericWebhook.UsernameSet { + readND.Config.GenericWebhook.Username = s.Config.GenericWebhook.Username + } + } } type NDStruct struct { - SlackUrl string `json:"slack_url,omitempty"` - MicrosoftTeamsUrl string `json:"microsoft_teams_url,omitempty"` - PagerDutyIntegrationKey string `json:"pagerduty_integration_key,omitempty"` - GenericWebhookUrl string `json:"generic_webhook_url,omitempty"` - GenericWebhookPassword string `json:"generic_webhook_password,omitempty"` - GenericWebhookUsername string `json:"generic_webhook_username,omitempty"` settings.NotificationDestination } -func customDiffSlackUrl(k, old, new string, d *schema.ResourceData) bool { - return d.Get("slack_url") == new -} - -func customDiffMicrosoftTeamsUrl(k, old, new string, d *schema.ResourceData) bool { - return d.Get("microsoft_teams_url") == new -} - -func customDiffPagerdutyIntegrationKey(k, old, new string, d *schema.ResourceData) bool { - return d.Get("pagerduty_integration_key") == new -} - -func customDiffGenericWebhookUrl(k, old, new string, d *schema.ResourceData) bool { - return d.Get("generic_webhook_url") == new -} - -func customDiffGenericWebhookUsername(k, old, new string, d *schema.ResourceData) bool { - return d.Get("generic_webhook_username") == new -} - -func customDiffGenericWebhookPassword(k, old, new string, d *schema.ResourceData) bool { - return d.Get("generic_webhook_password") == new -} - func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema { // Required fields s.SchemaPath("display_name").SetRequired() @@ -65,23 +47,12 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab s.SchemaPath("config", "generic_webhook", "password_set").SetComputed() s.SchemaPath("config", "generic_webhook", "username_set").SetComputed() - s.SchemaPath("slack_url").SetComputed() - s.SchemaPath("microsoft_teams_url").SetComputed() - s.SchemaPath("pagerduty_integration_key").SetComputed() - s.SchemaPath("generic_webhook_url").SetComputed() - s.SchemaPath("generic_webhook_password").SetComputed() - s.SchemaPath("generic_webhook_username").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"}) @@ -90,14 +61,6 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab s.SchemaPath("config", "generic_webhook").SetRequiredWith([]string{"config.0.generic_webhook.0.url"}) s.SchemaPath("config", "email").SetRequiredWith([]string{"config.0.email.0.addresses"}) - // CustomSuppressDiff fields - s.SchemaPath("config", "slack", "url").SetCustomSuppressDiff(customDiffSlackUrl) - s.SchemaPath("config", "microsoft_teams", "url").SetCustomSuppressDiff(customDiffMicrosoftTeamsUrl) - s.SchemaPath("config", "pagerduty", "integration_key").SetCustomSuppressDiff(customDiffPagerdutyIntegrationKey) - s.SchemaPath("config", "generic_webhook", "url").SetCustomSuppressDiff(customDiffGenericWebhookUrl) - s.SchemaPath("config", "generic_webhook", "username").SetCustomSuppressDiff(customDiffGenericWebhookUsername) - s.SchemaPath("config", "generic_webhook", "password").SetCustomSuppressDiff(customDiffGenericWebhookPassword) - // Sensitive fields s.SchemaPath("config", "generic_webhook", "password").SetSensitive() s.SchemaPath("config", "generic_webhook", "username").SetSensitive() @@ -127,7 +90,6 @@ func ResourceNotificationDestination() common.Resource { return err } d.SetId(createdND.Id) - setState(d) return nil }, Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { @@ -135,6 +97,8 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } + var tempND settings.NotificationDestination + common.DataToStructPointer(d, ndSchema, &tempND) readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ Id: d.Id(), @@ -142,7 +106,7 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } - + setStruct(&tempND, readND) return common.StructToData(readND, ndSchema, d) }, Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { @@ -157,7 +121,6 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } - setState(d) return nil }, Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { From 726b346b7b8e96363b8676a221543ef8f4a97c74 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Mon, 29 Jul 2024 17:41:53 +0200 Subject: [PATCH 10/15] Added support for changing Destination type --- .../notification_destination_test.go | 59 ++++++++++ settings/resource_notification_destination.go | 107 ++++++++++++------ 2 files changed, 133 insertions(+), 33 deletions(-) diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index 5a9fe3f7cc..d12d6aa6bc 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -243,3 +243,62 @@ func TestAccNDGenericWebhook(t *testing.T) { }), }) } + +func TestAccConfigTypeChange(t *testing.T) { + display_name := "Notification Destination - " + qa.RandomName() + workspaceLevel(t, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + slack { + url = "https://hooks.slack.com/services/{var.RANDOM}" + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("SLACK"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }, step{ + Template: ` + resource "databricks_notification_destination" "this" { + display_name = "` + display_name + `" + config { + microsoft_teams { + url = "https://outlook.office.com/webhook/{var.RANDOM}" + } + } + } + `, + Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, settings.DestinationType("MICROSOFT_TEAMS"), ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil + }), + }) +} diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index 67729e244b..5b8deec342 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -3,6 +3,7 @@ package settings import ( "context" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/settings" "github.com/databricks/terraform-provider-databricks/common" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -29,6 +30,75 @@ func setStruct(s *settings.NotificationDestination, readND *settings.Notificatio } } +func Create(ctx context.Context, d *schema.ResourceData, w *databricks.WorkspaceClient) error { + var newNDrequest settings.CreateNotificationDestinationRequest + common.DataToStructPointer(d, ndSchema, &newNDrequest) + createdND, err := w.NotificationDestinations.Create(ctx, newNDrequest) + if err != nil { + return err + } + d.SetId(createdND.Id) + d.Set("destination_type", createdND.DestinationType) + return nil +} + +func Read(ctx context.Context, d *schema.ResourceData, w *databricks.WorkspaceClient) error { + var tempND settings.NotificationDestination + common.DataToStructPointer(d, ndSchema, &tempND) + + readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: d.Id(), + }) + if err != nil { + return err + } + setStruct(&tempND, readND) + 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() + updatedND, err := w.NotificationDestinations.Update(ctx, updateNDRequest) + if err != nil { + return err + } + d.Set("destination_type", updatedND.DestinationType) + return nil +} + +func Delete(ctx context.Context, d *schema.ResourceData, w *databricks.WorkspaceClient) error { + return w.NotificationDestinations.Delete(ctx, settings.DeleteNotificationDestinationRequest{ + Id: d.Id(), + }) +} + type NDStruct struct { settings.NotificationDestination } @@ -47,9 +117,6 @@ 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("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) @@ -83,54 +150,28 @@ func ResourceNotificationDestination() common.Resource { if err != nil { return err } - var newNDrequest settings.CreateNotificationDestinationRequest - common.DataToStructPointer(d, ndSchema, &newNDrequest) - createdND, err := w.NotificationDestinations.Create(ctx, newNDrequest) - if err != nil { - return err - } - d.SetId(createdND.Id) - return nil + return Create(ctx, d, w) }, Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { w, err := c.WorkspaceClient() if err != nil { return err } - var tempND settings.NotificationDestination - common.DataToStructPointer(d, ndSchema, &tempND) - - readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: d.Id(), - }) - if err != nil { - return err - } - setStruct(&tempND, readND) - return common.StructToData(readND, ndSchema, d) + return Read(ctx, d, w) }, Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { w, err := c.WorkspaceClient() if err != nil { return err } - var updateNDRequest settings.UpdateNotificationDestinationRequest - common.DataToStructPointer(d, ndSchema, &updateNDRequest) - updateNDRequest.Id = d.Id() - _, err = w.NotificationDestinations.Update(ctx, updateNDRequest) - if err != nil { - return err - } - return nil + return Update(ctx, d, w) }, Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { w, err := c.WorkspaceClient() if err != nil { return err } - return w.NotificationDestinations.Delete(ctx, settings.DeleteNotificationDestinationRequest{ - Id: d.Id(), - }) + return Delete(ctx, d, w) }, } } From 80c1d596630e1f582e5cbd23abbcc61b5192bd84 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Wed, 31 Jul 2024 10:51:45 +0200 Subject: [PATCH 11/15] wip --- .../notification_destination_test.go | 137 ++++-------------- settings/resource_notification_destination.go | 46 +++--- 2 files changed, 52 insertions(+), 131 deletions(-) diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index d12d6aa6bc..e097ae088f 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -11,6 +11,23 @@ import ( "github.com/stretchr/testify/require" ) +func check(t *testing.T, ctx context.Context, client *common.DatabricksClient, id, display_name string, config_type settings.DestinationType) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, config_type, ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil +} + func TestAccNDEmail(t *testing.T) { display_name := "Email Notification Destination - " + qa.RandomName() workspaceLevel(t, step{ @@ -36,20 +53,7 @@ func TestAccNDEmail(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("EMAIL"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeEmail) }), }) } @@ -68,20 +72,7 @@ func TestAccNDSlack(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("SLACK"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeSlack) }), }, step{ Template: ` @@ -95,20 +86,7 @@ func TestAccNDSlack(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("SLACK"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeSlack) }), }) } @@ -138,20 +116,7 @@ func TestAccNDMicrosoftTeams(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("MICROSOFT_TEAMS"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeMicrosoftTeams) }), }) } @@ -181,20 +146,7 @@ func TestAccNDPagerduty(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("PAGERDUTY"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypePagerduty) }), }) } @@ -226,20 +178,7 @@ func TestAccNDGenericWebhook(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("WEBHOOK"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeWebhook) }), }) } @@ -258,20 +197,7 @@ func TestAccConfigTypeChange(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("SLACK"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeSlack) }), }, step{ Template: ` @@ -285,20 +211,7 @@ func TestAccConfigTypeChange(t *testing.T) { } `, Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, - }) - if err != nil { - return err - } - assert.Equal(t, settings.DestinationType("MICROSOFT_TEAMS"), ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil + return check(t, ctx, client, id, display_name, settings.DestinationTypeMicrosoftTeams) }), }) } diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index 5b8deec342..2421cd3a6f 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -10,22 +10,32 @@ import ( ) func setStruct(s *settings.NotificationDestination, readND *settings.NotificationDestination) { - switch readND.DestinationType { - case settings.DestinationTypeSlack: - readND.Config.Slack.Url = s.Config.Slack.Url - case settings.DestinationTypePagerduty: - readND.Config.Pagerduty.IntegrationKey = s.Config.Pagerduty.IntegrationKey - case settings.DestinationTypeMicrosoftTeams: - readND.Config.MicrosoftTeams.Url = s.Config.MicrosoftTeams.Url - case settings.DestinationTypeWebhook: - if readND.Config.GenericWebhook.UrlSet { - readND.Config.GenericWebhook.Url = s.Config.GenericWebhook.Url - } - if readND.Config.GenericWebhook.PasswordSet { - readND.Config.GenericWebhook.Password = s.Config.GenericWebhook.Password - } - if readND.Config.GenericWebhook.UsernameSet { - readND.Config.GenericWebhook.Username = s.Config.GenericWebhook.Username + if readND.Config != nil && s.Config != nil { + switch readND.DestinationType { + case settings.DestinationTypeSlack: + if readND.Config.Slack != nil && s.Config.Slack != nil { + readND.Config.Slack.Url = s.Config.Slack.Url + } + case settings.DestinationTypePagerduty: + if readND.Config.Pagerduty != nil && s.Config.Pagerduty != nil { + readND.Config.Pagerduty.IntegrationKey = s.Config.Pagerduty.IntegrationKey + } + case settings.DestinationTypeMicrosoftTeams: + if readND.Config.MicrosoftTeams != nil && s.Config.MicrosoftTeams != nil { + readND.Config.MicrosoftTeams.Url = s.Config.MicrosoftTeams.Url + } + case settings.DestinationTypeWebhook: + if readND.Config.GenericWebhook != nil && s.Config.GenericWebhook != nil { + if readND.Config.GenericWebhook.UrlSet { + readND.Config.GenericWebhook.Url = s.Config.GenericWebhook.Url + } + if readND.Config.GenericWebhook.PasswordSet { + readND.Config.GenericWebhook.Password = s.Config.GenericWebhook.Password + } + if readND.Config.GenericWebhook.UsernameSet { + readND.Config.GenericWebhook.Username = s.Config.GenericWebhook.Username + } + } } } } @@ -38,7 +48,6 @@ func Create(ctx context.Context, d *schema.ResourceData, w *databricks.Workspace return err } d.SetId(createdND.Id) - d.Set("destination_type", createdND.DestinationType) return nil } @@ -85,11 +94,10 @@ func Update(ctx context.Context, d *schema.ResourceData, w *databricks.Workspace var updateNDRequest settings.UpdateNotificationDestinationRequest common.DataToStructPointer(d, ndSchema, &updateNDRequest) updateNDRequest.Id = d.Id() - updatedND, err := w.NotificationDestinations.Update(ctx, updateNDRequest) + _, err := w.NotificationDestinations.Update(ctx, updateNDRequest) if err != nil { return err } - d.Set("destination_type", updatedND.DestinationType) return nil } From adc6f7bf9d2ec674a1a413d60801b9b7bb45700c Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Wed, 31 Jul 2024 12:49:56 +0200 Subject: [PATCH 12/15] modified doc for notification destination --- docs/resources/notification_destination.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/resources/notification_destination.md b/docs/resources/notification_destination.md index ba9c78d4c5..2ce1c1375b 100644 --- a/docs/resources/notification_destination.md +++ b/docs/resources/notification_destination.md @@ -97,3 +97,6 @@ In addition to all arguments above, the following attributes are exported: * `id` - The unique ID of the Notification Destination. + +## Note +* If the type of notification destination is changed, the existing notification destination will be deleted and a new notification destination will be created with the new type. \ No newline at end of file From ea6ceb5f81dec96c5ba5d419fc8cd5581d5e7dd3 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Wed, 31 Jul 2024 13:55:45 +0200 Subject: [PATCH 13/15] added force send fields --- settings/resource_notification_destination.go | 32 +++---------- .../resource_notification_destination_test.go | 48 +++---------------- 2 files changed, 14 insertions(+), 66 deletions(-) diff --git a/settings/resource_notification_destination.go b/settings/resource_notification_destination.go index 2421cd3a6f..40a75ecd14 100644 --- a/settings/resource_notification_destination.go +++ b/settings/resource_notification_destination.go @@ -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() @@ -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) diff --git a/settings/resource_notification_destination_test.go b/settings/resource_notification_destination_test.go index 67e1e86e0f..2e8597b51c 100644 --- a/settings/resource_notification_destination_test.go +++ b/settings/resource_notification_destination_test.go @@ -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) @@ -87,7 +87,7 @@ func TestNDRead(t *testing.T) { display_name = "Notification Destination" config { email { - addresses = ["abc@gmail.com"] + addresses = ["abc@email.com"] } } `, @@ -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{ @@ -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) @@ -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) @@ -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", @@ -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{"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") -// } From ae1c4f1750bc966d8cdbb219961be9c7abe1d8d3 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Wed, 31 Jul 2024 14:05:39 +0200 Subject: [PATCH 14/15] fixed a unit test --- .../resource_notification_destination_test.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/settings/resource_notification_destination_test.go b/settings/resource_notification_destination_test.go index 2e8597b51c..f552b90d0a 100644 --- a/settings/resource_notification_destination_test.go +++ b/settings/resource_notification_destination_test.go @@ -103,7 +103,7 @@ func TestNDUpdate(t *testing.T) { e := w.GetMockNotificationDestinationsAPI().EXPECT() e.Update(mock.Anything, settings.UpdateNotificationDestinationRequest{ Id: "xyz", - DisplayName: "Notification Destination", + DisplayName: "Notification Destination - 2", Config: &settings.Config{ Email: &settings.EmailConfig{ Addresses: []string{"pqr@email.com"}, @@ -111,7 +111,7 @@ func TestNDUpdate(t *testing.T) { }, }).Return(&settings.NotificationDestination{ Id: "xyz", - DisplayName: "Notification Destination", + DisplayName: "Notification Destination - 2", DestinationType: "EMAIL", Config: &settings.Config{ Email: &settings.EmailConfig{ @@ -123,7 +123,7 @@ func TestNDUpdate(t *testing.T) { Id: "xyz", }).Return(&settings.NotificationDestination{ Id: "xyz", - DisplayName: "Notification Destination", + DisplayName: "Notification Destination - 2", DestinationType: "EMAIL", Config: &settings.Config{ Email: &settings.EmailConfig{ @@ -136,7 +136,7 @@ func TestNDUpdate(t *testing.T) { Update: true, ID: "xyz", HCL: ` - display_name = "Notification Destination" + display_name = "Notification Destination - 2" config { email { addresses = ["pqr@email.com"] @@ -144,13 +144,16 @@ func TestNDUpdate(t *testing.T) { } `, InstanceState: map[string]string{ - "id": "xyz", - "display_name": "Notification Destination", - "config": `{"email":{"addresses":["abc@email.com"]}}`, + "id": "xyz", + "display_name": "Notification Destination", + "config.#": "1", + "config.0.email.#": "1", + "config.0.email.0.addresses.#": "1", + "config.0.email.0.addresses.0": "abc@email.com", }, }.ApplyAndExpectData(t, map[string]any{ "id": "xyz", - "display_name": "Notification Destination", + "display_name": "Notification Destination - 2", }) } From cc9cc4d2adf91009812377d3146b66eae54ffde9 Mon Sep 17 00:00:00 2001 From: Divyansh Vijayvergia Date: Thu, 1 Aug 2024 15:49:27 +0200 Subject: [PATCH 15/15] modified docs and integration tests --- docs/resources/notification_destination.md | 5 +- .../notification_destination_test.go | 63 ++++++++----------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/docs/resources/notification_destination.md b/docs/resources/notification_destination.md index 2ce1c1375b..21c1170402 100644 --- a/docs/resources/notification_destination.md +++ b/docs/resources/notification_destination.md @@ -90,13 +90,10 @@ The following arguments are supported: * `username` - (Optional) The username for basic authentication. * `password` - (Optional) The password for basic authentication. +-> **NOTE** If the type of notification destination is changed, the existing notification destination will be deleted and a new notification destination will be created with the new type. ## Attribute Reference In addition to all arguments above, the following attributes are exported: * `id` - The unique ID of the Notification Destination. - - -## Note -* If the type of notification destination is changed, the existing notification destination will be deleted and a new notification destination will be created with the new type. \ No newline at end of file diff --git a/internal/acceptance/notification_destination_test.go b/internal/acceptance/notification_destination_test.go index e097ae088f..c2648a579e 100644 --- a/internal/acceptance/notification_destination_test.go +++ b/internal/acceptance/notification_destination_test.go @@ -7,25 +7,28 @@ import ( "github.com/databricks/databricks-sdk-go/service/settings" "github.com/databricks/terraform-provider-databricks/common" "github.com/databricks/terraform-provider-databricks/qa" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func check(t *testing.T, ctx context.Context, client *common.DatabricksClient, id, display_name string, config_type settings.DestinationType) error { - w, err := client.WorkspaceClient() - if err != nil { - return err - } - ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ - Id: id, +func checkND(t *testing.T, display_name string, config_type settings.DestinationType) resource.TestCheckFunc { + return resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { + w, err := client.WorkspaceClient() + if err != nil { + return err + } + ndResource, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{ + Id: id, + }) + if err != nil { + return err + } + assert.Equal(t, config_type, ndResource.DestinationType) + assert.Equal(t, display_name, ndResource.DisplayName) + require.NoError(t, err) + return nil }) - if err != nil { - return err - } - assert.Equal(t, config_type, ndResource.DestinationType) - assert.Equal(t, display_name, ndResource.DisplayName) - require.NoError(t, err) - return nil } func TestAccNDEmail(t *testing.T) { @@ -52,9 +55,7 @@ func TestAccNDEmail(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeEmail) - }), + Check: checkND(t, display_name, settings.DestinationTypeEmail), }) } @@ -71,9 +72,7 @@ func TestAccNDSlack(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeSlack) - }), + Check: checkND(t, display_name, settings.DestinationTypeSlack), }, step{ Template: ` resource "databricks_notification_destination" "this" { @@ -85,9 +84,7 @@ func TestAccNDSlack(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeSlack) - }), + Check: checkND(t, display_name, settings.DestinationTypeSlack), }) } @@ -115,9 +112,7 @@ func TestAccNDMicrosoftTeams(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeMicrosoftTeams) - }), + Check: checkND(t, display_name, settings.DestinationTypeMicrosoftTeams), }) } @@ -145,9 +140,7 @@ func TestAccNDPagerduty(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypePagerduty) - }), + Check: checkND(t, display_name, settings.DestinationTypePagerduty), }) } @@ -177,9 +170,7 @@ func TestAccNDGenericWebhook(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeWebhook) - }), + Check: checkND(t, display_name, settings.DestinationTypeWebhook), }) } @@ -196,9 +187,7 @@ func TestAccConfigTypeChange(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeSlack) - }), + Check: checkND(t, display_name, settings.DestinationTypeSlack), }, step{ Template: ` resource "databricks_notification_destination" "this" { @@ -210,8 +199,6 @@ func TestAccConfigTypeChange(t *testing.T) { } } `, - Check: resourceCheck("databricks_notification_destination.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { - return check(t, ctx, client, id, display_name, settings.DestinationTypeMicrosoftTeams) - }), + Check: checkND(t, display_name, settings.DestinationTypeMicrosoftTeams), }) }