Add support for virtual machine disks

This commit is contained in:
Sander van Harmelen 2015-05-22 21:40:50 +02:00
Родитель a64871e43a
Коммит 6432bc1626
2 изменённых файлов: 326 добавлений и 11 удалений

Просмотреть файл

@ -2,13 +2,23 @@
package virtualmachinedisk
import (
"encoding/xml"
"fmt"
"github.com/Azure/azure-sdk-for-go/management"
)
const (
azureVMDiskURL = "services/disks/%s"
addDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks"
addDiskURL = "services/disks"
deleteDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks/%d"
deleteDiskURL = "services/disks/%s"
getDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks/%d"
getDiskURL = "services/disks/%s"
listDisksURL = "services/disks"
updateDataDiskURL = "services/hostedservices/%s/deployments/%s/roles/%s/DataDisks/%d"
updateDiskURL = "services/disks/%s"
errParamNotSpecified = "Parameter %s is not specified."
)
@ -17,15 +27,203 @@ func NewClient(client management.Client) DiskClient {
return DiskClient{client: client}
}
func (c DiskClient) DeleteDisk(diskName string, deleteVhdToo bool) (management.OperationID, error) {
if diskName == "" {
return "", fmt.Errorf(errParamNotSpecified, "diskName")
// AddDataDisk adds a data disk to a Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157199.aspx
func (c DiskClient) AddDataDisk(
service string,
deployment string,
role string,
params CreateDataDiskParameters) (management.OperationID, error) {
if service == "" {
return "", fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return "", fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return "", fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(azureVMDiskURL, diskName)
if deleteVhdToo {
requestURL := fmt.Sprintf(addDataDiskURL, service, deployment, role)
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePostRequest(requestURL, req)
}
// AddDisk adds an operating system disk or data disk to the user image repository
//
// https://msdn.microsoft.com/en-us/library/azure/jj157178.aspx
func (c DiskClient) AddDisk(params CreateDiskParameters) (management.OperationID, error) {
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePostRequest(addDiskURL, req)
}
// DeleteDataDisk removes the specified data disk from a Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157179.aspx
func (c DiskClient) DeleteDataDisk(
service string,
deployment string,
role string,
lun int,
deleteVHD bool) (management.OperationID, error) {
if service == "" {
return "", fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return "", fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return "", fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(deleteDataDiskURL, service, deployment, role, lun)
if deleteVHD {
requestURL += "?comp=media"
}
return c.client.SendAzureDeleteRequest(requestURL)
}
// DeleteDisk deletes the specified data or operating system disk from the image
// repository that is associated with the specified subscription
//
// https://msdn.microsoft.com/en-us/library/azure/jj157200.aspx
func (c DiskClient) DeleteDisk(name string, deleteVHD bool) (management.OperationID, error) {
if name == "" {
return "", fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(deleteDiskURL, name)
if deleteVHD {
requestURL += "?comp=media"
}
return c.client.SendAzureDeleteRequest(requestURL)
}
// GetDataDisk retrieves the specified data disk from a Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157180.aspx
func (c DiskClient) GetDataDisk(
service string,
deployment string,
role string,
lun int) (DataDiskResponse, error) {
var response DataDiskResponse
if service == "" {
return response, fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return response, fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return response, fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(getDataDiskURL, service, deployment, role, lun)
data, err := c.client.SendAzureGetRequest(requestURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
// GetDisk retrieves information about the specified disk
//
// https://msdn.microsoft.com/en-us/library/azure/dn775053.aspx
func (c DiskClient) GetDisk(name string) (DiskResponse, error) {
var response DiskResponse
if name == "" {
return response, fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(getDiskURL, name)
data, err := c.client.SendAzureGetRequest(requestURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
// ListDisks retrieves a list of the disks in the image repository that is associated
// with the specified subscription
//
// https://msdn.microsoft.com/en-us/library/azure/jj157176.aspx
func (c DiskClient) ListDisks() (ListDiskResponse, error) {
var response ListDiskResponse
data, err := c.client.SendAzureGetRequest(listDisksURL)
if err != nil {
return response, err
}
err = xml.Unmarshal(data, &response)
return response, err
}
// UpdateDataDisk updates the configuration of the specified data disk that is
// attached to the specified Virtual Machine
//
// https://msdn.microsoft.com/en-us/library/azure/jj157190.aspx
func (c DiskClient) UpdateDataDisk(
service string,
deployment string,
role string,
lun int,
params UpdateDataDiskParameters) (management.OperationID, error) {
if service == "" {
return "", fmt.Errorf(errParamNotSpecified, "service")
}
if deployment == "" {
return "", fmt.Errorf(errParamNotSpecified, "deployment")
}
if role == "" {
return "", fmt.Errorf(errParamNotSpecified, "role")
}
requestURL := fmt.Sprintf(updateDataDiskURL, service, deployment, role, lun)
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePutRequest(requestURL, "", req)
}
// UpdateDisk updates the label of an existing disk in the image repository that is
// associated with the specified subscription
//
// https://msdn.microsoft.com/en-us/library/azure/jj157205.aspx
func (c DiskClient) UpdateDisk(
name string,
params UpdateDiskParameters) (management.OperationID, error) {
if name == "" {
return "", fmt.Errorf(errParamNotSpecified, "name")
}
requestURL := fmt.Sprintf(updateDiskURL, name)
req, err := xml.Marshal(params)
if err != nil {
return "", err
}
return c.client.SendAzurePutRequest(requestURL, "", req)
}

Просмотреть файл

@ -1,17 +1,134 @@
package virtualmachinedisk
import "github.com/Azure/azure-sdk-for-go/management"
import (
"encoding/xml"
"github.com/Azure/azure-sdk-for-go/management"
)
// DiskClient is used to perform operations on Azure Disks
type DiskClient struct {
client management.Client
}
// CreateDiskParameters represents a disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type CreateDiskParameters struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Disk"`
OS OperatingSystemType `xml:",omitempty"`
Label string
MediaLink string `xml:",omitempty"`
Name string
}
// UpdateDiskParameters represents a disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type UpdateDiskParameters struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Disk"`
Name string
Label string `xml:",omitempty"`
ResizedSizeInGB int `xml:",omitempty"`
}
// ListDiskResponse represents a disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type ListDiskResponse struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Disks"`
Disk []DiskResponse
}
// DiskResponse represents a disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type DiskResponse struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Disk"`
AffinityGroup string
AttachedTo Resource
IsCorrupted bool
OS OperatingSystemType
Location string
LogicalDiskSizeInGB int
MediaLink string
Name string
SourceImageName string
CreatedTime string
IOType IOType
}
// Resource describes the resource details a disk is currently attached to
type Resource struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure AttachedTo"`
DeploymentName string
HostedServiceName string
RoleName string
}
// IOType represents an IO type
type IOType string
// These constants represent the possible IO types
const (
IOTypeProvisioned IOType = "Provisioned"
IOTypeStandard IOType = "Standard"
)
// OperatingSystemType represents an operating system type
type OperatingSystemType string
// These constants represent the valid operating system types
const (
OperatingSystemTypeNull OperatingSystemType = "NULL"
OperatingSystemTypeLinux OperatingSystemType = "Linux"
OperatingSystemTypeWindows OperatingSystemType = "Windows"
)
// CreateDataDiskParameters represents a data disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type CreateDataDiskParameters struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure DataVirtualHardDisk"`
HostCaching HostCachingType `xml:",omitempty"`
DiskLabel string `xml:",omitempty"`
DiskName string `xml:",omitempty"`
Lun int `xml:",omitempty"`
LogicalDiskSizeInGB int `xml:",omitempty"`
MediaLink string
SourceMediaLink string `xml:",omitempty"`
}
// UpdateDataDiskParameters represents a data disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type UpdateDataDiskParameters struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure DataVirtualHardDisk"`
HostCaching HostCachingType `xml:",omitempty"`
DiskName string
Lun int
MediaLink string
}
// DataDiskResponse represents a data disk
//
// https://msdn.microsoft.com/en-us/library/azure/jj157188.aspx
type DataDiskResponse struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure DataVirtualHardDisk"`
HostCaching HostCachingType
DiskLabel string
DiskName string
Lun int
LogicalDiskSizeInGB int
MediaLink string
}
// HostCachingType represents a host caching type
type HostCachingType string
// Enum values for HostCachingType
// These constants represent the valid host caching types
const (
HostCachingTypeNone = HostCachingType("None")
HostCachingTypeReadOnly = HostCachingType("ReadOnly")
HostCachingTypeReadWrite = HostCachingType("ReadWrite")
HostCachingTypeNone HostCachingType = "None"
HostCachingTypeReadOnly HostCachingType = "ReadOnly"
HostCachingTypeReadWrite HostCachingType = "ReadWrite"
)