/
resize.go
194 lines (162 loc) · 4.93 KB
/
resize.go
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
package main
import (
"context"
"fmt"
"time"
"github.com/go-openapi/strfmt"
"github.com/urfave/cli"
"github.com/manifoldco/go-manifold"
"github.com/manifoldco/go-manifold/idtype"
"github.com/manifoldco/manifold-cli/api"
"github.com/manifoldco/manifold-cli/clients"
catalogcache "github.com/manifoldco/manifold-cli/data/catalog"
"github.com/manifoldco/manifold-cli/errs"
cModels "github.com/manifoldco/manifold-cli/generated/catalog/models"
mModels "github.com/manifoldco/manifold-cli/generated/marketplace/models"
"github.com/manifoldco/manifold-cli/generated/provisioning/client/operation"
"github.com/manifoldco/manifold-cli/generated/provisioning/models"
"github.com/manifoldco/manifold-cli/middleware"
"github.com/manifoldco/manifold-cli/prompts"
)
func init() {
resizeCmd := cli.Command{
Name: "resize",
ArgsUsage: "[resource-name]",
Usage: "Resize a resource",
Category: "RESOURCES",
Action: middleware.Chain(middleware.EnsureSession, middleware.LoadDirPrefs, middleware.LoadTeamPrefs,
resizeResourceCmd),
Flags: append(teamFlags, []cli.Flag{
projectFlag(),
skipFlag(),
planFlag(),
}...),
}
cmds = append(cmds, resizeCmd)
}
func resizeResourceCmd(cliCtx *cli.Context) error {
ctx := context.Background()
if err := maxOptionalArgsLength(cliCtx, 1); err != nil {
return err
}
name, err := optionalArgName(cliCtx, 0, "resource")
if err != nil {
return err
}
userID, userIDErr := loadUserID(ctx)
if userIDErr != nil && userIDErr != errUserActionAsTeam {
return userIDErr
}
teamID, teamIDErr := validateTeamID(cliCtx)
if teamIDErr != nil {
return teamIDErr
}
if teamID == nil && userIDErr == errUserActionAsTeam {
return errUserActionAsTeam
}
dontWait := cliCtx.Bool("no-wait")
planName := cliCtx.String("plan")
client, err := api.New(api.Analytics, api.Catalog, api.Marketplace, api.Provisioning)
if err != nil {
return err
}
catalog, err := catalogcache.New(ctx, client.Catalog)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not load client catalog: %s", err), -1)
}
res, err := clients.FetchResources(ctx, client.Marketplace, teamID, cliCtx.String("project"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not load resources: %s", err), -1)
}
if len(res) == 0 {
return errs.ErrNoResources
}
projects, err := clients.FetchProjects(ctx, client.Marketplace, teamID)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not load projects: %s", err), -1)
}
rIdx, _, err := prompts.SelectResource(res, projects, name)
if err != nil {
return prompts.HandleSelectError(err, "Could not select resource")
}
r := res[rIdx]
if r.Body.Source != nil && *r.Body.Source == "custom" {
return cli.NewExitError("Cannot resize a custom resource", -1)
}
plans := filterPlansByProductID(catalog.Plans(), *r.Body.ProductID)
if len(plans) == 0 {
return errs.ErrNoPlans
}
pIdx, _, err := prompts.SelectPlan(plans, planName)
if err != nil {
return prompts.HandleSelectError(err, "Could not select Plan")
}
p := plans[pIdx]
spin := prompts.NewSpinner(fmt.Sprintf("Updating resource \"%s\"", r.Body.Label))
spin.Start()
defer spin.Stop()
if err := resizeResource(ctx, r, p, client, teamID, userID, dontWait); err != nil {
return cli.NewExitError(fmt.Sprintf("Could not update resource \"%s\": %s", string(r.Body.Label), err), -1)
}
fmt.Printf("\nYour resource %q has been resized to the plan %q\n", r.Body.Label, p.Body.Label)
return nil
}
func resizeResource(ctx context.Context, r *mModels.Resource, p *cModels.Plan,
client *api.API, tid, uid *manifold.ID, dontWait bool,
) error {
ID, err := manifold.NewID(idtype.Operation)
if err != nil {
return err
}
typeStr := "operation"
version := int64(1)
state := "resize"
curTime := strfmt.DateTime(time.Now())
op := &models.Operation{
ID: ID,
Type: &typeStr,
Version: &version,
Body: &models.Resize{
PlanID: p.ID,
ResourceID: r.ID,
State: &state,
},
}
op.Body.SetCreatedAt(&curTime)
op.Body.SetUpdatedAt(&curTime)
if tid == nil {
op.Body.SetUserID(uid)
} else {
op.Body.SetTeamID(tid)
}
resize := operation.NewPutOperationsIDParamsWithContext(ctx)
resize.SetBody(op)
resize.SetID(ID.String())
res, err := client.Provisioning.Operation.PutOperationsID(resize, nil)
if err != nil {
switch e := err.(type) {
case *operation.PutOperationsIDBadRequest:
return e.Payload
case *operation.PutOperationsIDUnauthorized:
return e.Payload
case *operation.PutOperationsIDNotFound:
return e.Payload
case *operation.PutOperationsIDConflict:
return e.Payload
case *operation.PutOperationsIDInternalServerError:
return errs.ErrSomethingWentHorriblyWrong
default:
return err
}
}
aParams := map[string]string{
"plan": string(p.Body.Label),
"price": toPrice(*p.Body.Cost),
}
client.Analytics.Track(ctx, "Resize Operation", &aParams)
if dontWait {
return nil
}
_, err = waitForOp(ctx, client.Provisioning, res.Payload)
return err
}