Skip to content

Commit

Permalink
databricks#3950: made requested changes, data source is now case-inse…
Browse files Browse the repository at this point in the history
…nsitive, and returns a list if no matches are found.
  • Loading branch information
dgomez04 committed Oct 15, 2024
1 parent 4164c4b commit c493663
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
7 changes: 5 additions & 2 deletions docs/data-sources/notification_destinations.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
subcategory: "Workspace"
---
# databricks_notification_destination Data Source
# databricks_notification_destinations Data Source

-> **NOTE** Ensure that the notification destinations you are attempting to retrieve have been created and are accessible within your workspace.

Expand Down Expand Up @@ -43,7 +43,7 @@ data "databricks_notification_destinations" "filtered_notification" {

The following arguments are supported:

* `display_name_contains` - (Optional) A substring to filter Notification Destinations by their display name.
* `display_name_contains` - (Optional) A **case-insensitive** substring to filter Notification Destinations by their display name.
* `type` - (Optional) The type of the Notification Destination to filter by. Valid values are:
* `EMAIL` - Filters Notification Destinations of type Email.
* `MICROSOFT_TEAMS` - Filters Notification Destinations of type Microsoft Teams.
Expand All @@ -54,7 +54,10 @@ The following arguments are supported:
## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `notification_destinations` - A list of Notification Destinations matching the specified criteria. Each element contains the following attributes:
* `id` - The unique ID of the Notification Destination.
* `display_name` - The display name of the Notification Destination.
* `destination_type` - The type of the notification destination. Possible values are `EMAIL`, `MICROSOFT_TEAMS`, `PAGERDUTY`, `SLACK`, or `WEBHOOK`.

If no matches are found, an empty list will be returned.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package notificationdestinations
import (
"context"
"fmt"
"slices"
"strings"

"github.com/databricks/databricks-sdk-go/service/settings"
Expand All @@ -28,19 +29,20 @@ type NotificationDestinationsDataSource struct {
}

type NotificationDestinationsInfo struct {
Id types.String `tfsdk:"id" tf:"computed"`
DisplayNameContains types.String `tfsdk:"display_name_contains" tf:"optional"`
Type types.String `tfsdk:"type" tf:"optional"`
NotificationDestinations []settings_tf.ListNotificationDestinationsResult `tfsdk:"notification_destinations" tf:"computed"`
}

func (d *NotificationDestinationsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "databricks_notification_destinations_pluginframework"
resp.TypeName = "databricks_notification_destinations"
}

func (d *NotificationDestinationsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
attrs, blocks := tfschema.DataSourceStructToSchemaMap(NotificationDestinationsInfo{}, nil)
resp.Schema = schema.Schema{
Attributes: tfschema.DataSourceStructToSchemaMap(NotificationDestinationsInfo{}, nil),
Attributes: attrs,
Blocks: blocks,
}
}

Expand All @@ -51,50 +53,41 @@ func (d *NotificationDestinationsDataSource) Configure(_ context.Context, req da
}

func validateType(notificationType string) diag.Diagnostics {
validTypes := map[string]struct{}{
string(settings.DestinationTypeEmail): {},
string(settings.DestinationTypeMicrosoftTeams): {},
string(settings.DestinationTypePagerduty): {},
string(settings.DestinationTypeSlack): {},
string(settings.DestinationTypeWebhook): {},
validTypes := []string{
string(settings.DestinationTypeEmail),
string(settings.DestinationTypeMicrosoftTeams),
string(settings.DestinationTypePagerduty),
string(settings.DestinationTypeSlack),
string(settings.DestinationTypeWebhook),
}

if _, valid := validTypes[notificationType]; !valid {
return diag.Diagnostics{diag.NewErrorDiagnostic(fmt.Sprintf("Invalid type '%s'; valid types are EMAIL, MICROSOFT_TEAMS, PAGERDUTY, SLACK, WEBHOOK.", notificationType), "")}
if !slices.Contains(validTypes, notificationType) {
return diag.Diagnostics{diag.NewErrorDiagnostic(fmt.Sprintf("Invalid type '%s'; valid types are %s.", notificationType, strings.Join(validTypes, ", ")), "")}
}
return nil
}

func validateLength(destinations []settings_tf.ListNotificationDestinationsResult) diag.Diagnostics {
if len(destinations) == 0 {
return diag.Diagnostics{diag.NewErrorDiagnostic("Could not find any notification destinations with the specified criteria.", "")}
}
return nil
}

func appendToResponse(resp *datasource.ReadResponse, diags diag.Diagnostics) bool {
func AppendDiagAndCheckErrors(resp *datasource.ReadResponse, diags diag.Diagnostics) bool {
resp.Diagnostics.Append(diags...)
return resp.Diagnostics.HasError()
}

func (d *NotificationDestinationsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
w, diags := d.Client.GetWorkspaceClient()
if appendToResponse(resp, diags) {
if AppendDiagAndCheckErrors(resp, diags) {
return
}

var notificationInfo NotificationDestinationsInfo
if appendToResponse(resp, req.Config.Get(ctx, &notificationInfo)) {
if AppendDiagAndCheckErrors(resp, req.Config.Get(ctx, &notificationInfo)) {
return
}

notificationType := notificationInfo.Type.ValueString()
notificationDisplayName := notificationInfo.DisplayNameContains.ValueString()
notificationDisplayName := strings.ToLower(notificationInfo.DisplayNameContains.ValueString())

if notificationType != "" {
if appendToResponse(resp, validateType(notificationType)) {
return
}
if notificationType != "" && AppendDiagAndCheckErrors(resp, validateType(notificationType)) {
return
}

notificationsGoSdk, err := w.NotificationDestinations.ListAll(ctx, settings.ListNotificationDestinationsRequest{})
Expand All @@ -105,21 +98,17 @@ func (d *NotificationDestinationsDataSource) Read(ctx context.Context, req datas

var notificationsTfSdk []settings_tf.ListNotificationDestinationsResult
for _, notification := range notificationsGoSdk {
if (notification.DestinationType.String() != notificationType) || (notificationDisplayName != "" && !strings.Contains(notification.DisplayName, notificationDisplayName)) {
if (notification.DestinationType.String() != notificationType) || (notificationDisplayName != "" && !strings.Contains(strings.ToLower(notification.DisplayName), notificationDisplayName)) {
continue
}

var notificationDestination settings_tf.ListNotificationDestinationsResult
if appendToResponse(resp, converters.GoSdkToTfSdkStruct(ctx, notification, &notificationDestination)) {
if AppendDiagAndCheckErrors(resp, converters.GoSdkToTfSdkStruct(ctx, notification, &notificationDestination)) {
return
}
notificationsTfSdk = append(notificationsTfSdk, notificationDestination)
}

if appendToResponse(resp, validateLength(notificationsTfSdk)) {
return
}

notificationInfo.NotificationDestinations = notificationsTfSdk
resp.Diagnostics.Append(resp.State.Set(ctx, notificationInfo)...)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package notificationdestinations
import (
"testing"

"github.com/databricks/terraform-provider-databricks/internal/service/settings_tf"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/stretchr/testify/assert"
)
Expand All @@ -14,10 +13,3 @@ func TestValidateType_InvalidType(t *testing.T) {
assert.True(t, actualDiagnostics.HasError())
assert.Equal(t, expectedDiagnostics, actualDiagnostics)
}

func TestValidateLength(t *testing.T) {
actualDiagnostics := validateLength([]settings_tf.ListNotificationDestinationsResult{})
expectedDiagnostics := diag.Diagnostics{diag.NewErrorDiagnostic("Could not find any notification destinations with the specified criteria.", "")}
assert.True(t, actualDiagnostics.HasError())
assert.Equal(t, expectedDiagnostics, actualDiagnostics)
}

0 comments on commit c493663

Please sign in to comment.