forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_storage.go
186 lines (152 loc) · 5.84 KB
/
block_storage.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
185
186
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// BlockStorageService is the interface to interact with Block-Storage endpoint on the Vultr API
// Link : https://www.vultr.com/api/#tag/block
type BlockStorageService interface {
Create(ctx context.Context, blockReq *BlockStorageCreate) (*BlockStorage, *http.Response, error)
Get(ctx context.Context, blockID string) (*BlockStorage, *http.Response, error)
Update(ctx context.Context, blockID string, blockReq *BlockStorageUpdate) error
Delete(ctx context.Context, blockID string) error
List(ctx context.Context, options *ListOptions) ([]BlockStorage, *Meta, *http.Response, error)
Attach(ctx context.Context, blockID string, attach *BlockStorageAttach) error
Detach(ctx context.Context, blockID string, detach *BlockStorageDetach) error
}
// BlockStorageServiceHandler handles interaction with the block-storage methods for the Vultr API
type BlockStorageServiceHandler struct {
client *Client
}
// BlockStorage represents Vultr Block-Storage
type BlockStorage struct {
ID string `json:"id"`
Cost float32 `json:"cost"`
Status string `json:"status"`
SizeGB int `json:"size_gb"`
Region string `json:"region"`
DateCreated string `json:"date_created"`
AttachedToInstance string `json:"attached_to_instance"`
Label string `json:"label"`
MountID string `json:"mount_id"`
BlockType string `json:"block_type"`
}
// BlockStorageCreate struct is used for creating Block Storage.
type BlockStorageCreate struct {
Region string `json:"region"`
SizeGB int `json:"size_gb"`
Label string `json:"label,omitempty"`
BlockType string `json:"block_type,omitempty"`
}
// BlockStorageUpdate struct is used to update Block Storage.
type BlockStorageUpdate struct {
SizeGB int `json:"size_gb,omitempty"`
Label string `json:"label,omitempty"`
}
// BlockStorageAttach struct used to define if a attach should be restart the instance.
type BlockStorageAttach struct {
InstanceID string `json:"instance_id"`
Live *bool `json:"live,omitempty"`
}
// BlockStorageDetach struct used to define if a detach should be restart the instance.
type BlockStorageDetach struct {
Live *bool `json:"live,omitempty"`
}
type blockStoragesBase struct {
Blocks []BlockStorage `json:"blocks"`
Meta *Meta `json:"meta"`
}
type blockStorageBase struct {
Block *BlockStorage `json:"block"`
}
// Create builds out a block storage
func (b *BlockStorageServiceHandler) Create(ctx context.Context, blockReq *BlockStorageCreate) (*BlockStorage, *http.Response, error) {
uri := "/v2/blocks"
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, blockReq)
if err != nil {
return nil, nil, err
}
block := new(blockStorageBase)
resp, err := b.client.DoWithContext(ctx, req, block)
if err != nil {
return nil, resp, err
}
return block.Block, resp, nil
}
// Get returns a single block storage instance based ony our blockID you provide from your Vultr Account
func (b *BlockStorageServiceHandler) Get(ctx context.Context, blockID string) (*BlockStorage, *http.Response, error) {
uri := fmt.Sprintf("/v2/blocks/%s", blockID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
block := new(blockStorageBase)
resp, err := b.client.DoWithContext(ctx, req, block)
if err != nil {
return nil, resp, err
}
return block.Block, resp, nil
}
// Update a block storage subscription.
func (b *BlockStorageServiceHandler) Update(ctx context.Context, blockID string, blockReq *BlockStorageUpdate) error {
uri := fmt.Sprintf("/v2/blocks/%s", blockID)
req, err := b.client.NewRequest(ctx, http.MethodPatch, uri, blockReq)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// Delete a block storage subscription from your Vultr account
func (b *BlockStorageServiceHandler) Delete(ctx context.Context, blockID string) error {
uri := fmt.Sprintf("/v2/blocks/%s", blockID)
req, err := b.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// List returns a list of all block storage instances on your Vultr Account
func (b *BlockStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]BlockStorage, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/blocks"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
blocks := new(blockStoragesBase)
resp, err := b.client.DoWithContext(ctx, req, blocks)
if err != nil {
return nil, nil, resp, err
}
return blocks.Blocks, blocks.Meta, resp, nil
}
// Attach will link a given block storage to a given Vultr instance
// If Live is set to true the block storage will be attached without reloading the instance
func (b *BlockStorageServiceHandler) Attach(ctx context.Context, blockID string, attach *BlockStorageAttach) error {
uri := fmt.Sprintf("/v2/blocks/%s/attach", blockID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, attach)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// Detach will de-link a given block storage to the Vultr instance it is attached to
// If Live is set to true the block storage will be detached without reloading the instance
func (b *BlockStorageServiceHandler) Detach(ctx context.Context, blockID string, detach *BlockStorageDetach) error {
uri := fmt.Sprintf("/v2/blocks/%s/detach", blockID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, detach)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}