-
Notifications
You must be signed in to change notification settings - Fork 48
/
Request.go
184 lines (151 loc) · 5.12 KB
/
Request.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
package snapshot
import (
"errors"
"fmt"
cblog "github.com/cloud-barista/cb-log"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/cloudit/client"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/cloudit/client/ace/server"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"github.com/sirupsen/logrus"
"strings"
"time"
)
var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("CB-SPIDER")
}
type SnapshotReqInfo struct {
Name string `json:"name,omitempty"`
VolumeId string `json:"volumeId,omitempty"`
}
type SnapshotInfo struct {
Name string
Id string
ServerName string
State string
CreatedAt string
// miscellaneous properties
Bootable string
Creator string
TemplateId string
}
func ToIRSMyImage(restClient *client.RestClient, associatedSnapshots *[]SnapshotInfo) (irs.MyImageInfo, error) {
var result irs.MyImageInfo
isAvailable := true
result.Status = irs.MyImageAvailable
result.SourceVM.SystemId = ""
for _, snapshot := range *associatedSnapshots {
if snapshot.Bootable == "yes" {
result.IId.NameId = strings.Split(snapshot.Name, "-dev-")[0]
result.IId.SystemId = snapshot.Id
result.SourceVM.NameId = snapshot.ServerName
if snapshot.CreatedAt != "" {
timeArr := strings.Split(snapshot.CreatedAt, " ")
timeFormatStr := fmt.Sprintf("%sT%sZ", timeArr[0], timeArr[1])
if createTime, err := time.Parse(time.RFC3339, timeFormatStr); err == nil {
result.CreatedTime = createTime
}
}
}
if isAvailable && getSnapshotStatus(snapshot.State) == irs.MyImageUnavailable {
isAvailable = false
result.Status = irs.MyImageUnavailable
}
}
requestOpts := client.RequestOpts{
MoreHeaders: restClient.AuthenticatedHeaders(),
}
serverList, err := server.List(restClient, &requestOpts)
if err != nil {
return irs.MyImageInfo{}, err
}
for _, server := range *serverList {
if server.Name == result.SourceVM.NameId {
result.SourceVM.SystemId = server.ID
break
}
}
if result.SourceVM.NameId == "" {
result.SourceVM.NameId = "Deleted"
}
if result.SourceVM.SystemId == "" {
result.SourceVM.SystemId = "Deleted"
}
return result, nil
}
func List(restClient *client.RestClient, requestOpts *client.RequestOpts) (*[]SnapshotInfo, error) {
requestURL := restClient.CreateRequestBaseURL(client.ACE, "snapshots")
cblogger.Info(requestURL)
var result client.Result
if _, result.Err = restClient.Get(requestURL, &result.Body, requestOpts); result.Err != nil {
return nil, result.Err
}
var snapshotList []SnapshotInfo
if err := result.ExtractInto(&snapshotList); err != nil {
return nil, err
}
return &snapshotList, nil
}
func Get(restClient *client.RestClient, snapshotId string, requestOpts *client.RequestOpts) (SnapshotInfo, error) {
requestURL := restClient.CreateRequestBaseURL(client.ACE, "snapshots")
cblogger.Info(requestURL)
var result client.Result
if _, result.Err = restClient.Get(requestURL, &result.Body, requestOpts); result.Err != nil {
return SnapshotInfo{}, result.Err
}
var snapshotList []SnapshotInfo
if err := result.ExtractInto(&snapshotList); err != nil {
return SnapshotInfo{}, err
}
for _, snapshot := range snapshotList {
if snapshot.Id == snapshotId {
return snapshot, nil
}
}
return SnapshotInfo{}, errors.New("Snapshot not found")
}
//func GetSnapshotsByMyImage(restClient *client.RestClient, myImageNameId string, requestOpts *client.RequestOpts) (SnapshotInfo, error) {
//
//}
func CreateSnapshot(restClient *client.RestClient, requestOpts *client.RequestOpts) (SnapshotInfo, error) {
requestURL := restClient.CreateRequestBaseURL(client.ACE, "snapshots")
cblogger.Info(requestURL)
var result client.Result
if _, result.Err = restClient.Post(requestURL, nil, &result.Body, requestOpts); result.Err != nil {
return SnapshotInfo{}, result.Err
}
var snapshot SnapshotInfo
if err := result.ExtractInto(&snapshot); err != nil {
return SnapshotInfo{}, err
}
return snapshot, nil
}
func DeleteSnapshot(restClient *client.RestClient, snapshotId string, requestOpts *client.RequestOpts) (bool, error) {
requestURL := restClient.CreateRequestBaseURL(client.ACE, "snapshots", snapshotId)
cblogger.Info(requestURL)
var result client.Result
if _, result.Err = restClient.Delete(requestURL, requestOpts); result.Err != nil {
return false, result.Err
}
return true, nil
}
func CreateVolumeBySnapshot(restClient *client.RestClient, snapshotId string, requestOpts *client.RequestOpts) (bool, error) {
requestURL := restClient.CreateRequestBaseURL(client.ACE, "snapshots", snapshotId, "volume")
cblogger.Info(requestURL)
var result client.Result
restClient.Post(requestURL, nil, &result.Body, requestOpts)
return true, nil
}
func getSnapshotStatus(snapshotStatus string) irs.MyImageStatus {
var resultStatus string
switch strings.ToLower(snapshotStatus) {
case "completed":
resultStatus = "Available"
case "creating", "deleting", "converting", "failed":
resultStatus = "Unavailable"
default:
resultStatus = "Unavailable"
}
return irs.MyImageStatus(resultStatus)
}