...

Source file src/github.com/Azure/azure-sdk-for-go/services/classic/management/affinitygroup/client.go

Documentation: github.com/Azure/azure-sdk-for-go/services/classic/management/affinitygroup

     1  // +build go1.7
     2  
     3  package affinitygroup
     4  
     5  // Copyright (c) Microsoft Corporation. All rights reserved.
     6  // Licensed under the MIT License. See License.txt in the project root for license information.
     7  
     8  import (
     9  	"encoding/base64"
    10  	"encoding/xml"
    11  	"fmt"
    12  
    13  	"github.com/Azure/azure-sdk-for-go/services/classic/management"
    14  )
    15  
    16  const (
    17  	azureCreateAffinityGroupURL = "/affinitygroups"
    18  	azureGetAffinityGroupURL    = "/affinitygroups/%s"
    19  	azureListAffinityGroupsURL  = "/affinitygroups"
    20  	azureUpdateAffinityGroupURL = "/affinitygroups/%s"
    21  	azureDeleteAffinityGroupURL = "/affinitygroups/%s"
    22  
    23  	errParameterNotSpecified = "Parameter %s not specified."
    24  )
    25  
    26  // AffinityGroupClient simply contains a management.Client and has
    27  // methods for doing all affinity group-related API calls to Azure.
    28  type AffinityGroupClient struct {
    29  	mgmtClient management.Client
    30  }
    31  
    32  // NewClient returns an AffinityGroupClient with the given management.Client.
    33  func NewClient(mgmtClient management.Client) AffinityGroupClient {
    34  	return AffinityGroupClient{mgmtClient}
    35  }
    36  
    37  // CreateAffinityGroup creates a new affinity group.
    38  //
    39  // https://msdn.microsoft.com/en-us/library/azure/gg715317.aspx
    40  func (c AffinityGroupClient) CreateAffinityGroup(params CreateAffinityGroupParams) error {
    41  	params.Label = encodeLabel(params.Label)
    42  
    43  	req, err := xml.Marshal(params)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	_, err = c.mgmtClient.SendAzurePostRequest(azureCreateAffinityGroupURL, req)
    49  	return err
    50  }
    51  
    52  // GetAffinityGroup returns the system properties that are associated with the
    53  // specified affinity group.
    54  //
    55  // https://msdn.microsoft.com/en-us/library/azure/ee460789.aspx
    56  func (c AffinityGroupClient) GetAffinityGroup(name string) (AffinityGroup, error) {
    57  	var affgroup AffinityGroup
    58  	if name == "" {
    59  		return affgroup, fmt.Errorf(errParameterNotSpecified, "name")
    60  	}
    61  
    62  	url := fmt.Sprintf(azureGetAffinityGroupURL, name)
    63  	resp, err := c.mgmtClient.SendAzureGetRequest(url)
    64  	if err != nil {
    65  		return affgroup, err
    66  	}
    67  
    68  	err = xml.Unmarshal(resp, &affgroup)
    69  	affgroup.Label = decodeLabel(affgroup.Label)
    70  	return affgroup, err
    71  }
    72  
    73  // ListAffinityGroups lists the affinity groups off Azure.
    74  //
    75  // https://msdn.microsoft.com/en-us/library/azure/ee460797.aspx
    76  func (c AffinityGroupClient) ListAffinityGroups() (ListAffinityGroupsResponse, error) {
    77  	var affinitygroups ListAffinityGroupsResponse
    78  
    79  	resp, err := c.mgmtClient.SendAzureGetRequest(azureListAffinityGroupsURL)
    80  	if err != nil {
    81  		return affinitygroups, err
    82  	}
    83  
    84  	err = xml.Unmarshal(resp, &affinitygroups)
    85  
    86  	for i, grp := range affinitygroups.AffinityGroups {
    87  		affinitygroups.AffinityGroups[i].Label = decodeLabel(grp.Label)
    88  	}
    89  
    90  	return affinitygroups, err
    91  }
    92  
    93  // UpdateAffinityGroup updates the label or description for an the group.
    94  //
    95  // https://msdn.microsoft.com/en-us/library/azure/gg715316.aspx
    96  func (c AffinityGroupClient) UpdateAffinityGroup(name string, params UpdateAffinityGroupParams) error {
    97  	if name == "" {
    98  		return fmt.Errorf(errParameterNotSpecified, "name")
    99  	}
   100  
   101  	params.Label = encodeLabel(params.Label)
   102  	req, err := xml.Marshal(params)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	url := fmt.Sprintf(azureUpdateAffinityGroupURL, name)
   108  	_, err = c.mgmtClient.SendAzurePutRequest(url, "text/xml", req)
   109  	return err
   110  }
   111  
   112  // DeleteAffinityGroup deletes the given affinity group.
   113  //
   114  // https://msdn.microsoft.com/en-us/library/azure/gg715314.aspx
   115  func (c AffinityGroupClient) DeleteAffinityGroup(name string) error {
   116  	if name == "" {
   117  		return fmt.Errorf(errParameterNotSpecified, name)
   118  	}
   119  
   120  	url := fmt.Sprintf(azureDeleteAffinityGroupURL, name)
   121  	_, err := c.mgmtClient.SendAzureDeleteRequest(url)
   122  	return err
   123  }
   124  
   125  // encodeLabel is a helper function which encodes the given string
   126  // to the base64 string which will be sent to Azure as a Label.
   127  func encodeLabel(label string) string {
   128  	return base64.StdEncoding.EncodeToString([]byte(label))
   129  }
   130  
   131  // decodeLabel is a helper function which decodes the base64 encoded
   132  // label received from Azure into standard encoding.
   133  func decodeLabel(label string) string {
   134  	res, _ := base64.StdEncoding.DecodeString(label)
   135  	return string(res)
   136  }
   137  

View as plain text