コード例 #1
0
        public void GetEmptyWebspaceTest()
        {
            WebSpaces getWebSpaces = Cache.GetWebSpaces("NotExisting");

            Assert.IsNotNull(getWebSpaces);
            Assert.AreEqual <int>(0, getWebSpaces.Count);
        }
コード例 #2
0
ファイル: Cache.cs プロジェクト: johnkors/azure-sdk-tools
        public static void AddWebSpace(string subscriptionId, WebSpace webSpace)
        {
            WebSpaces webSpaces = GetWebSpaces(subscriptionId);
            if (webSpaces == null)
            {
                webSpaces = new WebSpaces();
            }

            webSpaces.Add(webSpace);
            SaveSpaces(subscriptionId, webSpaces);
        }
コード例 #3
0
        public static void RemoveWebSpace(string subscriptionId, WebSpace webSpace)
        {
            WebSpaces webSpaces = GetWebSpaces(subscriptionId);

            if (webSpaces == null)
            {
                return;
            }

            webSpaces.RemoveAll(ws => ws.Name.Equals(webSpace.Name));
            SaveSpaces(subscriptionId, webSpaces);
        }
コード例 #4
0
        public static void AddWebSpace(string subscriptionId, WebSpace webSpace)
        {
            WebSpaces webSpaces = GetWebSpaces(subscriptionId);

            if (webSpaces == null)
            {
                webSpaces = new WebSpaces();
            }

            webSpaces.Add(webSpace);
            SaveSpaces(subscriptionId, webSpaces);
        }
コード例 #5
0
        public void AddWebSpaceTest()
        {
            WebSpace webSpace = new WebSpace {
                Name = "newwebspace"
            };

            // Add without any cache from before
            Cache.AddWebSpace(SubscriptionName, webSpace);

            WebSpaces getWebSpaces = Cache.GetWebSpaces(SubscriptionName);

            Assert.IsNotNull(getWebSpaces.Find(ws => ws.Name.Equals("newwebspace")));
        }
コード例 #6
0
        public static WebSpaces GetWebSpacesWithCache(this IWebsitesServiceManagement proxy, string subscriptionId)
        {
            WebSpaces webSpaces = Cache.GetWebSpaces(subscriptionId);

            if (webSpaces != null && webSpaces.Count > 0)
            {
                return(webSpaces);
            }

            webSpaces = GetWebSpaces(proxy, subscriptionId);
            Cache.SaveSpaces(subscriptionId, webSpaces);

            return(webSpaces);
        }
コード例 #7
0
        public static void SaveSpaces(string subscriptionId, WebSpaces webSpaces)
        {
            try
            {
                string webspacesFile = Path.Combine(GlobalPathInfo.AzureAppDir,
                                                    string.Format("spaces.{0}.json", subscriptionId));
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();

                // Make sure path exists
                Directory.CreateDirectory(GlobalPathInfo.AzureAppDir);
                File.WriteAllText(webspacesFile, javaScriptSerializer.Serialize(webSpaces));
            }
            catch
            {
                // Do nothing. Caching is optional.
            }
        }
コード例 #8
0
        /// <summary>
        /// Lists available website locations.
        /// </summary>
        /// <returns>List of location names</returns>
        public List <string> ListAvailableLocations()
        {
            List <string> locations           = new List <string>();
            WebSpaces     webspaces           = WebsiteChannel.GetWebSpaces(subscriptionId);
            List <string> webspacesGeoRegions = new List <string>();

            webspaces.ForEach(w => webspacesGeoRegions.Add(w.GeoRegion));
            GeoRegions regions = new GeoRegions();

            using (HttpClient client = CreateWebsitesHttpClient())
            {
                regions = client.GetXml <GeoRegions>(UriElements.WebSpacesGeoRegionsRoot, Logger);
            }

            regions.ForEach(r => locations.Add(r.Name));
            locations = locations.Union(webspacesGeoRegions).ToList();

            return(locations);
        }
コード例 #9
0
        public void GetSetWebSpacesTest()
        {
            // Test no webspaces
            Assert.AreEqual <int>(0, Cache.GetWebSpaces(SubscriptionName).Count);

            // Test valid webspaces
            WebSpaces webSpaces = new WebSpaces(new List <WebSpace> {
                new WebSpace {
                    Name = "webspace1"
                }, new WebSpace {
                    Name = "webspace2"
                }
            });

            Cache.SaveSpaces(SubscriptionName, webSpaces);

            WebSpaces getWebSpaces = Cache.GetWebSpaces(SubscriptionName);

            Assert.IsNotNull(getWebSpaces.Find(ws => ws.Name.Equals("webspace1")));
            Assert.IsNotNull(getWebSpaces.Find(ws => ws.Name.Equals("webspace2")));
        }
