-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolumes.go
106 lines (92 loc) · 2.74 KB
/
volumes.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
package scaleway
import "fmt"
// VolumesService handles communication with the volumes related
// methods of the Scaleway API.
//
// Scaleway API docs: https://developer.scaleway.com/#volumes-volumes
type VolumesService struct {
client *Client
}
// Volume represents a Scaleway volume.
type Volume struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
ExportURI string `json:"export_uri,omitempty"`
Organization string `json:"organization,omitempty"`
//Server string `json:"server,omitempty"`
Size uint64 `json:"size,omitempty"`
Type string `json:"volume_type,omitempty"`
}
// VolumeRequest represents a request to create a volume.
type VolumeRequest struct {
Name string `json:"name"`
Organization string `json:"organization"`
Type string `json:"volume_type"`
Size int `json:"size"`
}
// volumeResponse represents a Scaleway volume creation response.
type volumeResponse struct {
Volume *Volume `json:"volume"`
}
// volumeListResponse represents a Scaleway volume list response.
type volumeListResponse struct {
Volumes []*Volume `json:"volumes"`
}
// Create volume corresponding as data storage for your server.
func (s *VolumesService) Create(vr *VolumeRequest) (*Volume, *Response, error) {
u := fmt.Sprintf("/volumes")
req, err := s.client.NewRequestCompute("POST", u, vr)
if err != nil {
return nil, nil, err
}
volume := new(volumeResponse)
resp, err := s.client.Do(req, volume)
if err != nil {
return nil, nil, err
}
return volume.Volume, resp, nil
}
// List returns a list of all volumes associate to your account.
func (s *VolumesService) List() ([]*Volume, *Response, error) {
return s.listVolumes()
}
func (s *VolumesService) listVolumes() ([]*Volume, *Response, error) {
u := fmt.Sprintf("/volumes")
req, err := s.client.NewRequestCompute("GET", u, nil)
if err != nil {
return nil, nil, err
}
volumes := new(volumeListResponse)
resp, err := s.client.Do(req, volumes)
if err != nil {
return nil, nil, err
}
return volumes.Volumes, resp, nil
}
// Get returns info for a specific volume.
func (s *VolumesService) Get(id string) (*Volume, *Response, error) {
u := fmt.Sprintf("/volumes/%s", id)
req, err := s.client.NewRequestCompute("GET", u, nil)
if err != nil {
return nil, nil, err
}
volume := new(volumeResponse)
resp, err := s.client.Do(req, volume)
if err != nil {
return nil, nil, err
}
return volume.Volume, resp, nil
}
// Delete deletes a volume.
func (s *VolumesService) Delete(id string) (*Response, error) {
u := fmt.Sprintf("/volumes/%s", id)
req, err := s.client.NewRequestCompute("DELETE", u, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, err
}
return resp, nil
}