Skip to content

This package demonstrates how to manage Azure virtual machines using Go

License

Notifications You must be signed in to change notification settings

seyadava/virtual-machines-go-manage

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

services platforms author
virtual-machines
go
mcardosos

Azure Virtual Machine Management Sample using Azure SDK for Go

This sample demonstrates how to manage your Azure virtual machine (VM) using Go, and specifically how to:

  • Create a virtual machine
  • Tag a virtual machine
  • Attach and detach data disks
  • Update the OS disk size
  • Start, restart and stop a virtual machine
  • List virtual machines
  • Delete a virtual machine

If you don't have a Microsoft Azure subscription you can get a FREE trial account here.

On this page

Run this sample

  1. If you don't already have it, install Go 1.7.

  2. Clone the repository.

    git clone https://github.com:Azure-Samples/virtual-machines-go-manage.git
    
  3. Install the dependencies using glide.

    cd virtual-machines-go-manage
    glide install
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
    

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    go run example.go
    

What does example.go do?

First, it creates all resources needed before creating a VM (resource group, storage account, virtual network, subnet)

Create a VM

publicIPaddress, nicParameters := createPIPandNIC(vmName, subnetInfo)
vm := setVMparameters(vmName, "Canonical", "UbuntuServer", "16.04.0-LTS", *nicParameters.ID)
_, errChan := vmClient.CreateOrUpdate(groupName, vmName, vm, nil)

Get the VM properties

vm, err := vmClient.Get(groupName, vmName, compute.InstanceView)

Tag the VM

vm.Tags = &(map[string]*string{
	"who rocks": to.StringPtr("golang"),
	"where":     to.StringPtr("on azure"),
	})
_, errChan := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Attach data disks to the VM

	vm.StorageProfile.DataDisks = &[]compute.DataDisk{
		{
			Lun:  to.Int32Ptr(0),
			Name: to.StringPtr("dataDisk"),
			Vhd: &compute.VirtualHardDisk{
				URI: to.StringPtr(fmt.Sprintf(vhdURItemplate, accountName, fmt.Sprintf("dataDisks-%v", vmName))),
			},
			CreateOption: compute.DiskCreateOptionTypesEmpty,
			DiskSizeGB:   to.Int32Ptr(1),
		},
	}
	_, errChan := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Detach data disks

vm.StorageProfile.DataDisks = &[]compute.DataDisk{}
_, errChan := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Updates the VM's OS disk size

if vm.StorageProfile.OsDisk.DiskSizeGB == nil {
	vm.StorageProfile.OsDisk.DiskSizeGB = to.Int32Ptr(0)
}
_, errChan := vmClient.Deallocate(groupName, vmName, nil)
if *vm.StorageProfile.OsDisk.DiskSizeGB <= 0 {
	*vm.StorageProfile.OsDisk.DiskSizeGB = 256
}
*vm.StorageProfile.OsDisk.DiskSizeGB += 10
_, errChan = vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Starts, restarts and stops the VM

_, errChan := vmClient.Start(groupName, vmName, nil)
_, errChan = vmClient.Restart(groupName, vmName, nil)
_, errChan = vmClient.PowerOff(groupName, vmName, nil)

Lists all the VMs in your subscription.

vmList, err := vmClient.ListAll()

Delete the VMs and other resources

_, errChan := vmClient.Delete(groupName, linuxVMname, nil)
_, errChan = vmClient.Delete(groupName, windowsVMname, nil)

_, errChan = groupClient.Delete(groupName, nil)

More information


This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

About

This package demonstrates how to manage Azure virtual machines using Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%