services | platforms | author |
---|---|---|
virtual-machines |
go |
mcardosos |
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
-
If you don't already have it, install Go 1.7.
-
Clone the repository.
git clone https://github.com:Azure-Samples/virtual-machines-go-manage.git
-
Install the dependencies using glide.
cd virtual-machines-go-manage glide install
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
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 ofexport
. -
Run the sample.
go run example.go
First, it creates all resources needed before creating a VM (resource group, storage account, virtual network, subnet)
publicIPaddress, nicParameters := createPIPandNIC(vmName, subnetInfo)
vm := setVMparameters(vmName, "Canonical", "UbuntuServer", "16.04.0-LTS", *nicParameters.ID)
_, errChan := vmClient.CreateOrUpdate(groupName, vmName, vm, nil)
vm, err := vmClient.Get(groupName, vmName, compute.InstanceView)
vm.Tags = &(map[string]*string{
"who rocks": to.StringPtr("golang"),
"where": to.StringPtr("on azure"),
})
_, errChan := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)
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)
vm.StorageProfile.DataDisks = &[]compute.DataDisk{}
_, errChan := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)
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)
_, errChan := vmClient.Start(groupName, vmName, nil)
_, errChan = vmClient.Restart(groupName, vmName, nil)
_, errChan = vmClient.PowerOff(groupName, vmName, nil)
vmList, err := vmClient.ListAll()
_, errChan := vmClient.Delete(groupName, linuxVMname, nil)
_, errChan = vmClient.Delete(groupName, windowsVMname, nil)
_, errChan = groupClient.Delete(groupName, nil)
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.