public void TestUpdateSourceControlUpdates()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);

                webSitesClient.SourceControls.Update(GitHubSourceControl.Name, new SourceControlUpdateParameters()
                {
                    Properties = GitHubSourceControl.Properties
                });

                var sourceControlGetResponse = webSitesClient.SourceControls.Get("GitHub");

                AssertSourceControl(GitHubSourceControl, sourceControlGetResponse.SourceControl);

                var sourceControlListResponse = webSitesClient.SourceControls.List();

                Assert.True(sourceControlListResponse.SourceControls.Count >= 4, "Invalid source controls count at " + sourceControlListResponse.SourceControls.Count);
                var github = sourceControlListResponse.SourceControls.FirstOrDefault(s => s.Name == "GitHub");
                AssertSourceControl(GitHubSourceControl, github);

                var bitbucket = sourceControlListResponse.SourceControls.FirstOrDefault(s => s.Name == "Bitbucket");
                AssertSourceControl(BitbucketSourceControl, bitbucket);
            }
        }
        public void CreateAndVerifyListOfWebHostingPlan()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                var resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string whpName1 = TestUtilities.GenerateName("csmwhp");
                string whpName2 = TestUtilities.GenerateName("csmwhp");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");

                var location = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                {
                    WebHostingPlan = new WebHostingPlan
                    {
                        Name = whpName1,
                        Location = location,
                        Properties = new WebHostingPlanProperties
                        {
                            Sku = SkuOptions.Shared
                        }
                    }
                });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                {
                    WebHostingPlan = new WebHostingPlan
                    {
                        Name = whpName2,
                        Location = location,
                        Properties = new WebHostingPlanProperties
                        {
                            Sku = SkuOptions.Basic,
                            NumberOfWorkers = 1,
                            WorkerSize = WorkerSizeOptions.Small
                        }
                    }
                });

                var webHostingPlanResponse = webSitesClient.WebHostingPlans.List(resourceGroupName);

                var whp1 = webHostingPlanResponse.WebHostingPlans.First(f => f.Name == whpName1);
                var whp2 = webHostingPlanResponse.WebHostingPlans.First(f => f.Name == whpName2);
                Assert.Equal(whp1.Properties.Sku, SkuOptions.Shared);
                Assert.Equal(whp2.Properties.Sku, SkuOptions.Basic);
            }
        }
        public void CreateAndVerifyWebHostingPlan()
        {
            var handler = new RecordedDelegatingHandler {StatusCodeToReturn = HttpStatusCode.OK};

            using (var context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                var resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string webHostingPlanName = TestUtilities.GenerateName("csmsf");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");

                var location = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                        {
                            Location = location
                        });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                    {
                        WebHostingPlan = new WebHostingPlan
                            {
                                Name = webHostingPlanName,
                                Location = location,
                                Properties = new WebHostingPlanProperties
                                {
                                    NumberOfWorkers = 1,
                                    WorkerSize = WorkerSizeOptions.Small,
                                    Sku = SkuOptions.Basic
                                }
                            }
                    });

                var webHostingPlanResponse = webSitesClient.WebHostingPlans.Get(resourceGroupName, webHostingPlanName);

                Assert.Equal(webHostingPlanName, webHostingPlanResponse.WebHostingPlan.Name);
                Assert.Equal(1, webHostingPlanResponse.WebHostingPlan.Properties.NumberOfWorkers);
                Assert.Equal(WorkerSizeOptions.Small, webHostingPlanResponse.WebHostingPlan.Properties.WorkerSize);
                Assert.Equal(SkuOptions.Basic, webHostingPlanResponse.WebHostingPlan.Properties.Sku);
            }
        }
        public void GetAndSetSiteLimits()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.OK };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                var resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string whpName = TestUtilities.GenerateName("cswhp");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");

                var locationName = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");
                string siteName = TestUtilities.GenerateName("csmws");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = locationName
                    });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                {
                    WebHostingPlan = new WebHostingPlan
                    {
                        Name = whpName,
                        Location = locationName,
                        Properties = new WebHostingPlanProperties
                        {
                            NumberOfWorkers = 1,
                            WorkerSize = WorkerSizeOptions.Small
                        }
                    }
                });

                var createResponse = webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, siteName, null, new WebSiteCreateOrUpdateParameters()
                {
                    WebSite = new WebSiteBase()
                    {
                        Name = siteName,
                        Location = locationName,
                        Properties = new WebSiteBaseProperties()
                        {
                            ServerFarm = whpName
                        }
                    }
                });

                #region Get/Set Site limits

                var expectedSitelimits = new SiteLimits()
                {
                    MaxDiskSizeInMb = 512,
                    MaxMemoryInMb = 1024,
                    MaxPercentageCpu = 70.5
                };
                var parameters = new WebSiteUpdateConfigurationParameters()
                {
                    Location = locationName,
                    Properties = new WebSiteUpdateConfigurationDetails()
                    {
                        Limits = expectedSitelimits
                    }
                };


                var siteUpdateConfigResponse = webSitesClient.WebSites.UpdateConfiguration(
                    resourceGroupName,
                    siteName,
                    null,
                    parameters);

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

                var siteGetConfigResponse = webSitesClient.WebSites.GetConfiguration(resourceGroupName,
                    siteName, null, null);

                Assert.NotNull(siteGetConfigResponse);
                Assert.NotNull(siteGetConfigResponse.Resource);
                Assert.NotNull(siteGetConfigResponse.Resource.Properties);
                var limits = siteGetConfigResponse.Resource.Properties.Limits;
                Assert.NotNull(limits);
                Assert.Equal(expectedSitelimits.MaxDiskSizeInMb, limits.MaxDiskSizeInMb);
                Assert.Equal(expectedSitelimits.MaxMemoryInMb, limits.MaxMemoryInMb);
                Assert.Equal(expectedSitelimits.MaxPercentageCpu, limits.MaxPercentageCpu);

                #endregion Get/Set Site limits

                webSitesClient.WebSites.Delete(resourceGroupName, siteName, null, new WebSiteDeleteParameters()
                {
                    DeleteAllSlots = true,
                    DeleteMetrics = true
                });

                webSitesClient.WebHostingPlans.Delete(resourceGroupName, whpName);
            }
        }
        private void RunWebsiteTestScenario(WebsiteTestDelegate testAction, SkuOptions sku = SkuOptions.Shared)
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.OK };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start(4);
                WebSiteManagementClient webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                ResourceManagementClient resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string webSiteName = TestUtilities.GenerateName("csmws");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                string webHostingPlanName = TestUtilities.GenerateName("csmwhp");
                string location = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName,
                    new WebHostingPlanCreateOrUpdateParameters
                    {
                        WebHostingPlan = new WebHostingPlan
                        {
                            Name = webHostingPlanName,
                            Location = location,
                            Properties = new WebHostingPlanProperties()
                            {
                                Sku = sku
                            }
                        }
                    });

                var webSite = webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, webSiteName, null, new WebSiteCreateOrUpdateParameters
                {
                    WebSite = new WebSiteBase
                    {
                        Name = webSiteName,
                        Location = location,
                        Tags = new Dictionary<string, string> { { "tag1", "value1" }, { "tag2", "" } },
                        Properties = new WebSiteBaseProperties
                        {
                            ServerFarm = webHostingPlanName
                        }
                    }
                });

                Assert.Equal(webSiteName, webSite.WebSite.Name);
                Assert.Equal(webSite.WebSite.Properties.ServerFarm, webHostingPlanName);
                Assert.Equal("value1", webSite.WebSite.Tags["tag1"]);
                Assert.Equal("", webSite.WebSite.Tags["tag2"]);

                testAction(webSiteName, resourceGroupName, webHostingPlanName, location, webSitesClient, resourcesClient);
            }
        }
        public void GetAndSetAdminSiteWebHostingPlan()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                var resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string webSiteName = TestUtilities.GenerateName("csmws");
                string webHostingPlanName = TestUtilities.GenerateName("csmsf");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");

                var location = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                {
                    WebHostingPlan = new WebHostingPlan
                    {
                        Name = webHostingPlanName,
                        Location = location,
                        Properties = new WebHostingPlanProperties
                        {
                            NumberOfWorkers = 1,
                            WorkerSize = WorkerSizeOptions.Small,
                            Sku = SkuOptions.Standard
                        }
                    }
                });

                webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, webSiteName, null, new WebSiteCreateOrUpdateParameters
                {
                    WebSite = new WebSiteBase
                    {
                        Name = webSiteName,
                        Location = location,
                        Tags = new Dictionary<string, string> { { "tag1", "value1" }, { "tag2", "" } },
                        Properties = new WebSiteBaseProperties
                        {
                            ServerFarm = webHostingPlanName
                        }
                    }
                });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                {
                    WebHostingPlan = new WebHostingPlan
                    {
                        Name = webHostingPlanName,
                        Location = location,
                        Properties = new WebHostingPlanProperties
                        {
                            NumberOfWorkers = 1,
                            WorkerSize = WorkerSizeOptions.Small,
                            Sku = SkuOptions.Standard,
                            AdminSiteName = webSiteName
                        }
                    }
                });

                var webHostingPlanResponse = webSitesClient.WebHostingPlans.Get(resourceGroupName, webHostingPlanName);

                Assert.Equal(webHostingPlanName, webHostingPlanResponse.WebHostingPlan.Name);
                Assert.Equal(1, webHostingPlanResponse.WebHostingPlan.Properties.NumberOfWorkers);
                Assert.Equal(WorkerSizeOptions.Small, webHostingPlanResponse.WebHostingPlan.Properties.WorkerSize);
                Assert.Equal(SkuOptions.Standard, webHostingPlanResponse.WebHostingPlan.Properties.Sku);
                Assert.Equal(webSiteName, webHostingPlanResponse.WebHostingPlan.Properties.AdminSiteName);
            }
        }
 public static ResourceManagementClient GetResourcesClient(RecordedDelegatingHandler handler)
 {
     handler.IsPassThrough = true;
     return TestBase.GetServiceClient<ResourceManagementClient>(new CSMTestEnvironmentFactory()).WithHandler(handler);
 }
        public void ListBackupsAndScheduledBackupRoundTrip()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.OK };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = GetWebSitesClient(handler);
                var resourcesClient = GetResourcesClient(handler);

                string farmName = TestUtilities.GenerateName("csmsf");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");

                string locationName = "West US";
                string siteName = TestUtilities.GenerateName("csmws");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = locationName
                    });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName, new WebHostingPlanCreateOrUpdateParameters
                {
                    WebHostingPlan = new WebHostingPlan
                    {
                        Name = farmName,
                        Location = locationName,
                        Properties = new WebHostingPlanProperties
                        {
                            NumberOfWorkers = 1,
                            WorkerSize = WorkerSizeOptions.Small
                        }
                    }
                });

                webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, siteName, null, new WebSiteCreateOrUpdateParameters()
                {
                    WebSite = new WebSiteBase()
                    {
                        Name = siteName,
                        Location = locationName,
                        Properties = new WebSiteBaseProperties()
                        {
                            ServerFarm = farmName
                        }
                    }
                });

                var backupResponse = webSitesClient.WebSites.ListBackups(resourceGroupName, siteName, null);
                Assert.Equal(0, backupResponse.BackupList.Properties.Count); // , "Backup list should be empty"

                // the following URL just have a proper format, but it is not valid - for an API test it is not needed to be valid,
                // since we are just testing a roundtrip here
                string storageUrl = "https://nonexistingusername3567.blob.core.windows.net/backup/?sv=2012-02-12&st=2013-12-05T19%3A30%3A45Z&se=2017-12-04T19%3A30%3A45Z&sr=c&sp=rwdl&sig=3BY5sbzQ2NeKvdaelzxc8inxJgE1mGq2a%2BaqUeFGJYo%3D";

                var sr = new BackupRequest()
                {
                    Enabled = false,
                    BackupSchedule = new BackupSchedule()
                    {
                        FrequencyInterval = 17,
                        FrequencyUnit = FrequencyUnit.Day,
                        KeepAtLeastOneBackup = true,
                        RetentionPeriodInDays = 26,
                        StartTime = DateTime.Now.AddDays(5)
                    },
                    Name = "abc",
                    StorageAccountUrl = storageUrl
                };

                webSitesClient.WebSites.UpdateBackupConfiguration(resourceGroupName, siteName, null, new BackupRequestEnvelope()
                {
                    Location = locationName,
                    Request = sr
                });

                var backupConfiguration = webSitesClient.WebSites.GetBackupConfiguration(resourceGroupName, siteName, null);

                Assert.Equal(sr.Enabled, backupConfiguration.BackupSchedule.Properties.Enabled);
                Assert.Equal(sr.BackupSchedule.FrequencyInterval, backupConfiguration.BackupSchedule.Properties.BackupSchedule.FrequencyInterval);
                Assert.Equal(sr.BackupSchedule.FrequencyUnit, backupConfiguration.BackupSchedule.Properties.BackupSchedule.FrequencyUnit);
                Assert.Equal(sr.BackupSchedule.KeepAtLeastOneBackup, backupConfiguration.BackupSchedule.Properties.BackupSchedule.KeepAtLeastOneBackup);
                Assert.Equal(sr.Name, backupConfiguration.BackupSchedule.Properties.Name);

                webSitesClient.WebSites.Delete(resourceGroupName, siteName, null, new WebSiteDeleteParameters()
                {
                    DeleteAllSlots = true,
                    DeleteMetrics = true
                });

                webSitesClient.WebHostingPlans.Delete(resourceGroupName, farmName);

                var serverFarmResponse = webSitesClient.WebHostingPlans.List(resourceGroupName);

                Assert.Equal(0, serverFarmResponse.WebHostingPlans.Count);
            }
        }
        public void CreateAndVerifyGetOnAWebsiteSlot()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                var resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string webSiteName = TestUtilities.GenerateName("csmws");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                string whpName = TestUtilities.GenerateName("cswhp");
                const string slotName = "staging";
                string siteWithSlotName = string.Format("{0}({1})", webSiteName, slotName);

                var location = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                        {
                            Location = location
                        });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName,
                    new WebHostingPlanCreateOrUpdateParameters
                    {
                        WebHostingPlan = new WebHostingPlan
                        {
                            Name = whpName,
                            Location = location,
                            Properties = new WebHostingPlanProperties
                            {
                                Sku = SkuOptions.Standard
                            }
                        }
                    });

                webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, webSiteName, null, new WebSiteCreateOrUpdateParameters
                    {
                        WebSite = new WebSiteBase
                            {
                                Name = webSiteName,
                                Location = location,
                                Tags = new Dictionary<string, string> { { "tag1", "value1" }, { "tag2", "" } },
                                Properties = new WebSiteBaseProperties
                                {
                                    ServerFarm = whpName
                                }
                            }
                    });

                webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, webSiteName, slotName,
                    new WebSiteCreateOrUpdateParameters
                    {
                        WebSite = new WebSiteBase
                        {
                            Location = location,
                            Properties = new WebSiteBaseProperties
                            {
                                ServerFarm = whpName
                            }
                        }
                    });

                // TODO: Replace with GetSite with slotName API once its CSM related issue is resolved.
                var webSiteSlotCollection = webSitesClient.WebSites.List(resourceGroupName, webSiteName, null);
                Assert.Equal(1, webSiteSlotCollection.WebSites.Count);
                Assert.Equal(siteWithSlotName, webSiteSlotCollection.WebSites[0].Name);
            }
        }
        public void CreateAndDeleteWebSiteSlot()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.OK };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var webSitesClient = ResourceGroupHelper.GetWebSitesClient(handler);
                var resourcesClient = ResourceGroupHelper.GetResourcesClient(handler);

                string webSiteName = TestUtilities.GenerateName("csmws");
                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                string whpName = TestUtilities.GenerateName("csmwhp");
                const string slotName = "staging";
                string siteWithSlotName = string.Format("{0}({1})", webSiteName, slotName);

                var location = ResourceGroupHelper.GetResourceLocation(resourcesClient, "Microsoft.Web/sites");

                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });

                webSitesClient.WebHostingPlans.CreateOrUpdate(resourceGroupName,
                    new WebHostingPlanCreateOrUpdateParameters
                    {
                        WebHostingPlan = new WebHostingPlan
                        {
                            Name = whpName,
                            Location = location,
                            Properties = new WebHostingPlanProperties()
                            {
                                Sku = SkuOptions.Standard
                            }
                        }
                    });

                webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, webSiteName, null, new WebSiteCreateOrUpdateParameters
                {
                    WebSite = new WebSiteBase
                    {
                        Name = webSiteName,
                        Location = location,
                        Properties = new WebSiteBaseProperties
                        {
                            ServerFarm = whpName
                        }
                    }
                });

                webSitesClient.WebSites.CreateOrUpdate(resourceGroupName, webSiteName, slotName,
                    new WebSiteCreateOrUpdateParameters
                    {
                        WebSite = new WebSiteBase
                        {
                            Location = location,
                            Properties = new WebSiteBaseProperties
                            {
                                ServerFarm = whpName
                            }
                        }
                    });

                webSitesClient.WebSites.Delete(resourceGroupName, webSiteName, slotName, new WebSiteDeleteParameters
                {
                    DeleteAllSlots = false
                });

                var webSites = webSitesClient.WebSites.List(resourceGroupName, webSiteName, null);

                Assert.Equal(0, webSites.WebSites.Count);
            }
        }