forked from bizflycloud/gobizfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscaling_launch_configuration.go
139 lines (118 loc) · 4.02 KB
/
autoscaling_launch_configuration.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
package gobizfly
import (
"context"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
)
// LaunchConfiguration - is represents a launch configurations
type LaunchConfiguration struct {
AvailabilityZone string `json:"availability_zone"`
Created string `json:"created_at,omitempty"`
DataDisks []*AutoScalingDataDisk `json:"datadisks,omitempty"`
Flavor string `json:"flavor"`
ID string `json:"id,omitempty"`
Metadata map[string]interface{} `json:"metadata"`
Name string `json:"name"`
NetworkPlan string `json:"network_plan,omitempty"`
Networks []*AutoScalingNetworks `json:"networks,omitempty"`
OperatingSystem AutoScalingOperatingSystem `json:"os"`
ProfileType string `json:"profile_type,omitempty"`
RootDisk *AutoScalingDataDisk `json:"rootdisk"`
SecurityGroups []*string `json:"security_groups,omitempty"`
SSHKey string `json:"key_name,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
UserData string `json:"user_data,omitempty"`
}
// List launch configurations
func (lc *launchConfiguration) List(ctx context.Context, all bool) ([]*LaunchConfiguration, error) {
req, err := lc.client.NewRequest(ctx, http.MethodGet, autoScalingServiceName, lc.resourcePath(), nil)
if err != nil {
return nil, err
}
if all {
q := req.URL.Query()
q.Add("all", "true")
req.URL.RawQuery = q.Encode()
}
resp, err := lc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
LaunchConfigurations []*LaunchConfiguration `json:"profiles"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
for _, LaunchConfiguration := range data.LaunchConfigurations {
// Force ProfileType = Type
LaunchConfiguration.ProfileType = LaunchConfiguration.Type
if LaunchConfiguration.OperatingSystem.Error != "" {
LaunchConfiguration.Status = statusError
} else {
LaunchConfiguration.Status = statusActive
}
}
return data.LaunchConfigurations, nil
}
// Get a launch configuration
func (lc *launchConfiguration) Get(ctx context.Context, profileID string) (*LaunchConfiguration, error) {
req, err := lc.client.NewRequest(ctx, http.MethodGet, autoScalingServiceName, lc.itemPath(profileID), nil)
if err != nil {
return nil, err
}
resp, err := lc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data := &LaunchConfiguration{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
// Force ProfileType = Type
data.ProfileType = data.Type
if data.OperatingSystem.Error != "" {
data.Status = statusError
} else {
data.Status = statusActive
}
return data, nil
}
// Delete a launch configuration
func (lc *launchConfiguration) Delete(ctx context.Context, profileID string) error {
req, err := lc.client.NewRequest(ctx, http.MethodDelete, autoScalingServiceName, lc.itemPath(profileID), nil)
if err != nil {
return err
}
resp, err := lc.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}
// Create a launch configuration
func (lc *launchConfiguration) Create(ctx context.Context, lcr *LaunchConfiguration) (*LaunchConfiguration, error) {
if _, ok := SliceContains(networkPlan, lcr.NetworkPlan); !ok {
return nil, errors.New("UNSUPPORTED network plan")
}
req, err := lc.client.NewRequest(ctx, http.MethodPost, autoScalingServiceName, lc.resourcePath(), &lcr)
if err != nil {
return nil, err
}
resp, err := lc.client.Do(ctx, req)
if err != nil {
return nil, err
}
data := &LaunchConfiguration{}
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return nil, err
}
return data, nil
}