Пример #1
0
        private async Task FindResourceGroupAsync(string resourceGroupName, CancellationToken cancellationToken)
        {
            Debug.Assert(subscriptionId != null);

            if (!string.IsNullOrEmpty(resourceGroupName))
            {
                var resourceGroup = await resourceManager.GetAsync($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}",
                                                                   cancellationToken);

                this.resourceGroupName     = resourceGroupName;
                this.resourceGroupLocation = resourceGroup.Value <string>("location");
                return;
            }

            var resourceGroups = (await resourceManager.GetAsync($"/subscriptions/{subscriptionId}/resourceGroups",
                                                                 cancellationToken))["value"].ToArray();

            if (resourceGroups.Length == 0)
            {
                throw new ArgumentException($"No Resource Groups found in the subscription {subscriptionId}");
            }

            int num;

            if (resourceGroups.Length > 1)
            {
                Console.WriteLine("Resource Groups found in the subscription:");
                Console.WriteLine();
                for (int i = 0; i < resourceGroups.Length; i++)
                {
                    Console.WriteLine("[{0}] {1}", i + 1, resourceGroups[i].Value <string>("name"));
                }
                Console.WriteLine();
                Console.Write("Please choose the Resource Group number to use: ");
                string response = Console.ReadLine();
                if (!int.TryParse(response, out num) || num < 1 || num > resourceGroups.Length)
                {
                    throw new ArgumentException("Invalid Resource Group number");
                }
                Console.WriteLine();
            }
            else
            {
                num = 1;
            }

            Debug.Assert(num > 0 && num <= resourceGroups.Length);
            this.resourceGroupName = resourceGroups[num - 1].Value <string>("name");
            resourceGroupLocation  = resourceGroups[num - 1].Value <string>("location");
        }
Пример #2
0
        private async Task CreateVirtualMachineAsync(string rootUsername, string sshPublicKey,
                                                     string vmSize, CancellationToken cancellationToken)
        {
            Debug.Assert(networkInterfaceCardId != null);

            Console.Write("Creating Virtual Machine...");

            var id = $"{uniqueResourceIdentifier}-vm";

            var storageProfile = new JObject()
            {
                { "imageReference", new JObject()
                  {
                      { "publisher", "Canonical" },
                      { "offer", "UbuntuServer" },
                      { "sku", "16.04-LTS" },
                      { "version", "latest" }
                  } },
                { "osDisk", new JObject()
                  {
                      { "createOption", "fromImage" },
                  } }
            };

            var osProfile = new JObject()
            {
                { "computerName", id },
                { "adminUsername", rootUsername },
                { "linuxConfiguration", new JObject()
                  {
                      { "disablePasswordAuthentication", true },
                      { "ssh", new JObject()
                        {
                            { "publicKeys", new JArray()
                            {
                                new JObject()
                                {
                                    { "path", $"/home/{rootUsername}/.ssh/authorized_keys" },
                                    { "keyData", sshPublicKey }
                                }
                            } }
                        } }
                  } }
            };

            var networkProfile = new JObject()
            {
                { "networkInterfaces", new JArray()
                  {
                      new JObject()
                      {
                          { "id", networkInterfaceCardId },
                          { "properties", new JObject()
                            {
                                { "primary", true }
                            } }
                      }
                  } }
            };

            var diagnosticsProfile = new JObject()
            {
                { "bootDiagnostics", new JObject()
                  {
                      { "enabled", false }
                  } }
            };

            JToken vm = new JObject()
            {
                { "name", id },
                { "location", targetVM.ResourceGroupLocation },
                { "properties", new JObject()
                  {
                      { "hardwareProfile", new JObject()
                        {
                            { "vmSize", vmSize }
                        } },
                      { "storageProfile", storageProfile },
                      { "osProfile", osProfile },
                      { "networkProfile", networkProfile },
                      { "diagnosticsProfile", diagnosticsProfile }
                  } }
            };

            virtualMachineId = $"/subscriptions/{targetVM.SubscriptionId}/resourceGroups/{targetVM.ResourceGroupName}" +
                               $"/providers/Microsoft.Compute/virtualMachines/{id}?api-version=2017-03-30";

            await resourceManager.PutAsync($"{virtualMachineId}", vm.ToString(), cancellationToken);

            do
            {
                Console.Write(".");
                await Task.Delay(TimeSpan.FromSeconds(5));

                vm = await resourceManager.GetAsync(virtualMachineId, cancellationToken);

                UpdateVirtualMachineAssets(vm);
            } while (!IsVmRunning(vm));
            Console.WriteLine("done ({0})", id);
        }