...

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

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

     1  // +build go1.7
     2  
     3  package virtualmachine
     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/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  // GetResourceExtensions lists the resource extensions that are available to add
    22  // to a virtual machine.
    23  //
    24  // See https://msdn.microsoft.com/en-us/library/azure/dn495441.aspx
    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  // Extensions is a list of extensions returned by the ListExtensions response
    38  type Extensions struct {
    39  	XMLName    xml.Name        `xml:"http://schemas.microsoft.com/windowsazure Extensions"`
    40  	Extensions []ExtensionInfo `xml:"Extension"`
    41  }
    42  
    43  // ExtensionInfo defined the type retured by GetExtension
    44  // https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-get-extension
    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  // GetExtension retrieves information about a specified extension that was added to a cloud service.
    59  // https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-get-extension
    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  // ListExtensions lists all of the extensions that were added to a cloud service.
    79  // https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-list-extensions
    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  // AddExtensionOptions defines the options available for adding extensions to a cloud service
    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  // AddExtensionRequest is the type used to submit AddExtension requests
   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  // AddExtension addes an extension to the cloud service
   123  // https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-add-extension
   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  // DeleteExtension deletes the specified extension from a cloud service.
   160  // https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-delete-extension
   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