1
2
3 package virtualmachine
4
5
6
7
8 import (
9 "encoding/xml"
10 "fmt"
11
12 "github.com/Azure/azure-sdk-for-go/services/classic/management"
13 )
14
15 const (
16 azureResourceExtensionsURL = "services/resourceextensions"
17 azureCloudServiceExtensionsURL = "services/hostedservices/%s/extensions"
18 azureCloudServiceExtensionURL = "services/hostedservices/%s/extensions/%s"
19 )
20
21
22
23
24
25 func (c VirtualMachineClient) GetResourceExtensions() (extensions []ResourceExtension, err error) {
26 data, err := c.client.SendAzureGetRequest(azureResourceExtensionsURL)
27 if err != nil {
28 return extensions, err
29 }
30
31 var response ResourceExtensions
32 err = xml.Unmarshal(data, &response)
33 extensions = response.List
34 return
35 }
36
37
38 type Extensions struct {
39 XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Extensions"`
40 Extensions []ExtensionInfo `xml:"Extension"`
41 }
42
43
44
45 type ExtensionInfo struct {
46 XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Extension"`
47 ProviderNameSpace string
48 Type string
49 ID string `xml:"Id"`
50 Version string
51 Thumbprint string
52 PublicConfigurationSchema string
53 ThumbprintAlgorithm string
54 IsJSONExtension bool `xml:"IsJsonExtension"`
55 DisallowMajorVersionUpgrade bool
56 }
57
58
59
60 func (c VirtualMachineClient) GetExtension(cloudServiceName string, extensionID string) (extension ExtensionInfo, err error) {
61
62 if cloudServiceName == "" {
63 return ExtensionInfo{}, fmt.Errorf(errParamNotSpecified, "cloudServiceName")
64 }
65 if extensionID == "" {
66 return ExtensionInfo{}, fmt.Errorf(errParamNotSpecified, "extensionID")
67 }
68
69 requestURL := fmt.Sprintf(azureCloudServiceExtensionURL, cloudServiceName, extensionID)
70 data, err := c.client.SendAzureGetRequest(requestURL)
71 if err != nil {
72 return ExtensionInfo{}, err
73 }
74 err = xml.Unmarshal(data, &extension)
75 return
76 }
77
78
79
80 func (c VirtualMachineClient) ListExtensions(cloudServiceName string) (extensions []ExtensionInfo, err error) {
81
82 if cloudServiceName == "" {
83 return []ExtensionInfo{}, fmt.Errorf(errParamNotSpecified, "cloudServiceName")
84 }
85
86 requestURL := fmt.Sprintf(azureCloudServiceExtensionsURL, cloudServiceName)
87 data, err := c.client.SendAzureGetRequest(requestURL)
88 if err != nil {
89 return []ExtensionInfo{}, err
90 }
91 var response Extensions
92 err = xml.Unmarshal(data, &response)
93 extensions = response.Extensions
94 return
95 }
96
97
98 type AddExtensionOptions struct {
99 ProviderNameSpace string
100 Type string
101 ID string
102 Thumbprint string
103 ThumbprintAlgorithm string
104 PublicConfiguration string
105 PrivateConfiguration string
106 Version string
107 }
108
109
110 type AddExtensionRequest struct {
111 XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Extension"`
112 ProviderNameSpace string
113 Type string
114 ID string `xml:"Id"`
115 Thumbprint string
116 ThumbprintAlgorithm string
117 PublicConfiguration string
118 PrivateConfiguration string
119 Version string
120 }
121
122
123
124 func (c VirtualMachineClient) AddExtension(cloudServiceName string, options AddExtensionOptions) (management.OperationID, error) {
125
126 if cloudServiceName == "" {
127 return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
128 }
129 if options.ID == "" {
130 return "", fmt.Errorf(errParamNotSpecified, "options.ID")
131 }
132 if options.ProviderNameSpace == "" {
133 return "", fmt.Errorf(errParamNotSpecified, "options.ProviderNameSpace")
134 }
135 if options.Type == "" {
136 return "", fmt.Errorf(errParamNotSpecified, "options.Type")
137 }
138
139 req := AddExtensionRequest{
140 ProviderNameSpace: options.ProviderNameSpace,
141 Type: options.Type,
142 ID: options.ID,
143 Thumbprint: options.Thumbprint,
144 ThumbprintAlgorithm: options.ThumbprintAlgorithm,
145 PublicConfiguration: options.PublicConfiguration,
146 PrivateConfiguration: options.PrivateConfiguration,
147 Version: options.Version,
148 }
149
150 data, err := xml.Marshal(req)
151 if err != nil {
152 return "", err
153 }
154
155 requestURL := fmt.Sprintf(azureCloudServiceExtensionsURL, cloudServiceName)
156 return c.client.SendAzurePostRequest(requestURL, data)
157 }
158
159
160
161 func (c VirtualMachineClient) DeleteExtension(cloudServiceName string, extensionID string) (management.OperationID, error) {
162
163 if cloudServiceName == "" {
164 return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
165 }
166 if extensionID == "" {
167 return "", fmt.Errorf(errParamNotSpecified, "extensionID")
168 }
169
170 requestURL := fmt.Sprintf(azureCloudServiceExtensionURL, cloudServiceName, extensionID)
171 return c.client.SendAzureDeleteRequest(requestURL)
172 }
173
View as plain text