forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
606 lines (518 loc) · 23.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// +build go1.7
// Package virtualmachine provides a client for Virtual Machines.
package virtualmachine
// Copyright 2017 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"encoding/xml"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/classic/management"
)
const (
azureDeploymentListURL = "services/hostedservices/%s/deployments"
azureDeploymentURL = "services/hostedservices/%s/deployments/%s"
azureUpdateDeploymentURL = "services/hostedservices/%s/deployments/%s?comp=%s"
azureDeploymentSlotSwapURL = "services/hostedservices/%s"
azureDeploymentSlotURL = "services/hostedservices/%s/deploymentslots/%s"
azureUpdateDeploymentSlotConfigurationURL = "services/hostedservices/%s/deploymentslots/%s?comp=%s"
deleteAzureDeploymentURL = "services/hostedservices/%s/deployments/%s?comp=media"
azureDeleteDeploymentBySlotURL = "services/hostedservices/%s/deploymentslots/%s"
azureAddRoleURL = "services/hostedservices/%s/deployments/%s/roles"
azureRoleURL = "services/hostedservices/%s/deployments/%s/roles/%s"
azureOperationsURL = "services/hostedservices/%s/deployments/%s/roleinstances/%s/Operations"
azureRoleSizeListURL = "rolesizes"
errParamNotSpecified = "Parameter %s is not specified."
)
//NewClient is used to instantiate a new VirtualMachineClient from an Azure client
func NewClient(client management.Client) VirtualMachineClient {
return VirtualMachineClient{client: client}
}
// CreateDeploymentOptions can be used to create a customized deployement request
type CreateDeploymentOptions struct {
DNSServers []DNSServer
LoadBalancers []LoadBalancer
ReservedIPName string
VirtualNetworkName string
}
// CreateDeployment creates a deployment and then creates a virtual machine
// in the deployment based on the specified configuration.
//
// https://msdn.microsoft.com/en-us/library/azure/jj157194.aspx
func (vm VirtualMachineClient) CreateDeployment(
role Role,
cloudServiceName string,
options CreateDeploymentOptions) (management.OperationID, error) {
req := DeploymentRequest{
Name: role.RoleName,
DeploymentSlot: "Production",
Label: role.RoleName,
RoleList: []Role{role},
DNSServers: options.DNSServers,
LoadBalancers: options.LoadBalancers,
ReservedIPName: options.ReservedIPName,
VirtualNetworkName: options.VirtualNetworkName,
}
data, err := xml.Marshal(req)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureDeploymentListURL, cloudServiceName)
return vm.client.SendAzurePostRequest(requestURL, data)
}
// CreateDeploymentFromPackageOptions can be used to create a customized deployement request
type CreateDeploymentFromPackageOptions struct {
Name string
PackageURL string
Label string
Configuration string
StartDeployment bool
TreatWarningsAsError bool
ExtendedProperties []ExtendedProperty
ExtensionConfiguration ExtensionConfiguration
}
// CreateDeploymentRequest is the type for creating a deployment of a cloud service package
// in the deployment based on the specified configuration. See
// https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-create-deployment
type CreateDeploymentRequest struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure CreateDeployment"`
// Required parameters:
Name string `` // Specifies the name of the deployment.
PackageURL string `xml:"PackageUrl"` // Specifies a URL that refers to the location of the service package in the Blob service. The service package can be located either in a storage account beneath the same subscription or a Shared Access Signature (SAS) URI from any storage account.
Label string `` // Specifies an identifier for the deployment that is base-64 encoded. The identifier can be up to 100 characters in length. It is recommended that the label be unique within the subscription. The label can be used for your tracking purposes.
Configuration string `` // Specifies the base-64 encoded service configuration file for the deployment.
// Optional parameters:
StartDeployment bool `` // Indicates whether to start the deployment immediately after it is created. The default value is false
TreatWarningsAsError bool `` // Indicates whether to treat package validation warnings as errors. The default value is false. If set to true, the Created Deployment operation fails if there are validation warnings on the service package.
ExtendedProperties []ExtendedProperty `xml:">ExtendedProperty,omitempty"` // Array of ExtendedProprties. Each extended property must have both a defined name and value. You can have a maximum of 25 extended property name and value pairs.
ExtensionConfiguration ExtensionConfiguration `xml:",omitempty"`
}
// CreateDeploymentFromPackage creates a deployment from a cloud services package (.cspkg) and configuration file (.cscfg)
func (vm VirtualMachineClient) CreateDeploymentFromPackage(
cloudServiceName string,
deploymentSlot DeploymentSlot,
options CreateDeploymentFromPackageOptions) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
req := CreateDeploymentRequest{
Name: options.Name,
Label: options.Label,
Configuration: options.Configuration,
PackageURL: options.PackageURL,
StartDeployment: options.StartDeployment,
TreatWarningsAsError: options.TreatWarningsAsError,
ExtendedProperties: options.ExtendedProperties,
ExtensionConfiguration: options.ExtensionConfiguration,
}
data, err := xml.Marshal(req)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureDeploymentSlotURL, cloudServiceName, deploymentSlot)
return vm.client.SendAzurePostRequest(requestURL, data)
}
// SwapDeploymentRequest is the type used for specifying information to swap the deployments in
// a cloud service
// https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-swap-deployment
type SwapDeploymentRequest struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Swap"`
// Required parameters:
Production string
SourceDeployment string
}
// SwapDeployment initiates a virtual IP address swap between the staging and production deployment environments for a service.
// If the service is currently running in the staging environment, it will be swapped to the production environment.
// If it is running in the production environment, it will be swapped to staging.
func (vm VirtualMachineClient) SwapDeployment(
cloudServiceName string) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
productionDeploymentName, err := vm.GetDeploymentNameForSlot(cloudServiceName, DeploymentSlotProduction)
if err != nil {
return "", err
}
stagingDeploymentName, err := vm.GetDeploymentNameForSlot(cloudServiceName, DeploymentSlotStaging)
if err != nil {
return "", err
}
req := SwapDeploymentRequest{
Production: productionDeploymentName,
SourceDeployment: stagingDeploymentName,
}
data, err := xml.Marshal(req)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureDeploymentSlotSwapURL, cloudServiceName)
return vm.client.SendAzurePostRequest(requestURL, data)
}
// ChangeDeploymentConfigurationRequestOptions can be used to update configuration of a deployment
type ChangeDeploymentConfigurationRequestOptions struct {
Mode UpgradeType
Configuration string
TreatWarningsAsError bool
ExtendedProperties []ExtendedProperty
ExtensionConfiguration ExtensionConfiguration
}
// ChangeDeploymentConfigurationRequest is the type for changing the configuration of a deployment of a cloud service p
// https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-change-deployment-configuration
type ChangeDeploymentConfigurationRequest struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure ChangeConfiguration"`
// Required parameters:
Configuration string `` // Specifies the base-64 encoded service configuration file for the deployment.
// Optional parameters:
Mode UpgradeType `` // Specifies the type of Upgrade (Auto | Manual | Simultaneous) .
TreatWarningsAsError bool `` // Indicates whether to treat package validation warnings as errors. The default value is false. If set to true, the Created Deployment operation fails if there are validation warnings on the service package.
ExtendedProperties []ExtendedProperty `xml:">ExtendedProperty,omitempty"` // Array of ExtendedProprties. Each extended property must have both a defined name and value. You can have a maximum of 25 extended property name and value pairs.
ExtensionConfiguration ExtensionConfiguration `xml:",omitempty"`
}
// ChangeDeploymentConfiguration updates the configuration for a deployment from a configuration file (.cscfg)
func (vm VirtualMachineClient) ChangeDeploymentConfiguration(
cloudServiceName string,
deploymentSlot DeploymentSlot,
options ChangeDeploymentConfigurationRequestOptions) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
req := ChangeDeploymentConfigurationRequest{
Mode: options.Mode,
Configuration: options.Configuration,
TreatWarningsAsError: options.TreatWarningsAsError,
ExtendedProperties: options.ExtendedProperties,
ExtensionConfiguration: options.ExtensionConfiguration,
}
if req.Mode == "" {
req.Mode = UpgradeTypeAuto
}
data, err := xml.Marshal(req)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureUpdateDeploymentSlotConfigurationURL, cloudServiceName, deploymentSlot, "config")
return vm.client.SendAzurePostRequest(requestURL, data)
}
// UpdateDeploymentStatusRequest is the type used to make UpdateDeploymentStatus requests
type UpdateDeploymentStatusRequest struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure UpdateDeploymentStatus"`
// Required parameters:
Status string
}
// UpdateDeploymentStatus changes the running status of a deployment. The status of a deployment can be running or suspended.
// https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-update-deployment-status
func (vm VirtualMachineClient) UpdateDeploymentStatus(
cloudServiceName string,
deploymentSlot DeploymentSlot,
status string) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if status != "Running" && status != "Suspended" {
return "", fmt.Errorf("Invalid status provided")
}
req := UpdateDeploymentStatusRequest{
Status: status,
}
data, err := xml.Marshal(req)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureUpdateDeploymentSlotConfigurationURL, cloudServiceName, deploymentSlot, "status")
return vm.client.SendAzurePostRequest(requestURL, data)
}
// UpdateDeploymentStatusByName changes the running status of a deployment. The status of a deployment can be running or suspended.
// https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-update-deployment-status
func (vm VirtualMachineClient) UpdateDeploymentStatusByName(
cloudServiceName string,
deploymentName string,
status string) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if status != "Running" && status != "Suspended" {
return "", fmt.Errorf("Invalid status provided")
}
req := UpdateDeploymentStatusRequest{
Status: status,
}
data, err := xml.Marshal(req)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureUpdateDeploymentURL, cloudServiceName, deploymentName, "status")
return vm.client.SendAzurePostRequest(requestURL, data)
}
// GetDeploymentName queries an existing Azure cloud service for the name of the Deployment,
// if any, in its 'Production' slot (the only slot possible). If none exists, it returns empty
// string but no error
//
//https://msdn.microsoft.com/en-us/library/azure/ee460804.aspx
func (vm VirtualMachineClient) GetDeploymentName(cloudServiceName string) (string, error) {
return vm.GetDeploymentNameForSlot(cloudServiceName, DeploymentSlotProduction)
}
// GetDeploymentNameForSlot queries an existing Azure cloud service for the name of the Deployment,
// in a given slot. If none exists, it returns empty
// string but no error
//
//https://msdn.microsoft.com/en-us/library/azure/ee460804.aspx
func (vm VirtualMachineClient) GetDeploymentNameForSlot(cloudServiceName string, deploymentSlot DeploymentSlot) (string, error) {
var deployment DeploymentResponse
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
requestURL := fmt.Sprintf(azureDeploymentSlotURL, cloudServiceName, deploymentSlot)
response, err := vm.client.SendAzureGetRequest(requestURL)
if err != nil {
if management.IsResourceNotFoundError(err) {
return "", nil
}
return "", err
}
err = xml.Unmarshal(response, &deployment)
if err != nil {
return "", err
}
return deployment.Name, nil
}
func (vm VirtualMachineClient) GetDeployment(cloudServiceName, deploymentName string) (DeploymentResponse, error) {
var deployment DeploymentResponse
if cloudServiceName == "" {
return deployment, fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return deployment, fmt.Errorf(errParamNotSpecified, "deploymentName")
}
requestURL := fmt.Sprintf(azureDeploymentURL, cloudServiceName, deploymentName)
response, azureErr := vm.client.SendAzureGetRequest(requestURL)
if azureErr != nil {
return deployment, azureErr
}
err := xml.Unmarshal(response, &deployment)
return deployment, err
}
// GetDeploymentBySlot used to retrieve deployment events for a single deployment slot (staging or production)
func (vm VirtualMachineClient) GetDeploymentBySlot(cloudServiceName string, deploymentSlot DeploymentSlot) (DeploymentResponse, error) {
var deployment DeploymentResponse
if cloudServiceName == "" {
return deployment, fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentSlot == "" {
return deployment, fmt.Errorf(errParamNotSpecified, "deploymentSlot")
}
requestURL := fmt.Sprintf(azureDeploymentSlotURL, cloudServiceName, deploymentSlot)
response, azureErr := vm.client.SendAzureGetRequest(requestURL)
if azureErr != nil {
return deployment, azureErr
}
err := xml.Unmarshal(response, &deployment)
return deployment, err
}
func (vm VirtualMachineClient) DeleteDeployment(cloudServiceName, deploymentName string) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
requestURL := fmt.Sprintf(deleteAzureDeploymentURL, cloudServiceName, deploymentName)
return vm.client.SendAzureDeleteRequest(requestURL)
}
func (vm VirtualMachineClient) DeleteDeploymentBySlot(cloudServiceName string, deploymentSlot DeploymentSlot) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentSlot == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentSlot")
}
requestURL := fmt.Sprintf(azureDeleteDeploymentBySlotURL, cloudServiceName, deploymentSlot)
return vm.client.SendAzureDeleteRequest(requestURL)
}
func (vm VirtualMachineClient) GetRole(cloudServiceName, deploymentName, roleName string) (*Role, error) {
if cloudServiceName == "" {
return nil, fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return nil, fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return nil, fmt.Errorf(errParamNotSpecified, "roleName")
}
role := new(Role)
requestURL := fmt.Sprintf(azureRoleURL, cloudServiceName, deploymentName, roleName)
response, azureErr := vm.client.SendAzureGetRequest(requestURL)
if azureErr != nil {
return nil, azureErr
}
err := xml.Unmarshal(response, role)
if err != nil {
return nil, err
}
return role, nil
}
// AddRole adds a Virtual Machine to a deployment of Virtual Machines, where role name = VM name
// See https://msdn.microsoft.com/en-us/library/azure/jj157186.aspx
func (vm VirtualMachineClient) AddRole(cloudServiceName string, deploymentName string, role Role) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
data, err := xml.Marshal(PersistentVMRole{Role: role})
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureAddRoleURL, cloudServiceName, deploymentName)
return vm.client.SendAzurePostRequest(requestURL, data)
}
// UpdateRole updates the configuration of the specified virtual machine
// See https://msdn.microsoft.com/en-us/library/azure/jj157187.aspx
func (vm VirtualMachineClient) UpdateRole(cloudServiceName, deploymentName, roleName string, role Role) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return "", fmt.Errorf(errParamNotSpecified, "roleName")
}
data, err := xml.Marshal(PersistentVMRole{Role: role})
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureRoleURL, cloudServiceName, deploymentName, roleName)
return vm.client.SendAzurePutRequest(requestURL, "text/xml", data)
}
func (vm VirtualMachineClient) StartRole(cloudServiceName, deploymentName, roleName string) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return "", fmt.Errorf(errParamNotSpecified, "roleName")
}
startRoleOperationBytes, err := xml.Marshal(StartRoleOperation{
OperationType: "StartRoleOperation",
})
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureOperationsURL, cloudServiceName, deploymentName, roleName)
return vm.client.SendAzurePostRequest(requestURL, startRoleOperationBytes)
}
func (vm VirtualMachineClient) ShutdownRole(cloudServiceName, deploymentName, roleName string, postaction PostShutdownAction) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return "", fmt.Errorf(errParamNotSpecified, "roleName")
}
shutdownRoleOperationBytes, err := xml.Marshal(ShutdownRoleOperation{
OperationType: "ShutdownRoleOperation",
PostShutdownAction: postaction,
})
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureOperationsURL, cloudServiceName, deploymentName, roleName)
return vm.client.SendAzurePostRequest(requestURL, shutdownRoleOperationBytes)
}
func (vm VirtualMachineClient) RestartRole(cloudServiceName, deploymentName, roleName string) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return "", fmt.Errorf(errParamNotSpecified, "roleName")
}
restartRoleOperationBytes, err := xml.Marshal(RestartRoleOperation{
OperationType: "RestartRoleOperation",
})
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureOperationsURL, cloudServiceName, deploymentName, roleName)
return vm.client.SendAzurePostRequest(requestURL, restartRoleOperationBytes)
}
func (vm VirtualMachineClient) DeleteRole(cloudServiceName, deploymentName, roleName string, deleteVHD bool) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return "", fmt.Errorf(errParamNotSpecified, "roleName")
}
requestURL := fmt.Sprintf(azureRoleURL, cloudServiceName, deploymentName, roleName)
if deleteVHD {
requestURL += "?comp=media"
}
return vm.client.SendAzureDeleteRequest(requestURL)
}
func (vm VirtualMachineClient) GetRoleSizeList() (RoleSizeList, error) {
roleSizeList := RoleSizeList{}
response, err := vm.client.SendAzureGetRequest(azureRoleSizeListURL)
if err != nil {
return roleSizeList, err
}
err = xml.Unmarshal(response, &roleSizeList)
return roleSizeList, err
}
// CaptureRole captures a VM role. If reprovisioningConfigurationSet is non-nil,
// the VM role is redeployed after capturing the image, otherwise, the original
// VM role is deleted.
//
// NOTE: an image resulting from this operation shows up in
// osimage.GetImageList() as images with Category "User".
func (vm VirtualMachineClient) CaptureRole(cloudServiceName, deploymentName, roleName, imageName, imageLabel string,
reprovisioningConfigurationSet *ConfigurationSet) (management.OperationID, error) {
if cloudServiceName == "" {
return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
}
if deploymentName == "" {
return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
}
if roleName == "" {
return "", fmt.Errorf(errParamNotSpecified, "roleName")
}
if reprovisioningConfigurationSet != nil &&
!(reprovisioningConfigurationSet.ConfigurationSetType == ConfigurationSetTypeLinuxProvisioning ||
reprovisioningConfigurationSet.ConfigurationSetType == ConfigurationSetTypeWindowsProvisioning) {
return "", fmt.Errorf("ConfigurationSet type can only be WindowsProvisioningConfiguration or LinuxProvisioningConfiguration")
}
operation := CaptureRoleOperation{
OperationType: "CaptureRoleOperation",
PostCaptureAction: PostCaptureActionReprovision,
ProvisioningConfiguration: reprovisioningConfigurationSet,
TargetImageLabel: imageLabel,
TargetImageName: imageName,
}
if reprovisioningConfigurationSet == nil {
operation.PostCaptureAction = PostCaptureActionDelete
}
data, err := xml.Marshal(operation)
if err != nil {
return "", err
}
return vm.client.SendAzurePostRequest(fmt.Sprintf(azureOperationsURL, cloudServiceName, deploymentName, roleName), data)
}