Rename remote modification check

This commit is contained in:
Pieter Noordhuis 2024-10-24 16:29:52 +02:00
parent 93155f1c77
commit c4df41c3d6
No known key found for this signature in database
GPG Key ID: 12ACCCC104CF2930
3 changed files with 21 additions and 31 deletions

View File

@ -16,7 +16,7 @@ type dashboardState struct {
ETag string ETag string
} }
func collectDashboards(ctx context.Context, b *bundle.Bundle) ([]dashboardState, error) { func collectDashboardsFromState(ctx context.Context, b *bundle.Bundle) ([]dashboardState, error) {
state, err := ParseResourcesState(ctx, b) state, err := ParseResourcesState(ctx, b)
if err != nil && state == nil { if err != nil && state == nil {
return nil, err return nil, err
@ -28,22 +28,12 @@ func collectDashboards(ctx context.Context, b *bundle.Bundle) ([]dashboardState,
continue continue
} }
for _, instance := range resource.Instances { for _, instance := range resource.Instances {
id := instance.Attributes.ID
if id == "" {
continue
}
switch resource.Type { switch resource.Type {
case "databricks_dashboard": case "databricks_dashboard":
etag := instance.Attributes.ETag
if etag == "" {
continue
}
dashboards = append(dashboards, dashboardState{ dashboards = append(dashboards, dashboardState{
Name: resource.Name, Name: resource.Name,
ID: id, ID: instance.Attributes.ID,
ETag: etag, ETag: instance.Attributes.ETag,
}) })
} }
} }
@ -52,14 +42,14 @@ func collectDashboards(ctx context.Context, b *bundle.Bundle) ([]dashboardState,
return dashboards, nil return dashboards, nil
} }
type checkModifiedDashboards struct { type checkDashboardsModifiedRemotely struct {
} }
func (l *checkModifiedDashboards) Name() string { func (l *checkDashboardsModifiedRemotely) Name() string {
return "CheckModifiedDashboards" return "CheckDashboardsModifiedRemotely"
} }
func (l *checkModifiedDashboards) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { func (l *checkDashboardsModifiedRemotely) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
// This mutator is relevant only if the bundle includes dashboards. // This mutator is relevant only if the bundle includes dashboards.
if len(b.Config.Resources.Dashboards) == 0 { if len(b.Config.Resources.Dashboards) == 0 {
return nil return nil
@ -70,7 +60,7 @@ func (l *checkModifiedDashboards) Apply(ctx context.Context, b *bundle.Bundle) d
return nil return nil
} }
dashboards, err := collectDashboards(ctx, b) dashboards, err := collectDashboardsFromState(ctx, b)
if err != nil { if err != nil {
return diag.FromErr(err) return diag.FromErr(err)
} }
@ -122,6 +112,6 @@ func (l *checkModifiedDashboards) Apply(ctx context.Context, b *bundle.Bundle) d
return diags return diags
} }
func CheckModifiedDashboards() *checkModifiedDashboards { func CheckDashboardsModifiedRemotely() *checkDashboardsModifiedRemotely {
return &checkModifiedDashboards{} return &checkDashboardsModifiedRemotely{}
} }

View File

@ -40,7 +40,7 @@ func mockDashboardBundle(t *testing.T) *bundle.Bundle {
return b return b
} }
func TestCheckModifiedDashboards_NoDashboards(t *testing.T) { func TestCheckDashboardsModifiedRemotely_NoDashboards(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
b := &bundle.Bundle{ b := &bundle.Bundle{
BundleRootPath: dir, BundleRootPath: dir,
@ -52,17 +52,17 @@ func TestCheckModifiedDashboards_NoDashboards(t *testing.T) {
}, },
} }
diags := bundle.Apply(context.Background(), b, CheckModifiedDashboards()) diags := bundle.Apply(context.Background(), b, CheckDashboardsModifiedRemotely())
assert.Empty(t, diags) assert.Empty(t, diags)
} }
func TestCheckModifiedDashboards_FirstDeployment(t *testing.T) { func TestCheckDashboardsModifiedRemotely_FirstDeployment(t *testing.T) {
b := mockDashboardBundle(t) b := mockDashboardBundle(t)
diags := bundle.Apply(context.Background(), b, CheckModifiedDashboards()) diags := bundle.Apply(context.Background(), b, CheckDashboardsModifiedRemotely())
assert.Empty(t, diags) assert.Empty(t, diags)
} }
func TestCheckModifiedDashboards_ExistingStateNoChange(t *testing.T) { func TestCheckDashboardsModifiedRemotely_ExistingStateNoChange(t *testing.T) {
ctx := context.Background() ctx := context.Background()
b := mockDashboardBundle(t) b := mockDashboardBundle(t)
@ -81,11 +81,11 @@ func TestCheckModifiedDashboards_ExistingStateNoChange(t *testing.T) {
b.SetWorkpaceClient(m.WorkspaceClient) b.SetWorkpaceClient(m.WorkspaceClient)
// No changes, so no diags. // No changes, so no diags.
diags := bundle.Apply(ctx, b, CheckModifiedDashboards()) diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely())
assert.Empty(t, diags) assert.Empty(t, diags)
} }
func TestCheckModifiedDashboards_ExistingStateChange(t *testing.T) { func TestCheckDashboardsModifiedRemotely_ExistingStateChange(t *testing.T) {
ctx := context.Background() ctx := context.Background()
b := mockDashboardBundle(t) b := mockDashboardBundle(t)
@ -104,14 +104,14 @@ func TestCheckModifiedDashboards_ExistingStateChange(t *testing.T) {
b.SetWorkpaceClient(m.WorkspaceClient) b.SetWorkpaceClient(m.WorkspaceClient)
// The dashboard has changed, so expect an error. // The dashboard has changed, so expect an error.
diags := bundle.Apply(ctx, b, CheckModifiedDashboards()) diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely())
if assert.Len(t, diags, 1) { if assert.Len(t, diags, 1) {
assert.Equal(t, diag.Error, diags[0].Severity) assert.Equal(t, diag.Error, diags[0].Severity)
assert.Equal(t, `dashboard "dash1" has been modified remotely`, diags[0].Summary) assert.Equal(t, `dashboard "dash1" has been modified remotely`, diags[0].Summary)
} }
} }
func TestCheckModifiedDashboards_ExistingStateFailureToGet(t *testing.T) { func TestCheckDashboardsModifiedRemotely_ExistingStateFailureToGet(t *testing.T) {
ctx := context.Background() ctx := context.Background()
b := mockDashboardBundle(t) b := mockDashboardBundle(t)
@ -127,7 +127,7 @@ func TestCheckModifiedDashboards_ExistingStateFailureToGet(t *testing.T) {
b.SetWorkpaceClient(m.WorkspaceClient) b.SetWorkpaceClient(m.WorkspaceClient)
// Unable to get the dashboard, so expect an error. // Unable to get the dashboard, so expect an error.
diags := bundle.Apply(ctx, b, CheckModifiedDashboards()) diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely())
if assert.Len(t, diags, 1) { if assert.Len(t, diags, 1) {
assert.Equal(t, diag.Error, diags[0].Severity) assert.Equal(t, diag.Error, diags[0].Severity)
assert.Equal(t, `failed to get dashboard "dash1"`, diags[0].Summary) assert.Equal(t, `failed to get dashboard "dash1"`, diags[0].Summary)

View File

@ -152,7 +152,7 @@ func Deploy(outputHandler sync.OutputHandler) bundle.Mutator {
bundle.Defer( bundle.Defer(
bundle.Seq( bundle.Seq(
terraform.StatePull(), terraform.StatePull(),
terraform.CheckModifiedDashboards(), terraform.CheckDashboardsModifiedRemotely(),
deploy.StatePull(), deploy.StatePull(),
mutator.ValidateGitDetails(), mutator.ValidateGitDetails(),
artifacts.CleanUp(), artifacts.CleanUp(),