forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
130 lines (105 loc) · 3.93 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
// +build go1.7
// Package hostedservice provides a client for Hosted Services.
package hostedservice
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import (
"encoding/base64"
"encoding/xml"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/classic/management"
)
const (
azureXmlns = "http://schemas.microsoft.com/windowsazure"
azureDeploymentListURL = "services/hostedservices/%s/deployments"
azureHostedServiceListURL = "services/hostedservices"
azureHostedServiceAvailabilityURL = "services/hostedservices/operations/isavailable/%s"
azureDeploymentURL = "services/hostedservices/%s/deployments/%s"
deleteAzureDeploymentURL = "services/hostedservices/%s/deployments/%s"
getHostedServicePropertiesURL = "services/hostedservices/%s"
azureServiceCertificateURL = "services/hostedservices/%s/certificates"
errParamNotSpecified = "Parameter %s is not specified."
)
//NewClient is used to return a handle to the HostedService API
func NewClient(client management.Client) HostedServiceClient {
return HostedServiceClient{client: client}
}
func (h HostedServiceClient) CreateHostedService(params CreateHostedServiceParameters) error {
req, err := xml.Marshal(params)
if err != nil {
return err
}
_, err = h.client.SendAzurePostRequest(azureHostedServiceListURL, req) // not a long running operation
return err
}
func (h HostedServiceClient) CheckHostedServiceNameAvailability(dnsName string) (AvailabilityResponse, error) {
var r AvailabilityResponse
if dnsName == "" {
return r, fmt.Errorf(errParamNotSpecified, "dnsName")
}
requestURL := fmt.Sprintf(azureHostedServiceAvailabilityURL, dnsName)
response, err := h.client.SendAzureGetRequest(requestURL)
if err != nil {
return r, err
}
err = xml.Unmarshal(response, &r)
return r, err
}
func (h HostedServiceClient) DeleteHostedService(dnsName string, deleteDisksAndBlobs bool) (management.OperationID, error) {
if dnsName == "" {
return "", fmt.Errorf(errParamNotSpecified, "dnsName")
}
requestURL := fmt.Sprintf(getHostedServicePropertiesURL, dnsName)
if deleteDisksAndBlobs {
requestURL += "?comp=media"
}
return h.client.SendAzureDeleteRequest(requestURL)
}
func (h HostedServiceClient) GetHostedService(name string) (HostedService, error) {
hostedService := HostedService{}
if name == "" {
return hostedService, fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(getHostedServicePropertiesURL, name)
response, err := h.client.SendAzureGetRequest(requestURL)
if err != nil {
return hostedService, err
}
err = xml.Unmarshal(response, &hostedService)
if err != nil {
return hostedService, err
}
decodedLabel, err := base64.StdEncoding.DecodeString(hostedService.LabelBase64)
if err != nil {
return hostedService, err
}
hostedService.Label = string(decodedLabel)
return hostedService, nil
}
func (h HostedServiceClient) ListHostedServices() (ListHostedServicesResponse, error) {
var response ListHostedServicesResponse
data, err := h.client.SendAzureGetRequest(azureHostedServiceListURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
func (h HostedServiceClient) AddCertificate(dnsName string, certData []byte, certificateFormat CertificateFormat, password string) (management.OperationID, error) {
if dnsName == "" {
return "", fmt.Errorf(errParamNotSpecified, "dnsName")
}
certBase64 := base64.StdEncoding.EncodeToString(certData)
addCertificate := CertificateFile{
Data: certBase64,
CertificateFormat: certificateFormat,
Password: password,
Xmlns: azureXmlns,
}
buffer, err := xml.Marshal(addCertificate)
if err != nil {
return "", err
}
requestURL := fmt.Sprintf(azureServiceCertificateURL, dnsName)
return h.client.SendAzurePostRequest(requestURL, buffer)
}