-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathpostgres_project.go
More file actions
129 lines (110 loc) · 3.81 KB
/
Copy pathpostgres_project.go
File metadata and controls
129 lines (110 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package dresources
import (
"context"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/common/types/fieldmask"
"github.com/databricks/databricks-sdk-go/service/postgres"
)
type ResourcePostgresProject struct {
client *databricks.WorkspaceClient
}
type PostgresProjectState = resources.PostgresProjectConfig
func (*ResourcePostgresProject) New(client *databricks.WorkspaceClient) *ResourcePostgresProject {
return &ResourcePostgresProject{client: client}
}
func (*ResourcePostgresProject) PrepareState(input *resources.PostgresProject) *PostgresProjectState {
return &PostgresProjectState{
ProjectId: input.ProjectId,
ProjectSpec: input.ProjectSpec,
}
}
func (*ResourcePostgresProject) RemapState(remote *postgres.Project) *PostgresProjectState {
// Extract project_id from hierarchical name: "projects/{project_id}"
// TODO: log error when we have access to the context
components, _ := ParsePostgresName(remote.Name)
return &PostgresProjectState{
ProjectId: components.ProjectID,
// 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.
ProjectSpec: postgres.ProjectSpec{
BudgetPolicyId: "",
CustomTags: nil,
DefaultBranch: "",
DefaultEndpointSettings: nil,
DisplayName: "",
EnablePgNativeLogin: false,
HistoryRetentionDuration: nil,
PgVersion: 0,
ForceSendFields: nil,
},
}
}
func (r *ResourcePostgresProject) DoRead(ctx context.Context, id string) (*postgres.Project, error) {
return r.client.Postgres.GetProject(ctx, postgres.GetProjectRequest{Name: id})
}
func (r *ResourcePostgresProject) DoCreate(ctx context.Context, config *PostgresProjectState) (string, *postgres.Project, error) {
waiter, err := r.client.Postgres.CreateProject(ctx, postgres.CreateProjectRequest{
ProjectId: config.ProjectId,
Project: postgres.Project{
Spec: &config.ProjectSpec,
InitialEndpointSpec: nil,
// Output-only fields.
CreateTime: nil,
Name: "",
Status: nil,
Uid: "",
UpdateTime: nil,
ForceSendFields: nil,
},
})
if err != nil {
return "", nil, err
}
// Wait for the project to be ready (long-running operation)
result, err := waiter.Wait(ctx)
if err != nil {
return "", nil, err
}
return result.Name, result, nil
}
func (r *ResourcePostgresProject) DoUpdate(ctx context.Context, id string, config *PostgresProjectState, entry *PlanEntry) (*postgres.Project, 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 Project object,
// not relative to our flattened state type.
fieldPaths := collectUpdatePathsWithPrefix(entry.Changes, "spec.")
waiter, err := r.client.Postgres.UpdateProject(ctx, postgres.UpdateProjectRequest{
Project: postgres.Project{
Spec: &config.ProjectSpec,
InitialEndpointSpec: nil,
// Output-only fields.
CreateTime: nil,
Name: "",
Status: nil,
Uid: "",
UpdateTime: nil,
ForceSendFields: nil,
},
Name: id,
UpdateMask: fieldmask.FieldMask{
Paths: fieldPaths,
},
})
if err != nil {
return nil, err
}
// Wait for the update to complete
result, err := waiter.Wait(ctx)
return result, err
}
func (r *ResourcePostgresProject) DoDelete(ctx context.Context, id string) error {
waiter, err := r.client.Postgres.DeleteProject(ctx, postgres.DeleteProjectRequest{
Name: id,
})
if err != nil {
return err
}
return waiter.Wait(ctx)
}