Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bundle/config/resources/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ type Dashboard struct {
FilePath string `json:"file_path,omitempty"`
}

func (r *Dashboard) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, r)
}

func (r Dashboard) MarshalJSON() ([]byte, error) {
return marshal.Marshal(r)
}

func (*Dashboard) Exists(ctx context.Context, w *databricks.WorkspaceClient, id string) (bool, error) {
_, err := w.Lakeview.Get(ctx, dashboards.GetDashboardRequest{
DashboardId: id,
Expand Down
9 changes: 9 additions & 0 deletions bundle/config/resources/database_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/databricks/cli/libs/workspaceurls"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/marshal"
"github.com/databricks/databricks-sdk-go/service/database"
)

Expand All @@ -16,6 +17,14 @@ type DatabaseCatalog struct {
database.DatabaseCatalog
}

func (d *DatabaseCatalog) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, d)
}

func (d DatabaseCatalog) MarshalJSON() ([]byte, error) {
return marshal.Marshal(d)
}

func (d *DatabaseCatalog) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Database.GetDatabaseCatalog(ctx, database.GetDatabaseCatalogRequest{Name: name})
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions bundle/config/resources/database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/databricks/cli/libs/workspaceurls"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/marshal"
"github.com/databricks/databricks-sdk-go/service/database"
)

Expand All @@ -18,6 +19,14 @@ type DatabaseInstance struct {
Permissions []Permission `json:"permissions,omitempty"`
}

func (d *DatabaseInstance) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, d)
}

func (d DatabaseInstance) MarshalJSON() ([]byte, error) {
return marshal.Marshal(d)
}

func (d *DatabaseInstance) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Database.GetDatabaseInstance(ctx, database.GetDatabaseInstanceRequest{Name: name})
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/genie_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ type GenieSpace struct {
FilePath string `json:"file_path,omitempty"`
}

func (r *GenieSpace) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, r)
}

func (r GenieSpace) MarshalJSON() ([]byte, error) {
return marshal.Marshal(r)
}

func (*GenieSpace) Exists(ctx context.Context, w *databricks.WorkspaceClient, id string) (bool, error) {
_, err := w.Genie.GetSpace(ctx, dashboards.GenieGetSpaceRequest{
SpaceId: id,
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ type PostgresBranch struct {
PostgresBranchConfig
}

func (b *PostgresBranch) UnmarshalJSON(data []byte) error {
return marshal.Unmarshal(data, b)
}

func (b PostgresBranch) MarshalJSON() ([]byte, error) {
return marshal.Marshal(b)
}

func (b *PostgresBranch) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetBranch(ctx, postgres.GetBranchRequest{Name: name})
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ type PostgresCatalog struct {
PostgresCatalogConfig
}

func (c *PostgresCatalog) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, c)
}

func (c PostgresCatalog) MarshalJSON() ([]byte, error) {
return marshal.Marshal(c)
}

func (c *PostgresCatalog) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetCatalog(ctx, postgres.GetCatalogRequest{Name: name})
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ type PostgresDatabase struct {
PostgresDatabaseConfig
}

func (d *PostgresDatabase) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, d)
}

func (d PostgresDatabase) MarshalJSON() ([]byte, error) {
return marshal.Marshal(d)
}

func (d *PostgresDatabase) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetDatabase(ctx, postgres.GetDatabaseRequest{Name: name})
if apierr.IsMissing(err) {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ type PostgresEndpoint struct {
PostgresEndpointConfig
}

func (e *PostgresEndpoint) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, e)
}

func (e PostgresEndpoint) MarshalJSON() ([]byte, error) {
return marshal.Marshal(e)
}

func (e *PostgresEndpoint) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetEndpoint(ctx, postgres.GetEndpointRequest{Name: name})
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ type PostgresProject struct {
Permissions []Permission `json:"permissions,omitempty"`
}

func (p *PostgresProject) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, p)
}

func (p PostgresProject) MarshalJSON() ([]byte, error) {
return marshal.Marshal(p)
}

func (p *PostgresProject) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetProject(ctx, postgres.GetProjectRequest{Name: name})
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ type PostgresRole struct {
PostgresRoleConfig
}

func (r *PostgresRole) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, r)
}

func (r PostgresRole) MarshalJSON() ([]byte, error) {
return marshal.Marshal(r)
}

func (r *PostgresRole) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetRole(ctx, postgres.GetRoleRequest{Name: name})
if apierr.IsMissing(err) {
Expand Down
8 changes: 8 additions & 0 deletions bundle/config/resources/postgres_synced_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ type PostgresSyncedTable struct {
PostgresSyncedTableConfig
}

func (s *PostgresSyncedTable) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, s)
}

func (s PostgresSyncedTable) MarshalJSON() ([]byte, error) {
return marshal.Marshal(s)
}

func (s *PostgresSyncedTable) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Postgres.GetSyncedTable(ctx, postgres.GetSyncedTableRequest{Name: name})
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions bundle/config/resources/synced_database_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/databricks/cli/libs/workspaceurls"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/marshal"
"github.com/databricks/databricks-sdk-go/service/database"
)

Expand All @@ -16,6 +17,14 @@ type SyncedDatabaseTable struct {
database.SyncedDatabaseTable
}

func (s *SyncedDatabaseTable) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, s)
}

func (s SyncedDatabaseTable) MarshalJSON() ([]byte, error) {
return marshal.Marshal(s)
}

func (s *SyncedDatabaseTable) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
_, err := w.Database.GetSyncedDatabaseTable(ctx, database.GetSyncedDatabaseTableRequest{Name: name})
if err != nil {
Expand Down
31 changes: 28 additions & 3 deletions bundle/direct/dresources/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dresources
import (
"encoding/json"
"reflect"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -96,8 +97,11 @@ func TestRoundtripFixtureStateType(t *testing.T) {
// independent of which fields a realistic value would populate. StateType and
// RemoteType are validated as pointer-to-struct by the adapter, so typeOf always
// returns a pointer here.
func testRoundtripAllFields(t *testing.T, label string, typeOf func(*Adapter) reflect.Type) {
func testRoundtripAllFields(t *testing.T, label string, typeOf func(*Adapter) reflect.Type, skip []string) {
for resourceType, resource := range SupportedResources {
if slices.Contains(skip, resourceType) {
continue
}
adapter, err := NewAdapter(resource, resourceType, nil)
require.NoError(t, err)

Expand All @@ -112,15 +116,36 @@ func testRoundtripAllFields(t *testing.T, label string, typeOf func(*Adapter) re
// TestRoundtripAllFieldsStateType verifies StateType survives a JSON round-trip
// with every field populated. StateType is persisted to the state file.
func TestRoundtripAllFieldsStateType(t *testing.T) {
testRoundtripAllFields(t, "StateType", (*Adapter).StateType)
testRoundtripAllFields(t, "StateType", (*Adapter).StateType, nil)
}

// TestRoundtripAllFieldsRemoteType verifies RemoteType survives a JSON round-trip
// with every field populated. RemoteType is emitted in the plan's "remote_state"
// field, so a wrapper embedding an SDK type with its own MarshalJSON must define
// its own or its extra fields vanish.
func TestRoundtripAllFieldsRemoteType(t *testing.T) {
testRoundtripAllFields(t, "RemoteType", (*Adapter).RemoteType)
testRoundtripAllFields(t, "RemoteType", (*Adapter).RemoteType, nil)
}

// TestRoundtripAllFieldsInputConfigType verifies InputConfigType, the typed
// bundle config a resource is loaded into, survives a JSON round-trip with every
// field populated. Bundle config is normally read and written through libs/dyn,
// which walks the struct itself and never calls these marshalers, so this is a
// latent trap rather than live corruption. It is guarded anyway because it is the
// same trap as StateType and RemoteType: a resource that embeds a member with its
// own MarshalJSON and defines none of its own inherits that method by promotion
// and silently drops id, url, lifecycle, modified_status and permissions.
//
// cluster_policies is exempt. Its definition and policy_family_definition_overrides
// are typed `any` and deliberately shadow same-named string fields in the embedded
// compute.CreatePolicy, so a policy document can be authored as inline YAML
// (ConfigureClusterPolicyDefinition normalizes it to the JSON string the API wants
// before deploy). Only the shallower `any` is reachable by that JSON name, but
// marshal.Unmarshal hands the whole payload to the embedded member, so the shadowed
// string comes back holding the raw JSON text and reads as a lost field. Its other
// fields go unchecked on this surface as a result.
func TestRoundtripAllFieldsInputConfigType(t *testing.T) {
testRoundtripAllFields(t, "InputConfigType", (*Adapter).InputConfigType, []string{"cluster_policies"})
}

// fillNonZero recursively populates v with non-zero values so that every
Expand Down
Loading