-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpcs_cmd.go
167 lines (138 loc) · 4.26 KB
/
vpcs_cmd.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
// Copyright (c) 2017-2021 Ingram Micro Inc.
package cmd
import (
"fmt"
"github.com/ingrammicro/cio/api/network"
"github.com/ingrammicro/cio/api/types"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/urfave/cli"
)
// WireUpVPC prepares common resources to send request to Concerto API
func WireUpVPC(c *cli.Context) (ds *network.VPCService, f format.Formatter) {
f = format.GetFormatter()
config, err := utils.GetConcertoConfig()
if err != nil {
f.PrintFatal("Couldn't wire up config", err)
}
hcs, err := utils.NewHTTPConcertoService(config)
if err != nil {
f.PrintFatal("Couldn't wire up concerto service", err)
}
ds, err = network.NewVPCService(hcs)
if err != nil {
f.PrintFatal("Couldn't wire up VPC service", err)
}
return ds, f
}
// VPCList subcommand function
func VPCList(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPC(c)
vpcs, err := vpcSvc.ListVPCs()
if err != nil {
formatter.PrintFatal("Couldn't receive VPC data", err)
}
labelables := make([]types.Labelable, len(vpcs))
for i := 0; i < len(vpcs); i++ {
labelables[i] = types.Labelable(vpcs[i])
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
filteredLabelables := LabelFiltering(c, labelables, labelIDsByName)
LabelAssignNamesForIDs(c, filteredLabelables, labelNamesByID)
vpcs = make([]*types.Vpc, len(filteredLabelables))
for i, labelable := range filteredLabelables {
v, ok := labelable.(*types.Vpc)
if !ok {
formatter.PrintFatal(LabelFilteringUnexpected,
fmt.Errorf("expected labelable to be a *types.Vpc, got a %T", labelable))
}
vpcs[i] = v
}
if err = formatter.PrintList(vpcs); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// VPCShow subcommand function
func VPCShow(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPC(c)
checkRequiredFlags(c, []string{"id"}, formatter)
vpc, err := vpcSvc.GetVPC(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive VPC data", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
vpc.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*vpc); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// VPCCreate subcommand function
func VPCCreate(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPC(c)
checkRequiredFlags(c, []string{"name", "cidr", "cloud-account-id", "realm-provider-name"}, formatter)
vpcIn := map[string]interface{}{
"name": c.String("name"),
"cidr": c.String("cidr"),
"cloud_account_id": c.String("cloud-account-id"),
"realm_provider_name": c.String("realm-provider-name"),
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
if c.IsSet("labels") {
vpcIn["label_ids"] = LabelResolution(c, c.String("labels"), &labelNamesByID, &labelIDsByName)
}
vpc, err := vpcSvc.CreateVPC(&vpcIn)
if err != nil {
formatter.PrintFatal("Couldn't create VPC", err)
}
vpc.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*vpc); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// VPCUpdate subcommand function
func VPCUpdate(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPC(c)
checkRequiredFlags(c, []string{"id", "name"}, formatter)
vpcIn := map[string]interface{}{
"name": c.String("name"),
}
vpc, err := vpcSvc.UpdateVPC(c.String("id"), &vpcIn)
if err != nil {
formatter.PrintFatal("Couldn't update VPC", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
vpc.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*vpc); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// VPCDelete subcommand function
func VPCDelete(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPC(c)
checkRequiredFlags(c, []string{"id"}, formatter)
err := vpcSvc.DeleteVPC(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't delete VPC", err)
}
return nil
}
// VPCDiscard subcommand function
func VPCDiscard(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPC(c)
checkRequiredFlags(c, []string{"id"}, formatter)
err := vpcSvc.DiscardVPC(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't discard VPC", err)
}
return nil
}