#pragma warning disable 4014
        public async Task <HttpResponseMessage> Preview([FromBody] JObject parameters, string subscriptionId, string templateUrl)
        {
            JObject             responseObj = new JObject();
            List <string>       providers   = new List <string>(32);
            HttpResponseMessage response    = null;

            using (var client = GetRMClient(subscriptionId))
            {
                ResourceGroupCreateOrUpdateResult resourceResult = null;
                string tempRGName = Guid.NewGuid().ToString();

                try
                {
                    resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(tempRGName, new BasicResourceGroup { Location = "East US" });

                    // For now we just default to East US for the resource group location.
                    var basicDeployment = new BasicDeployment
                    {
                        Parameters   = parameters.ToString(),
                        TemplateLink = new TemplateLink(new Uri(templateUrl))
                    };

                    var deploymentResult = await client.Deployments.ValidateAsync(tempRGName, tempRGName, basicDeployment);

                    if (deploymentResult.StatusCode == HttpStatusCode.OK)
                    {
                        foreach (var p in deploymentResult.Properties.Providers)
                        {
                            if (sm_providerMap.ContainsKey(p.Namespace))
                            {
                                providers.Add(sm_providerMap[p.Namespace]);
                            }
                            else
                            {
                                providers.Add(p.Namespace);
                            }
                        }

                        responseObj["providers"] = JArray.FromObject(providers);
                        response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
                    }
                    else
                    {
                        responseObj["error"] = deploymentResult.Error.Message;
                        response             = Request.CreateResponse(deploymentResult.StatusCode, responseObj);
                    }
                }
                finally
                {
                    if (resourceResult != null &&
                        (resourceResult.StatusCode == HttpStatusCode.Created || resourceResult.StatusCode == HttpStatusCode.OK))
                    {
                        string token = GetTokenFromHeader();
                        Task.Run(() => { DeleteResourceGroup(subscriptionId, token, tempRGName); });
                    }
                }
            }

            return(response);
        }
        public void TryCreateResourceGroup(string resourceGroupName, string location)
        {
            // get the resource group first
            bool exists = false;
            ResourceGroupGetResult newlyCreatedGroup = null;

            try
            {
                newlyCreatedGroup = resourceManagementClient.ResourceGroups.Get(resourceGroupName);
                exists            = true;
            }
            catch
            {
                // do nothing because it means it doesn't exist
            }

            if (!exists)
            {
                ResourceGroupCreateOrUpdateResult result =
                    resourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                                           new ResourceGroup {
                    Location = location
                });
                newlyCreatedGroup = resourceManagementClient.ResourceGroups.Get(resourceGroupName);
            }

            ThrowIfTrue(newlyCreatedGroup == null, "resourceManagementClient.ResourceGroups.Get returned null.");
            ThrowIfTrue(!resourceGroupName.Equals(newlyCreatedGroup.ResourceGroup.Name),
                        string.Format("resourceGroupName is not equal to {0}", resourceGroupName));
        }
Exemple #3
0
 public void CanCreateResourceGroup()
 {
     using (UndoContext context = UndoContext.Current)
     {
         context.Start();
         string groupName = TestUtilities.GenerateName("csmrg");
         ResourceManagementClient          client = this.GetResourceManagementClient(new RecordedDelegatingHandler());
         ResourceGroupCreateOrUpdateResult result = client.ResourceGroups.CreateOrUpdate(groupName,
                                                                                         new ResourceGroup
         {
             Location = DefaultLocation,
             Tags     = new Dictionary <string, string>()
             {
                 { "department", "finance" }, { "tagname", "tagvalue" }
             },
         });
         var listResult  = client.ResourceGroups.List(new ResourceGroupListParameters());
         var listedGroup = listResult.ResourceGroups.FirstOrDefault((g) => string.Equals(g.Name, groupName, StringComparison.Ordinal));
         Assert.NotNull(listedGroup);
         Assert.Equal("finance", listedGroup.Tags["department"]);
         Assert.Equal("tagvalue", listedGroup.Tags["tagname"]);
         Assert.True(ResourcesManagementTestUtilities.LocationsAreEqual(DefaultLocation, listedGroup.Location),
                     string.Format("Expected location '{0}' did not match actual location '{1}'", DefaultLocation, listedGroup.Location));
         var gottenGroup = client.ResourceGroups.Get(groupName);
         Assert.NotNull(gottenGroup);
         Assert.Equal <string>(groupName, gottenGroup.ResourceGroup.Name);
         Assert.True(ResourcesManagementTestUtilities.LocationsAreEqual(DefaultLocation, gottenGroup.ResourceGroup.Location),
                     string.Format("Expected location '{0}' did not match actual location '{1}'", DefaultLocation, gottenGroup.ResourceGroup.Location));
     }
 }
