Ejemplo n.º 1
0
        public static async Task <string> createSRVaultAsync(string customerId, string subscriptionId, string groupName, string vaultName, string location)
        {
            // Do we need to register the resource provider because it is a sandbox???
            await registerRSAsync(customerId, subscriptionId, "Microsoft.RecoveryServices");

            string token = await REST.getArmTokenAsync(customerId, UserAuth : true);

            var credential  = new TokenCredentials(token);
            var VaultClient = new RecoveryServicesClient(credential)
            {
                SubscriptionId = subscriptionId
            };
            Vault vault = new Vault()
            {
                Location = location,
                Sku      = new Microsoft.Azure.Management.RecoveryServices.Models.Sku()
                {
                    Name = SkuName.Standard
                },
                Properties = new VaultProperties()
            };
            var newVault = await VaultClient.Vaults.CreateOrUpdateAsync(groupName, vaultName, vault);

            //return VaultClient
            return(newVault.Id.ToString());
        }
Ejemplo n.º 2
0
        private static async Task <string> createResourceGroupTryAsync(string customerId, string subscriptionId, string groupName, string location)
        {
            string token = await REST.getArmTokenAsync(customerId, UserAuth : true);

            if (token != null)
            {
                var credential = new TokenCredentials(token);
                var armClient  = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(credential)
                {
                    SubscriptionId = subscriptionId
                };
                var resourceGroup = new Microsoft.Azure.Management.ResourceManager.Models.ResourceGroup {
                    Location = location
                };
                try
                {
                    var rg = await armClient.ResourceGroups.CreateOrUpdateAsync(groupName, resourceGroup);

                    return(rg.Id.ToString());
                }
                catch
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public static async Task <bool> DeleteResourceGroupAsync(string customerId, string subscriptionId, string groupName)
        {
            string token = await REST.getArmTokenAsync(customerId, UserAuth : true);

            if (token != null)
            {
                var credential = new TokenCredentials(token);
                var armClient  = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(credential)
                {
                    SubscriptionId = subscriptionId
                };
                armClient.ResourceGroups.Delete(groupName);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        public static async Task registerRSAsync(string customerId, string subscriptionId, string rpName)
        {
            string token = await REST.getArmTokenAsync(customerId, UserAuth : true);

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

            // Check the registration state until it is Registered. Wait 5 seconds before each retry
            do
            {
                var RPlist = (await armClient.Providers.ListAsync()).ToList();
                foreach (var provider in RPlist)
                {
                    if (provider.NamespaceProperty == rpName)
                    {
                        if (provider.RegistrationState == "NotRegistered")
                        {
                            await armClient.Providers.RegisterAsync(rpName);
                        }
                        else
                        {
                            if (provider.RegistrationState == "Registered")
                            {
                                rpRegistered = true;
                            }
                            else
                            {
                                Thread.Sleep(5000);
                            }
                        }
                    }
                }
            } while (rpRegistered == false);
        }
Ejemplo n.º 5
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);
            }
        }