Beispiel #1
0
        public static void UpdateSubscriptions(this PSCmdlet cmdlet, IDictionary <string, SubscriptionData> subscriptionsData, string subscriptionDataFile)
        {
            GlobalComponents globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, subscriptionDataFile);

            globalComponents.SubscriptionManager.Subscriptions = subscriptionsData;
            globalComponents.SaveSubscriptions();
        }
Beispiel #2
0
        public static void SetDefaultSubscription(this PSCmdlet cmdlet, string subscriptionName, string subscriptionDataFile)
        {
            GlobalComponents globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, subscriptionDataFile);

            if (globalComponents.Subscriptions.Count > 1)
            {
                var defaultSubscription = globalComponents.Subscriptions.Values.FirstOrDefault(subscription => subscription.IsDefault);
                if (defaultSubscription != null)
                {
                    defaultSubscription.IsDefault = false;
                }

                if (subscriptionName != null)
                {
                    defaultSubscription           = globalComponents.Subscriptions.Values.First(subscription => subscription.SubscriptionName == subscriptionName);
                    defaultSubscription.IsDefault = true;
                }
                else
                {
                    defaultSubscription           = globalComponents.Subscriptions.Values.First();
                    defaultSubscription.IsDefault = true;
                }

                globalComponents.SaveSubscriptions();
            }
        }
Beispiel #3
0
        public static SubscriptionData GetCurrentSubscription(this PSCmdlet cmdlet)
        {
            // Check if there is a current subscription already set
            var currentSubscription = SessionManager.GetVariable(cmdlet, Constants.CurrentSubscriptionEnvironmentVariable) as SubscriptionData;

            if (currentSubscription == null)
            {
                try
                {
                    // Check if there is a default subscription available
                    GlobalComponents globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory);
                    currentSubscription =
                        globalComponents.Subscriptions.Values.FirstOrDefault(subscription => subscription.IsDefault);

                    if (currentSubscription != null)
                    {
                        // Set the default subscription to be the new current subscription
                        SessionManager.SetVariable(cmdlet, Constants.CurrentSubscriptionEnvironmentVariable,
                                                   currentSubscription);
                    }
                }
                catch (FileNotFoundException)
                {
                    return(null);
                }
            }

            return(currentSubscription);
        }
Beispiel #4
0
        internal void RemoveSubscriptionProcess(string subscriptionName, string subscriptionsDataFile)
        {
            // Import subscriptions from subscriptions file
            var globalComponents = GlobalComponents.Load(
                GlobalPathInfo.GlobalSettingsDirectory,
                this.ResolvePath(subscriptionsDataFile));

            if (globalComponents.Subscriptions.ContainsKey(subscriptionName))
            {
                var subscription = globalComponents.Subscriptions[subscriptionName];

                // Warn the user if the removed subscription is the default one.
                if (subscription.IsDefault)
                {
                    this.SafeWriteWarning(Resources.RemoveDefaultSubscription);
                }

                // Warn the user if the removed subscription is the current one.
                if (this.GetCurrentSubscription() == subscription)
                {
                    this.SafeWriteWarning(Resources.RemoveCurrentSubscription);
                }

                globalComponents.Subscriptions.Remove(subscriptionName);
                globalComponents.SaveSubscriptions();
            }
            else
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidSubscription, subscriptionName));
            }
        }
Beispiel #5
0
        public PublishContext(
            ServiceSettings settings,
            string packagePath,
            string configPath,
            string serviceName,
            string deploymentName,
            string rootPath)
        {
            Validate.ValidateNullArgument(settings, Resources.InvalidServiceSettingMessage);
            Validate.ValidateStringIsNullOrEmpty(packagePath, "packagePath");
            Validate.ValidateFileFull(configPath, Resources.ServiceConfiguration);
            Validate.ValidateStringIsNullOrEmpty(serviceName, "serviceName");

            this.ServiceSettings = settings;
            this.PackagePath     = packagePath;
            this.ConfigPath      = configPath;
            this.RootPath        = rootPath;
            this.ServiceName     = serviceName;
            this.DeploymentName  = string.IsNullOrEmpty(deploymentName) ?
                                   char.ToLower(ServiceSettings.Slot[0]) + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff")
                : deploymentName;

            if (!string.IsNullOrEmpty(settings.Subscription))
            {
                GlobalComponents globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory);
                SubscriptionId = globalComponents.GetSubscriptionId(settings.Subscription);
            }
            else
            {
                throw new ArgumentNullException("settings.Subscription", Resources.InvalidSubscriptionNameMessage);
            }
        }
        public DeploymentSettings(ServiceSettings settings, string packagePath, string configPath, string label, string deploymentName)
        {
            Validate.ValidateNullArgument(settings, Resources.InvalidServiceSettingMessage);
            Validate.ValidateFileFull(packagePath, Resources.Package);
            Validate.ValidateFileFull(configPath, Resources.ServiceConfiguration);
            Validate.ValidateStringIsNullOrEmpty(label, "Label");
            Validate.ValidateStringIsNullOrEmpty(deploymentName, "Deployment name");

            this.ServiceSettings = settings;
            this.PackagePath     = packagePath;
            this.ConfigPath      = configPath;
            this.Label           = label;
            this.DeploymentName  = deploymentName;

            if (!string.IsNullOrEmpty(settings.Subscription))
            {
                GlobalComponents globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory);

                SubscriptionId = globalComponents.GetSubscriptionId(settings.Subscription);
            }
            else
            {
                throw new ArgumentNullException("settings.Subscription", Resources.InvalidSubscriptionNameMessage);
            }
        }
        public bool RemoveAzureServiceProcess(string rootName, string inSubscription, string inServiceName)
        {
            string          serviceName;
            ServiceSettings settings = GetDefaultSettings(rootName, inServiceName, null, null, null, inSubscription,
                                                          out serviceName);

            if (string.IsNullOrEmpty(serviceName))
            {
                throw new Exception(Resources.InvalidServiceName);
            }

            if (!Force.IsPresent &&
                !ShouldProcess("", string.Format(Resources.RemoveServiceWarning, serviceName),
                               Resources.ShouldProcessCaption))
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(settings.Subscription))
            {
                var globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory);
                CurrentSubscription = globalComponents.Subscriptions.Values.First(
                    sub => sub.SubscriptionName == settings.Subscription);
            }

            SafeWriteObjectWithTimestamp(Resources.RemoveServiceStartMessage, serviceName);
            SafeWriteObjectWithTimestamp(Resources.RemoveDeploymentMessage);
            StopAndRemove(rootName, serviceName, CurrentSubscription.SubscriptionName, ArgumentConstants.Slots[Slot.Production]);
            StopAndRemove(rootName, serviceName, CurrentSubscription.SubscriptionName, ArgumentConstants.Slots[Slot.Staging]);
            SafeWriteObjectWithTimestamp(Resources.RemoveServiceMessage);
            RemoveService(serviceName);
            return(true);
        }
 public void GlobalComponentsLoadInvalidPublishSettingsSchemaFail()
 {
     Testing.AssertThrows <FileNotFoundException>(
         () => GlobalComponents.Load("DoesNotExistDirectory"),
         ex =>
     {
         Assert.AreEqual <string>(ex.Message, Resources.GlobalComponents_Load_PublishSettingsNotFound);
         Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
     });
 }
 public void GlobalComponentsCreateNewNullAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidSubscriptionsData)
     {
         try
         {
             GlobalComponents.Load(null, fileName);
             Assert.Fail("No exception thrown");
         }
         catch (Exception ex)
         {
             Assert.IsTrue(ex is ArgumentException);
             Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
         }
     }
 }
 public void GlobalComponentsLoadExistingEmptyAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         try
         {
             GlobalComponents.Load("fake");
             Assert.Fail("No exception thrown");
         }
         catch (Exception ex)
         {
             Assert.IsTrue(ex is FileNotFoundException);
             Assert.AreEqual <string>(ex.Message, Resources.GlobalComponents_Load_PublishSettingsNotFound);
             Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
         }
     }
 }
 public void GlobalComponentsCreateNewEmptyAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidSubscriptionsData)
     {
         try
         {
             GlobalComponents.Load(string.Empty, fileName);
             Assert.Fail("No exception thrown");
         }
         catch (Exception ex)
         {
             Assert.IsTrue(ex is ArgumentException);
             Assert.AreEqual <string>(ex.Message, string.Format(Resources.InvalidOrEmptyArgumentMessage, Resources.AzureDirectoryName));
             Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
         }
     }
 }
Beispiel #12
0
        public void RemoveSubscriptionProcess(string subscriptionName, string subscriptionsDataFile)
        {
            // Import subscriptions from subscriptions file
            var globalComponents = GlobalComponents.Load(
                GlobalPathInfo.GlobalSettingsDirectory,
                this.ResolvePath(subscriptionsDataFile));

            if (globalComponents.Subscriptions.ContainsKey(subscriptionName))
            {
                var subscription = globalComponents.Subscriptions[subscriptionName];

                // Warn the user if the removed subscription is the default one.
                if (subscription.IsDefault)
                {
                    WriteWarning(Resources.RemoveDefaultSubscription);
                    // Change default to another one
                    var newSubscriptionDefault = globalComponents.Subscriptions.Values.FirstOrDefault(s => !s.SubscriptionId.Equals(subscription.SubscriptionId));
                    if (newSubscriptionDefault != null)
                    {
                        newSubscriptionDefault.IsDefault = true;
                    }
                }

                // Warn the user if the removed subscription is the current one.
                SubscriptionData currentSubscription = this.GetCurrentSubscription();
                if (currentSubscription != null && currentSubscription.SubscriptionId.Equals(subscription.SubscriptionId))
                {
                    WriteWarning(Resources.RemoveCurrentSubscription);

                    // Clear current subscription to another one
                    this.ClearCurrentSubscription();
                }

                globalComponents.Subscriptions.Remove(subscriptionName);
                globalComponents.SaveSubscriptions();

                if (PassThru.IsPresent)
                {
                    WriteObject(true);
                }
            }
            else
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidSubscription, subscriptionName));
            }
        }
 public void GlobalComponentsLoadExistingNullAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         try
         {
             GlobalComponents.Load(null);
             Assert.Fail("No exception thrown");
         }
         catch (Exception ex)
         {
             Assert.IsTrue(ex is ArgumentException);
             Assert.AreEqual <string>("Value cannot be null. Parameter name: 'azurePath'", ex.Message);
             Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
         }
     }
 }
        public void GlobalComponentsLoadExisting()
        {
            for (var i = 0; i < Data.ValidPublishSettings.Count; i++)
            {
                var publishSettingsFile = Data.ValidPublishSettings[i];

                // Prepare
                new ImportAzurePublishSettingsCommand().ImportSubscriptionProcess(publishSettingsFile, null);
                GlobalComponents globalComponents        = GlobalComponents.Load(Data.AzureAppDir);
                PublishData      actualPublishSettings   = General.DeserializeXmlFile <PublishData>(Path.Combine(Data.AzureAppDir, Resources.PublishSettingsFileName));
                PublishData      expectedPublishSettings = General.DeserializeXmlFile <PublishData>(publishSettingsFile);

                // Assert
                AzureAssert.AreEqualGlobalComponents(new GlobalPathInfo(Data.AzureAppDir), expectedPublishSettings, globalComponents);

                // Clean
                globalComponents.DeleteGlobalComponents();
            }
        }
        public void TestRemoveNonDefaultSubscriptionProcess()
        {
            for (var i = 0; i < Data.ValidPublishSettings.Count; i++)
            {
                var targetFile = Path.Combine(Directory.GetParent(Data.ValidSubscriptionsData[i]).FullName, "removeagain" + Path.GetFileName(Data.ValidSubscriptionsData[i]));
                File.Copy(Data.ValidSubscriptionsData[i], targetFile, true);
                var globalComponents = GlobalComponents.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, targetFile, Data.ValidPublishSettings[i]);

                var removeSubscriptionCommand = new RemoveAzureSubscriptionCommand();
                removeSubscriptionCommand.RemoveSubscriptionProcess("mysub2", targetFile);

                var subscriptionsManager = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, targetFile);
                Assert.IsFalse(subscriptionsManager.Subscriptions.Values.Any(subscription => subscription.SubscriptionName == "mysub2"));
                Assert.IsTrue(subscriptionsManager.Subscriptions.Values.Any(subscription => subscription.IsDefault));

                // Clean
                globalComponents.DeleteGlobalComponents();
            }
        }
 public void GlobalComponentsLoadExistingInvalidDirectoryNameAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         foreach (string invalidDirectoryName in Data.InvalidServiceRootName)
         {
             try
             {
                 GlobalComponents.Load(invalidDirectoryName);
                 Assert.Fail("No exception thrown");
             }
             catch (Exception ex)
             {
                 Assert.IsTrue(ex is ArgumentException);
                 Assert.AreEqual <string>(ex.Message, "Illegal characters in path.");
                 Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
             }
         }
     }
 }
Beispiel #17
0
        public static void SetCurrentSubscription(this PSCmdlet cmdlet, string subscriptionName, string subscriptionDataFile)
        {
            if (subscriptionName == null)
            {
                throw new ArgumentNullException("subscriptionName", Resources.InvalidSubscriptionName);
            }

            var globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, subscriptionDataFile);

            SubscriptionData subscriptionData;

            if (!globalComponents.Subscriptions.TryGetValue(subscriptionName, out subscriptionData))
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.InvariantCulture,
                                                Resources.InvalidSubscription,
                                                subscriptionName));
            }

            SetCurrentSubscription(cmdlet, subscriptionData);
        }
        public void TestCreateSubscriptionNoImport()
        {
            var setSubscriptionCommand = new SetSubscriptionCommandStub();

            // Check that we cant get a current subscription as there is no working directory
            Assert.IsNull(setSubscriptionCommand.GetCurrentSubscription());

            // Create a new subscription and a new working directory implicitly.
            var publishSettings = General.DeserializeXmlFile <PublishData>(Data.ValidPublishSettings.First(), string.Empty);
            var certificate     = new X509Certificate2(Convert.FromBase64String(publishSettings.Items[0].ManagementCertificate), string.Empty);

            setSubscriptionCommand.SetSubscriptionProcess("CommonSettings", "newSubscription", "newSubscriptionId", certificate, "newEndpoint", null, null, null);
            var updatedSubscription = setSubscriptionCommand.GetSubscriptions(null).Values.First(subscription => subscription.SubscriptionName == "newSubscription");

            Assert.AreEqual("newSubscriptionId", updatedSubscription.SubscriptionId);
            Assert.AreEqual("newEndpoint", updatedSubscription.ServiceEndpoint);

            // Clean
            var globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory);

            globalComponents.DeleteGlobalComponents();
        }
Beispiel #19
0
        public virtual string SetDeploymentStatusProcess(string rootPath, string newStatus, string slot, string subscription, string serviceName)
        {
            if (!string.IsNullOrEmpty(subscription))
            {
                var globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory);
                CurrentSubscription = globalComponents.Subscriptions.Values.First(
                    sub => sub.SubscriptionName == subscription);
            }

            string result = CheckDeployment(newStatus, serviceName, slot);

            if (string.IsNullOrEmpty(result))
            {
                SetDeployment(newStatus, serviceName, slot);
                var deploymentStatusCommand = new GetDeploymentStatus(Channel)
                {
                    ShareChannel = ShareChannel, CurrentSubscription = CurrentSubscription
                };
                deploymentStatusCommand.WaitForState(newStatus, rootPath, serviceName, slot, CurrentSubscription.SubscriptionName);
            }

            return(result);
        }
Beispiel #20
0
        public void TestSetDefaultSubscription()
        {
            var globalComponents = GlobalComponents.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, null, Data.ValidPublishSettings.First());

            var newPath = Path.Combine(GlobalPathInfo.GlobalSettingsDirectory, "test.xml");

            File.Copy(Data.ValidSubscriptionsData[0], newPath, true);

            var cmdletStub    = new CmdletStub();
            var subscriptions = cmdletStub.GetSubscriptions(Data.ValidSubscriptionsData[0]);

            var newDefaultSubscription = subscriptions.Values.First(subscription => !subscription.IsDefault);

            cmdletStub.SetDefaultSubscription(newDefaultSubscription.SubscriptionName, newPath);

            // Test - reimport and make sure the current subscription after import is the correct one
            var subscriptionsManager = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, newPath).SubscriptionManager;
            var defaultSubscription  = subscriptionsManager.Subscriptions.Values.First(subscription => subscription.IsDefault);

            Assert.AreEqual(newDefaultSubscription.SubscriptionName, defaultSubscription.SubscriptionName);

            globalComponents.DeleteGlobalComponents();
        }
        public void GlobalComponentsLoadIgnoresPublishExisting()
        {
            var publishSettingsFile        = Data.ValidPublishSettings.First();
            var subscriptionDataFile       = Data.ValidSubscriptionsData.First();
            var outputSubscriptionDataFile = Path.Combine(Directory.GetParent(subscriptionDataFile).FullName, "outputNoPublish.xml");

            File.Copy(subscriptionDataFile, outputSubscriptionDataFile);

            // Create with both an existing ouput subscription data file and the publish settings file
            GlobalComponents globalComponents = GlobalComponents.CreateFromPublishSettings(Data.AzureAppDir, outputSubscriptionDataFile, publishSettingsFile);

            Assert.AreEqual(5, globalComponents.Subscriptions.Count);

            // Remove one of the subscriptions from the publish settings file
            globalComponents.Subscriptions.Remove("TestSubscription1");
            globalComponents.SaveSubscriptions();

            // Load and make sure the subscription is still gone although it still is in the publish settings file
            globalComponents = GlobalComponents.Load(Data.AzureAppDir, outputSubscriptionDataFile);
            Assert.AreEqual(4, globalComponents.Subscriptions.Count);

            // Clean
            globalComponents.DeleteGlobalComponents();
        }
        /// <summary>
        /// Executes the set subscription cmdlet operation.
        /// </summary>
        /// <param name="parameterSetName">The type of set operation to perform.</param>
        /// <param name="subscriptionName">The existing or new subscription name.</param>
        /// <param name="subscriptionId">The subscription identifier for the existing or new subscription.</param>
        /// <param name="certificate">The certificate for the existing or new subscription.</param>
        /// <param name="serviceEndpoint">The service endpoint for the existing or new subscription.</param>
        /// <param name="defaultSubscription">The subscription name for the new subscription.</param>
        /// <param name="currentStorageAccount">The current storage account.</param>
        /// <param name="subscriptionDataFile">The input/output subscription data file to use.</param>
        internal void SetSubscriptionProcess(string parameterSetName, string subscriptionName, string subscriptionId, X509Certificate2 certificate, string serviceEndpoint, string defaultSubscription, string currentStorageAccount, string subscriptionDataFile)
        {
            SubscriptionData currentSubscription = this.GetCurrentSubscription();

            if (currentSubscription != null &&
                !String.IsNullOrEmpty(currentSubscription.ServiceEndpoint) &&
                String.IsNullOrEmpty(serviceEndpoint))
            {
                // If the current subscription already has a service endpoint do not overwrite if not specified
                serviceEndpoint = currentSubscription.ServiceEndpoint;
            }
            else if (String.IsNullOrEmpty(serviceEndpoint))
            {
                // No current subscription and nothing specified initialize with the default
                serviceEndpoint = ConfigurationConstants.ServiceManagementEndpoint;
            }

            if (parameterSetName == "DefaultSubscription")
            {
                // Set a new default subscription
                this.SetDefaultSubscription(defaultSubscription, subscriptionDataFile);
                this.SetCurrentSubscription(defaultSubscription, subscriptionDataFile);
            }
            else if (parameterSetName == "ResetDefaultSubscription")
            {
                // Reset default subscription
                this.SetDefaultSubscription(null, subscriptionDataFile);
            }
            else
            {
                // Update or create a new subscription
                GlobalComponents globalComponents;
                try
                {
                    globalComponents = GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, subscriptionDataFile);
                }
                catch (FileNotFoundException)
                {
                    // assume that import has never been ran and just create it.
                    globalComponents = GlobalComponents.Create(GlobalPathInfo.GlobalSettingsDirectory,
                                                               subscriptionDataFile,
                                                               certificate,
                                                               serviceEndpoint);
                }

                SubscriptionData subscription = globalComponents.Subscriptions.ContainsKey(subscriptionName)
                    ? globalComponents.Subscriptions[subscriptionName]
                    : new SubscriptionData {
                    SubscriptionName = subscriptionName, IsDefault = (globalComponents.Subscriptions.Count == 0)
                };

                if (parameterSetName == "CommonSettings")
                {
                    SetCommonSettingsProcess(subscription, subscriptionId, certificate, serviceEndpoint, currentStorageAccount);
                }

                // Create or update
                globalComponents.Subscriptions[subscription.SubscriptionName] = subscription;
                globalComponents.SaveSubscriptions(subscriptionDataFile);

                currentSubscription = this.GetCurrentSubscription();
                if (currentSubscription == null || string.Compare(currentSubscription.SubscriptionName, subscription.SubscriptionName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // If the user modifies a subscription using Set-AzureSubscription, but doesn't call Select-AzureSubscription
                    // it is not immediately reflected in the code. This takes into account if they modify the current subscription
                    // that they shouldn't have to call Select-AzureSubscription once again to have the values updated in session.
                    this.SetCurrentSubscription(subscription.SubscriptionName, subscriptionDataFile);

                    if (currentSubscription != null)
                    {
                        WriteMessage(string.Format(
                                         Resources.UpdatedSettings,
                                         subscriptionName,
                                         currentSubscription.SubscriptionName));
                    }
                }
            }
        }
 public void RemovePublishSettingsProcess(string azureSdkDirPath)
 {
     GlobalComponents.Load(azureSdkDirPath).DeleteGlobalComponents();
 }
Beispiel #24
0
 public static IDictionary <string, SubscriptionData> GetSubscriptions(this PSCmdlet cmdlet, string subscriptionDataFile)
 {
     return(GlobalComponents.Load(GlobalPathInfo.GlobalSettingsDirectory, subscriptionDataFile).Subscriptions);
 }