This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
connectors.go
234 lines (196 loc) · 7.24 KB
/
connectors.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
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
package cli
import (
"sort"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/cloud"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v1connectors"
"github.com/hortonworks/cb-cli/client_cloudbreak/v2connectors"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
)
var regionsHeader = []string{"Name", "Description"}
var diskHeader = []string{"Name", "Description"}
var availabilityZonesHeader = []string{"Name"}
var instanceHeader = []string{"Name", "Cpu", "Memory", "AvailabilityZone"}
type regionsOut struct {
Name string `json:"Name" yaml:"Name"`
Description string `json:"Description" yaml:"Description"`
}
type diskOut struct {
Name string `json:"Name" yaml:"Name"`
Description string `json:"Description" yaml:"Description"`
}
type availabilityZonesOut struct {
Name string `json:"Name" yaml:"Name"`
}
type instanceOut struct {
Name string `json:"Name" yaml:"Name"`
Cpu string `json:"Cpu" yaml:"Cpu"`
Memory string `json:"Memory" yaml:"Memory"`
AvailabilityZone string `json:"AvailabilityZone" yaml:"AvailabilityZone"`
}
type hasName interface {
GetName() string
}
func (r *regionsOut) DataAsStringArray() []string {
return []string{r.Name, r.Description}
}
func (r *regionsOut) GetName() string {
return r.Name
}
func (r *diskOut) DataAsStringArray() []string {
return []string{r.Name, r.Description}
}
func (r *diskOut) GetName() string {
return r.Name
}
func (r *availabilityZonesOut) DataAsStringArray() []string {
return []string{r.Name}
}
func (r *availabilityZonesOut) GetName() string {
return r.Name
}
func (r *instanceOut) DataAsStringArray() []string {
return []string{r.Name, r.Cpu, r.Memory, r.AvailabilityZone}
}
func (r *instanceOut) GetName() string {
return r.Name
}
func ListRegions(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list regions")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listRegionsImpl(cbClient.Cloudbreak.V2connectors, output.WriteList, c.String(FlCredential.Name))
}
func ListAvailabilityZones(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list availability zones")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listAvailabilityZonesImpl(cbClient.Cloudbreak.V2connectors, output.WriteList, c.String(FlCredential.Name), c.String(FlRegion.Name))
}
func ListAwsVolumeTypes(c *cli.Context) {
cloud.SetProviderType(cloud.AWS)
listVolumeTypes(c)
}
func ListAzureVolumeTypes(c *cli.Context) {
cloud.SetProviderType(cloud.AZURE)
listVolumeTypes(c)
}
func ListGcpVolumeTypes(c *cli.Context) {
cloud.SetProviderType(cloud.GCP)
listVolumeTypes(c)
}
func listVolumeTypes(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list volume types")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listVolumeTypesImpl(cbClient.Cloudbreak.V1connectors, output.WriteList)
}
func ListInstanceTypes(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list instance types")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listInstanceTypesImpl(cbClient.Cloudbreak.V2connectors, output.WriteList, c.String(FlCredential.Name), c.String(FlRegion.Name), c.String(FlAvailabilityZoneOptional.Name))
}
type getConnectorsClient interface {
GetRegionsByCredentialID(*v2connectors.GetRegionsByCredentialIDParams) (*v2connectors.GetRegionsByCredentialIDOK, error)
}
type getDiskTypesClient interface {
GetDisktypes(*v1connectors.GetDisktypesParams) (*v1connectors.GetDisktypesOK, error)
}
type getInstanceTypesClient interface {
GetVMTypesByCredentialID(*v2connectors.GetVMTypesByCredentialIDParams) (*v2connectors.GetVMTypesByCredentialIDOK, error)
}
func listRegionsImpl(client getConnectorsClient, writer func([]string, []utils.Row), credentialName string) {
log.Infof("[listRegionsImpl] sending regions list request")
req := &models_cloudbreak.PlatformResourceRequestJSON{
CredentialName: credentialName,
}
regionsResp, err := client.GetRegionsByCredentialID(v2connectors.NewGetRegionsByCredentialIDParams().WithBody(req))
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for name, dispname := range regionsResp.Payload.DisplayNames {
tableRows = append(tableRows, ®ionsOut{name, dispname})
}
sortByName(tableRows)
writer(regionsHeader, tableRows)
}
func listAvailabilityZonesImpl(client getConnectorsClient, writer func([]string, []utils.Row), credentialName string, region string) {
log.Infof("[listAvailabilityZonesImpl] sending availability zones list request")
req := &models_cloudbreak.PlatformResourceRequestJSON{
CredentialName: credentialName,
}
regionsResp, err := client.GetRegionsByCredentialID(v2connectors.NewGetRegionsByCredentialIDParams().WithBody(req))
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, zone := range regionsResp.Payload.AvailabilityZones[region] {
tableRows = append(tableRows, &availabilityZonesOut{zone})
}
sortByName(tableRows)
writer(availabilityZonesHeader, tableRows)
}
func listVolumeTypesImpl(client getDiskTypesClient, writer func([]string, []utils.Row)) {
log.Infof("[listVolumeTypesImpl] sending volume type list request")
provider := cloud.GetProvider().GetName()
volumeResp, err := client.GetDisktypes(v1connectors.NewGetDisktypesParams())
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for t, desc := range volumeResp.Payload.DisplayNames[*provider] {
tableRows = append(tableRows, &diskOut{t, desc})
}
sortByName(tableRows)
writer(diskHeader, tableRows)
}
func listInstanceTypesImpl(client getInstanceTypesClient, writer func([]string, []utils.Row), credentialName string, region string, avzone string) {
log.Infof("[listInstanceTypesImpl] sending instance type list request")
req := &models_cloudbreak.PlatformResourceRequestJSON{
CredentialName: credentialName,
Region: region,
}
instanceResp, err := client.GetVMTypesByCredentialID(v2connectors.NewGetVMTypesByCredentialIDParams().WithBody(req))
if err != nil {
utils.LogErrorAndExit(err)
}
machines := instanceResp.Payload.VMTypes[avzone]
if len(machines.VirtualMachines) == 0 {
machines = instanceResp.Payload.VMTypes[region]
}
if len(machines.VirtualMachines) == 0 && len(avzone) == 0 {
for k := range instanceResp.Payload.VMTypes {
if strings.HasPrefix(k, region) {
avzone = k
machines = instanceResp.Payload.VMTypes[k]
break
}
}
}
tableRows := []utils.Row{}
for _, instance := range machines.VirtualMachines {
tableRows = append(tableRows, &instanceOut{instance.Value, instance.VMTypeMetaJSON.Properties["Cpu"], instance.VMTypeMetaJSON.Properties["Memory"], avzone})
}
sortByName(tableRows)
writer(instanceHeader, tableRows)
}
func sortByName(rows []utils.Row) {
sort.Slice(rows, func(i, j int) bool {
vi, oki := rows[i].(hasName)
vj, okj := rows[j].(hasName)
if !oki || !okj {
return i < j
}
return vi.GetName() < vj.GetName()
})
}