forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
131 lines (107 loc) · 3.8 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
package affinitygroup
import (
"encoding/base64"
"encoding/xml"
"fmt"
"github.com/Azure/azure-sdk-for-go/management"
)
const (
azureCreateAffinityGroupURL = "/affinitygroups"
azureGetAffinityGroupURL = "/affinitygroups/%s"
azureListAffinityGroupsURL = "/affinitygroups"
azureUpdateAffinityGroupURL = "/affinitygroups/%s"
azureDeleteAffinityGroupURL = "/affinitygroups/%s"
errParameterNotSpecified = "Parameter %s not specified."
)
// AffinityGroupClient simply contains a management.Client and has
// methods for doing all affinity group-related API calls to Azure.
type AffinityGroupClient struct {
mgmtClient management.Client
}
// NewClient returns an AffinityGroupClient with the given management.Client.
func NewClient(mgmtClient management.Client) AffinityGroupClient {
return AffinityGroupClient{mgmtClient}
}
// CreateAffinityGroup creates a new affinity group.
//
// https://msdn.microsoft.com/en-us/library/azure/gg715317.aspx
func (c AffinityGroupClient) CreateAffinityGroup(params CreateAffinityGroupParams) error {
params.Label = encodeLabel(params.Label)
req, err := xml.Marshal(params)
if err != nil {
return err
}
_, err = c.mgmtClient.SendAzurePostRequest(azureCreateAffinityGroupURL, req)
return err
}
// GetAffinityGroup returns the system properties that are associated with the
// specified affinity group.
//
// https://msdn.microsoft.com/en-us/library/azure/ee460789.aspx
func (c AffinityGroupClient) GetAffinityGroup(name string) (AffinityGroup, error) {
var affgroup AffinityGroup
if name == "" {
return affgroup, fmt.Errorf(errParameterNotSpecified, "name")
}
url := fmt.Sprintf(azureGetAffinityGroupURL, name)
resp, err := c.mgmtClient.SendAzureGetRequest(url)
if err != nil {
return affgroup, err
}
err = xml.Unmarshal(resp, &affgroup)
affgroup.Label = decodeLabel(affgroup.Label)
return affgroup, err
}
// ListAffinityGroups lists the affinity groups off Azure.
//
// https://msdn.microsoft.com/en-us/library/azure/ee460797.aspx
func (c AffinityGroupClient) ListAffinityGroups() (ListAffinityGroupsResponse, error) {
var affinitygroups ListAffinityGroupsResponse
resp, err := c.mgmtClient.SendAzureGetRequest(azureListAffinityGroupsURL)
if err != nil {
return affinitygroups, err
}
err = xml.Unmarshal(resp, &affinitygroups)
for i, grp := range affinitygroups.AffinityGroups {
affinitygroups.AffinityGroups[i].Label = decodeLabel(grp.Label)
}
return affinitygroups, err
}
// UpdateAffinityGroup updates the label or description for an the group.
//
// https://msdn.microsoft.com/en-us/library/azure/gg715316.aspx
func (c AffinityGroupClient) UpdateAffinityGroup(name string, params UpdateAffinityGroupParams) error {
if name == "" {
return fmt.Errorf(errParameterNotSpecified, "name")
}
params.Label = encodeLabel(params.Label)
req, err := xml.Marshal(params)
if err != nil {
return err
}
url := fmt.Sprintf(azureUpdateAffinityGroupURL, name)
_, err = c.mgmtClient.SendAzurePutRequest(url, "text/xml", req)
return err
}
// DeleteAffinityGroup deletes the given affinity group.
//
// https://msdn.microsoft.com/en-us/library/azure/gg715314.aspx
func (c AffinityGroupClient) DeleteAffinityGroup(name string) error {
if name == "" {
return fmt.Errorf(errParameterNotSpecified, name)
}
url := fmt.Sprintf(azureDeleteAffinityGroupURL, name)
_, err := c.mgmtClient.SendAzureDeleteRequest(url)
return err
}
// encodeLabel is a helper function which encodes the given string
// to the base64 string which will be sent to Azure as a Label.
func encodeLabel(label string) string {
return base64.StdEncoding.EncodeToString([]byte(label))
}
// decodeLabel is a helper function which decodes the base64 encoded
// label recieved from Azure into standard encoding.
func decodeLabel(label string) string {
res, _ := base64.StdEncoding.DecodeString(label)
return string(res)
}