Example #1
0
        static void Main(string[] args)
        {
            //Set variables
            var location               = "redmond";
            var baseUriString          = "https://management.redmond.ext-n22r1002.masd.stbtest.microsoft.com/";
            var resourceGroupName      = "testrg";
            var servicePrincipalId     = "6656e01b-b5ce-43be-99b6-2f109e255343";
            var servicePrincipalSecret = "e69Tve6T4XvtQUrvFXVR1B5X4ZYBS+QNY3zKQ5s2JHA=";
            var azureResourceId        = "https://management.azurestackci07.onmicrosoft.com/29ae84a9-c761-4678-8cf6-0c28b6952a41";
            var tenantId               = "2b3697e6-a7a2-4cdd-a3d4-f4ef6505cd4f";
            var subscriptionId         = "7b5e2e72-d4ca-49de-b45f-b814d2b3aa07";
            var vmName             = "virtualMachineName";
            var vnetName           = "virtualNetworkName";
            var subnetName         = "subnetName";
            var subnetAddress      = "10.0.0.0/24";
            var vnetAddresses      = "10.0.0.0/16";
            var ipName             = "ipName";
            var nicName            = "networkInterfaceName";
            var storagePrefix      = "redmond.ext-n22r1002.masd.stbtest.microsoft.com";
            var storageAccountName = "storageaccountt";

            Console.WriteLine("Get credential token");
            var credentials = new CustomLoginCredentials(servicePrincipalId, servicePrincipalSecret, azureResourceId, tenantId);

            try
            {
                Console.WriteLine("Instantiate resource management client");
                var rmClient = GetResourceManagementClient(new Uri(baseUriString), credentials, subscriptionId);

                Console.WriteLine("Create resource group");
                var rmTask = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    new Profile2018ResourceManager.Models.ResourceGroup
                {
                    Location = location
                });
                rmTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create resource group. Exception: {0}", ex.Message));
            }

            Console.WriteLine("Instantiate network client");
            var networkClient = GetNetworkClient(new Uri(baseUriString), credentials, subscriptionId);
            var subnet        = new Profile2018Network.Models.Subnet();

            try
            {
                Console.WriteLine("Create vitual network");
                var vnet = new Profile2018Network.Models.VirtualNetwork
                {
                    Location     = location,
                    AddressSpace = new Profile2018Network.Models.AddressSpace
                    {
                        AddressPrefixes = new List <string> {
                            vnetAddresses
                        }
                    },
                    Subnets = new List <Profile2018Network.Models.Subnet>
                    {
                        new Profile2018Network.Models.Subnet
                        {
                            AddressPrefix = subnetAddress,
                            Name          = subnetName
                        }
                    }
                };
                var vnetTask = networkClient.VirtualNetworks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vnetName,
                    vnet);
                vnetTask.Wait();
                subnet = vnetTask.Result.Body.Subnets[0];
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual network. Exception: {0}", ex.Message));
            }

            var ip = new Profile2018Network.Models.PublicIPAddress();

            try
            {
                Console.WriteLine("Create IP");
                var ipProperties = new Profile2018Network.Models.PublicIPAddress
                {
                    Location = location,
                    PublicIPAllocationMethod = Profile2018Network.Models.IPAllocationMethod.Dynamic,
                };
                var ipTask = networkClient.PublicIPAddresses.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    ipName,
                    ipProperties);
                ipTask.Wait();
                ip = ipTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create IP. Exception: {0}", ex.Message));
            }

            var nic = new Profile2018Network.Models.NetworkInterface();

            try
            {
                Console.WriteLine("Create network interface");
                var nicProperties = new Profile2018Network.Models.NetworkInterface
                {
                    Location         = location,
                    IpConfigurations = new List <Profile2018Network.Models.NetworkInterfaceIPConfiguration>
                    {
                        new Profile2018Network.Models.NetworkInterfaceIPConfiguration
                        {
                            Name = string.Format("{0}-ipconfig", nicName),
                            PrivateIPAllocationMethod = "Dynamic",
                            PublicIPAddress           = ip,
                            Subnet = subnet
                        }
                    }
                };

                var nicTask = networkClient.NetworkInterfaces.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    nicName,
                    nicProperties);
                nicTask.Wait();
                nic = nicTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create network interface. Exception: {0}", ex.Message));
            }

            var storage       = new Profile2018Storage.Models.StorageAccount();
            var storageClient = GetStorageClient(new Uri(baseUriString), credentials, subscriptionId);

            try
            {
                Console.WriteLine("Create storage account");
                var storageProperties = new Profile2018Storage.Models.StorageAccountCreateParameters
                {
                    Location = location,
                    Kind     = Profile2018Storage.Models.Kind.Storage,
                    Sku      = new Profile2018Storage.Models.Sku(Profile2018Storage.Models.SkuName.StandardLRS)
                };

                var storageTask = storageClient.StorageAccounts.CreateWithHttpMessagesAsync(resourceGroupName, storageAccountName, storageProperties);
                storageTask.Wait();
                storage = storageTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create network interface. Exception: {0}", ex.Message));
            }

            try
            {
                Console.WriteLine("Instantiate compute client");
                var computeClient = GetComputeClient(new Uri(baseUriString), credentials, subscriptionId);

                Console.WriteLine("Create virtual machine");
                var vmParameters = new Profile2018Compute.Models.VirtualMachine
                {
                    Location       = location,
                    NetworkProfile = new Profile2018Compute.Models.NetworkProfile
                    {
                        NetworkInterfaces = new List <Profile2018Compute.Models.NetworkInterfaceReference>
                        {
                            new Profile2018Compute.Models.NetworkInterfaceReference
                            {
                                Id      = nic.Id,
                                Primary = true
                            }
                        }
                    },
                    StorageProfile = new Profile2018Compute.Models.StorageProfile
                    {
                        OsDisk = new Profile2018Compute.Models.OSDisk
                        {
                            Name = "osDisk",
                            Vhd  = new Profile2018Compute.Models.VirtualHardDisk
                            {
                                Uri = string.Format(vhdURItemplate, storageAccountName, storagePrefix, vmName)
                            },
                            CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.FromImage
                        },
                        ImageReference = new Profile2018Compute.Models.ImageReference
                        {
                            Publisher = "Canonical",
                            Offer     = "UbuntuServer",
                            Sku       = "16.04-LTS",
                            Version   = "latest"
                        }
                    },
                    OsProfile = new Profile2018Compute.Models.OSProfile
                    {
                        ComputerName  = vmName,
                        AdminUsername = "******",
                        AdminPassword = "******"
                    },
                    HardwareProfile = new Profile2018Compute.Models.HardwareProfile
                    {
                        VmSize = "Standard_A1"
                    }
                };

                var vmTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vmName,
                    vmParameters);
                vmTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual machine. Exception: {0}", ex.Message));
            }
        }
        static void Main(string[] args)
        {
            //Set variables
            var location               = "location";
            var baseUriString          = "baseUriString";
            var resourceGroupName      = "resourceGroupName";
            var servicePrincipalId     = "servicePrincipalID";
            var servicePrincipalSecret = "servicePrincipalSecret";
            var azureResourceId        = "resourceID";
            var tenantId               = "tenantID";
            var subscriptionId         = "subscriptionID";
            var vmName        = "virtualMachineName";
            var vnetName      = "virtualNetworkName";
            var subnetName    = "subnetName";
            var subnetAddress = "10.0.0.0/24";
            var vnetAddresses = "10.0.0.0/16";
            var ipName        = "ipName";
            var nicName       = "networkInterfaceName";
            var diskName      = "diskName";

            Console.WriteLine("Get credential token");
            var credentials = new CustomLoginCredentials(servicePrincipalId, servicePrincipalSecret, azureResourceId, tenantId);

            try
            {
                Console.WriteLine("Instantiate resource management client");
                var rmClient = GetResourceManagementClient(new Uri(baseUriString), credentials, subscriptionId);

                Console.WriteLine("Create resource group");
                var rmTask = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    new Profile2018ResourceManager.Models.ResourceGroup
                {
                    Location = location
                });
                rmTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create resource group. Exception: {0}", ex.Message));
            }

            Console.WriteLine("Instantiate network client");
            var networkClient = GetNetworkClient(new Uri(baseUriString), credentials, subscriptionId);
            var subnet        = new Profile2018Network.Models.Subnet();

            try
            {
                Console.WriteLine("Create vitual network");
                var vnet = new Profile2018Network.Models.VirtualNetwork
                {
                    Location     = location,
                    AddressSpace = new Profile2018Network.Models.AddressSpace
                    {
                        AddressPrefixes = new List <string> {
                            vnetAddresses
                        }
                    },
                    Subnets = new List <Profile2018Network.Models.Subnet>
                    {
                        new Profile2018Network.Models.Subnet
                        {
                            AddressPrefix = subnetAddress,
                            Name          = subnetName
                        }
                    }
                };
                var vnetTask = networkClient.VirtualNetworks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vnetName,
                    vnet);
                vnetTask.Wait();
                subnet = vnetTask.Result.Body.Subnets[0];
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual network. Exception: {0}", ex.Message));
            }

            var ip = new Profile2018Network.Models.PublicIPAddress();

            try
            {
                Console.WriteLine("Create IP");
                var ipProperties = new Profile2018Network.Models.PublicIPAddress
                {
                    Location = location,
                    PublicIPAllocationMethod = Profile2018Network.Models.IPAllocationMethod.Dynamic,
                };
                var ipTask = networkClient.PublicIPAddresses.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    ipName,
                    ipProperties);
                ipTask.Wait();
                ip = ipTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create IP. Exception: {0}", ex.Message));
            }

            var nic = new Profile2018Network.Models.NetworkInterface();

            try
            {
                Console.WriteLine("Create network interface");
                var nicProperties = new Profile2018Network.Models.NetworkInterface
                {
                    Location         = location,
                    IpConfigurations = new List <Profile2018Network.Models.NetworkInterfaceIPConfiguration>
                    {
                        new Profile2018Network.Models.NetworkInterfaceIPConfiguration
                        {
                            Name = string.Format("{0}-ipconfig", nicName),
                            PrivateIPAllocationMethod = "Dynamic",
                            PublicIPAddress           = ip,
                            Subnet = subnet
                        }
                    }
                };

                var nicTask = networkClient.NetworkInterfaces.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    nicName,
                    nicProperties);
                nicTask.Wait();
                nic = nicTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create network interface. Exception: {0}", ex.Message));
            }

            try
            {
                Console.WriteLine("Instantiate compute client");
                var computeClient  = GetComputeClient(new Uri(baseUriString), credentials, subscriptionId);
                var diskProperties = new Profile2018Compute.Models.Disk
                {
                    CreationData = new Profile2018Compute.Models.CreationData
                    {
                        CreateOption = Profile2018Compute.Models.DiskCreateOption.Empty,
                    },
                    Location = location,
                    Sku      = new Profile2018Compute.Models.DiskSku
                    {
                        Name = Profile2018Compute.Models.StorageAccountTypes.StandardLRS
                    },
                    DiskSizeGB = 1,
                };
                var diskTask = computeClient.Disks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    diskName,
                    diskProperties);
                diskTask.Wait();

                Console.WriteLine("Create virtual machine");
                var vmProperties = new Profile2018Compute.Models.VirtualMachine
                {
                    Location       = location,
                    NetworkProfile = new Profile2018Compute.Models.NetworkProfile
                    {
                        NetworkInterfaces = new List <Profile2018Compute.Models.NetworkInterfaceReference>
                        {
                            new Profile2018Compute.Models.NetworkInterfaceReference
                            {
                                Id      = nic.Id,
                                Primary = true
                            }
                        }
                    },
                    StorageProfile = new Profile2018Compute.Models.StorageProfile
                    {
                        DataDisks = new List <Profile2018Compute.Models.DataDisk>
                        {
                            new Profile2018Compute.Models.DataDisk
                            {
                                CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.Attach,
                                ManagedDisk  = new Profile2018Compute.Models.ManagedDiskParameters
                                {
                                    StorageAccountType = Profile2018Compute.Models.StorageAccountTypes.StandardLRS,
                                    Id = diskTask.Result.Body.Id
                                }
                            }
                        },
                        OsDisk = new Profile2018Compute.Models.OSDisk
                        {
                            Name         = "osDisk",
                            CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.FromImage
                        },
                        ImageReference = new Profile2018Compute.Models.ImageReference
                        {
                            Publisher = "Canonical",
                            Offer     = "UbuntuServer",
                            Sku       = "16.04-LTS",
                            Version   = "latest"
                        }
                    },
                    OsProfile = new Profile2018Compute.Models.OSProfile
                    {
                        ComputerName  = vmName,
                        AdminUsername = "******",
                        AdminPassword = "******"
                    },
                    HardwareProfile = new Profile2018Compute.Models.HardwareProfile
                    {
                        VmSize = "Standard_A1"
                    }
                };

                var vmTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vmName,
                    vmProperties);
                vmTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual machine. Exception: {0}", ex.Message));
            }
        }
        static void runSample(string tenantId, string subscriptionId, string servicePrincipalId, string servicePrincipalSecret, string location, string armEndpoint)
        {
            var resourceGroupName  = SdkContext.RandomResourceName("rgDotnetSdk", 24);
            var vmName             = SdkContext.RandomResourceName("vmDotnetSdk", 24);
            var vmNameManagedDisk  = SdkContext.RandomResourceName("vmManagedDotnetSdk", 24);
            var vnetName           = SdkContext.RandomResourceName("vnetDotnetSdk", 24);
            var subnetName         = SdkContext.RandomResourceName("subnetDotnetSdk", 24);
            var subnetAddress      = "10.0.0.0/24";
            var vnetAddresses      = "10.0.0.0/16";
            var ipName             = SdkContext.RandomResourceName("ipDotnetSdk", 24);
            var nicName            = SdkContext.RandomResourceName("nicDotnetSdk", 24);;
            var diskName           = SdkContext.RandomResourceName("diskDotnetSdk", 24);
            var storageAccountName = SdkContext.RandomResourceName("storageaccount", 18);
            var username           = "******";
            var password           = "******";

            Console.WriteLine("Get credential token");
            var adSettings  = getActiveDirectoryServiceSettings(armEndpoint);
            var credentials = ApplicationTokenProvider.LoginSilentAsync(tenantId, servicePrincipalId, servicePrincipalSecret, adSettings).GetAwaiter().GetResult();

            Console.WriteLine("Instantiate resource management client");
            var rmClient = GetResourceManagementClient(new Uri(armEndpoint), credentials, subscriptionId);

            Console.WriteLine("Instantiate storage account client");
            var storageClient = GetStorageClient(new Uri(armEndpoint), credentials, subscriptionId);

            Console.WriteLine("Instantiate network client");
            var networkClient = GetNetworkClient(new Uri(armEndpoint), credentials, subscriptionId);

            Console.WriteLine("Instantiate compute client");
            var computeClient = GetComputeClient(new Uri(armEndpoint), credentials, subscriptionId);

            // Create a resource group
            try
            {
                Console.WriteLine("Create resource group");
                var rmTask = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    new Profile2018ResourceManager.Models.ResourceGroup
                {
                    Location = location
                });
                rmTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create resource group. Exception: {0}", ex.Message));
            }

            // Create a Storage Account
            var storageAccount = new Profile2018Storage.Models.StorageAccount();

            try
            {
                Console.WriteLine(String.Format("Creating a storage account with name:{0}", storageAccountName));
                var storageProperties = new Profile2018Storage.Models.StorageAccountCreateParameters
                {
                    Location = location,
                    Kind     = Profile2018Storage.Models.Kind.Storage,
                    Sku      = new Profile2018Storage.Models.Sku(Profile2018Storage.Models.SkuName.StandardLRS)
                };

                var storageTask = storageClient.StorageAccounts.CreateWithHttpMessagesAsync(resourceGroupName, storageAccountName, storageProperties);
                storageTask.Wait();
                storageAccount = storageTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create storage account {0}. Exception: {1}", storageAccountName, ex.Message));
            }

            var subnet = new Profile2018Network.Models.Subnet();

            // Create virtual network
            try
            {
                Console.WriteLine("Create vitual network");
                var vnet = new Profile2018Network.Models.VirtualNetwork
                {
                    Location     = location,
                    AddressSpace = new Profile2018Network.Models.AddressSpace
                    {
                        AddressPrefixes = new List <string> {
                            vnetAddresses
                        }
                    }
                };
                var vnetTask = networkClient.VirtualNetworks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vnetName,
                    vnet);
                vnetTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual network. Exception: {0}", ex.Message));
            }

            // Create subnet in the virtual network
            try
            {
                Console.WriteLine("Create a subnet");
                var subnetTask = networkClient.Subnets.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vnetName, subnetName, new Profile2018Network.Models.Subnet
                {
                    AddressPrefix = subnetAddress,
                    Name          = subnetName
                });
                subnetTask.Wait();
                subnet = subnetTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create subnet. Exception: {0}", ex.Message));
            }

            // Create a public address
            var ip = new Profile2018Network.Models.PublicIPAddress();

            try
            {
                Console.WriteLine("Create IP");
                var ipProperties = new Profile2018Network.Models.PublicIPAddress
                {
                    Location = location,
                    PublicIPAllocationMethod = Profile2018Network.Models.IPAllocationMethod.Dynamic,
                };
                var ipTask = networkClient.PublicIPAddresses.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    ipName,
                    ipProperties);
                ipTask.Wait();
                ip = ipTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create IP. Exception: {0}", ex.Message));
            }

            // Create a network interface
            var nic = new Profile2018Network.Models.NetworkInterface();
            var vmStorageProfile = new Profile2018Compute.Models.StorageProfile();

            try
            {
                Console.WriteLine("Create network interface");
                var nicProperties = new Profile2018Network.Models.NetworkInterface
                {
                    Location         = location,
                    IpConfigurations = new List <Profile2018Network.Models.NetworkInterfaceIPConfiguration>
                    {
                        new Profile2018Network.Models.NetworkInterfaceIPConfiguration
                        {
                            Name = string.Format("{0}-ipconfig", nicName),
                            PrivateIPAllocationMethod = "Dynamic",
                            PublicIPAddress           = ip,
                            Subnet = subnet
                        }
                    }
                };

                var nicTask = networkClient.NetworkInterfaces.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    nicName,
                    nicProperties);
                nicTask.Wait();
                nic = nicTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create network interface. Exception: {0}", ex.Message));
            }

            // Create a data disk
            var disk = new Profile2018Compute.Models.Disk();

            try
            {
                Console.WriteLine("Create a data disk");
                var diskProperties = new Profile2018Compute.Models.Disk
                {
                    CreationData = new Profile2018Compute.Models.CreationData
                    {
                        CreateOption = Profile2018Compute.Models.DiskCreateOption.Empty,
                    },
                    Location = location,
                    Sku      = new Profile2018Compute.Models.DiskSku
                    {
                        Name = Profile2018Compute.Models.StorageAccountTypes.StandardLRS
                    },
                    DiskSizeGB = 1,
                };
                var diskTask = computeClient.Disks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    diskName,
                    diskProperties);
                diskTask.Wait();
                disk = diskTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create data disk. Exception: {0}", ex.Message));
            }

            // VM Hardware profile
            var vmHardwareProfile = new Profile2018Compute.Models.HardwareProfile
            {
                VmSize = "Standard_A1"
            };

            // VM OS Profile
            var vmOsProfile = new Profile2018Compute.Models.OSProfile
            {
                ComputerName  = vmName,
                AdminUsername = username,
                AdminPassword = password
            };

            // VM Network profile
            var vmNetworkProfile = new Profile2018Compute.Models.NetworkProfile
            {
                NetworkInterfaces = new List <Profile2018Compute.Models.NetworkInterfaceReference>
                {
                    new Profile2018Compute.Models.NetworkInterfaceReference
                    {
                        Id      = nic.Id,
                        Primary = true
                    }
                }
            };

            // VM Storage profile
            string diskUri    = string.Format("{0}test/{1}.vhd", storageAccount.PrimaryEndpoints.Blob, diskName);
            var    osDiskName = "osDisk";
            string osDiskUri  = string.Format("{0}test/{1}.vhd", storageAccount.PrimaryEndpoints.Blob, osDiskName);

            vmStorageProfile = new Profile2018Compute.Models.StorageProfile
            {
                OsDisk = new Profile2018Compute.Models.OSDisk
                {
                    Name         = osDiskName,
                    CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.FromImage,
                    Caching      = Profile2018Compute.Models.CachingTypes.ReadWrite,
                    OsType       = Profile2018Compute.Models.OperatingSystemTypes.Linux,
                    Vhd          = new Profile2018Compute.Models.VirtualHardDisk
                    {
                        Uri = osDiskUri
                    }
                },
                ImageReference = new Profile2018Compute.Models.ImageReference
                {
                    Publisher = "Canonical",
                    Offer     = "UbuntuServer",
                    Sku       = "16.04-LTS",
                    Version   = "latest"
                },
                DataDisks = null
            };

            // Create Linux VM
            var linuxVm = new Profile2018Compute.Models.VirtualMachine();

            try
            {
                Console.WriteLine("Create a virtual machine");
                var t1     = DateTime.Now;
                var vmTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vmName,
                    new Profile2018Compute.Models.VirtualMachine
                {
                    Location        = location,
                    NetworkProfile  = vmNetworkProfile,
                    StorageProfile  = vmStorageProfile,
                    OsProfile       = vmOsProfile,
                    HardwareProfile = vmHardwareProfile
                });
                vmTask.Wait();
                linuxVm = vmTask.Result.Body;
                var t2 = DateTime.Now;
                vmStorageProfile = linuxVm.StorageProfile;
                Console.WriteLine(String.Format("Create virtual machine {0} took {1} seconds", linuxVm.Id, (t2 - t1).TotalSeconds.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual machine. Exception: {0}", ex.Message));
            }

            // Update - Tag the virtual machine
            try
            {
                Console.WriteLine("Tag virtual machine");
                var vmTagTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, new Profile2018Compute.Models.VirtualMachine
                {
                    Location = location,
                    Tags     = new Dictionary <string, string> {
                        { "who-rocks", "java" }, { "where", "on azure stack" }
                    }
                });
                vmTagTask.Wait();
                linuxVm = vmTagTask.Result.Body;
                Console.WriteLine(string.Format("Taged virtual machine {0}", linuxVm.Id));
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not tag virtual machine. Exception: {0}", ex.Message));
            }

            // Update - Add data disk
            try
            {
                Console.WriteLine("Attach data disk to virtual machine");
                string newDataDiskName   = "dataDisk2";
                string newDataDiskVhdUri = string.Format("{0}test/{1}.vhd", storageAccount.PrimaryEndpoints.Blob, newDataDiskName);
                var    dataDisk          = new Profile2018Compute.Models.DataDisk
                {
                    CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.Empty,
                    Caching      = Profile2018Compute.Models.CachingTypes.ReadOnly,
                    DiskSizeGB   = 1,
                    Lun          = 2,
                    Name         = newDataDiskName,
                    Vhd          = new Profile2018Compute.Models.VirtualHardDisk
                    {
                        Uri = newDataDiskVhdUri
                    }
                };
                vmStorageProfile.DataDisks.Add(dataDisk);
                var addTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, new Profile2018Compute.Models.VirtualMachine
                {
                    Location       = location,
                    StorageProfile = vmStorageProfile
                });
                addTask.Wait();
                vmStorageProfile = addTask.Result.Body.StorageProfile;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not add data disk to virtual machine. Exception: {0}", ex.Message));
            }

            // Update - detach data disk
            try
            {
                Console.WriteLine("Detach data disk from virtual machine");
                vmStorageProfile.DataDisks.RemoveAt(0);
                var detachTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, new Profile2018Compute.Models.VirtualMachine {
                    Location       = location,
                    StorageProfile = vmStorageProfile
                });
                detachTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not detach data disk from virtual machine. Exception: {0}", ex.Message));
            }

            // Restart the virtual machine
            try
            {
                Console.WriteLine("Restart virtual machine");
                var restartTask = computeClient.VirtualMachines.RestartWithHttpMessagesAsync(resourceGroupName, vmName);
                restartTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not restart virtual machine. Exception: {0}", ex.Message));
            }

            // Stop(powerOff) the virtual machine
            try
            {
                Console.WriteLine("Power off virtual machine");
                var stopTask = computeClient.VirtualMachines.PowerOffWithHttpMessagesAsync(resourceGroupName, vmName);
                stopTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not power off virtual machine. Exception: {0}", ex.Message));
            }

            // Delete VM
            try
            {
                Console.WriteLine("Delete virtual machine");
                var deleteTask = computeClient.VirtualMachines.DeleteWithHttpMessagesAsync(resourceGroupName, vmName);
                deleteTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not delete virtual machine. Exception: {0}", ex.Message));
            }

            // VM Storage profile managed disk
            vmStorageProfile = new Profile2018Compute.Models.StorageProfile
            {
                DataDisks = new List <Profile2018Compute.Models.DataDisk>
                {
                    new Profile2018Compute.Models.DataDisk
                    {
                        CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.Attach,
                        ManagedDisk  = new Profile2018Compute.Models.ManagedDiskParameters
                        {
                            StorageAccountType = Profile2018Compute.Models.StorageAccountTypes.StandardLRS,
                            Id = disk.Id
                        },
                        Caching    = Profile2018Compute.Models.CachingTypes.ReadOnly,
                        DiskSizeGB = 1,
                        Lun        = 1,
                        Name       = diskName,
                    }
                },
                OsDisk = new Profile2018Compute.Models.OSDisk
                {
                    Name         = osDiskName,
                    CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.FromImage,
                },
                ImageReference = new Profile2018Compute.Models.ImageReference
                {
                    Publisher = "Canonical",
                    Offer     = "UbuntuServer",
                    Sku       = "16.04-LTS",
                    Version   = "latest"
                }
            };

            // Create Linux VM with managed disks
            var linuxVmManagedDisk = new Profile2018Compute.Models.VirtualMachine();

            try
            {
                Console.WriteLine("Create a virtual machine with managed disk");
                var t1     = DateTime.Now;
                var vmTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vmNameManagedDisk,
                    new Profile2018Compute.Models.VirtualMachine
                {
                    Location        = location,
                    NetworkProfile  = vmNetworkProfile,
                    StorageProfile  = vmStorageProfile,
                    OsProfile       = vmOsProfile,
                    HardwareProfile = vmHardwareProfile
                });
                vmTask.Wait();
                linuxVmManagedDisk = vmTask.Result.Body;
                var t2 = DateTime.Now;
                vmStorageProfile = linuxVm.StorageProfile;
                Console.WriteLine(String.Format("Create virtual machine with managed disk {0} took {1} seconds", linuxVmManagedDisk.Id, (t2 - t1).TotalSeconds.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual machine with managed disk. Exception: {0}", ex.Message));
            }

            // Delete VM with managed disk
            try
            {
                Console.WriteLine("Delete virtual machine with managed disk");
                var deleteTask = computeClient.VirtualMachines.DeleteWithHttpMessagesAsync(resourceGroupName, vmNameManagedDisk);
                deleteTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not delete virtual machine with managed disk. Exception: {0}", ex.Message));
            }
        }
