Exemple #1
0
        public static async Task <string> createVMsAsync(string customerId, string subscriptionId, string groupName, string vmName, string location)
        {
            // Get Template into a string
            // Otherwise, the property "TemplateLink" might be used as well in the deployment creation
            // See https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.resourcemanager.models.deploymentproperties.templatelink?view=azure-dotnet
            var    myTemplate  = "";
            string templateUrl = "https://raw.githubusercontent.com/erjosito/AzureBlackMagic/master/genericLinuxVM-count-templ.json";
            var    webRequest  = System.Net.WebRequest.Create(templateUrl);

            using (var response = webRequest.GetResponse())
                using (var content = response.GetResponseStream())
                    using (var reader = new StreamReader(content))
                    {
                        myTemplate = reader.ReadToEnd();
                    }

            // Get token from ARM API
            string token = await REST.getArmTokenAsync(customerId, UserAuth : true);

            var credential = new TokenCredentials(token);
            var armClient  = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(credential)
            {
                SubscriptionId = subscriptionId
            };

            // Define parameters for template
            string vmAdminUsername = System.Configuration.ConfigurationManager.AppSettings["vmAdminUsername"];
            string vmAdminPassword = System.Configuration.ConfigurationManager.AppSettings["vmAdminPassword"];
            var    myParameters    = new Dictionary <string, Dictionary <string, object> > {
                { "vmName", new Dictionary <string, object> {
                      { "value", vmName }
                  } },
                { "vmType", new Dictionary <string, object> {
                      { "value", "centos" }
                  } },
                { "vmSize", new Dictionary <string, object> {
                      { "value", "Standard_B1s" }
                  } },
                { "installExtension", new Dictionary <string, object> {
                      { "value", "yes" }
                  } },
                { "adminUsername", new Dictionary <string, object> {
                      { "value", vmAdminUsername }
                  } },
                { "adminPassword", new Dictionary <string, object> {
                      { "value", vmAdminPassword }
                  } },
                { "vmCount", new Dictionary <string, object> {
                      { "value", 5 }
                  } },
            };

            // Create resource group
            var resourceGroupResponse = await armClient.ResourceGroups.CreateOrUpdateAsync(groupName,
                                                                                           new Microsoft.Azure.Management.ResourceManager.Models.ResourceGroup {
                Location = location
            });

            // Deploy template
            // The try loop should catch ARM deployment timeouts
            try
            {
                /*
                 * Task<Microsoft.Azure.Management.ResourceManager.Models.DeploymentExtended> deploymentTask = armClient.Deployments.CreateOrUpdateAsync(groupName, vmName,
                 *        new Microsoft.Azure.Management.ResourceManager.Models.Deployment
                 *        {
                 *            Properties = new Microsoft.Azure.Management.ResourceManager.Models.DeploymentProperties
                 *            {
                 *                Mode = Microsoft.Azure.Management.ResourceManager.Models.DeploymentMode.Incremental,
                 *                Template = myTemplate,
                 *                Parameters = myParameters
                 *            }
                 *        });
                 *
                 * ForgetTask(deploymentTask);
                 */

                Task.Run(() => armClient.Deployments.CreateOrUpdateAsync(groupName, vmName,
                                                                         new Microsoft.Azure.Management.ResourceManager.Models.Deployment
                {
                    Properties = new Microsoft.Azure.Management.ResourceManager.Models.DeploymentProperties
                    {
                        Mode       = Microsoft.Azure.Management.ResourceManager.Models.DeploymentMode.Incremental,
                        Template   = myTemplate,
                        Parameters = myParameters
                    }
                }));

                /*
                 * var hasSucceeded = deploymentExtended.Properties.ProvisioningState == "Succeeded";
                 * if (hasSucceeded)
                 * {
                 *  return vmName;
                 * }
                 * else
                 * {
                 *  return null;
                 * }
                 */
                return(null);
            }
            catch
            {
                return(null);
            }
        }