-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.go
More file actions
398 lines (375 loc) · 11.6 KB
/
Copy pathresource.go
File metadata and controls
398 lines (375 loc) · 11.6 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package cli
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/open-source-cloud/devstack/internal/orchestrate"
"github.com/open-source-cloud/devstack/internal/resource"
"github.com/open-source-cloud/devstack/internal/state"
"github.com/open-source-cloud/devstack/internal/workspace"
)
// newResourceCmd wires `devstack resource list|show|create|rm|gc` — the
// engine-agnostic data-plane resource verbs (spec 27). list/show are lock-free
// ledger reads; create/rm/gc mirror the up-saga provision flow (lock → overlay →
// provisioner → ledger → event) via internal/orchestrate. Secrets are masked
// unless --show-secrets.
func newResourceCmd(g *GlobalOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "resource",
Short: "Manage per-project resources inside shared engines (databases, buckets, …)",
}
cmd.AddCommand(
newResourceListCmd(g),
newResourceShowCmd(g),
newResourceCreateCmd(g),
newResourceRmCmd(g),
newResourceGcCmd(g),
)
return cmd
}
// secretAttrKeys are attribute names whose value is a credential — masked in
// output unless --show-secrets (spec 27 §Credential surfacing).
var secretAttrKeys = map[string]bool{"password": true, "secret": true, "secretkey": true, "token": true}
func maskAttrs(a resource.Attrs, show bool) map[string]string {
out := map[string]string{}
for k, v := range a {
if secretAttrKeys[k] && !show {
out[k] = "***"
continue
}
out[k] = v
}
return out
}
func newResourceListCmd(g *GlobalOpts) *cobra.Command {
var project, engine, kind string
cmd := &cobra.Command{
Use: "list",
Short: "List provisioned resources from the ownership ledger",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
mgr, closeFn, err := buildManager(cmd)
if err != nil {
return err
}
defer closeFn()
var rows []state.Provisioned
if project != "" {
rows, err = mgr.DB.ProvisionedFor(project)
} else {
rows, err = mgr.DB.AllProvisioned()
}
if err != nil {
return err
}
// kind filter (engine is derived, not stored; filter best-effort by kind).
var filtered []state.Provisioned
for _, r := range rows {
if kind != "" && r.Kind != kind {
continue
}
if engine != "" && !resource.SupportsKind(engine, r.Kind) {
continue
}
filtered = append(filtered, r)
}
if g.JSON {
return writeJSON(cmd, map[string]any{"resources": filtered})
}
w := cmd.OutOrStdout()
if len(filtered) == 0 {
fmt.Fprintln(w, "no provisioned resources")
return nil
}
for _, r := range filtered {
fmt.Fprintf(w, "%-12s %-12s %-24s %s\n", r.Project, r.Kind, r.Name, r.CreatedAt)
}
return nil
},
}
cmd.Flags().StringVar(&project, "project", "", "only this project's resources")
cmd.Flags().StringVar(&engine, "engine", "", "filter by engine (postgres/redis/minio/…)")
cmd.Flags().StringVar(&kind, "kind", "", "filter by kind (database/bucket/…)")
return cmd
}
func newResourceShowCmd(g *GlobalOpts) *cobra.Command {
var project, engine string
var showSecrets bool
cmd := &cobra.Command{
Use: "show <name>",
Short: "Show a resource's connection attributes (secrets masked unless --show-secrets)",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
mgr, closeFn, err := buildManager(cmd)
if err != nil {
return err
}
defer closeFn()
rows, err := mgr.DB.AllProvisioned()
if err != nil {
return err
}
var match *state.Provisioned
for i := range rows {
if rows[i].Name != name {
continue
}
if project != "" && rows[i].Project != project {
continue
}
match = &rows[i]
break
}
if match == nil {
return fmt.Errorf("no provisioned resource named %q", name)
}
if engine == "" {
engine = mgr.Model.Workspace.Shared[firstSharedForKind(mgr, match.Kind)].Template
}
// Derive non-secret attrs from the ledger row + workspace model (no daemon).
attrs := resource.Attrs{
"project": match.Project,
"kind": match.Kind,
"name": match.Name,
"host": "shared-" + engineInstanceName(mgr, engine),
"password": match.Project, // predictable dev-cred (masked unless --show-secrets)
}
out := maskAttrs(attrs, showSecrets)
if g.JSON {
return writeJSON(cmd, out)
}
w := cmd.OutOrStdout()
for _, k := range sortedKeysOf(out) {
fmt.Fprintf(w, "%-10s %s\n", k, out[k])
}
return nil
},
}
cmd.Flags().StringVar(&project, "project", "", "disambiguate by owner project")
cmd.Flags().StringVar(&engine, "engine", "", "engine hint for attribute derivation")
cmd.Flags().BoolVar(&showSecrets, "show-secrets", false, "print credential values (diagnostics only)")
return cmd
}
func newResourceCreateCmd(g *GlobalOpts) *cobra.Command {
var project, credentials string
var params []string
cmd := &cobra.Command{
Use: "create <engine> <kind> <name>",
Short: "Create a resource on a running shared engine (idempotent)",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
engine, kind, name := args[0], args[1], args[2]
d, closeFn, err := buildUpDeps(cmd)
if err != nil {
return err
}
defer closeFn()
owner := project
if owner == "" {
owner = defaultProject(d)
}
cred := resource.CredPredictable
if credentials != "" {
cred = resource.CredentialPolicy(credentials)
}
p, err := parseParams(params)
if err != nil {
return err
}
r := resource.Resource{Engine: engine, Kind: kind, Name: name, Owner: owner, Params: p, CredKind: cred}
attrs, err := orchestrate.CreateResource(cmd.Context(), d, r)
if err != nil {
return err
}
if g.JSON {
return writeJSON(cmd, map[string]any{"created": map[string]string{"engine": engine, "kind": kind, "name": name, "project": owner}, "attrs": maskAttrs(attrs, false)})
}
fmt.Fprintf(cmd.OutOrStdout(), "created %s %s %q for project %q\n", engine, kind, name, owner)
return nil
},
}
cmd.Flags().StringVar(&project, "project", "", "owner project (default: the workspace's single/first project)")
cmd.Flags().StringSliceVar(¶ms, "param", nil, "kind-specific parameter k=v (repeatable)")
cmd.Flags().StringVar(&credentials, "credentials", "", "predictable|generated (default: predictable)")
return cmd
}
func newResourceRmCmd(g *GlobalOpts) *cobra.Command {
var project, engine string
var purge, yes bool
cmd := &cobra.Command{
Use: "rm <name>",
Short: "Un-track a resource (or --purge-data to also drop it)",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
if purge && g.JSON && !yes {
return fmt.Errorf("refusing to --purge-data without --yes for --json/non-interactive use")
}
d, closeFn, err := buildUpDeps(cmd)
if err != nil {
return err
}
defer closeFn()
owner := project
if owner == "" {
owner = defaultProject(d)
}
// Resolve the kind from the ledger (a resource is identified by name+owner).
rows, err := d.DB.ProvisionedFor(owner)
if err != nil {
return err
}
var kind string
for _, r := range rows {
if r.Name == name && r.Kind != "role" {
kind = r.Kind
break
}
}
if kind == "" {
return fmt.Errorf("no provisioned resource named %q for project %q", name, owner)
}
if engine == "" {
engine = engineForKindGuess(kind)
}
if purge && !yes {
if !confirm(cmd, fmt.Sprintf("This DROPS %s %q (data is destroyed). Type 'yes' to continue: ", kind, name)) {
fmt.Fprintln(cmd.OutOrStdout(), "aborted")
return nil
}
}
r := resource.Resource{Engine: engine, Kind: kind, Name: name, Owner: owner}
if err := orchestrate.DropResource(cmd.Context(), d, r, purge); err != nil {
return err
}
verb := "un-tracked"
if purge {
verb = "dropped"
}
if g.JSON {
return writeJSON(cmd, map[string]any{"removed": map[string]any{"name": name, "kind": kind, "project": owner, "purged": purge}})
}
fmt.Fprintf(cmd.OutOrStdout(), "%s %s %q (project %q)\n", verb, kind, name, owner)
return nil
},
}
cmd.Flags().StringVar(&project, "project", "", "owner project")
cmd.Flags().StringVar(&engine, "engine", "", "engine (default: inferred from kind)")
cmd.Flags().BoolVar(&purge, "purge-data", false, "also DROP the resource (destructive)")
cmd.Flags().BoolVar(&yes, "yes", false, "skip the confirmation prompt")
return cmd
}
func newResourceGcCmd(g *GlobalOpts) *cobra.Command {
var yes bool
cmd := &cobra.Command{
Use: "gc",
Short: "Reclaim resources whose owner project left the workspace (destructive)",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if g.JSON && !yes {
return fmt.Errorf("refusing to gc without --yes for --json/non-interactive use")
}
d, closeFn, err := buildUpDeps(cmd)
if err != nil {
return err
}
defer closeFn()
active := map[string]bool{}
for _, p := range sortedProjectNames(d.Model) {
active[p] = true
}
// Preview the orphans so the confirmation is informed.
orphans, err := d.DB.OrphanedProvisioned(active)
if err != nil {
return err
}
if len(orphans) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "no orphaned resources to reclaim")
return nil
}
if !yes {
if !confirm(cmd, fmt.Sprintf("This DROPS %d orphaned resource(s) (data destroyed). Type 'yes' to continue: ", len(orphans))) {
fmt.Fprintln(cmd.OutOrStdout(), "aborted")
return nil
}
}
res, err := orchestrate.GCResources(cmd.Context(), d, active)
if err != nil {
return err
}
if g.JSON {
return writeJSON(cmd, res)
}
w := cmd.OutOrStdout()
for _, r := range res.Reaped {
fmt.Fprintf(w, "reaped %s %s (project %s)\n", r["kind"], r["name"], r["project"])
}
for _, s := range res.Skipped {
fmt.Fprintf(w, "skipped %s %s: %s\n", s["kind"], s["name"], s["reason"])
}
return nil
},
}
cmd.Flags().BoolVar(&yes, "yes", false, "skip the confirmation prompt (required for --json)")
return cmd
}
// --- helpers ---------------------------------------------------------------
// defaultProject resolves the default project for a data-plane command: the
// active-context resolution (DEVSTACK_PROJECT → persisted active → single/first),
// spec 30.
func defaultProject(d orchestrate.UpDeps) string {
return resolveActiveProject(d.Model, d.DB)
}
// engineForKindGuess maps a ledger kind back to its engine for the live set
// (postgres only this milestone). Ambiguous/unknown kinds default to postgres.
func engineForKindGuess(kind string) string {
switch kind {
case "bucket", "lifecycle", "access_key":
return "minio"
case "redis_index", "acl_user":
return "redis"
default:
return "postgres"
}
}
func engineInstanceName(mgr *workspace.Manager, engine string) string {
if inst, ok := orchestrate.ResolveInstance(mgr.Model, engine); ok {
return inst
}
return engine
}
func firstSharedForKind(mgr *workspace.Manager, kind string) string {
engine := engineForKindGuess(kind)
if inst, ok := orchestrate.ResolveInstance(mgr.Model, engine); ok {
return inst
}
// Fall back to any shared name so the map lookup does not panic.
for _, n := range mgr.Model.SharedNames() {
return n
}
return ""
}
func parseParams(kv []string) (map[string]any, error) {
if len(kv) == 0 {
return nil, nil
}
out := map[string]any{}
for _, pair := range kv {
i := strings.IndexByte(pair, '=')
if i <= 0 {
return nil, fmt.Errorf("invalid --param %q (want k=v)", pair)
}
out[pair[:i]] = pair[i+1:]
}
return out, nil
}
func sortedKeysOf(m map[string]string) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
sort.Strings(out)
return out
}