Example #4
0
        public async Task <AzureOperationResponse <Profile2018Network.Models.PublicIPAddress> > CreatePublicIpAddress(
            string publicIpName,
            string resourceGroupName,
            string location,
            string allocationMethod = "Dynamic",
            string publicIpAddress  = null,
            IList <Tuple <string, string> > tags = null)
        {
            if (client == null)
            {
                return(new AzureOperationResponse <Profile2018Network.Models.PublicIPAddress>
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = System.Net.HttpStatusCode.ExpectationFailed,
                        ReasonPhrase = "Client is not instantiated"
                    }
                });
            }

            var publicIp = new Profile2018Network.Models.PublicIPAddress
            {
                Location = location,
            };

            if (String.Equals("dynamic", allocationMethod, StringComparison.OrdinalIgnoreCase))
            {
                allocationMethod = Profile2018Network.Models.IPAllocationMethod.Dynamic;
            }
            else
            {
                allocationMethod = Profile2018Network.Models.IPAllocationMethod.Static;
                if (!String.IsNullOrEmpty(publicIpAddress))
                {
                    publicIp.IpAddress = publicIpAddress;
                }
            }
            publicIp.PublicIPAllocationMethod = allocationMethod;

            foreach (var tag in tags ?? new List <Tuple <string, string> >())
            {
                publicIp.Tags.Add(tag.Item1, tag.Item2);
            }

            try
            {
                var publicIpTask = await client.PublicIPAddresses.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName : resourceGroupName,
                    publicIpAddressName : publicIpName,
                    parameters : publicIp);

                return(publicIpTask);
            }
            catch (Exception ex)
            {
                return(new AzureOperationResponse <Profile2018Network.Models.PublicIPAddress>
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        ReasonPhrase = ex.Message
                    }
                });
            }
        }