Exemple #4
0
        public ResourceGroupFixture()
        {
            ResourceManagementClient client =
                TestBase.GetServiceClient <ResourceManagementClient>(new CSMTestEnvironmentFactory());

            // Register subscription
            AzureOperationResponse registerResponse = client.Providers.Register(SearchNamespace);

            Assert.Equal(HttpStatusCode.OK, registerResponse.StatusCode);

            // Get a valid location for search services.
            ProviderGetResult providerResult = client.Providers.Get(SearchNamespace);

            Assert.Equal(HttpStatusCode.OK, providerResult.StatusCode);

            // We only support one resource type.
            Location = providerResult.Provider.ResourceTypes.First().Locations.First();

            // Create resource group
            ResourceGroupName = TestUtilities.GenerateName();
            ResourceGroupCreateOrUpdateResult resourceGroupResult =
                client.ResourceGroups.CreateOrUpdate(ResourceGroupName, new ResourceGroup(Location));

            Assert.Equal(HttpStatusCode.Created, resourceGroupResult.StatusCode);
        }
 private void TryCreateResourceGroup(string resourceGroupName, string location)
 {
     ResourceGroupCreateOrUpdateResult result = ResourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup { Location = location });
     var newlyCreatedGroup = ResourceManagementClient.ResourceGroups.Get(resourceGroupName);
     ThrowIfTrue(newlyCreatedGroup == null, "resourceManagementClient.ResourceGroups.Get returned null.");
     ThrowIfTrue(!resourceGroupName.Equals(newlyCreatedGroup.ResourceGroup.Name), string.Format("resourceGroupName is not equal to {0}", resourceGroupName));
 }
Exemple #6
0
#pragma warning disable 4014
        public async Task <HttpResponseMessage> Preview(DeployInputs inputs)
        {
            JObject             responseObj = new JObject();
            List <string>       providers   = new List <string>(32);
            HttpResponseMessage response    = null;

            using (var client = GetRMClient(inputs.subscriptionId))
            {
                ResourceGroupCreateOrUpdateResult resourceResult = null;
                string tempRGName = Guid.NewGuid().ToString();

                try
                {
                    resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(
                        tempRGName,
                        new ResourceGroup { Location = inputs.resourceGroup.location });

                    Deployment basicDeployment = await this.GetDeploymentPayload(inputs);

                    var deploymentResult = await client.Deployments.ValidateAsync(tempRGName, tempRGName, basicDeployment);

                    if (deploymentResult.StatusCode == HttpStatusCode.OK)
                    {
                        foreach (var p in deploymentResult.Properties.Providers)
                        {
                            if (sm_providerMap.ContainsKey(p.Namespace))
                            {
                                providers.Add(sm_providerMap[p.Namespace]);
                            }
                            else
                            {
                                providers.Add(p.Namespace);
                            }
                        }

                        responseObj["providers"] = JArray.FromObject(providers);
                        response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
                    }
                    else
                    {
                        responseObj["error"] = deploymentResult.Error.Message;
                        response             = Request.CreateResponse(deploymentResult.StatusCode, responseObj);
                    }
                }
                finally
                {
                    if (resourceResult != null &&
                        (resourceResult.StatusCode == HttpStatusCode.Created || resourceResult.StatusCode == HttpStatusCode.OK))
                    {
                        string token = GetTokenFromHeader();
                        Task.Run(() => { DeleteResourceGroup(inputs.subscriptionId, token, tempRGName); });
                    }
                }
            }

            return(response);
        }
Exemple #7
0
        private void Initialize()
        {
            handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            resourcesClient = ComputeManagementTestUtilities.GetResourceManagementClient(handler);
            computeClient   = ComputeManagementTestUtilities.GetComputeManagementClient(handler);

            subId    = computeClient.Credentials.SubscriptionId;
            location = ComputeManagementTestUtilities.DefaultLocation;

            resourceGroupName = TestUtilities.GenerateName(testPrefix);

            resourceGroup = resourcesClient.ResourceGroups.CreateOrUpdate(
                resourceGroupName,
                new ResourceGroup
            {
                Location = location
            });
        }
Exemple #8
0
        private async Task <string> CreateResourceGroupAsync(TokenCloudCredentials credential, string rgName)
        {
            ResourceGroup resourceGroup = new ResourceGroup {
                Location = this.settings.Region
            };

            using (ResourceManagementClient resourceManagementClient = new ResourceManagementClient(credential))
            {
                ResourceGroupExistsResult exists = await resourceManagementClient.ResourceGroups.CheckExistenceAsync(rgName);

                if (exists.Exists)
                {
                    return("Exists");
                }

                ResourceGroupCreateOrUpdateResult rgResult = await resourceManagementClient.ResourceGroups.CreateOrUpdateAsync(rgName, resourceGroup);

                return(rgResult.StatusCode.ToString());
            }
        }