forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
230 lines (194 loc) · 6.27 KB
/
client.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Package virtualmachinedisk provides a client for Virtual Machine Disks.
package virtualmachinedisk
import (
"encoding/xml"
"fmt"
"github.com/Azure/azure-sdk-for-go/management"
)
const (
addDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks"
addDiskURL = "services/disks"
deleteDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks/%d"
deleteDiskURL = "services/disks/%s"
getDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks/%d"
getDiskURL = "services/disks/%s"
listDisksURL = "services/disks"
updateDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks/%d"
updateDiskURL = "services/disks/%s"
errParamNotSpecified = "Parameter %s is not specified."
)
//NewClient is used to instantiate a new DiskClient from an Azure client
func NewClient(client management.Client) DiskClient {
return DiskClient{client: client}
}
// AddDataDisk adds a data disk to a Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157199.aspx
func (c DiskClient) AddDataDisk(
service string,
deployment string,
role string,
params CreateDataDiskParameters) (management.OperationID, error) {
if service == "" {
return "", fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return "", fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return "", fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(addDataDiskURL, service, deployment, role)
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePostRequest(requestURL, req)
}
// AddDisk adds an operating system disk or data disk to the user image repository
//
// https://msdn.microsoft.com/en-us/library/azure/jj157178.aspx
func (c DiskClient) AddDisk(params CreateDiskParameters) (management.OperationID, error) {
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePostRequest(addDiskURL, req)
}
// DeleteDataDisk removes the specified data disk from a Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157179.aspx
func (c DiskClient) DeleteDataDisk(
service string,
deployment string,
role string,
lun int,
deleteVHD bool) (management.OperationID, error) {
if service == "" {
return "", fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return "", fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return "", fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(deleteDataDiskURL, service, deployment, role, lun)
if deleteVHD {
requestURL += "?comp=media"
}
return c.client.SendAzureDeleteRequest(requestURL)
}
// DeleteDisk deletes the specified data or operating system disk from the image
// repository that is associated with the specified subscription
//
// https://msdn.microsoft.com/en-us/library/azure/jj157200.aspx
func (c DiskClient) DeleteDisk(name string, deleteVHD bool) error {
if name == "" {
return fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(deleteDiskURL, name)
if deleteVHD {
requestURL += "?comp=media"
}
_, err := c.client.SendAzureDeleteRequest(requestURL) // request is handled synchronously
return err
}
// GetDataDisk retrieves the specified data disk from a Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157180.aspx
func (c DiskClient) GetDataDisk(
service string,
deployment string,
role string,
lun int) (DataDiskResponse, error) {
var response DataDiskResponse
if service == "" {
return response, fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return response, fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return response, fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(getDataDiskURL, service, deployment, role, lun)
data, err := c.client.SendAzureGetRequest(requestURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
// GetDisk retrieves information about the specified disk
//
// https://msdn.microsoft.com/en-us/library/azure/dn775053.aspx
func (c DiskClient) GetDisk(name string) (DiskResponse, error) {
var response DiskResponse
if name == "" {
return response, fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(getDiskURL, name)
data, err := c.client.SendAzureGetRequest(requestURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
// ListDisks retrieves a list of the disks in the image repository that is associated
// with the specified subscription
//
// https://msdn.microsoft.com/en-us/library/azure/jj157176.aspx
func (c DiskClient) ListDisks() (ListDiskResponse, error) {
var response ListDiskResponse
data, err := c.client.SendAzureGetRequest(listDisksURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
// UpdateDataDisk updates the configuration of the specified data disk that is
// attached to the specified Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157190.aspx
func (c DiskClient) UpdateDataDisk(
service string,
deployment string,
role string,
lun int,
params UpdateDataDiskParameters) (management.OperationID, error) {
if service == "" {
return "", fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return "", fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return "", fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(updateDataDiskURL, service, deployment, role, lun)
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePutRequest(requestURL, "", req)
}
// UpdateDisk updates the label of an existing disk in the image repository that is
// associated with the specified subscription
//
// https://msdn.microsoft.com/en-us/library/azure/jj157205.aspx
func (c DiskClient) UpdateDisk(
name string,
params UpdateDiskParameters) (management.OperationID, error) {
if name == "" {
return "", fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(updateDiskURL, name)
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePutRequest(requestURL, "", req)
}