Пример #1
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
                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));
                    }
                }
            }
        }