コード例 #10
0
        public static Site GetSiteWithCache(
            this IWebsitesServiceManagement proxy,
            string subscriptionId,
            string website,
            string propertiesToInclude)
        {
            // Try to get the website's webspace from the cache
            Site site = Cache.GetSite(subscriptionId, website, propertiesToInclude);

            if (site != null)
            {
                try
                {
                    return(proxy.GetSite(subscriptionId, site.WebSpace, site.Name, propertiesToInclude));
                }
                catch
                {
                    // The website is removed or it's webspace changed.
                    Cache.RemoveSite(subscriptionId, site);
                    throw;
                }
            }

            // Get all available webspace using REST API
            WebSpaces webspaces = proxy.GetWebSpaces(subscriptionId);

            // Iterate over all the webspaces until finding the website.
            foreach (WebSpace webspace in webspaces)
            {
                Sites websites     = proxy.GetSites(subscriptionId, webspace.Name, propertiesToInclude);
                var   matchWebsite = websites.FirstOrDefault(w => w.Name.Equals(website, System.StringComparison.InvariantCultureIgnoreCase));
                if (matchWebsite != null)
                {
                    return(matchWebsite);
                }
            }

            // The website does not exist.
            return(null);
        }
コード例 #11
0
        public void GetSetWebSpacesTest()
        {
            // Test no webspaces
            Assert.IsNull(Cache.GetWebSpaces(SubscriptionName));

            // Test valid webspaces
            WebSpaces webSpaces = new WebSpaces(new List<WebSpace> { new WebSpace { Name = "webspace1" }, new WebSpace { Name = "webspace2" }});
            Cache.SaveSpaces(SubscriptionName, webSpaces);

            WebSpaces getWebSpaces = Cache.GetWebSpaces(SubscriptionName);
            Assert.IsNotNull(getWebSpaces.Find(ws => ws.Name.Equals("webspace1")));
            Assert.IsNotNull(getWebSpaces.Find(ws => ws.Name.Equals("webspace2")));
        }
コード例 #12
0
ファイル: Cache.cs プロジェクト: johnkors/azure-sdk-tools
        public static void SaveSpaces(string subscriptionId, WebSpaces webSpaces)
        {
            try
            {
                string webspacesFile = Path.Combine(GlobalPathInfo.AzureAppDir,
                                                    string.Format("spaces.{0}.json", subscriptionId));
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();

                // Make sure path exists
                Directory.CreateDirectory(GlobalPathInfo.AzureAppDir);
                File.WriteAllText(webspacesFile, javaScriptSerializer.Serialize(webSpaces));
            }
            catch
            {
                // Do nothing. Caching is optional.
            }
        }
