Skip to content

Commit

Permalink
Rename remote modification check
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Oct 24, 2024
1 parent 93155f1 commit c4df41c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type dashboardState struct {
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)
if err != nil && state == nil {
return nil, err
Expand All @@ -28,22 +28,12 @@ func collectDashboards(ctx context.Context, b *bundle.Bundle) ([]dashboardState,
continue
}
for _, instance := range resource.Instances {
id := instance.Attributes.ID
if id == "" {
continue
}

switch resource.Type {
case "databricks_dashboard":
etag := instance.Attributes.ETag
if etag == "" {
continue
}

dashboards = append(dashboards, dashboardState{
Name: resource.Name,
ID: id,
ETag: etag,
ID: instance.Attributes.ID,
ETag: instance.Attributes.ETag,
})
}
}
Expand All @@ -52,14 +42,14 @@ func collectDashboards(ctx context.Context, b *bundle.Bundle) ([]dashboardState,
return dashboards, nil
}

type checkModifiedDashboards struct {
type checkDashboardsModifiedRemotely struct {
}

func (l *checkModifiedDashboards) Name() string {
return "CheckModifiedDashboards"
func (l *checkDashboardsModifiedRemotely) Name() string {
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.
if len(b.Config.Resources.Dashboards) == 0 {
return nil
Expand All @@ -70,7 +60,7 @@ func (l *checkModifiedDashboards) Apply(ctx context.Context, b *bundle.Bundle) d
return nil
}

dashboards, err := collectDashboards(ctx, b)
dashboards, err := collectDashboardsFromState(ctx, b)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -122,6 +112,6 @@ func (l *checkModifiedDashboards) Apply(ctx context.Context, b *bundle.Bundle) d
return diags
}

func CheckModifiedDashboards() *checkModifiedDashboards {
return &checkModifiedDashboards{}
func CheckDashboardsModifiedRemotely() *checkDashboardsModifiedRemotely {
return &checkDashboardsModifiedRemotely{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func mockDashboardBundle(t *testing.T) *bundle.Bundle {
return b
}

func TestCheckModifiedDashboards_NoDashboards(t *testing.T) {
func TestCheckDashboardsModifiedRemotely_NoDashboards(t *testing.T) {
dir := t.TempDir()
b := &bundle.Bundle{
BundleRootPath: dir,
Expand All @@ -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)
}

func TestCheckModifiedDashboards_FirstDeployment(t *testing.T) {
func TestCheckDashboardsModifiedRemotely_FirstDeployment(t *testing.T) {
b := mockDashboardBundle(t)
diags := bundle.Apply(context.Background(), b, CheckModifiedDashboards())
diags := bundle.Apply(context.Background(), b, CheckDashboardsModifiedRemotely())
assert.Empty(t, diags)
}

func TestCheckModifiedDashboards_ExistingStateNoChange(t *testing.T) {
func TestCheckDashboardsModifiedRemotely_ExistingStateNoChange(t *testing.T) {
ctx := context.Background()

b := mockDashboardBundle(t)
Expand All @@ -81,11 +81,11 @@ func TestCheckModifiedDashboards_ExistingStateNoChange(t *testing.T) {
b.SetWorkpaceClient(m.WorkspaceClient)

// No changes, so no diags.
diags := bundle.Apply(ctx, b, CheckModifiedDashboards())
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely())
assert.Empty(t, diags)
}

func TestCheckModifiedDashboards_ExistingStateChange(t *testing.T) {
func TestCheckDashboardsModifiedRemotely_ExistingStateChange(t *testing.T) {
ctx := context.Background()

b := mockDashboardBundle(t)
Expand All @@ -104,14 +104,14 @@ func TestCheckModifiedDashboards_ExistingStateChange(t *testing.T) {
b.SetWorkpaceClient(m.WorkspaceClient)

// 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) {
assert.Equal(t, diag.Error, diags[0].Severity)
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()

b := mockDashboardBundle(t)
Expand All @@ -127,7 +127,7 @@ func TestCheckModifiedDashboards_ExistingStateFailureToGet(t *testing.T) {
b.SetWorkpaceClient(m.WorkspaceClient)

// 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) {
assert.Equal(t, diag.Error, diags[0].Severity)
assert.Equal(t, `failed to get dashboard "dash1"`, diags[0].Summary)
Expand Down
2 changes: 1 addition & 1 deletion bundle/phases/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func Deploy(outputHandler sync.OutputHandler) bundle.Mutator {
bundle.Defer(
bundle.Seq(
terraform.StatePull(),
terraform.CheckModifiedDashboards(),
terraform.CheckDashboardsModifiedRemotely(),
deploy.StatePull(),
mutator.ValidateGitDetails(),
artifacts.CleanUp(),
Expand Down

0 comments on commit c4df41c

Please sign in to comment.