コード例 #1
0
        /// <summary>
        /// Performs the API operation(s) against subscriptions as administrator.
        /// </summary>
        protected override void ExecuteCore()
        {
            if (this.MyInvocation.InvocationName.Equals("Set-AzureRmManagedSubscription", StringComparison.OrdinalIgnoreCase))
            {
                this.WriteWarning("Alias Set-AzureRmManagedSubscription will be deprecated in a future release. Please use the cmdlet name Set-AzsUserSubscription instead");
            }

            if (this.MyInvocation.InvocationName.Equals("Set-AzsTenantSubscription", StringComparison.OrdinalIgnoreCase))
            {
                this.WriteWarning("Alias Set-AzsTenantSubscription will be deprecated in a future release. Please use the cmdlet name Set-AzsUserSubscription instead");
            }

            if (ShouldProcess(this.Subscription.SubscriptionId, VerbsCommon.Set))
            {
                using (var client = this.GetAzureStackClient())
                {
                    this.WriteVerbose(
                        Resources.UpdatingManagedSubscription.FormatArgs(
                            this.Subscription.SubscriptionId));

                    var parameters = new SubscriptionCreateOrUpdateAsAdminParameters(this.Subscription);
                    var result     = client.TenantSubscriptions.CreateOrUpdate(parameters).Subscription;
                }
            }
        }
 /// <summary>
 /// Create or updates the subscription
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.ISubscriptionOperationsAsAdmin.
 /// </param>
 /// <param name='parameters'>
 /// Required. Subscription update parameters
 /// </param>
 /// <returns>
 /// Result of the create or the update operation of the subscription
 /// </returns>
 public static Task <SubscriptionCreateOrUpdateAsAdminResult> CreateOrUpdateAsync(this ISubscriptionOperationsAsAdmin operations, SubscriptionCreateOrUpdateAsAdminParameters parameters)
 {
     return(operations.CreateOrUpdateAsync(parameters, CancellationToken.None));
 }
 /// <summary>
 /// Create or updates the subscription
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.ISubscriptionOperationsAsAdmin.
 /// </param>
 /// <param name='parameters'>
 /// Required. Subscription update parameters
 /// </param>
 /// <returns>
 /// Result of the create or the update operation of the subscription
 /// </returns>
 public static SubscriptionCreateOrUpdateAsAdminResult CreateOrUpdate(this ISubscriptionOperationsAsAdmin operations, SubscriptionCreateOrUpdateAsAdminParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((ISubscriptionOperationsAsAdmin)s).CreateOrUpdateAsync(parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
        /// <summary>
        /// Create or updates the subscription
        /// </summary>
        /// <param name='parameters'>
        /// Required. Subscription update parameters
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Result of the create or the update operation of the subscription
        /// </returns>
        public async Task <SubscriptionCreateOrUpdateAsAdminResult> CreateOrUpdateAsync(SubscriptionCreateOrUpdateAsAdminParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Subscription == null)
            {
                throw new ArgumentNullException("parameters.Subscription");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateOrUpdateAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/providers/Microsoft.Subscriptions.Admin/subscriptions/";
            if (parameters.Subscription.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(parameters.Subscription.SubscriptionId);
            }
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=" + Uri.EscapeDataString(this.Client.ApiVersion));
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Put;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Serialize Request
                string requestContent = null;
                JToken requestDoc     = null;

                JObject subscriptionCreateOrUpdateAsAdminParametersValue = new JObject();
                requestDoc = subscriptionCreateOrUpdateAsAdminParametersValue;

                if (parameters.Subscription.Id != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["id"] = parameters.Subscription.Id;
                }

                if (parameters.Subscription.SubscriptionId != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["subscriptionId"] = parameters.Subscription.SubscriptionId;
                }

                if (parameters.Subscription.DelegatedProviderSubscriptionId != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["delegatedProviderSubscriptionId"] = parameters.Subscription.DelegatedProviderSubscriptionId;
                }

                if (parameters.Subscription.DisplayName != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["displayName"] = parameters.Subscription.DisplayName;
                }

                if (parameters.Subscription.ExternalReferenceId != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["externalReferenceId"] = parameters.Subscription.ExternalReferenceId;
                }

                if (parameters.Subscription.Owner != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["owner"] = parameters.Subscription.Owner;
                }

                if (parameters.Subscription.TenantId != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["tenantId"] = parameters.Subscription.TenantId;
                }

                subscriptionCreateOrUpdateAsAdminParametersValue["routingResourceManagerType"] = parameters.Subscription.RoutingResourceManagerType.ToString();

                if (parameters.Subscription.OfferId != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["offerId"] = parameters.Subscription.OfferId;
                }

                if (parameters.Subscription.State != null)
                {
                    subscriptionCreateOrUpdateAsAdminParametersValue["state"] = parameters.Subscription.State.Value.ToString();
                }

                requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    SubscriptionCreateOrUpdateAsAdminResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new SubscriptionCreateOrUpdateAsAdminResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            AdminSubscriptionDefinition subscriptionInstance = new AdminSubscriptionDefinition();
                            result.Subscription = subscriptionInstance;

                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                subscriptionInstance.Id = idInstance;
                            }

                            JToken subscriptionIdValue = responseDoc["subscriptionId"];
                            if (subscriptionIdValue != null && subscriptionIdValue.Type != JTokenType.Null)
                            {
                                string subscriptionIdInstance = ((string)subscriptionIdValue);
                                subscriptionInstance.SubscriptionId = subscriptionIdInstance;
                            }

                            JToken delegatedProviderSubscriptionIdValue = responseDoc["delegatedProviderSubscriptionId"];
                            if (delegatedProviderSubscriptionIdValue != null && delegatedProviderSubscriptionIdValue.Type != JTokenType.Null)
                            {
                                string delegatedProviderSubscriptionIdInstance = ((string)delegatedProviderSubscriptionIdValue);
                                subscriptionInstance.DelegatedProviderSubscriptionId = delegatedProviderSubscriptionIdInstance;
                            }

                            JToken displayNameValue = responseDoc["displayName"];
                            if (displayNameValue != null && displayNameValue.Type != JTokenType.Null)
                            {
                                string displayNameInstance = ((string)displayNameValue);
                                subscriptionInstance.DisplayName = displayNameInstance;
                            }

                            JToken externalReferenceIdValue = responseDoc["externalReferenceId"];
                            if (externalReferenceIdValue != null && externalReferenceIdValue.Type != JTokenType.Null)
                            {
                                string externalReferenceIdInstance = ((string)externalReferenceIdValue);
                                subscriptionInstance.ExternalReferenceId = externalReferenceIdInstance;
                            }

                            JToken ownerValue = responseDoc["owner"];
                            if (ownerValue != null && ownerValue.Type != JTokenType.Null)
                            {
                                string ownerInstance = ((string)ownerValue);
                                subscriptionInstance.Owner = ownerInstance;
                            }

                            JToken tenantIdValue = responseDoc["tenantId"];
                            if (tenantIdValue != null && tenantIdValue.Type != JTokenType.Null)
                            {
                                string tenantIdInstance = ((string)tenantIdValue);
                                subscriptionInstance.TenantId = tenantIdInstance;
                            }

                            JToken routingResourceManagerTypeValue = responseDoc["routingResourceManagerType"];
                            if (routingResourceManagerTypeValue != null && routingResourceManagerTypeValue.Type != JTokenType.Null)
                            {
                                ResourceManagerType routingResourceManagerTypeInstance = ((ResourceManagerType)Enum.Parse(typeof(ResourceManagerType), ((string)routingResourceManagerTypeValue), true));
                                subscriptionInstance.RoutingResourceManagerType = routingResourceManagerTypeInstance;
                            }

                            JToken offerIdValue = responseDoc["offerId"];
                            if (offerIdValue != null && offerIdValue.Type != JTokenType.Null)
                            {
                                string offerIdInstance = ((string)offerIdValue);
                                subscriptionInstance.OfferId = offerIdInstance;
                            }

                            JToken stateValue = responseDoc["state"];
                            if (stateValue != null && stateValue.Type != JTokenType.Null)
                            {
                                SubscriptionState stateInstance = ((SubscriptionState)Enum.Parse(typeof(SubscriptionState), ((string)stateValue), true));
                                subscriptionInstance.State = stateInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }