-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathecloud.go
More file actions
212 lines (183 loc) · 6.98 KB
/
Copy pathecloud.go
File metadata and controls
212 lines (183 loc) · 6.98 KB
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
package ecloud
import (
"errors"
"fmt"
"os"
"strings"
"github.com/ans-group/cli/internal/pkg/factory"
"github.com/ans-group/cli/internal/pkg/helper"
"github.com/ans-group/sdk-go/pkg/service/ecloud"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
func ECloudRootCmd(f factory.ClientFactory, fs afero.Fs) *cobra.Command {
cmd := &cobra.Command{
Use: "ecloud",
Short: "Commands relating to eCloud service",
}
// Child root commands
v1envset := len(os.Getenv("ANS_ECLOUD")) > 0
vpcEnvSet := len(os.Getenv("ANS_ECLOUD_VPC")) > 0
if v1envset || !vpcEnvSet {
cmd.AddCommand(ecloudVirtualMachineRootCmd(f))
cmd.AddCommand(ecloudSolutionRootCmd(f))
cmd.AddCommand(ecloudSiteRootCmd(f))
cmd.AddCommand(ecloudV1HostRootCmd(f))
cmd.AddCommand(ecloudFirewallRootCmd(f))
cmd.AddCommand(ecloudPodRootCmd(f))
cmd.AddCommand(ecloudDatastoreRootCmd(f))
cmd.AddCommand(ecloudApplianceRootCmd(f))
cmd.AddCommand(ecloudCreditRootCmd(f))
}
// -- eCloud VPC
if vpcEnvSet || !v1envset {
cmd.AddCommand(ecloudAvailabilityZoneRootCmd(f))
cmd.AddCommand(ecloudDHCPRootCmd(f))
cmd.AddCommand(ecloudFirewallPolicyRootCmd(f))
cmd.AddCommand(ecloudFirewallRuleRootCmd(f))
cmd.AddCommand(ecloudFirewallRulePortRootCmd(f))
cmd.AddCommand(ecloudFloatingIPRootCmd(f))
cmd.AddCommand(ecloudHostRootCmd(f))
cmd.AddCommand(ecloudHostGroupRootCmd(f))
cmd.AddCommand(ecloudHostSpecRootCmd(f))
cmd.AddCommand(ecloudImageRootCmd(f))
cmd.AddCommand(ecloudInstanceRootCmd(f))
cmd.AddCommand(ecloudIOPSRootCmd(f))
cmd.AddCommand(ecloudIPAddressRootCmd(f))
cmd.AddCommand(ecloudLoadBalancerRootCmd(f))
cmd.AddCommand(ecloudLoadBalancerSpecRootCmd(f))
cmd.AddCommand(ecloudNATOverloadRuleRootCmd(f))
cmd.AddCommand(ecloudNetworkRootCmd(f))
cmd.AddCommand(ecloudNetworkPolicyRootCmd(f))
cmd.AddCommand(ecloudNetworkRuleRootCmd(f))
cmd.AddCommand(ecloudNetworkRulePortRootCmd(f))
cmd.AddCommand(ecloudNICRootCmd(f))
cmd.AddCommand(ecloudRegionRootCmd(f))
cmd.AddCommand(ecloudRouterRootCmd(f))
cmd.AddCommand(ecloudRouterThroughputRootCmd(f))
cmd.AddCommand(ecloudSSHKeyPairRootCmd(f, fs))
cmd.AddCommand(ecloudTaskRootCmd(f))
cmd.AddCommand(ecloudVIPRootCmd(f))
cmd.AddCommand(ecloudVolumeRootCmd(f))
cmd.AddCommand(ecloudVPCRootCmd(f))
cmd.AddCommand(ecloudVPNEndpointRootCmd(f))
cmd.AddCommand(ecloudVPNProfileGroupRootCmd(f))
cmd.AddCommand(ecloudVPNServiceRootCmd(f))
cmd.AddCommand(ecloudVPNSessionRootCmd(f))
cmd.AddCommand(ecloudVPNGatewayRootCmd(f))
cmd.AddCommand(ecloudVolumeGroupRootCmd(f))
cmd.AddCommand(ecloudAffinityRuleRootCmd(f))
cmd.AddCommand(ecloudAffinityRuleMemberRootCmd(f))
cmd.AddCommand(ecloudResourceTierRootCmd(f))
cmd.AddCommand(ecloudBackupGatewayRootCmd(f))
}
return cmd
}
// GetCreateTagRequestFromStringArrayFlag returns an array of CreateTagRequest structs from given tag string array flag
func GetCreateTagRequestFromStringArrayFlag(tagsFlag []string) ([]ecloud.CreateTagRequest, error) {
var tags []ecloud.CreateTagRequest
for _, tagFlag := range tagsFlag {
key, value, err := GetKeyValueFromStringFlag(tagFlag)
if err != nil {
return tags, err
}
tags = append(tags, ecloud.CreateTagRequest{Key: key, Value: value})
}
return tags, nil
}
// GetCreateVirtualMachineRequestParameterFromStringArrayFlag returns an array of CreateVirtualMachineRequestParameter structs from given string array flag
func GetCreateVirtualMachineRequestParameterFromStringArrayFlag(parametersFlag []string) ([]ecloud.CreateVirtualMachineRequestParameter, error) {
var parameters []ecloud.CreateVirtualMachineRequestParameter
for _, parameterFlag := range parametersFlag {
key, value, err := GetKeyValueFromStringFlag(parameterFlag)
if err != nil {
return parameters, err
}
parameters = append(parameters, ecloud.CreateVirtualMachineRequestParameter{Key: key, Value: value})
}
return parameters, nil
}
// GetKeyValueFromStringFlag returns a string map from given string flag. Expects format 'key=value'
func GetKeyValueFromStringFlag(flag string) (key, value string, err error) {
if flag == "" {
return key, value, errors.New("Missing key/value")
}
parts := strings.Split(flag, "=")
if len(parts) < 2 || len(parts) > 2 {
return key, value, errors.New("Invalid format, expecting: key=value")
}
if parts[0] == "" {
return key, value, errors.New("Missing key")
}
if parts[1] == "" {
return key, value, errors.New("Missing value")
}
return parts[0], parts[1], nil
}
// SolutionTemplateExistsWaitFunc returns WaitFunc for waiting for a template to exist
func SolutionTemplateExistsWaitFunc(service ecloud.ECloudService, solutionID int, templateName string, exists bool) helper.WaitFunc {
return func() (finished bool, err error) {
_, err = service.GetSolutionTemplate(solutionID, templateName)
if err != nil {
if _, ok := err.(*ecloud.TemplateNotFoundError); ok {
return (exists == false), nil
}
return false, fmt.Errorf("Failed to retrieve solution template [%s]: %s", templateName, err.Error())
}
return (exists == true), nil
}
}
// PodTemplateExistsWaitFunc returns WaitFunc for waiting for a template to exist
func PodTemplateExistsWaitFunc(service ecloud.ECloudService, podID int, templateName string, exists bool) helper.WaitFunc {
return func() (finished bool, err error) {
_, err = service.GetPodTemplate(podID, templateName)
if err != nil {
if _, ok := err.(*ecloud.TemplateNotFoundError); ok {
return (exists == false), nil
}
return false, fmt.Errorf("Failed to retrieve pod template [%s]: %s", templateName, err.Error())
}
return (exists == true), nil
}
}
type GetResourceSyncStatusFunc func() (ecloud.SyncStatus, error)
func ResourceSyncStatusWaitFunc(fn GetResourceSyncStatusFunc, expectedStatus ecloud.SyncStatus) helper.WaitFunc {
return func() (finished bool, err error) {
status, err := fn()
if err != nil {
return false, fmt.Errorf("Failed to retrieve status for resource: %s", err)
}
if status == ecloud.SyncStatusFailed {
return false, fmt.Errorf("Resource in [%s] state", ecloud.SyncStatusFailed.String())
}
if status == expectedStatus {
return true, nil
}
return false, nil
}
}
func TaskStatusWaitFunc(service ecloud.ECloudService, taskID string, expectedStatus ecloud.TaskStatus) helper.WaitFunc {
return func() (finished bool, err error) {
task, err := service.GetTask(taskID)
if err != nil {
return false, fmt.Errorf("Failed to retrieve task status: %s", err)
}
if task.Status == ecloud.TaskStatusFailed {
return false, fmt.Errorf("Task in [%s] state", ecloud.TaskStatusFailed)
}
if task.Status == expectedStatus {
return true, nil
}
return false, nil
}
}
type ecloudServiceCobraRunEFunc func(service ecloud.ECloudService, cmd *cobra.Command, args []string) error
func ecloudCobraRunEFunc(f factory.ClientFactory, rf ecloudServiceCobraRunEFunc) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
c, err := f.NewClient()
if err != nil {
return err
}
return rf(c.ECloudService(), cmd, args)
}
}