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
120 changes: 40 additions & 80 deletions acceptance/bundle/refschema/out.fields.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2695,29 +2695,21 @@ resources.pipelines.*.permissions[*].group_name string ALL
resources.pipelines.*.permissions[*].level iam.PermissionLevel ALL
resources.pipelines.*.permissions[*].service_principal_name string ALL
resources.pipelines.*.permissions[*].user_name string ALL
resources.postgres_branches.*.branch_id string INPUT STATE
resources.postgres_branches.*.branch_id string ALL
resources.postgres_branches.*.create_time *time.Time REMOTE
resources.postgres_branches.*.expire_time *time.Time INPUT STATE
resources.postgres_branches.*.expire_time *time.Time ALL
resources.postgres_branches.*.id string INPUT
resources.postgres_branches.*.is_protected bool INPUT STATE
resources.postgres_branches.*.is_protected bool ALL
resources.postgres_branches.*.lifecycle resources.Lifecycle INPUT
resources.postgres_branches.*.lifecycle.prevent_destroy bool INPUT
resources.postgres_branches.*.modified_status string INPUT
resources.postgres_branches.*.name string REMOTE
resources.postgres_branches.*.no_expiry bool INPUT STATE
resources.postgres_branches.*.no_expiry bool ALL
resources.postgres_branches.*.parent string ALL
resources.postgres_branches.*.replace_existing bool INPUT STATE
resources.postgres_branches.*.source_branch string INPUT STATE
resources.postgres_branches.*.source_branch_lsn string INPUT STATE
resources.postgres_branches.*.source_branch_time *time.Time INPUT STATE
resources.postgres_branches.*.spec *postgres.BranchSpec REMOTE
resources.postgres_branches.*.spec.expire_time *time.Time REMOTE
resources.postgres_branches.*.spec.is_protected bool REMOTE
resources.postgres_branches.*.spec.no_expiry bool REMOTE
resources.postgres_branches.*.spec.source_branch string REMOTE
resources.postgres_branches.*.spec.source_branch_lsn string REMOTE
resources.postgres_branches.*.spec.source_branch_time *time.Time REMOTE
resources.postgres_branches.*.spec.ttl *duration.Duration REMOTE
resources.postgres_branches.*.source_branch string ALL
resources.postgres_branches.*.source_branch_lsn string ALL
resources.postgres_branches.*.source_branch_time *time.Time ALL
resources.postgres_branches.*.status *postgres.BranchStatus REMOTE
resources.postgres_branches.*.status.branch_id string REMOTE
resources.postgres_branches.*.status.current_state postgres.BranchStatusState REMOTE
Expand All @@ -2730,45 +2722,31 @@ resources.postgres_branches.*.status.source_branch string REMOTE
resources.postgres_branches.*.status.source_branch_lsn string REMOTE
resources.postgres_branches.*.status.source_branch_time *time.Time REMOTE
resources.postgres_branches.*.status.state_change_time *time.Time REMOTE
resources.postgres_branches.*.ttl *duration.Duration INPUT STATE
resources.postgres_branches.*.ttl *duration.Duration ALL
resources.postgres_branches.*.uid string REMOTE
resources.postgres_branches.*.update_time *time.Time REMOTE
resources.postgres_branches.*.url string INPUT
resources.postgres_endpoints.*.autoscaling_limit_max_cu float64 INPUT STATE
resources.postgres_endpoints.*.autoscaling_limit_min_cu float64 INPUT STATE
resources.postgres_endpoints.*.autoscaling_limit_max_cu float64 ALL
resources.postgres_endpoints.*.autoscaling_limit_min_cu float64 ALL
resources.postgres_endpoints.*.create_time *time.Time REMOTE
resources.postgres_endpoints.*.disabled bool INPUT STATE
resources.postgres_endpoints.*.endpoint_id string INPUT STATE
resources.postgres_endpoints.*.endpoint_type postgres.EndpointType INPUT STATE
resources.postgres_endpoints.*.group *postgres.EndpointGroupSpec INPUT STATE
resources.postgres_endpoints.*.group.enable_readable_secondaries bool INPUT STATE
resources.postgres_endpoints.*.group.max int INPUT STATE
resources.postgres_endpoints.*.group.min int INPUT STATE
resources.postgres_endpoints.*.disabled bool ALL
resources.postgres_endpoints.*.endpoint_id string ALL
resources.postgres_endpoints.*.endpoint_type postgres.EndpointType ALL
resources.postgres_endpoints.*.group *postgres.EndpointGroupSpec ALL
resources.postgres_endpoints.*.group.enable_readable_secondaries bool ALL
resources.postgres_endpoints.*.group.max int ALL
resources.postgres_endpoints.*.group.min int ALL
resources.postgres_endpoints.*.id string INPUT
resources.postgres_endpoints.*.lifecycle resources.Lifecycle INPUT
resources.postgres_endpoints.*.lifecycle.prevent_destroy bool INPUT
resources.postgres_endpoints.*.modified_status string INPUT
resources.postgres_endpoints.*.name string REMOTE
resources.postgres_endpoints.*.no_suspension bool INPUT STATE
resources.postgres_endpoints.*.no_suspension bool ALL
resources.postgres_endpoints.*.parent string ALL
resources.postgres_endpoints.*.replace_existing bool INPUT STATE
resources.postgres_endpoints.*.settings *postgres.EndpointSettings INPUT STATE
resources.postgres_endpoints.*.settings.pg_settings map[string]string INPUT STATE
resources.postgres_endpoints.*.settings.pg_settings.* string INPUT STATE
resources.postgres_endpoints.*.spec *postgres.EndpointSpec REMOTE
resources.postgres_endpoints.*.spec.autoscaling_limit_max_cu float64 REMOTE
resources.postgres_endpoints.*.spec.autoscaling_limit_min_cu float64 REMOTE
resources.postgres_endpoints.*.spec.disabled bool REMOTE
resources.postgres_endpoints.*.spec.endpoint_type postgres.EndpointType REMOTE
resources.postgres_endpoints.*.spec.group *postgres.EndpointGroupSpec REMOTE
resources.postgres_endpoints.*.spec.group.enable_readable_secondaries bool REMOTE
resources.postgres_endpoints.*.spec.group.max int REMOTE
resources.postgres_endpoints.*.spec.group.min int REMOTE
resources.postgres_endpoints.*.spec.no_suspension bool REMOTE
resources.postgres_endpoints.*.spec.settings *postgres.EndpointSettings REMOTE
resources.postgres_endpoints.*.spec.settings.pg_settings map[string]string REMOTE
resources.postgres_endpoints.*.spec.settings.pg_settings.* string REMOTE
resources.postgres_endpoints.*.spec.suspend_timeout_duration *duration.Duration REMOTE
resources.postgres_endpoints.*.settings *postgres.EndpointSettings ALL
resources.postgres_endpoints.*.settings.pg_settings map[string]string ALL
resources.postgres_endpoints.*.settings.pg_settings.* string ALL
resources.postgres_endpoints.*.status *postgres.EndpointStatus REMOTE
resources.postgres_endpoints.*.status.autoscaling_limit_max_cu float64 REMOTE
resources.postgres_endpoints.*.status.autoscaling_limit_min_cu float64 REMOTE
Expand All @@ -2788,28 +2766,28 @@ resources.postgres_endpoints.*.status.settings *postgres.EndpointSettings REMOTE
resources.postgres_endpoints.*.status.settings.pg_settings map[string]string REMOTE
resources.postgres_endpoints.*.status.settings.pg_settings.* string REMOTE
resources.postgres_endpoints.*.status.suspend_timeout_duration *duration.Duration REMOTE
resources.postgres_endpoints.*.suspend_timeout_duration *duration.Duration INPUT STATE
resources.postgres_endpoints.*.suspend_timeout_duration *duration.Duration ALL
resources.postgres_endpoints.*.uid string REMOTE
resources.postgres_endpoints.*.update_time *time.Time REMOTE
resources.postgres_endpoints.*.url string INPUT
resources.postgres_projects.*.budget_policy_id string INPUT STATE
resources.postgres_projects.*.budget_policy_id string ALL
resources.postgres_projects.*.create_time *time.Time REMOTE
resources.postgres_projects.*.custom_tags []postgres.ProjectCustomTag INPUT STATE
resources.postgres_projects.*.custom_tags[*] postgres.ProjectCustomTag INPUT STATE
resources.postgres_projects.*.custom_tags[*].key string INPUT STATE
resources.postgres_projects.*.custom_tags[*].value string INPUT STATE
resources.postgres_projects.*.default_branch string INPUT STATE
resources.postgres_projects.*.default_endpoint_settings *postgres.ProjectDefaultEndpointSettings INPUT STATE
resources.postgres_projects.*.default_endpoint_settings.autoscaling_limit_max_cu float64 INPUT STATE
resources.postgres_projects.*.default_endpoint_settings.autoscaling_limit_min_cu float64 INPUT STATE
resources.postgres_projects.*.default_endpoint_settings.no_suspension bool INPUT STATE
resources.postgres_projects.*.default_endpoint_settings.pg_settings map[string]string INPUT STATE
resources.postgres_projects.*.default_endpoint_settings.pg_settings.* string INPUT STATE
resources.postgres_projects.*.default_endpoint_settings.suspend_timeout_duration *duration.Duration INPUT STATE
resources.postgres_projects.*.custom_tags []postgres.ProjectCustomTag ALL
resources.postgres_projects.*.custom_tags[*] postgres.ProjectCustomTag ALL
resources.postgres_projects.*.custom_tags[*].key string ALL
resources.postgres_projects.*.custom_tags[*].value string ALL
resources.postgres_projects.*.default_branch string ALL
resources.postgres_projects.*.default_endpoint_settings *postgres.ProjectDefaultEndpointSettings ALL
resources.postgres_projects.*.default_endpoint_settings.autoscaling_limit_max_cu float64 ALL
resources.postgres_projects.*.default_endpoint_settings.autoscaling_limit_min_cu float64 ALL
resources.postgres_projects.*.default_endpoint_settings.no_suspension bool ALL
resources.postgres_projects.*.default_endpoint_settings.pg_settings map[string]string ALL
resources.postgres_projects.*.default_endpoint_settings.pg_settings.* string ALL
resources.postgres_projects.*.default_endpoint_settings.suspend_timeout_duration *duration.Duration ALL
resources.postgres_projects.*.delete_time *time.Time REMOTE
resources.postgres_projects.*.display_name string INPUT STATE
resources.postgres_projects.*.enable_pg_native_login bool INPUT STATE
resources.postgres_projects.*.history_retention_duration *duration.Duration INPUT STATE
resources.postgres_projects.*.display_name string ALL
resources.postgres_projects.*.enable_pg_native_login bool ALL
resources.postgres_projects.*.history_retention_duration *duration.Duration ALL
resources.postgres_projects.*.id string INPUT
resources.postgres_projects.*.initial_endpoint_spec *postgres.InitialEndpointSpec REMOTE
resources.postgres_projects.*.initial_endpoint_spec.group *postgres.EndpointGroupSpec REMOTE
Expand All @@ -2820,27 +2798,9 @@ resources.postgres_projects.*.lifecycle resources.Lifecycle INPUT
resources.postgres_projects.*.lifecycle.prevent_destroy bool INPUT
resources.postgres_projects.*.modified_status string INPUT
resources.postgres_projects.*.name string REMOTE
resources.postgres_projects.*.pg_version int INPUT STATE
resources.postgres_projects.*.project_id string INPUT STATE
resources.postgres_projects.*.pg_version int ALL
resources.postgres_projects.*.project_id string ALL
resources.postgres_projects.*.purge_time *time.Time REMOTE
resources.postgres_projects.*.spec *postgres.ProjectSpec REMOTE
resources.postgres_projects.*.spec.budget_policy_id string REMOTE
resources.postgres_projects.*.spec.custom_tags []postgres.ProjectCustomTag REMOTE
resources.postgres_projects.*.spec.custom_tags[*] postgres.ProjectCustomTag REMOTE
resources.postgres_projects.*.spec.custom_tags[*].key string REMOTE
resources.postgres_projects.*.spec.custom_tags[*].value string REMOTE
resources.postgres_projects.*.spec.default_branch string REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings *postgres.ProjectDefaultEndpointSettings REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings.autoscaling_limit_max_cu float64 REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings.autoscaling_limit_min_cu float64 REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings.no_suspension bool REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings.pg_settings map[string]string REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings.pg_settings.* string REMOTE
resources.postgres_projects.*.spec.default_endpoint_settings.suspend_timeout_duration *duration.Duration REMOTE
resources.postgres_projects.*.spec.display_name string REMOTE
resources.postgres_projects.*.spec.enable_pg_native_login bool REMOTE
resources.postgres_projects.*.spec.history_retention_duration *duration.Duration REMOTE
resources.postgres_projects.*.spec.pg_version int REMOTE
resources.postgres_projects.*.status *postgres.ProjectStatus REMOTE
resources.postgres_projects.*.status.branch_logical_size_limit_bytes int64 REMOTE
resources.postgres_projects.*.status.budget_policy_id string REMOTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"action": "skip",
"remote_state": {
"branch_id": "dev-branch",
"create_time": "[TIMESTAMP]",
"name": "[DEV_BRANCH_ID]",
"parent": "projects/test-pg-proj-[UNIQUE_NAME]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
},
"remote_state": {
"branch_id": "dev-branch",
"create_time": "[TIMESTAMP]",
"name": "[DEV_BRANCH_ID]",
"parent": "projects/test-pg-proj-[UNIQUE_NAME]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
},
"remote_state": {
"branch_id": "dev-branch",
"create_time": "[TIMESTAMP]",
"name": "[DEV_BRANCH_ID]",
"parent": "projects/test-pg-proj-[UNIQUE_NAME]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"action": "skip",
"remote_state": {
"create_time": "[TIMESTAMP]",
"endpoint_id": "my-endpoint",
"endpoint_type": "",
"name": "[MY_ENDPOINT_ID]",
"parent": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main",
"status": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
},
"remote_state": {
"create_time": "[TIMESTAMP]",
"endpoint_id": "my-endpoint",
"endpoint_type": "",
"name": "[MY_ENDPOINT_ID]",
"parent": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main",
"status": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
},
"remote_state": {
"create_time": "[TIMESTAMP]",
"endpoint_id": "my-endpoint",
"endpoint_type": "",
"name": "[MY_ENDPOINT_ID]",
"parent": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main",
"status": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"remote_state": {
"create_time": "[TIMESTAMP]",
"name": "[MY_PROJECT_ID]",
"project_id": "test-pg-proj-[UNIQUE_NAME]",
"status": {
"branch_logical_size_limit_bytes": [NUMID],
"default_branch": "[MY_PROJECT_ID]/branches/production",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"remote_state": {
"create_time": "[TIMESTAMP]",
"name": "[MY_PROJECT_ID]",
"project_id": "test-pg-proj-[UNIQUE_NAME]",
"status": {
"branch_logical_size_limit_bytes": [NUMID],
"default_branch": "[MY_PROJECT_ID]/branches/production",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"remote_state": {
"create_time": "[TIMESTAMP]",
"name": "[MY_PROJECT_ID]",
"project_id": "test-pg-proj-[UNIQUE_NAME]",
"status": {
"branch_logical_size_limit_bytes": [NUMID],
"default_branch": "[MY_PROJECT_ID]/branches/production",
Expand Down
94 changes: 69 additions & 25 deletions bundle/direct/dresources/postgres_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,37 @@ import (
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/common/types/fieldmask"
sdktime "github.com/databricks/databricks-sdk-go/common/types/time"
"github.com/databricks/databricks-sdk-go/marshal"
"github.com/databricks/databricks-sdk-go/service/postgres"
)

// PostgresBranchRemote is the return type for DoRead. It embeds BranchSpec so that
// all paths in StateType are valid paths in RemoteType, enabling drift detection
// for spec fields once the backend echoes spec on GET.
type PostgresBranchRemote struct {
postgres.BranchSpec

BranchId string `json:"branch_id,omitempty"`
Parent string `json:"parent,omitempty"`

Name string `json:"name,omitempty"`
Status *postgres.BranchStatus `json:"status,omitempty"`
Uid string `json:"uid,omitempty"`
CreateTime *sdktime.Time `json:"create_time,omitempty"`
UpdateTime *sdktime.Time `json:"update_time,omitempty"`
}

// Custom marshaler needed because embedded BranchSpec has its own MarshalJSON
// which would otherwise take over and ignore the additional fields.
func (s *PostgresBranchRemote) UnmarshalJSON(b []byte) error {
return marshal.Unmarshal(b, s)
}

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

type ResourcePostgresBranch struct {
client *databricks.WorkspaceClient
}
Expand All @@ -28,40 +56,52 @@ func (*ResourcePostgresBranch) PrepareState(input *resources.PostgresBranch) *Po
}
}

func (*ResourcePostgresBranch) RemapState(remote *postgres.Branch) *PostgresBranchState {
// Extract branch_id from hierarchical name: "projects/{project_id}/branches/{branch_id}"
// TODO: log error when we have access to the context
components, _ := ParsePostgresName(remote.Name)

func (*ResourcePostgresBranch) RemapState(remote *PostgresBranchRemote) *PostgresBranchState {
return &PostgresBranchState{
BranchId: components.BranchID,
BranchId: remote.BranchId,
Parent: remote.Parent,

// replace_existing is a create-time-only flag; the GET API never returns
// it, so RemapState leaves it false.
ReplaceExisting: false,

// The read API does not return the spec, only the status.
// This means we cannot detect remote drift for spec fields.
// Use an empty struct (not nil) so field-level diffing works correctly.
BranchSpec: postgres.BranchSpec{
ExpireTime: nil,
IsProtected: false,
NoExpiry: false,
SourceBranch: "",
SourceBranchLsn: "",
SourceBranchTime: nil,
Ttl: nil,
ForceSendFields: nil,
},
BranchSpec: remote.BranchSpec,
}
}

func (r *ResourcePostgresBranch) DoRead(ctx context.Context, id string) (*postgres.Branch, error) {
return r.client.Postgres.GetBranch(ctx, postgres.GetBranchRequest{Name: id})
// makePostgresBranchRemote converts the SDK Branch into the embedded remote shape.
// GET does not echo spec today (only status is returned); the embedded spec fields
// stay at their zero values, and resources.yml suppresses phantom drift via
// ignore_remote_changes with reason spec:input_only.
func makePostgresBranchRemote(branch *postgres.Branch) *PostgresBranchRemote {
// Extract branch_id from hierarchical name: "projects/{project_id}/branches/{branch_id}"
// TODO: log error when we have access to the context
components, _ := ParsePostgresName(branch.Name)
var spec postgres.BranchSpec
if branch.Spec != nil {
spec = *branch.Spec
}
return &PostgresBranchRemote{
BranchSpec: spec,
BranchId: components.BranchID,
Parent: branch.Parent,
Name: branch.Name,
Status: branch.Status,
Uid: branch.Uid,
CreateTime: branch.CreateTime,
UpdateTime: branch.UpdateTime,
}
}

func (r *ResourcePostgresBranch) DoRead(ctx context.Context, id string) (*PostgresBranchRemote, error) {
branch, err := r.client.Postgres.GetBranch(ctx, postgres.GetBranchRequest{Name: id})
if err != nil {
return nil, err
}
return makePostgresBranchRemote(branch), nil
}

func (r *ResourcePostgresBranch) DoCreate(ctx context.Context, config *PostgresBranchState) (string, *postgres.Branch, error) {
func (r *ResourcePostgresBranch) DoCreate(ctx context.Context, config *PostgresBranchState) (string, *PostgresBranchRemote, error) {
waiter, err := r.client.Postgres.CreateBranch(ctx, postgres.CreateBranchRequest{
BranchId: config.BranchId,
Parent: config.Parent,
Expand Down Expand Up @@ -90,10 +130,11 @@ func (r *ResourcePostgresBranch) DoCreate(ctx context.Context, config *PostgresB
return "", nil, err
}

return result.Name, result, nil
remote := makePostgresBranchRemote(result)
return remote.Name, remote, nil
}

func (r *ResourcePostgresBranch) DoUpdate(ctx context.Context, id string, config *PostgresBranchState, entry *PlanEntry) (*postgres.Branch, error) {
func (r *ResourcePostgresBranch) DoUpdate(ctx context.Context, id string, config *PostgresBranchState, entry *PlanEntry) (*PostgresBranchRemote, error) {
// Build update mask from fields that have action="update" in the changes map.
// This excludes immutable fields and fields that haven't changed.
// Prefix with "spec." because the API expects paths relative to the Branch object,
Expand Down Expand Up @@ -124,7 +165,10 @@ func (r *ResourcePostgresBranch) DoUpdate(ctx context.Context, id string, config

// Wait for the update to complete
result, err := waiter.Wait(ctx)
return result, err
if err != nil {
return nil, err
}
return makePostgresBranchRemote(result), nil
}

func (r *ResourcePostgresBranch) DoDelete(ctx context.Context, id string) error {
Expand Down
Loading
Loading