File: client.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 2.1.1~beta-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,596 kB
  • ctags: 7,237
  • sloc: makefile: 4
file content (110 lines) | stat: -rw-r--r-- 3,098 bytes parent folder | download | duplicates (2)
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
// Package virtualmachineimage provides a client for Virtual Machine Images.
package virtualmachineimage

import (
	"encoding/xml"
	"fmt"
	"net/url"

	"github.com/Azure/azure-sdk-for-go/management"
)

const (
	azureImageListURL         = "services/vmimages"
	azureImageDeleteURLformat = "services/vmimages/%s"
	azureRoleOperationsURL    = "services/hostedservices/%s/deployments/%s/roleinstances/%s/operations"
	errParamNotSpecified      = "Parameter %s is not specified."
)

//NewClient is used to instantiate a new Client from an Azure client
func NewClient(client management.Client) Client {
	return Client{client}
}

//ListVirtualMachineImages lists the available VM images, filtered by the optional parameters.
//See https://msdn.microsoft.com/en-us/library/azure/dn499770.aspx
func (c Client) ListVirtualMachineImages(parameters ListParameters) (ListVirtualMachineImagesResponse, error) {
	var imageList ListVirtualMachineImagesResponse

	listURL := azureImageListURL

	v := url.Values{}
	if parameters.Location != "" {
		v.Add("location", parameters.Location)
	}

	if parameters.Publisher != "" {
		v.Add("publisher", parameters.Publisher)
	}

	if parameters.Category != "" {
		v.Add("category", parameters.Category)
	}

	query := v.Encode()
	if query != "" {
		listURL = listURL + "?" + query
	}

	response, err := c.SendAzureGetRequest(listURL)
	if err != nil {
		return imageList, err
	}
	err = xml.Unmarshal(response, &imageList)
	return imageList, err
}

//DeleteVirtualMachineImage deletes the named VM image. If deleteVHDs is specified,
//the referenced OS and data disks are also deleted.
//See https://msdn.microsoft.com/en-us/library/azure/dn499769.aspx
func (c Client) DeleteVirtualMachineImage(name string, deleteVHDs bool) error {
	if name == "" {
		return fmt.Errorf(errParamNotSpecified, "name")
	}

	uri := fmt.Sprintf(azureImageDeleteURLformat, name)

	if deleteVHDs {
		uri = uri + "?comp=media"
	}

	_, err := c.SendAzureDeleteRequest(uri) // delete is synchronous for this operation
	return err
}

type ListParameters struct {
	Location  string
	Publisher string
	Category  string
}

const CategoryUser = "User"

//Capture captures a VM into a VM image. The VM has to be shut down previously.
//See https://msdn.microsoft.com/en-us/library/azure/dn499768.aspx
func (c Client) Capture(cloudServiceName, deploymentName, roleName string,
	name, label string, osState OSState, parameters CaptureParameters) (management.OperationID, error) {
	if cloudServiceName == "" {
		return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
	}
	if deploymentName == "" {
		return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
	}
	if roleName == "" {
		return "", fmt.Errorf(errParamNotSpecified, "roleName")
	}

	request := CaptureRoleAsVMImageOperation{
		VMImageName:       name,
		VMImageLabel:      label,
		OSState:           osState,
		CaptureParameters: parameters,
	}
	data, err := xml.Marshal(request)
	if err != nil {
		return "", err
	}

	return c.SendAzurePostRequest(fmt.Sprintf(azureRoleOperationsURL,
		cloudServiceName, deploymentName, roleName), data)
}