コード例 #13
0
        public override void ExecuteCmdlet()
        {
            WebsitesClient = WebsitesClient ?? new WebsitesClient(CurrentSubscription, WriteDebug);
            string suffix = WebsitesClient.GetWebsiteDnsSuffix();

            if (Git && GitHub)
            {
                throw new Exception("Please run the command with either -Git or -GitHub options. Not both.");
            }

            if (Git)
            {
                PublishingUsername = GetPublishingUser();
            }

            WebSpaces webspaceList = null;

            InvokeInOperationContext(() => { webspaceList = RetryCall(s => Channel.GetWebSpaces(s)); });
            if (Git && webspaceList.Count == 0)
            {
                // If location is still empty or null, give portal instructions.
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(!Git
                    ? error
                    : string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = null;

            if (string.IsNullOrEmpty(Location))
            {
                // If no location was provided as a parameter, try to default it
                webspace = webspaceList.FirstOrDefault();
                if (webspace == null)
                {
                    string defaultLocation;

                    try
                    {
                        defaultLocation = WebsitesClient.GetDefaultLocation();
                    }
                    catch
                    {
                        throw new Exception(Resources.CreateWebsiteFailed);
                    }

                    webspace = new WebSpace
                    {
                        Name         = Regex.Replace(defaultLocation.ToLower(), " ", "") + "webspace",
                        GeoRegion    = defaultLocation,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }
            else
            {
                // Find the webspace that corresponds to the georegion
                webspace = webspaceList.FirstOrDefault(w => w.GeoRegion.Equals(Location, StringComparison.OrdinalIgnoreCase));
                if (webspace == null)
                {
                    // If no webspace corresponding to the georegion was found, attempt to create it
                    webspace = new WebSpace
                    {
                        Name         = Regex.Replace(Location.ToLower(), " ", "") + "webspace",
                        GeoRegion    = Location,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }

            SiteWithWebSpace website = new SiteWithWebSpace
            {
                Name             = Name,
                HostNames        = new[] { string.Format("{0}.{1}", Name, suffix) },
                WebSpace         = webspace.Name,
                WebSpaceToCreate = webspace
            };

            if (!string.IsNullOrEmpty(Hostname))
            {
                List <string> newHostNames = new List <string>(website.HostNames);
                newHostNames.Add(Hostname);
                website.HostNames = newHostNames.ToArray();
            }

            try
            {
                CreateSite(webspace, website);
            }
            catch (EndpointNotFoundException)
            {
                // Create webspace with VirtualPlan failed, try with subscription id
                webspace.Plan = CurrentSubscription.SubscriptionId;
                CreateSite(webspace, website);
            }

            if (Git || GitHub)
            {
                try
                {
                    Directory.SetCurrentDirectory(SessionState.Path.CurrentFileSystemLocation.Path);
                }
                catch (Exception)
                {
                    // Do nothing if session state is not present
                }

                LinkedRevisionControl linkedRevisionControl = null;
                if (Git)
                {
                    linkedRevisionControl = new GitClient(this);
                }
                else if (GitHub)
                {
                    linkedRevisionControl = new GithubClient(this, GithubCredentials, GithubRepository);
                }

                linkedRevisionControl.Init();

                CopyIisNodeWhenServerJsPresent();
                UpdateLocalConfigWithSiteName(Name, webspace.Name);

                InitializeRemoteRepo(webspace.Name, Name);

                Site updatedWebsite = RetryCall(s => Channel.GetSite(s, webspace.Name, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (Git)
                {
                    AddRemoteToLocalGitRepo(updatedWebsite);
                }

                linkedRevisionControl.Deploy(updatedWebsite);
                linkedRevisionControl.Dispose();
            }
        }
コード例 #14
0
        public override void ExecuteCmdlet()
        {
            if (CurrentSubscription == null)
            {
                throw new Exception(Resources.NoDefaultSubscriptionMessage);
            }

            if (!string.IsNullOrEmpty(Name))
            {
                // Show website
                Site websiteObject = RetryCall(s => Channel.GetSite(s, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (websiteObject == null)
                {
                    throw new Exception(string.Format(Resources.InvalidWebsite, Name));
                }

                SiteConfig websiteConfiguration = null;
                InvokeInOperationContext(() =>
                {
                    websiteConfiguration = RetryCall(s => Channel.GetSiteConfig(s, websiteObject.WebSpace, websiteObject.Name));
                    WaitForOperation(CommandRuntime.ToString());
                });

                // Add to cache
                Cache.AddSite(CurrentSubscription.SubscriptionId, websiteObject);

                DiagnosticsSettings diagnosticsSettings = null;
                if (websiteObject.State == "Running")
                {
                    WebsitesClient      = WebsitesClient ?? new WebsitesClient(CurrentSubscription, WriteDebug);
                    diagnosticsSettings = WebsitesClient.GetApplicationDiagnosticsSettings(Name);
                }

                // Output results
                WriteObject(new SiteWithConfig(websiteObject, websiteConfiguration, diagnosticsSettings), false);
            }
            else
            {
                // Show websites
                WebSpaces webspaces = null;
                InvokeInOperationContext(() =>
                {
                    webspaces = RetryCall(s => Channel.GetWebSpaces(s));
                    WaitForOperation(CommandRuntime.ToString());
                });

                List <Site> websites = new List <Site>();
                foreach (var webspace in webspaces)
                {
                    InvokeInOperationContext(() =>
                    {
                        websites.AddRange(RetryCall(s => Channel.GetSites(s, webspace.Name, "repositoryuri,publishingpassword,publishingusername")));
                        WaitForOperation(CommandRuntime.ToString());
                    });
                }

                // Add to cache
                Cache.SaveSites(CurrentSubscription.SubscriptionId, new Sites(websites));

                // Output results
                WriteWebsites(websites);
            }
        }
コード例 #15
0
        public override void ExecuteCmdlet()
        {
            WebsitesClient = WebsitesClient ?? new WebsitesClient(CurrentSubscription, WriteDebug);
            string suffix = WebsitesClient.GetWebsiteDnsSuffix();

            if (Git && GitHub)
            {
                throw new Exception("Please run the command with either -Git or -GitHub options. Not both.");
            }

            if (Git)
            {
                PublishingUsername = GetPublishingUser();
            }

            WebSpaces webspaceList = null;

            InvokeInOperationContext(() => { webspaceList = RetryCall(s => Channel.GetWebSpacesWithCache(s)); });
            if (Git && webspaceList.Count == 0)
            {
                // If location is still empty or null, give portal instructions.
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(!Git
                    ? error
                    : string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = null;

            if (string.IsNullOrEmpty(Location))
            {
                // If no location was provided as a parameter, try to default it
                webspace = webspaceList.FirstOrDefault();
                if (webspace == null)
                {
                    webspace = new WebSpace
                    {
                        GeoRegion    = WebsitesClient.GetDefaultLocation(),
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }
            else
            {
                // Find the webspace that corresponds to the georegion
                webspace = webspaceList.FirstOrDefault(w => w.GeoRegion.Equals(Location, StringComparison.OrdinalIgnoreCase));
                if (webspace == null)
                {
                    // If no webspace corresponding to the georegion was found, attempt to create it
                    webspace = new WebSpace
                    {
                        Name         = Regex.Replace(Location.ToLower(), " ", "") + "webspace",
                        GeoRegion    = Location,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }

            SiteWithWebSpace website = new SiteWithWebSpace
            {
                Name             = Name,
                HostNames        = new[] { string.Format("{0}.{1}", Name, suffix) },
                WebSpace         = webspace.Name,
                WebSpaceToCreate = webspace
            };

            if (!string.IsNullOrEmpty(Hostname))
            {
                List <string> newHostNames = new List <string>(website.HostNames);
                newHostNames.Add(Hostname);
                website.HostNames = newHostNames.ToArray();
            }

            try
            {
                InvokeInOperationContext(() => RetryCall(s => Channel.CreateSite(s, webspace.Name, website)));

                // If operation succeeded try to update cache with new webspace if that's the case
                if (webspaceList.FirstOrDefault(ws => ws.Name.Equals(webspace.Name)) == null)
                {
                    Cache.AddWebSpace(CurrentSubscription.SubscriptionId, webspace);
                }

                Cache.AddSite(CurrentSubscription.SubscriptionId, website);
                SiteConfig websiteConfiguration = null;
                InvokeInOperationContext(() =>
                {
                    websiteConfiguration = RetryCall(s => Channel.GetSiteConfig(s, website.WebSpace, website.Name));
                    WaitForOperation(CommandRuntime.ToString());
                });
                WriteObject(new SiteWithConfig(website, websiteConfiguration));
            }
            catch (ProtocolException ex)
            {
                // Handle site creating indepently so that cmdlet is idempotent.
                string message = ProcessException(ex, false);
                if (message.Equals(string.Format(Resources.WebsiteAlreadyExistsReplacement,
                                                 Name)) && (Git || GitHub))
                {
                    WriteWarning(message);
                }
                else if (message.Equals(Resources.DefaultHostnamesValidation))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else
                {
                    WriteExceptionError(new Exception(message));
                }
            }

            if (Git || GitHub)
            {
                try
                {
                    Directory.SetCurrentDirectory(SessionState.Path.CurrentFileSystemLocation.Path);
                }
                catch (Exception)
                {
                    // Do nothing if session state is not present
                }

                LinkedRevisionControl linkedRevisionControl = null;
                if (Git)
                {
                    linkedRevisionControl = new GitClient(this);
                }
                else if (GitHub)
                {
                    linkedRevisionControl = new GithubClient(this, GithubCredentials, GithubRepository);
                }

                linkedRevisionControl.Init();

                CopyIisNodeWhenServerJsPresent();
                UpdateLocalConfigWithSiteName(Name, webspace.Name);

                InitializeRemoteRepo(webspace.Name, Name);

                Site updatedWebsite = RetryCall(s => Channel.GetSite(s, webspace.Name, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (Git)
                {
                    AddRemoteToLocalGitRepo(updatedWebsite);
                }

                linkedRevisionControl.Deploy(updatedWebsite);
                linkedRevisionControl.Dispose();
            }
        }