compute

package
v0.0.0-...-e747c7c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddEncyptionExtension

func AddEncyptionExtension(ctx context.Context, vmName, vaultName, keyID string) (ext compute.VirtualMachineExtension, err error)

AddEncyptionExtension add the disk encryption extension to the selected VM

func AddMSIExtension

func AddMSIExtension(ctx context.Context, vmName string) (ext compute.VirtualMachineExtension, err error)

AddMSIExtension adds the MSI (managed service identity) extension to a virtual machine.

func AttachDataDisks

func AttachDataDisks(ctx context.Context, vmName string) (vm compute.VirtualMachine, err error)

AttachDataDisks attaches a 1 GB data disk to the selected VM

func CreateAvailabilitySet

func CreateAvailabilitySet(ctx context.Context, avaSetName string) (compute.AvailabilitySet, error)

CreateAvailabilitySet creates an availability set

func CreateManagedDisk

func CreateManagedDisk(ctx context.Context, diskName string) (disk compute.Disk, err error)

CreateManagedDisk creates an empty, 64GB disk. The disk can be attached to a VM later.

func CreateVM

func CreateVM(ctx context.Context, vmName, nicName, username, password, sshPublicKeyPath string) (vm compute.VirtualMachine, err error)

CreateVM creates a new virtual machine with the specified name using the specified NIC. Username, password, and sshPublicKeyPath determine logon credentials.

Example
helpers.SetResourceGroupName("CreateVM")
ctx := context.Background()
defer resources.Cleanup(ctx)
_, err := resources.CreateGroup(ctx, helpers.ResourceGroupName())
if err != nil {
	helpers.PrintAndLog(err.Error())
}

_, err = network.CreateVirtualNetworkAndSubnets(ctx, virtualNetworkName, subnet1Name, subnet2Name)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created vnet and 2 subnets")

_, err = network.CreateNetworkSecurityGroup(ctx, nsgName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created network security group")

_, err = network.CreatePublicIP(ctx, ipName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created public IP")

_, err = network.CreateNIC(ctx, virtualNetworkName, subnet1Name, nsgName, ipName, nicName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created nic")

_, err = CreateVM(ctx, vmName, nicName, username, password, sshPublicKeyPath)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created VM")

// Now that the Vm has been created, we can do some simple operations on the VM

_, err = UpdateVM(ctx, vmName, map[string]*string{
	"who rocks": to.StringPtr("golang"),
	"where":     to.StringPtr("on azure"),
})
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("updated VM")

_, err = AttachDataDisks(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("attached data disks")

_, err = DetachDataDisks(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("detached data disks")

_, err = UpdateOSDiskSize(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("updated OS disk size")

_, err = StartVM(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("started VM")

_, err = RestartVM(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("restarted VM")

_, err = PowerOffVM(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("stopped VM")
Output:

created vnet and 2 subnets
created network security group
created public IP
created nic
created VM
updated VM
attached data disks
detached data disks
updated OS disk size
started VM
restarted VM
stopped VM

func CreateVMForMSI

func CreateVMForMSI(ctx context.Context, vmName, nicName, username, password string) (vm compute.VirtualMachine, err error)

CreateVMForMSI creates a virtual machine with a systems assigned identity type

Example
helpers.SetResourceGroupName("CreateVMForMSI")
ctx := context.Background()
defer resources.Cleanup(ctx)
_, err := resources.CreateGroup(ctx, helpers.ResourceGroupName())
if err != nil {
	helpers.PrintAndLog(err.Error())
}

_, err = network.CreateVirtualNetworkAndSubnets(ctx, virtualNetworkName, subnet1Name, subnet2Name)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created vnet and 2 subnets")

_, err = network.CreateNetworkSecurityGroup(ctx, nsgName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created network security group")

_, err = network.CreatePublicIP(ctx, ipName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created public IP")

_, err = network.CreateNIC(ctx, virtualNetworkName, subnet1Name, nsgName, ipName, nicName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created nic")

_, err = CreateVMForMSI(ctx, vmName, nicName, username, password)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("created VM")

_, err = AddMSIExtension(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("added MSI extension")

vm, err := GetVM(ctx, vmName)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("got VM")

list, err := authorization.ListRoles(ctx, "roleName eq 'Contributor'")
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("got role definitions list")

_, err = authorization.AssignRole(ctx, *vm.Identity.PrincipalID, *list.Values()[0].ID)
if err != nil {
	helpers.PrintAndLog(err.Error())
}
helpers.PrintAndLog("role assigned")
Output:

created vnet and 2 subnets
created network security group
created public IP
created nic
created VM
added MSI extension
got VM
got role definitions list
role assigned

func CreateVMWithLoadBalancer

func CreateVMWithLoadBalancer(ctx context.Context, vmName, lbName, vnetName, subnetName, pipName, availabilySetName string, natRule int) (vm compute.VirtualMachine, err error)

CreateVMWithLoadBalancer creates a virtual machine inside an availability set It also creates the NIC needed by the virtual machine. The NIC is set up with a loadbalancer's inbound NAT rule.

func CreateVMWithManagedDisk

func CreateVMWithManagedDisk(ctx context.Context, nicName, diskName, vmName string, username string, password string) (vm compute.VirtualMachine, err error)

CreateVMWithManagedDisk creates a VM, attaching an already existing data disk

func Deallocate

func Deallocate(ctx context.Context, vmName string) (osr compute.OperationStatusResponse, err error)

Deallocate deallocates the selected VM

func DetachDataDisks

func DetachDataDisks(ctx context.Context, vmName string) (vm compute.VirtualMachine, err error)

DetachDataDisks detaches all data disks from the selected VM

func GetAvailabilitySet

func GetAvailabilitySet(ctx context.Context, avaSetName string) (compute.AvailabilitySet, error)

GetAvailabilitySet gets info on an availability set

func GetVM

func GetVM(ctx context.Context, vmName string) (compute.VirtualMachine, error)

GetVM gets the specified VM info

func PowerOffVM

func PowerOffVM(ctx context.Context, vmName string) (osr compute.OperationStatusResponse, err error)

PowerOffVM stops the selected VM

func RestartVM

func RestartVM(ctx context.Context, vmName string) (osr compute.OperationStatusResponse, err error)

RestartVM restarts the selected VM

func StartVM

func StartVM(ctx context.Context, vmName string) (osr compute.OperationStatusResponse, err error)

StartVM starts the selected VM

func UpdateOSDiskSize

func UpdateOSDiskSize(ctx context.Context, vmName string) (d compute.Disk, err error)

UpdateOSDiskSize increases the selected VM's OS disk size

func UpdateVM

func UpdateVM(ctx context.Context, vmName string, tags map[string]*string) (vm compute.VirtualMachine, err error)

UpdateVM adds tags to the VM

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL