-
Notifications
You must be signed in to change notification settings - Fork 5
/
blockstorages.go
151 lines (132 loc) · 4.05 KB
/
blockstorages.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
package oneandone
import (
"net/http"
"time"
)
type BlockStorageRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Size *int `json:"size"`
ServerId string `json:"server,omitempty"`
DatacenterId string `json:"datacenter_id,omitempty"`
ExecutionGroup string `json:"execution_group,omitempty"`
}
type BlockStorage struct {
Identity
descField
Size int `json:"size"`
State string `json:"state,omitempty"`
// Name string `json:"name,omitempty"`
CreationDate time.Time `json:"creation_date,omitempty"`
Datacenter *Datacenter `json:"datacenter,omitempty"`
Server *BlockStorageServer `json:"server,omitempty"`
DiskID string `json:"disk_id,omitemtpy"`
UUID string `json:"uuid,omitemtpy"`
ApiPtr
}
type BlockStorageServer struct {
Id string `json:"id,omitempty"`
ServerId string `json:"server,omitempty"`
Name string `json:"name,omitempty"`
}
type UpdateBlockStorageRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
func (api *API) ListBlockStorages(args ...interface{}) ([]BlockStorage, error) {
url, err := processQueryParams(createUrl(api, blockStoragePathSegment), args...)
if err != nil {
return nil, err
}
result := []BlockStorage{}
err = api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
for index := range result {
result[index].api = api
}
return result, nil
}
func (api *API) GetBlockStorage(id string) (*BlockStorage, error) {
result := new(BlockStorage)
url := createUrl(api, blockStoragePathSegment, id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
func (api *API) GetBlockStorageServer(id string) (*BlockStorageServer, error) {
result := new(BlockStorageServer)
url := createUrl(api, blockStoragePathSegment, id, "server")
err := api.Client.Get(url, &result, http.StatusCreated)
if err != nil {
return nil, err
}
return result, nil
}
func (api *API) AddBlockStorageServer(blockStorageId string, serverId string) (*BlockStorage, error) {
result := new(BlockStorage)
req := BlockStorageServer{ServerId: serverId}
url := createUrl(api, blockStoragePathSegment, blockStorageId, "server")
err := api.Client.Post(url, &req, &result, http.StatusCreated)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
func (api *API) RemoveBlockStorageServer(blockStorageId string, serverId string) (*BlockStorage, error) {
result := new(BlockStorage)
blockStorage, err := api.GetBlockStorage(blockStorageId)
if err != nil {
return nil, err
}
req := BlockStorageServer{ServerId: serverId}
url := createUrl(api, blockStoragePathSegment, blockStorage.Id, "server")
err = api.Client.Delete(url, &req, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
func (api *API) CreateBlockStorage(request *BlockStorageRequest) (string, *BlockStorage, error) {
result := new(BlockStorage)
url := createUrl(api, blockStoragePathSegment)
err := api.Client.Post(url, request, &result, http.StatusCreated)
if err != nil {
return "", nil, err
}
result.api = api
return result.Id, result, nil
}
func (bs *BlockStorage) GetState() (string, error) {
in, err := bs.api.GetBlockStorage(bs.Id)
if in == nil {
return "", err
}
return in.State, err
}
func (api *API) DeleteBlockStorage(id string) (*BlockStorage, error) {
result := new(BlockStorage)
url := createUrl(api, blockStoragePathSegment, id)
err := api.Client.Delete(url, nil, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
func (api *API) UpdateBlockStorage(id string, request *UpdateBlockStorageRequest) (*BlockStorage, error) {
result := new(BlockStorage)
url := createUrl(api, blockStoragePathSegment, id)
err := api.Client.Put(url, &request, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}