|
| 1 | +package notificationdestinations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/databricks/databricks-sdk-go/service/settings" |
| 10 | + "github.com/databricks/terraform-provider-databricks/common" |
| 11 | + pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" |
| 12 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters" |
| 13 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema" |
| 14 | + "github.com/databricks/terraform-provider-databricks/internal/service/settings_tf" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 19 | +) |
| 20 | + |
| 21 | +func DataSourceNotificationDestinations() datasource.DataSource { |
| 22 | + return &NotificationDestinationsDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +var _ datasource.DataSourceWithConfigure = &NotificationDestinationsDataSource{} |
| 26 | + |
| 27 | +type NotificationDestinationsDataSource struct { |
| 28 | + Client *common.DatabricksClient |
| 29 | +} |
| 30 | + |
| 31 | +type NotificationDestinationsInfo struct { |
| 32 | + DisplayNameContains types.String `tfsdk:"display_name_contains" tf:"optional"` |
| 33 | + Type types.String `tfsdk:"type" tf:"optional"` |
| 34 | + NotificationDestinations []settings_tf.ListNotificationDestinationsResult `tfsdk:"notification_destinations" tf:"computed"` |
| 35 | +} |
| 36 | + |
| 37 | +func (d *NotificationDestinationsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 38 | + resp.TypeName = "databricks_notification_destinations" |
| 39 | +} |
| 40 | + |
| 41 | +func (d *NotificationDestinationsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 42 | + attrs, blocks := tfschema.DataSourceStructToSchemaMap(NotificationDestinationsInfo{}, nil) |
| 43 | + resp.Schema = schema.Schema{ |
| 44 | + Attributes: attrs, |
| 45 | + Blocks: blocks, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (d *NotificationDestinationsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 50 | + if d.Client == nil { |
| 51 | + d.Client = pluginfwcommon.ConfigureDataSource(req, resp) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func validateType(notificationType string) diag.Diagnostics { |
| 56 | + validTypes := []string{ |
| 57 | + string(settings.DestinationTypeEmail), |
| 58 | + string(settings.DestinationTypeMicrosoftTeams), |
| 59 | + string(settings.DestinationTypePagerduty), |
| 60 | + string(settings.DestinationTypeSlack), |
| 61 | + string(settings.DestinationTypeWebhook), |
| 62 | + } |
| 63 | + |
| 64 | + if !slices.Contains(validTypes, notificationType) { |
| 65 | + return diag.Diagnostics{diag.NewErrorDiagnostic(fmt.Sprintf("Invalid type '%s'; valid types are %s.", notificationType, strings.Join(validTypes, ", ")), "")} |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func AppendDiagAndCheckErrors(resp *datasource.ReadResponse, diags diag.Diagnostics) bool { |
| 71 | + resp.Diagnostics.Append(diags...) |
| 72 | + return resp.Diagnostics.HasError() |
| 73 | +} |
| 74 | + |
| 75 | +func (d *NotificationDestinationsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 76 | + w, diags := d.Client.GetWorkspaceClient() |
| 77 | + if AppendDiagAndCheckErrors(resp, diags) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + var notificationInfo NotificationDestinationsInfo |
| 82 | + if AppendDiagAndCheckErrors(resp, req.Config.Get(ctx, ¬ificationInfo)) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + notificationType := notificationInfo.Type.ValueString() |
| 87 | + notificationDisplayName := strings.ToLower(notificationInfo.DisplayNameContains.ValueString()) |
| 88 | + |
| 89 | + if notificationType != "" && AppendDiagAndCheckErrors(resp, validateType(notificationType)) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + notificationsGoSdk, err := w.NotificationDestinations.ListAll(ctx, settings.ListNotificationDestinationsRequest{}) |
| 94 | + if err != nil { |
| 95 | + resp.Diagnostics.AddError("Failed to fetch Notification Destinations", err.Error()) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + var notificationsTfSdk []settings_tf.ListNotificationDestinationsResult |
| 100 | + for _, notification := range notificationsGoSdk { |
| 101 | + if (notificationType != "" && notification.DestinationType.String() != notificationType) || |
| 102 | + (notificationDisplayName != "" && !strings.Contains(strings.ToLower(notification.DisplayName), notificationDisplayName)) { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + var notificationDestination settings_tf.ListNotificationDestinationsResult |
| 107 | + if AppendDiagAndCheckErrors(resp, converters.GoSdkToTfSdkStruct(ctx, notification, ¬ificationDestination)) { |
| 108 | + return |
| 109 | + } |
| 110 | + notificationsTfSdk = append(notificationsTfSdk, notificationDestination) |
| 111 | + } |
| 112 | + |
| 113 | + notificationInfo.NotificationDestinations = notificationsTfSdk |
| 114 | + resp.Diagnostics.Append(resp.State.Set(ctx, notificationInfo)...) |
| 115 | + |
| 116 | +} |
0 commit comments