-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnets_cmd.go
159 lines (129 loc) · 4.06 KB
/
subnets_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
// Copyright (c) 2017-2021 Ingram Micro Inc.
package cmd
import (
"github.com/ingrammicro/cio/api/network"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/urfave/cli"
)
// WireUpSubnet prepares common resources to send request to Concerto API
func WireUpSubnet(c *cli.Context) (ds *network.SubnetService, 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.NewSubnetService(hcs)
if err != nil {
f.PrintFatal("Couldn't wire up Subnet service", err)
}
return ds, f
}
// SubnetList subcommand function
func SubnetList(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"vpc-id"}, formatter)
subnets, err := subnetSvc.ListSubnets(c.String("vpc-id"))
if err != nil {
formatter.PrintFatal("Couldn't receive Subnet data", err)
}
if err = formatter.PrintList(subnets); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// SubnetShow subcommand function
func SubnetShow(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"id"}, formatter)
subnet, err := subnetSvc.GetSubnet(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive Subnet data", err)
}
if err = formatter.PrintItem(*subnet); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// SubnetCreate subcommand function
func SubnetCreate(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"vpc-id", "name", "cidr", "type"}, formatter)
subnetIn := map[string]interface{}{
"name": c.String("name"),
"cidr": c.String("cidr"),
"cloud_account_id": c.String("cloud-account-id"),
"type": c.String("type"),
}
subnet, err := subnetSvc.CreateSubnet(c.String("vpc-id"), &subnetIn)
if err != nil {
formatter.PrintFatal("Couldn't create Subnet", err)
}
if err = formatter.PrintItem(*subnet); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// SubnetUpdate subcommand function
func SubnetUpdate(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"id", "name"}, formatter)
subnetIn := map[string]interface{}{
"name": c.String("name"),
}
subnet, err := subnetSvc.UpdateSubnet(c.String("id"), &subnetIn)
if err != nil {
formatter.PrintFatal("Couldn't update Subnet", err)
}
if err = formatter.PrintItem(*subnet); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// SubnetDelete subcommand function
func SubnetDelete(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"id"}, formatter)
err := subnetSvc.DeleteSubnet(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't delete Subnet", err)
}
return nil
}
// SubnetServerList subcommand function
func SubnetServerList(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"id"}, formatter)
servers, err := subnetSvc.ListSubnetServers(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive servers data", err)
}
if err = formatter.PrintList(servers); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// SubnetServerArrayList subcommand function
func SubnetServerArrayList(c *cli.Context) error {
debugCmdFuncInfo(c)
subnetSvc, formatter := WireUpSubnet(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverArrays, err := subnetSvc.ListSubnetServerArrays(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive server arrays data", err)
}
if err = formatter.PrintList(serverArrays); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}