This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvolume.go
183 lines (149 loc) · 4.73 KB
/
volume.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Volume represents a Volume
type Volume struct {
// Identifier is a unique identifier for the volume
Identifier string `json:"id,omitempty"`
// Size is the allocated size of the volume
Size uint64 `json:"size,omitempty"`
// CreationDate is the creation date of the volume
CreationDate string `json:"creation_date,omitempty"`
// ModificationDate is the date of the last modification of the volume
ModificationDate string `json:"modification_date,omitempty"`
// Organization is the organization owning the volume
Organization string `json:"organization,omitempty"`
// Name is the name of the volume
Name string `json:"name,omitempty"`
// Server is the server using this image
Server *struct {
Identifier string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"server,omitempty"`
// VolumeType is a identifier for the kind of volume (default: l_ssd)
VolumeType string `json:"volume_type,omitempty"`
// ExportURI represents the url used by initrd/scripts to attach the volume
ExportURI string `json:"export_uri,omitempty"`
}
type volumeResponse struct {
Volume Volume `json:"volume,omitempty"`
}
// VolumeDefinition represents a volume definition
type VolumeDefinition struct {
// Name is the user-defined name of the volume
Name string `json:"name"`
// Image is the image used by the volume
Size uint64 `json:"size"`
// Bootscript is the bootscript used by the volume
Type string `json:"volume_type"`
// Organization is the owner of the volume
Organization string `json:"organization"`
}
// VolumePutDefinition represents a volume with nullable fields (for PUT)
type VolumePutDefinition struct {
Identifier *string `json:"id,omitempty"`
Size *uint64 `json:"size,omitempty"`
CreationDate *string `json:"creation_date,omitempty"`
ModificationDate *string `json:"modification_date,omitempty"`
Organization *string `json:"organization,omitempty"`
Name *string `json:"name,omitempty"`
Server struct {
Identifier *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
} `json:"server,omitempty"`
VolumeType *string `json:"volume_type,omitempty"`
ExportURI *string `json:"export_uri,omitempty"`
}
// CreateVolume creates a new volume
func (s *API) CreateVolume(definition VolumeDefinition) (*Volume, error) {
definition.Organization = s.Organization
if definition.Type == "" {
definition.Type = "l_ssd"
}
resp, err := s.PostResponse(s.computeAPI, "volumes", definition)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusCreated}, resp)
if err != nil {
return nil, err
}
var volume volumeResponse
if err = json.Unmarshal(body, &volume); err != nil {
return nil, err
}
return &volume.Volume, nil
}
// UpdateVolume updates a volume
func (s *API) UpdateVolume(volumeID string, definition VolumePutDefinition) (*Volume, error) {
resp, err := s.PutResponse(s.computeAPI, fmt.Sprintf("volumes/%s", volumeID), definition)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var volume volumeResponse
if err = json.Unmarshal(body, &volume); err != nil {
return nil, err
}
return &volume.Volume, nil
}
// DeleteVolume deletes a volume
func (s *API) DeleteVolume(volumeID string) error {
resp, err := s.DeleteResponse(s.computeAPI, fmt.Sprintf("volumes/%s", volumeID))
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := s.handleHTTPError([]int{http.StatusNoContent}, resp); err != nil {
return err
}
return nil
}
type volumesResponse struct {
Volumes []Volume `json:"volumes,omitempty"`
}
// GetVolumes gets the list of volumes from the API
func (s *API) GetVolumes() (*[]Volume, error) {
query := url.Values{}
resp, err := s.GetResponsePaginate(s.computeAPI, "volumes", query)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var volumes volumesResponse
if err = json.Unmarshal(body, &volumes); err != nil {
return nil, err
}
return &volumes.Volumes, nil
}
// GetVolume gets a volume from the API
func (s *API) GetVolume(volumeID string) (*Volume, error) {
resp, err := s.GetResponsePaginate(s.computeAPI, "volumes/"+volumeID, url.Values{})
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var volume volumeResponse
if err = json.Unmarshal(body, &volume); err != nil {
return nil, err
}
// FIXME region, arch, owner, title
return &volume.Volume, nil
}