forked from databricks/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
184 lines (150 loc) · 3.75 KB
/
state.go
File metadata and controls
184 lines (150 loc) · 3.75 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package dstate
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"sync"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/bundle/statemgmt/resourcestate"
"github.com/databricks/cli/internal/build"
"github.com/google/uuid"
)
const currentStateVersion = 1
type DeploymentState struct {
Path string
Data Database
mu sync.Mutex
}
type Database struct {
StateVersion int `json:"state_version"`
CLIVersion string `json:"cli_version"`
Lineage string `json:"lineage"`
Serial int `json:"serial"`
State map[string]ResourceEntry `json:"state"`
}
type ResourceEntry struct {
ID string `json:"__id__"`
State any `json:"state"`
}
func NewDatabase() Database {
return NewMigratedDatabase(uuid.New().String(), 1)
}
func NewMigratedDatabase(lineage string, serial int) Database {
return Database{
StateVersion: currentStateVersion,
CLIVersion: build.GetInfo().Version,
Lineage: lineage,
Serial: serial,
State: make(map[string]ResourceEntry),
}
}
func (db *DeploymentState) SaveState(key, newID string, state any) error {
db.AssertOpened()
db.mu.Lock()
defer db.mu.Unlock()
if db.Data.State == nil {
db.Data.State = make(map[string]ResourceEntry)
}
db.Data.State[key] = ResourceEntry{
ID: newID,
State: state,
}
return nil
}
func (db *DeploymentState) DeleteState(key string) error {
db.AssertOpened()
db.mu.Lock()
defer db.mu.Unlock()
if db.Data.State == nil {
return nil
}
delete(db.Data.State, key)
return nil
}
func (db *DeploymentState) GetResourceEntry(key string) (ResourceEntry, bool) {
db.AssertOpened()
db.mu.Lock()
defer db.mu.Unlock()
if db.Data.State == nil {
return ResourceEntry{}, false
}
result, ok := db.Data.State[key]
return result, ok
}
func (db *DeploymentState) Open(path string) error {
db.mu.Lock()
defer db.mu.Unlock()
if db.Path != "" {
if db.Path == path {
return nil
}
return fmt.Errorf("already read state %v, cannot open %v", db.Path, path)
}
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
db.Data = NewDatabase()
db.Path = path
return nil
}
return err
}
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
err = dec.Decode(&db.Data)
if err != nil {
return err
}
db.Path = path
return nil
}
func (db *DeploymentState) Finalize() error {
db.mu.Lock()
defer db.mu.Unlock()
db.Data.Serial += 1
return db.unlockedSave()
}
func (db *DeploymentState) AssertOpened() {
if db.Path == "" {
panic("internal error: DeploymentState must be opened first")
}
}
func (db *DeploymentState) ExportState(ctx context.Context) resourcestate.ExportedResourcesMap {
result := make(resourcestate.ExportedResourcesMap)
for key, entry := range db.Data.State {
// Extract etag for dashboards.
var etag string
switch dashboard := entry.State.(type) {
// Dashboard state has type map[string]any during bundle deployment.
// covered by test case: bundle/deploy/dashboard/detect-change
case map[string]any:
v, ok := dashboard["etag"].(string)
if ok {
etag = v
}
// Dashboard state has type *resources.DashboardConfig during bundle generation.
// covered by test case: bundle/deploy/dashboard/generate_inplace
case *resources.DashboardConfig:
etag = dashboard.Etag
}
result[key] = resourcestate.ResourceState{
ID: entry.ID,
ETag: etag,
}
}
return result
}
func (db *DeploymentState) unlockedSave() error {
db.AssertOpened()
data, err := json.MarshalIndent(db.Data, "", " ")
if err != nil {
return err
}
err = os.WriteFile(db.Path, data, 0o600)
if err != nil {
return fmt.Errorf("failed to save resources state to %#v: %w", db.Path, err)
}
return nil
}