forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
databricks#3950: migrated to the plugin framework
- Loading branch information
Showing
7 changed files
with
178 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...l/providers/pluginfw/resources/notificationdestinations/data_notification_destinations.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package notificationdestinations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/databricks/databricks-sdk-go/service/settings" | ||
"github.com/databricks/terraform-provider-databricks/common" | ||
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" | ||
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters" | ||
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema" | ||
"github.com/databricks/terraform-provider-databricks/internal/service/settings_tf" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func DataSourceNotificationDestinations() datasource.DataSource { | ||
return &NotificationDestinationsDataSource{} | ||
} | ||
|
||
var _ datasource.DataSourceWithConfigure = &NotificationDestinationsDataSource{} | ||
|
||
type NotificationDestinationsDataSource struct { | ||
Client *common.DatabricksClient | ||
} | ||
|
||
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" | ||
} | ||
|
||
func (d *NotificationDestinationsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: tfschema.DataSourceStructToSchemaMap(NotificationDestinationsInfo{}, nil), | ||
} | ||
} | ||
|
||
func (d *NotificationDestinationsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if d.Client == nil { | ||
d.Client = pluginfwcommon.ConfigureDataSource(req, resp) | ||
} | ||
} | ||
|
||
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): {}, | ||
} | ||
|
||
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), "")} | ||
} | ||
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 { | ||
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) { | ||
return | ||
} | ||
|
||
var notificationInfo NotificationDestinationsInfo | ||
if appendToResponse(resp, req.Config.Get(ctx, ¬ificationInfo)) { | ||
return | ||
} | ||
|
||
notificationType := notificationInfo.Type.ValueString() | ||
notificationDisplayName := notificationInfo.DisplayNameContains.ValueString() | ||
|
||
if notificationType != "" { | ||
if appendToResponse(resp, validateType(notificationType)) { | ||
return | ||
} | ||
} | ||
|
||
notificationsGoSdk, err := w.NotificationDestinations.ListAll(ctx, settings.ListNotificationDestinationsRequest{}) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Failed to fetch Notification Destinations", err.Error()) | ||
return | ||
} | ||
|
||
var notificationsTfSdk []settings_tf.ListNotificationDestinationsResult | ||
for _, notification := range notificationsGoSdk { | ||
if (notification.DestinationType.String() != notificationType) || (notificationDisplayName != "" && !strings.Contains(notification.DisplayName, notificationDisplayName)) { | ||
continue | ||
} | ||
|
||
var notificationDestination settings_tf.ListNotificationDestinationsResult | ||
if appendToResponse(resp, converters.GoSdkToTfSdkStruct(ctx, notification, ¬ificationDestination)) { | ||
return | ||
} | ||
notificationsTfSdk = append(notificationsTfSdk, notificationDestination) | ||
} | ||
|
||
if appendToResponse(resp, validateLength(notificationsTfSdk)) { | ||
return | ||
} | ||
|
||
notificationInfo.NotificationDestinations = notificationsTfSdk | ||
resp.Diagnostics.Append(resp.State.Set(ctx, notificationInfo)...) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...viders/pluginfw/resources/notificationdestinations/data_notification_destinations_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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" | ||
) | ||
|
||
func TestValidateType_InvalidType(t *testing.T) { | ||
actualDiagnostics := validateType("INVALID") | ||
expectedDiagnostics := diag.Diagnostics{diag.NewErrorDiagnostic("Invalid type 'INVALID'; valid types are EMAIL, MICROSOFT_TEAMS, PAGERDUTY, SLACK, WEBHOOK.", "")} | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.