Last active
July 14, 2021 21:21
-
-
Save ResidentMario/c864185d25c12dc43b50388dab311d5a to your computer and use it in GitHub Desktop.
Create Azure machine
This file contains hidden or 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
module go-azure | |
go 1.16 | |
require ( | |
github.com/Azure/azure-sdk-for-go v55.4.0+incompatible // indirect | |
github.com/Azure/go-autorest/autorest v0.11.19 // indirect | |
github.com/Azure/go-autorest/autorest/azure/auth v0.5.7 // indirect | |
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect | |
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect | |
) |
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
// "net/http" | |
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute" | |
"github.com/Azure/azure-sdk-for-go/profiles/latest/network/mgmt/network" | |
"github.com/Azure/go-autorest/autorest" | |
"github.com/Azure/go-autorest/autorest/azure/auth" | |
"github.com/Azure/go-autorest/autorest/to" | |
) | |
const subscriptionID = "0e47e142-f97c-4224-a431-a8dd863837dd" | |
func main() { | |
auth, err := GetClientAuthorizer() | |
if err != nil { | |
fmt.Println("%v", err) | |
return | |
} | |
vmClient := BuildVMClient(auth) | |
// diskClient := BuildDisksClient(auth) | |
ipClient := BuildPublicIPAddressesClient(auth) | |
nicClient := BuildNetworkInterfacesClient(auth) | |
region := "westus2" | |
resourceGroupName := "rg-spell-aleksey-local-azure-org" | |
pipName := "aleksey-test-ip" | |
nicName := "aleksey-test-nic" | |
vmName := "aleksey-test-vm" | |
subscriptionID := "0e47e142-f97c-4224-a431-a8dd863837dd" | |
subnetID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/spell-vnet/subnets/spell-subnet", | |
subscriptionID, resourceGroupName) | |
securityGroupID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/spell-security-group", | |
subscriptionID, resourceGroupName) | |
ctx := context.Background() | |
// Create PIP | |
ipFuture, stdErr := ipClient.CreateOrUpdate(ctx, resourceGroupName, pipName, network.PublicIPAddress{ | |
Location: ®ion, | |
}) | |
if stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
if stdErr := ipFuture.WaitForCompletionRef(ctx, ipClient.Client); stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
pip, stdErr := ipFuture.Result(ipClient) | |
fmt.Println("%v", pip) | |
if stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
// Create NIC | |
nicFuture, stdErr := nicClient.CreateOrUpdate(ctx, resourceGroupName, nicName, network.Interface{ | |
InterfacePropertiesFormat: &network.InterfacePropertiesFormat{ | |
IPConfigurations: &[]network.InterfaceIPConfiguration{ | |
{ | |
Name: to.StringPtr("main"), | |
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{ | |
PrivateIPAllocationMethod: network.IPAllocationMethodDynamic, | |
PublicIPAddress: &pip, | |
Subnet: &network.Subnet{ | |
ID: &subnetID, | |
}, | |
}, | |
}, | |
}, | |
NetworkSecurityGroup: &network.SecurityGroup{ | |
ID: &securityGroupID, | |
}, | |
}, | |
Location: ®ion, | |
}) | |
if stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
if stdErr := nicFuture.WaitForCompletionRef(ctx, nicClient.Client); stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
nic, stdErr := nicFuture.Result(nicClient) | |
if stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
fmt.Println("%v", nic) | |
backupVMConfig := compute.VirtualMachine{ | |
Location: to.StringPtr("westus2"), | |
VirtualMachineProperties: &compute.VirtualMachineProperties{ | |
HardwareProfile: &compute.HardwareProfile{ | |
VMSize: compute.VirtualMachineSizeTypesStandardF2sV2, | |
}, | |
NetworkProfile: &compute.NetworkProfile{ | |
NetworkInterfaces: &[]compute.NetworkInterfaceReference{ | |
{ | |
ID: nic.ID, | |
}, | |
}, | |
}, | |
// OsProfile: &compute.OSProfile{ | |
// AdminUsername: to.StringPtr("ubuntu"), | |
// ComputerName: &backupVMName, | |
// LinuxConfiguration: &compute.LinuxConfiguration{ | |
// DisablePasswordAuthentication: to.BoolPtr(true), | |
// SSH: &compute.SSHConfiguration{ | |
// PublicKeys: &[]compute.SSHPublicKey{ | |
// { | |
// Path: to.StringPtr("/home/ubuntu/.ssh/authorized_keys"), | |
// KeyData: &publicKey, | |
// }, | |
// }, | |
// }, | |
// }, | |
// }, | |
StorageProfile: &compute.StorageProfile{ | |
// ImageReference: &compute.ImageReference{ | |
// ID: &c.imageID, | |
// }, | |
OsDisk: &compute.OSDisk{ | |
CreateOption: compute.DiskCreateOptionTypesAttach, | |
Name: to.StringPtr("disk-spell-cpu-spot-7jrfatkz6j"), | |
// CONFIRMED: Name is not supposed to be fully qualified. | |
OsType: compute.OperatingSystemTypesLinux, | |
// CONFIRMED: ManagedDisk is non-optional. | |
ManagedDisk: &compute.ManagedDiskParameters{ | |
// StorageAccountType: compute.StorageAccountTypesPremiumLRS, | |
// CONFIRMED: ID is required, but what should it set to? | |
// FAIL: | |
// ID: to.StringPtr("/subscriptions/0e47e142-f97c-4224-a431-a8dd863837dd/resourceGroups/rg-spell-public-images/providers/Microsoft.Compute/galleries/SpellPublicImages/images/spell-worker-gpu-gen2/versions/0.143.0"), | |
// FAIL: | |
// ID: to.StringPtr("/subscriptions/0e47e142-f97c-4224-a431-a8dd863837dd/resourceGroups/rg-spell-public-images/providers/Microsoft.Compute/galleries/SpellPublicImages/images/spell-worker-gpu-gen2"), | |
ID: to.StringPtr("/subscriptions/0e47e142-f97c-4224-a431-a8dd863837dd/resourceGroups/RG-SPELL-ALEKSEY-LOCAL-AZURE-ORG/providers/Microsoft.Compute/disks/disk-spell-cpu-spot-7jrfatkz6j"), | |
}, | |
}, | |
}, | |
}, | |
} | |
// Added an Owner role assignment: | |
// https://portal.azure.com/#@spspell.onmicrosoft.com/resource/subscriptions/0e47e142-f97c-4224-a431-a8dd863837dd/resourceGroups/rg-spell-public-images/providers/Microsoft.Compute/galleries/SpellPublicImages/images/spell-worker-gpu-gen2/users | |
vmFuture, stdErr := vmClient.CreateOrUpdate(ctx, resourceGroupName, vmName, backupVMConfig) | |
if stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
if stdErr := vmFuture.WaitForCompletionRef(ctx, vmClient.Client); stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
vm, stdErr := vmFuture.Result(vmClient) | |
if stdErr != nil { | |
fmt.Println("%v", stdErr) | |
return | |
} | |
fmt.Println("%v", vm) | |
} | |
func GetClientAuthorizer() (autorest.Authorizer, error) { | |
clientCreds := auth.NewClientCredentialsConfig( | |
"02db84e7-217e-486c-ba5f-61d9e42c0ada", | |
"75T~NF-JbfUT.8cn.HPc1JiYcM72_1j~oq", | |
"c3084a14-3305-4fee-98fc-4f70239eec3f", | |
) | |
authorizer, e := clientCreds.Authorizer() | |
if e != nil { | |
return nil, e | |
} | |
return authorizer, nil | |
} | |
func BuildVMClient(authorizer autorest.Authorizer) compute.VirtualMachinesClient { | |
vmClient := compute.NewVirtualMachinesClient(subscriptionID) | |
vmClient.Authorizer = authorizer | |
// vmClient.Sender = &http.Client{} | |
return vmClient | |
} | |
func BuildDisksClient(authorizer autorest.Authorizer) compute.DisksClient { | |
diskClient := compute.NewDisksClient(subscriptionID) | |
diskClient.Authorizer = authorizer | |
// diskClient.Sender = &http.Client{} | |
return diskClient | |
} | |
func BuildPublicIPAddressesClient(authorizer autorest.Authorizer) network.PublicIPAddressesClient { | |
ipClient := network.NewPublicIPAddressesClient(subscriptionID) | |
ipClient.Authorizer = authorizer | |
// ipClient.Sender = &http.Client{} | |
return ipClient | |
} | |
func BuildNetworkInterfacesClient(authorizer autorest.Authorizer) network.InterfacesClient { | |
nicClient := network.NewInterfacesClient(subscriptionID) | |
nicClient.Authorizer = authorizer | |
// nicClient.Sender = &http.Client{} | |
return nicClient | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment