Created
September 20, 2019 02:25
-
-
Save ArcturusZhang/2c79235fb75d3d3ab95437bbf9aae592 to your computer and use it in GitHub Desktop.
Creating and destroying a virtual machine on azure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ( | |
ctx = context.Background() | |
) | |
func main() { | |
if authorizer, err := auth.NewAuthorizerFromCLI(); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Creating...") | |
create(authorizer) | |
fmt.Println("Validating...") | |
validate(authorizer) | |
fmt.Println("Deleting...") | |
delete(authorizer) | |
} | |
} | |
func validate(authorizer autorest.Authorizer) { | |
vnet := getVirtualNetwork(authorizer) | |
if b, err := json.Marshal(*vnet); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created vnet: \n%s\n", string(b)) | |
} | |
lb := getLoadBalancer(authorizer) | |
if b, err := json.Marshal(*lb); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created load balancer: \n%s\n", string(b)) | |
} | |
vm := getVirtualMachine(authorizer) | |
if b, err := json.Marshal(*vm); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created virtual machine: \n%s\n", string(b)) | |
} | |
} | |
func getVirtualNetwork(authorizer autorest.Authorizer) *network.VirtualNetwork { | |
client := network.NewVirtualNetworksClient(subsID) | |
client.Authorizer = authorizer | |
if vnet, err := client.Get(ctx, resGroup, virtualNetworkName, ""); err != nil { | |
panic(err) | |
} else { | |
return &vnet | |
} | |
} | |
func getSubnet(authorizer autorest.Authorizer) *network.Subnet { | |
client := network.NewSubnetsClient(subsID) | |
client.Authorizer = authorizer | |
if subnet, err := client.Get(ctx, resGroup, virtualNetworkName, subnetName, ""); err != nil { | |
panic(err) | |
} else { | |
return &subnet | |
} | |
} | |
func getPublicIP(authorizer autorest.Authorizer) *network.PublicIPAddress { | |
client := network.NewPublicIPAddressesClient(subsID) | |
client.Authorizer = authorizer | |
if ip, err := client.Get(ctx, resGroup, publicIPName, ""); err != nil { | |
panic(err) | |
} else { | |
return &ip | |
} | |
} | |
func getLoadBalancer(authorizer autorest.Authorizer) *network.LoadBalancer { | |
client := network.NewLoadBalancersClient(subsID) | |
client.Authorizer = authorizer | |
if lb, err := client.Get(ctx, resGroup, lbName, ""); err != nil { | |
panic(err) | |
} else { | |
return &lb | |
} | |
} | |
func getNetworkInterface(authorizer autorest.Authorizer) *network.Interface { | |
client := network.NewInterfacesClient(subsID) | |
client.Authorizer = authorizer | |
if netint, err := client.Get(ctx, resGroup, interfaceName, ""); err != nil { | |
panic(err) | |
} else { | |
return &netint | |
} | |
} | |
func getVirtualMachine(authorizer autorest.Authorizer) *compute.VirtualMachine { | |
client := compute.NewVirtualMachinesClient(subsID) | |
client.Authorizer = authorizer | |
if vm, err := client.Get(ctx, resGroup, vmName, ""); err != nil { | |
panic(err) | |
} else { | |
return &vm | |
} | |
} | |
func create(authorizer autorest.Authorizer) { | |
// create virtual network | |
createVirtualNetworks(authorizer) | |
// create subnet | |
createSubnet(authorizer) | |
// create public ip | |
createPublicIP(authorizer) | |
// create load balancer | |
createLoadBalancer(authorizer) | |
// create network interface | |
createNetworkInterface(authorizer) | |
// create virtual machine | |
createVirtualMachine(authorizer) | |
} | |
func createVirtualNetworks(authorizer autorest.Authorizer) { | |
client := network.NewVirtualNetworksClient(subsID) | |
client.Authorizer = authorizer | |
vnet := network.VirtualNetwork{ | |
Location: to.StringPtr(location), | |
VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{ | |
AddressSpace: &network.AddressSpace{ | |
AddressPrefixes: &[]string{ | |
"10.0.0.0/16", | |
}, | |
}, | |
}, | |
} | |
if future, err := client.CreateOrUpdate(ctx, resGroup, virtualNetworkName, vnet); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if vnet, err := future.Result(client); err != nil { | |
panic(err) | |
} else if b, err := json.Marshal(vnet); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created: \n%s\n", string(b)) | |
} | |
} | |
func createSubnet(authorizer autorest.Authorizer) { | |
client := network.NewSubnetsClient(subsID) | |
client.Authorizer = authorizer | |
subnet := network.Subnet{ | |
SubnetPropertiesFormat: &network.SubnetPropertiesFormat{ | |
AddressPrefix: to.StringPtr("10.0.2.0/24"), | |
}, | |
} | |
if future, err := client.CreateOrUpdate(ctx, resGroup, virtualNetworkName, subnetName, subnet); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if subnet, err := future.Result(client); err != nil { | |
panic(err) | |
} else if b, err := json.Marshal(subnet); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created: \n%s\n", string(b)) | |
} | |
} | |
func createPublicIP(authorizer autorest.Authorizer) { | |
client := network.NewPublicIPAddressesClient(subsID) | |
client.Authorizer = authorizer | |
publicIP := network.PublicIPAddress{ | |
Location: to.StringPtr(location), | |
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{ | |
PublicIPAllocationMethod: network.Static, | |
}, | |
} | |
if future, err := client.CreateOrUpdate(ctx, resGroup, publicIPName, publicIP); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if publicIP, err := future.Result(client); err != nil { | |
panic(err) | |
} else if b, err := json.Marshal(publicIP); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created: \n%s\n", string(b)) | |
} | |
} | |
func createLoadBalancer(authorizer autorest.Authorizer) { | |
client := network.NewLoadBalancersClient(subsID) | |
client.Authorizer = authorizer | |
lb := network.LoadBalancer{ | |
Location: to.StringPtr(location), | |
LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ | |
FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ | |
{ | |
Name: to.StringPtr("primary"), | |
FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ | |
PublicIPAddress: getPublicIP(authorizer), | |
}, | |
}, | |
}, | |
BackendAddressPools: &[]network.BackendAddressPool{ | |
{ | |
Name: to.StringPtr("testPool"), | |
}, | |
}, | |
}, | |
} | |
if future, err := client.CreateOrUpdate(ctx, resGroup, lbName, lb); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if lb, err := future.Result(client); err != nil { | |
panic(err) | |
} else if b, err := json.Marshal(lb); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created: \n%s\n", string(b)) | |
} | |
} | |
func createNetworkInterface(authorizer autorest.Authorizer) { | |
client := network.NewInterfacesClient(subsID) | |
client.Authorizer = authorizer | |
lb := getLoadBalancer(authorizer) | |
netint := network.Interface{ | |
Location: to.StringPtr(location), | |
InterfacePropertiesFormat: &network.InterfacePropertiesFormat{ | |
IPConfigurations: &[]network.InterfaceIPConfiguration{ | |
{ | |
Name: to.StringPtr("testconfiguration1"), | |
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{ | |
Subnet: getSubnet(authorizer), | |
PrivateIPAllocationMethod: network.Dynamic, | |
LoadBalancerBackendAddressPools: lb.BackendAddressPools, | |
}, | |
}, | |
}, | |
}, | |
} | |
if future, err := client.CreateOrUpdate(ctx, resGroup, interfaceName, netint); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if netint, err := future.Result(client); err != nil { | |
panic(err) | |
} else if b, err := json.Marshal(netint); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created: \n%s\n", string(b)) | |
} | |
} | |
func createVirtualMachine(authorizer autorest.Authorizer) { | |
client := compute.NewVirtualMachinesClient(subsID) | |
client.Authorizer = authorizer | |
netint := getNetworkInterface(authorizer) | |
vm := compute.VirtualMachine{ | |
Location: to.StringPtr(location), | |
VirtualMachineProperties: &compute.VirtualMachineProperties{ | |
NetworkProfile: &compute.NetworkProfile{ | |
NetworkInterfaces: &[]compute.NetworkInterfaceReference{ | |
{ | |
ID: netint.ID, | |
}, | |
}, | |
}, | |
HardwareProfile: &compute.HardwareProfile{ | |
VMSize: compute.VirtualMachineSizeTypesStandardDS1V2, | |
}, | |
StorageProfile: &compute.StorageProfile{ | |
ImageReference: &compute.ImageReference{ | |
Publisher: to.StringPtr("Canonical"), | |
Offer: to.StringPtr("UbuntuServer"), | |
Sku: to.StringPtr("16.04-LTS"), | |
Version: to.StringPtr("latest"), | |
}, | |
OsDisk: &compute.OSDisk{ | |
Name: to.StringPtr("myosdisk1"), | |
Caching: compute.CachingTypesReadWrite, | |
CreateOption: compute.DiskCreateOptionTypesFromImage, | |
ManagedDisk: &compute.ManagedDiskParameters{ | |
StorageAccountType: compute.StorageAccountTypesStandardLRS, | |
}, | |
}, | |
}, | |
OsProfile: &compute.OSProfile{ | |
ComputerName: to.StringPtr("hostname"), | |
AdminUsername: to.StringPtr("testadmin"), | |
AdminPassword: to.StringPtr("Password1234!"), | |
LinuxConfiguration: &compute.LinuxConfiguration{ | |
DisablePasswordAuthentication: to.BoolPtr(false), | |
}, | |
}, | |
}, | |
Tags: map[string]*string{ | |
"environment": to.StringPtr("staging"), | |
}, | |
} | |
if future, err := client.CreateOrUpdate(ctx, resGroup, vmName, vm); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if vm, err := future.Result(client); err != nil { | |
panic(err) | |
} else if b, err := json.Marshal(vm); err != nil { | |
panic(err) | |
} else { | |
fmt.Printf("Created: \n%s\n", string(b)) | |
} | |
} | |
func delete(authorizer autorest.Authorizer) { | |
// delete load balancer | |
deleteLoadBalancer(authorizer) | |
// delete publicIP | |
deletePublicIP(authorizer) | |
// delete virtual machine | |
deleteVirtualMachine(authorizer) | |
// delete network interface | |
deleteNetworkInterface(authorizer) | |
// delete virtual network | |
deleteVirtualNetwork(authorizer) | |
// delete subnet | |
deleteSubnet(authorizer) | |
} | |
func deleteVirtualNetwork(authorizer autorest.Authorizer) { | |
client := network.NewVirtualNetworksClient(subsID) | |
client.Authorizer = authorizer | |
if future, err := client.Delete(ctx, resGroup, virtualNetworkName); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if _, err := future.Result(client); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Deleted virtual network") | |
} | |
} | |
func deleteSubnet(authorizer autorest.Authorizer) { | |
client := network.NewSubnetsClient(subsID) | |
client.Authorizer = authorizer | |
if future, err := client.Delete(ctx, resGroup, virtualNetworkName, subnetName); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if _, err := future.Result(client); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Deleted subnet") | |
} | |
} | |
func deletePublicIP(authorizer autorest.Authorizer) { | |
client := network.NewPublicIPAddressesClient(subsID) | |
client.Authorizer = authorizer | |
if future, err := client.Delete(ctx, resGroup, publicIPName); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if _, err := future.Result(client); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Deleted publicIP") | |
} | |
} | |
func deleteLoadBalancer(authorizer autorest.Authorizer) { | |
client := network.NewLoadBalancersClient(subsID) | |
client.Authorizer = authorizer | |
if future, err := client.Delete(ctx, resGroup, lbName); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if _, err := future.Result(client); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Deleted loadBalancer") | |
} | |
} | |
func deleteNetworkInterface(authorizer autorest.Authorizer) { | |
client := network.NewInterfacesClient(subsID) | |
client.Authorizer = authorizer | |
if future, err := client.Delete(ctx, resGroup, interfaceName); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if _, err := future.Result(client); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Deleted network interface") | |
} | |
} | |
func deleteVirtualMachine(authorizer autorest.Authorizer) { | |
client := compute.NewVirtualMachinesClient(subsID) | |
client.Authorizer = authorizer | |
if future, err := client.Delete(ctx, resGroup, vmName); err != nil { | |
panic(err) | |
} else if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
panic(err) | |
} else if _, err := future.Result(client); err != nil { | |
panic(err) | |
} else { | |
fmt.Println("Deleted virtual machine") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment