Exemplo n.º 1
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))
            {
                GlobalSettingsManager globalSettingsManager = GlobalSettingsManager.Load(GlobalPathInfo.GlobalSettingsDirectory);
                SubscriptionId = globalSettingsManager.GetSubscriptionId(settings.Subscription);
            }
            else
            {
                throw new ArgumentNullException("settings.Subscription", Resources.InvalidSubscriptionNameMessage);
            }
        }
Exemplo n.º 2
0
        public void GlobalSettingsManagerLoadInvalidPublishSettingsSchemaFail()
        {
            GlobalSettingsManager.Load("DoesNotExistDirectory");

            Assert.IsNotNull(GlobalSettingsManager.Instance.PublishSettings);
            Assert.IsNotNull(GlobalSettingsManager.Instance.ServiceConfiguration);
            Assert.IsNotNull(GlobalSettingsManager.Instance.Certificate);
        }
Exemplo n.º 3
0
 public void GlobalSettingsManagerLoadInvalidPublishSettingsSchemaFail()
 {
     Testing.AssertThrows <FileNotFoundException>(
         () => GlobalSettingsManager.Load("DoesNotExistDirectory"),
         ex =>
     {
         Assert.AreEqual <string>(ex.Message, Resources.GlobalSettingsManager_Load_PublishSettingsNotFound);
         Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
     });
 }
Exemplo n.º 4
0
 public void GlobalSettingsManagerLoadExistingNullAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         GlobalSettingsManager.Load(null);
         Assert.IsNotNull(GlobalSettingsManager.Instance.PublishSettings);
         Assert.IsNotNull(GlobalSettingsManager.Instance.ServiceConfiguration);
         Assert.IsNotNull(GlobalSettingsManager.Instance.Certificate);
     }
 }
Exemplo n.º 5
0
        public void GlobalSettingsManagerLoadDoesNotExistAzureDirectoryGetDefaultValues()
        {
            foreach (string fileName in Data.ValidPublishSettings)
            {
                foreach (string invalidDirectoryName in Data.InvalidServiceRootName)
                {
                    GlobalSettingsManager.Load("DoesNotExistDirectory");

                    Assert.IsNotNull(GlobalSettingsManager.Instance.PublishSettings);
                    Assert.IsNotNull(GlobalSettingsManager.Instance.ServiceConfiguration);
                    Assert.IsNotNull(GlobalSettingsManager.Instance.Certificate);
                }
            }
        }
Exemplo n.º 6
0
 public void GlobalSettingsManagerCreateNewNullAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidSubscriptionsData)
     {
         try
         {
             GlobalSettingsManager.Load(null, fileName);
             Assert.Fail("No exception thrown");
         }
         catch (Exception ex)
         {
             Assert.IsTrue(ex is ArgumentException);
             Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
         }
     }
 }
Exemplo n.º 7
0
        public void GlobalSettingsManagerLoadExisting()
        {
            for (var i = 0; i < Data.ValidPublishSettings.Count; i++)
            {
                var publishSettingsFile = Data.ValidPublishSettings[i];

                // Prepare
                new ImportAzurePublishSettingsCommand().ImportSubscriptionFile(publishSettingsFile, null);
                GlobalSettingsManager globalSettingsManager   = GlobalSettingsManager.Load(GlobalPathInfo.GlobalSettingsDirectory);
                PublishData           actualPublishSettings   = General.DeserializeXmlFile <PublishData>(Path.Combine(GlobalPathInfo.GlobalSettingsDirectory, Resources.PublishSettingsFileName));
                PublishData           expectedPublishSettings = General.DeserializeXmlFile <PublishData>(publishSettingsFile);

                // Assert
                AzureAssert.AreEqualGlobalSettingsManager(new GlobalPathInfo(GlobalPathInfo.GlobalSettingsDirectory), expectedPublishSettings, globalSettingsManager);
            }
        }
Exemplo n.º 8
0
        public void RemoveSubscriptionProcess(string subscriptionName, string subscriptionsDataFile)
        {
            // Import subscriptions from subscriptions file
            var globalSettingsManager = GlobalSettingsManager.Load(
                GlobalPathInfo.GlobalSettingsDirectory,
                this.ResolvePath(subscriptionsDataFile));

            if (globalSettingsManager.Subscriptions.ContainsKey(subscriptionName))
            {
                var subscription = globalSettingsManager.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 = globalSettingsManager.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();
                }

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

                if (PassThru.IsPresent)
                {
                    WriteObject(true);
                }
            }
            else
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidSubscription, subscriptionName));
            }
        }
Exemplo n.º 9
0
 public void GlobalSettingsManagerCreateNewEmptyAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidSubscriptionsData)
     {
         try
         {
             GlobalSettingsManager.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));
         }
     }
 }
Exemplo n.º 10
0
 public void GlobalSettingsManagerLoadExistingNullAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         try
         {
             GlobalSettingsManager.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));
         }
     }
 }
Exemplo n.º 11
0
 public void GlobalSettingsManagerLoadExistingEmptyAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         try
         {
             GlobalSettingsManager.Load("fake");
             Assert.Fail("No exception thrown");
         }
         catch (Exception ex)
         {
             Assert.IsTrue(ex is FileNotFoundException);
             Assert.AreEqual <string>(ex.Message, Resources.GlobalSettingsManager_Load_PublishSettingsNotFound);
             Assert.IsFalse(Directory.Exists(Data.AzureAppDir));
         }
     }
 }
Exemplo n.º 12
0
 public void GlobalSettingsManagerLoadExistingInvalidDirectoryNameAzureDirectoryFail()
 {
     foreach (string fileName in Data.ValidPublishSettings)
     {
         foreach (string invalidDirectoryName in Data.InvalidServiceRootName)
         {
             try
             {
                 GlobalSettingsManager.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));
             }
         }
     }
 }
Exemplo n.º 13
0
        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 globalSettingsManager             = GlobalSettingsManager.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, targetFile, Data.ValidPublishSettings[i]);
                MockCommandRuntime mockCommandRuntime = new MockCommandRuntime();
                var removeSubscriptionCommand         = new RemoveAzureSubscriptionCommand();
                removeSubscriptionCommand.CommandRuntime = mockCommandRuntime;
                removeSubscriptionCommand.RemoveSubscriptionProcess("mysub2", targetFile);

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

                // Clean
                globalSettingsManager.DeleteGlobalSettingsManager();
            }
        }
Exemplo n.º 14
0
        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 globalSettingsManager = GlobalSettingsManager.Load(GlobalPathInfo.GlobalSettingsDirectory);

            globalSettingsManager.DeleteGlobalSettingsManager();
        }
Exemplo n.º 15
0
        public void TestSetDefaultSubscription()
        {
            var globalSettingsManager = GlobalSettingsManager.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 = GlobalSettingsManager.Load(GlobalPathInfo.GlobalSettingsDirectory, newPath).SubscriptionManager;
            var defaultSubscription  = subscriptionsManager.Subscriptions.Values.First(subscription => subscription.IsDefault);

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

            globalSettingsManager.DeleteGlobalSettingsManager();
        }
Exemplo n.º 16
0
        public void GlobalSettingsManagerLoadIgnoresPublishExisting()
        {
            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
            GlobalSettingsManager globalSettingsManager = GlobalSettingsManager.CreateFromPublishSettings(Data.AzureAppDir, outputSubscriptionDataFile, publishSettingsFile);

            Assert.AreEqual(6, globalSettingsManager.Subscriptions.Count);

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

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

            // Clean
            globalSettingsManager.DeleteGlobalSettingsManager();
        }
Exemplo n.º 17
0
        /// <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
                GlobalSettingsManager globalSettingsManager;
                try
                {
                    globalSettingsManager = GlobalSettingsManager.Load(GlobalPathInfo.GlobalSettingsDirectory, subscriptionDataFile);
                }
                catch (FileNotFoundException)
                {
                    // assume that import has never been ran and just create it.
                    globalSettingsManager = GlobalSettingsManager.Create(GlobalPathInfo.GlobalSettingsDirectory,
                                                                         subscriptionDataFile,
                                                                         certificate,
                                                                         serviceEndpoint);
                }

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

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

                // Create or update
                globalSettingsManager.Subscriptions[subscription.SubscriptionName] = subscription;
                globalSettingsManager.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)
 {
     GlobalSettingsManager.Load(azureSdkDirPath).DeleteGlobalSettingsManager();
 }