Exemplo n.º 1
0
        protected override void Execute()
        {
            if (!string.IsNullOrEmpty(Name))
            {
                Name = base.ParseNameForQuota(Name);
                QuotaGetResponse response = Client.Quotas.Get(Location, Name);
                WriteObject(new QuotaResponse(response.Quota));
            }
            else
            {
                QuotaListResponse response = Client.Quotas.List(Location);

                WriteObject(response.Quotas.Select(q => new QuotaResponse(q)), true);
            }
        }
        /// <summary>
        /// Process the get quota request using the supplied server name.  This will use the REST API
        /// to make the request.
        /// </summary>
        /// <param name="quotaName"></param>
        private void ProcessWithServerName(string quotaName)
        {
            try
            {
                SqlManagementClient sqlManagementClient = GetCurrentSqlClient();

                // Retrieve the list of servers
                QuotaListResponse   response = sqlManagementClient.Quotas.List(this.ServerName);
                IEnumerable <Quota> quotas   = response.Quotas;
                if (!string.IsNullOrEmpty(quotaName))
                {
                    // Quota name is specified, find the one with the
                    // same name.
                    quotas = response.Quotas.Where(q => q.Name == quotaName);
                    if (quotas.Count() == 0)
                    {
                        throw new ItemNotFoundException(string.Format(
                                                            CultureInfo.InvariantCulture,
                                                            Resources.GetAzureSqlDatabaseServerNotFound,
                                                            quotaName));
                    }
                }

                // Construct the result
                IEnumerable <SqlDatabaseServerQuotaContext> processResult = quotas.Select(
                    quota => new SqlDatabaseServerQuotaContext
                {
                    OperationStatus      = Services.Constants.OperationSuccess,
                    OperationDescription = CommandRuntime.ToString(),
                    OperationId          = response.RequestId,
                    ServerName           = this.ServerName,
                    Name  = quota.Name,
                    State = quota.State,
                    Type  = quota.Type,
                    Value = quota.Value,
                });

                this.WriteObject(processResult);
            }
            catch (Exception ex)
            {
                this.WriteErrorDetails(ex);
            }
        }
        /// <summary>
        /// Returns a list of quotas from the server.
        /// </summary>
        /// <param name='serverName'>
        /// Required. The name of the Azure SQL Database Server from which to
        /// get the quotas.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Represents the response structure for the Quota List operation.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.QuotaListResponse> ListAsync(string serverName, CancellationToken cancellationToken)
        {
            // Validate
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

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

            // Construct URL
            string url     = "/" + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId) + "/services/sqlservers/servers/" + Uri.EscapeDataString(serverName) + "/serverquotas";
            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.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2012-03-01");

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

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

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

                    // Create Result
                    QuotaListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new QuotaListResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
                    if (serviceResourcesSequenceElement != null)
                    {
                        foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
                        {
                            Quota serviceResourceInstance = new Quota();
                            result.Quotas.Add(serviceResourceInstance);

                            XElement valueElement = serviceResourcesElement.Element(XName.Get("Value", "http://schemas.microsoft.com/windowsazure"));
                            if (valueElement != null)
                            {
                                string valueInstance = valueElement.Value;
                                serviceResourceInstance.Value = valueInstance;
                            }

                            XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                            if (nameElement != null)
                            {
                                string nameInstance = nameElement.Value;
                                serviceResourceInstance.Name = nameInstance;
                            }

                            XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                            if (typeElement != null)
                            {
                                string typeInstance = typeElement.Value;
                                serviceResourceInstance.Type = typeInstance;
                            }

                            XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                            if (stateElement != null)
                            {
                                string stateInstance = stateElement.Value;
                                serviceResourceInstance.State = stateInstance;
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

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