/
update.go
154 lines (126 loc) · 3.99 KB
/
update.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
package main
import (
"context"
"fmt"
"github.com/urfave/cli"
"github.com/manifoldco/go-manifold"
"github.com/manifoldco/manifold-cli/api"
"github.com/manifoldco/manifold-cli/clients"
"github.com/manifoldco/manifold-cli/errs"
"github.com/manifoldco/manifold-cli/generated/marketplace/client/resource"
"github.com/manifoldco/manifold-cli/middleware"
"github.com/manifoldco/manifold-cli/prompts"
"github.com/manifoldco/manifold-cli/generated/marketplace/client"
"github.com/manifoldco/manifold-cli/generated/marketplace/models"
)
func init() {
updateCmd := cli.Command{
Name: "update",
ArgsUsage: "[resource-name]",
Usage: "Update a resource",
Category: "RESOURCES",
Action: middleware.Chain(middleware.EnsureSession, middleware.LoadDirPrefs,
middleware.LoadTeamPrefs, updateResourceCmd),
Flags: append(teamFlags, []cli.Flag{
projectFlag(),
}...),
}
cmds = append(cmds, updateCmd)
}
func updateResourceCmd(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
}
teamID, err := validateTeamID(cliCtx)
if err != nil {
return err
}
project, err := validateName(cliCtx, "project")
if err != nil {
return err
}
client, err := api.New(api.Marketplace)
if err != nil {
return err
}
var resources []*models.Resource
resources, err = clients.FetchResources(ctx, client.Marketplace, teamID, project)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("Failed to fetch the list of provisioned resources: %s", err), -1)
}
if len(resources) == 0 {
return cli.NewExitError("No resources found for updating", -1)
}
projects, err := clients.FetchProjects(ctx, client.Marketplace, teamID)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("Failed to fetch list of projects: %s", err), -1)
}
var resource *models.Resource
if name != "" {
var err error
resource, err = pickResourcesByName(resources, projects, name)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to fetch resource: %s", err), -1)
}
} else {
idx, _, err := prompts.SelectResource(resources, projects, name)
if err != nil {
return prompts.HandleSelectError(err, "Could not select Resource")
}
resource = resources[idx]
}
newName, err := promptName(cliCtx, nil, "resource", false)
if err != nil {
cli.NewExitError(fmt.Sprintf("Could not rename the resource: %s", err), -1)
}
newTitle := newName
prompts.SpinStart(fmt.Sprintf("Updating resource %q", resource.Body.Label))
mrb, err := updateResource(ctx, resource, client.Marketplace, newName, newTitle)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to update resource: %s", err), -1)
}
prompts.SpinStop()
fmt.Printf("Your instance \"%s\" has been updated\n", mrb.Body.Name)
return nil
}
func pickResourcesByName(resources []*models.Resource, projects []*models.Project, name string) (*models.Resource, error) {
if name == "" {
return nil, errs.ErrResourceNotFound
}
idx, _, err := prompts.SelectResource(resources, projects, name)
if err != nil {
return nil, err
}
return resources[idx], nil
}
func updateResource(ctx context.Context, r *models.Resource,
marketplaceClient *client.Marketplace, resourceName string, resourceTitle string) (*models.Resource, error) {
rename := &models.PublicUpdateResource{
Body: &models.PublicUpdateResourceBody{
Name: manifold.Name(resourceTitle),
Label: manifold.Label(resourceName),
},
}
c := resource.NewPatchResourcesIDParamsWithContext(ctx)
c.SetBody(rename)
c.SetID(r.ID.String())
patchRes, err := marketplaceClient.Resource.PatchResourcesID(c, nil)
if err != nil {
switch e := err.(type) {
case *resource.PatchResourcesIDBadRequest:
return nil, e.Payload
case *resource.PatchResourcesIDUnauthorized:
return nil, e.Payload
case *resource.PatchResourcesIDInternalServerError:
return nil, errs.ErrSomethingWentHorriblyWrong
}
}
return patchRes.Payload, err
}