...

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

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

     1  // +build go1.7
     2  
     3  // Package virtualmachineimage provides a client for Virtual Machine Images.
     4  package virtualmachineimage
     5  
     6  // Copyright (c) Microsoft Corporation. All rights reserved.
     7  // Licensed under the MIT License. See License.txt in the project root for license information.
     8  
     9  import (
    10  	"encoding/xml"
    11  	"fmt"
    12  	"net/url"
    13  
    14  	"github.com/Azure/azure-sdk-for-go/services/classic/management"
    15  )
    16  
    17  const (
    18  	azureImageListURL         = "services/vmimages"
    19  	azureImageDeleteURLformat = "services/vmimages/%s"
    20  	azureRoleOperationsURL    = "services/hostedservices/%s/deployments/%s/roleinstances/%s/operations"
    21  	errParamNotSpecified      = "Parameter %s is not specified."
    22  )
    23  
    24  //NewClient is used to instantiate a new Client from an Azure client
    25  func NewClient(client management.Client) Client {
    26  	return Client{client}
    27  }
    28  
    29  //ListVirtualMachineImages lists the available VM images, filtered by the optional parameters.
    30  //See https://msdn.microsoft.com/en-us/library/azure/dn499770.aspx
    31  func (c Client) ListVirtualMachineImages(parameters ListParameters) (ListVirtualMachineImagesResponse, error) {
    32  	var imageList ListVirtualMachineImagesResponse
    33  
    34  	listURL := azureImageListURL
    35  
    36  	v := url.Values{}
    37  	if parameters.Location != "" {
    38  		v.Add("location", parameters.Location)
    39  	}
    40  
    41  	if parameters.Publisher != "" {
    42  		v.Add("publisher", parameters.Publisher)
    43  	}
    44  
    45  	if parameters.Category != "" {
    46  		v.Add("category", parameters.Category)
    47  	}
    48  
    49  	query := v.Encode()
    50  	if query != "" {
    51  		listURL = listURL + "?" + query
    52  	}
    53  
    54  	response, err := c.SendAzureGetRequest(listURL)
    55  	if err != nil {
    56  		return imageList, err
    57  	}
    58  	err = xml.Unmarshal(response, &imageList)
    59  	return imageList, err
    60  }
    61  
    62  //DeleteVirtualMachineImage deletes the named VM image. If deleteVHDs is specified,
    63  //the referenced OS and data disks are also deleted.
    64  //See https://msdn.microsoft.com/en-us/library/azure/dn499769.aspx
    65  func (c Client) DeleteVirtualMachineImage(name string, deleteVHDs bool) error {
    66  	if name == "" {
    67  		return fmt.Errorf(errParamNotSpecified, "name")
    68  	}
    69  
    70  	uri := fmt.Sprintf(azureImageDeleteURLformat, name)
    71  
    72  	if deleteVHDs {
    73  		uri = uri + "?comp=media"
    74  	}
    75  
    76  	_, err := c.SendAzureDeleteRequest(uri) // delete is synchronous for this operation
    77  	return err
    78  }
    79  
    80  type ListParameters struct {
    81  	Location  string
    82  	Publisher string
    83  	Category  string
    84  }
    85  
    86  const CategoryUser = "User"
    87  
    88  //Capture captures a VM into a VM image. The VM has to be shut down previously.
    89  //See https://msdn.microsoft.com/en-us/library/azure/dn499768.aspx
    90  func (c Client) Capture(cloudServiceName, deploymentName, roleName string,
    91  	name, label string, osState OSState, parameters CaptureParameters) (management.OperationID, error) {
    92  	if cloudServiceName == "" {
    93  		return "", fmt.Errorf(errParamNotSpecified, "cloudServiceName")
    94  	}
    95  	if deploymentName == "" {
    96  		return "", fmt.Errorf(errParamNotSpecified, "deploymentName")
    97  	}
    98  	if roleName == "" {
    99  		return "", fmt.Errorf(errParamNotSpecified, "roleName")
   100  	}
   101  
   102  	request := CaptureRoleAsVMImageOperation{
   103  		VMImageName:       name,
   104  		VMImageLabel:      label,
   105  		OSState:           osState,
   106  		CaptureParameters: parameters,
   107  	}
   108  	data, err := xml.Marshal(request)
   109  	if err != nil {
   110  		return "", err
   111  	}
   112  
   113  	return c.SendAzurePostRequest(fmt.Sprintf(azureRoleOperationsURL,
   114  		cloudServiceName, deploymentName, roleName), data)
   115  }
   116  

View as plain text