private List <PSJobCollection> GetSchedulerJobCollection(CloudServiceListResponse.CloudService cloudService, string jobCollection)
        {
            List <PSJobCollection> lstSchedulerJobCollection = new List <PSJobCollection>();

            foreach (CloudServiceGetResponse.Resource csRes in csmClient.CloudServices.Get(cloudService.Name).Resources)
            {
                if (csRes.Type.Contains(Constants.JobCollectionResource))
                {
                    JobCollectionGetResponse jcGetResponse = schedulerManagementClient.JobCollections.Get(cloudService.Name, csRes.Name);
                    if (string.IsNullOrEmpty(jobCollection) || (!string.IsNullOrEmpty(jobCollection) && jcGetResponse.Name.Equals(jobCollection, StringComparison.OrdinalIgnoreCase)))
                    {
                        lstSchedulerJobCollection.Add(new PSJobCollection
                        {
                            CloudServiceName  = cloudService.Name,
                            JobCollectionName = jcGetResponse.Name,
                            MaxJobCount       = jcGetResponse.IntrinsicSettings.Quota.MaxJobCount.ToString(),
                            MaxRecurrence     = jcGetResponse.IntrinsicSettings.Quota.MaxRecurrence == null ? "" : jcGetResponse.IntrinsicSettings.Quota.MaxRecurrence.Interval.ToString() + " per " +
                                                jcGetResponse.IntrinsicSettings.Quota.MaxRecurrence.Frequency.ToString(),
                            State    = Enum.GetName(typeof(JobCollectionState), jcGetResponse.State),
                            Plan     = Enum.GetName(typeof(JobCollectionPlan), jcGetResponse.IntrinsicSettings.Plan),
                            Location = cloudService.GeoRegion,
                            Uri      = csmClient.BaseUri.AbsoluteUri + csmClient.Credentials.SubscriptionId + "cloudservices/" + cloudService.Name + Constants.JobCollectionResourceURL + jcGetResponse.Name
                        });
                    }
                }
            }

            return(lstSchedulerJobCollection);
        }
        /// <summary>
        /// The List Cloud Services operation enumerates Windows Azure Store
        /// entries that are provisioned for a subscription.
        /// </summary>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response structure for the Cloud Service List operation.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Store.Models.CloudServiceListResponse> ListAsync(CancellationToken cancellationToken)
        {
            // Validate

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

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

            // Construct URL
            string url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/CloudServices/";

            // 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", "2013-06-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), CloudExceptionType.Xml);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

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

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

                    XElement cloudServicesSequenceElement = responseDoc.Element(XName.Get("CloudServices", "http://schemas.microsoft.com/windowsazure"));
                    if (cloudServicesSequenceElement != null)
                    {
                        foreach (XElement cloudServicesElement in cloudServicesSequenceElement.Elements(XName.Get("CloudService", "http://schemas.microsoft.com/windowsazure")))
                        {
                            CloudServiceListResponse.CloudService cloudServiceInstance = new CloudServiceListResponse.CloudService();
                            result.CloudServices.Add(cloudServiceInstance);

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

                            XElement labelElement = cloudServicesElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure"));
                            if (labelElement != null)
                            {
                                string labelInstance = TypeConversion.FromBase64String(labelElement.Value);
                                cloudServiceInstance.Label = labelInstance;
                            }

                            XElement descriptionElement = cloudServicesElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure"));
                            if (descriptionElement != null)
                            {
                                string descriptionInstance = descriptionElement.Value;
                                cloudServiceInstance.Description = descriptionInstance;
                            }

                            XElement geoRegionElement = cloudServicesElement.Element(XName.Get("GeoRegion", "http://schemas.microsoft.com/windowsazure"));
                            if (geoRegionElement != null)
                            {
                                string geoRegionInstance = geoRegionElement.Value;
                                cloudServiceInstance.GeoRegion = geoRegionInstance;
                            }

                            XElement resourcesSequenceElement = cloudServicesElement.Element(XName.Get("Resources", "http://schemas.microsoft.com/windowsazure"));
                            if (resourcesSequenceElement != null)
                            {
                                foreach (XElement resourcesElement in resourcesSequenceElement.Elements(XName.Get("Resource", "http://schemas.microsoft.com/windowsazure")))
                                {
                                    CloudServiceListResponse.CloudService.AddOnResource resourceInstance = new CloudServiceListResponse.CloudService.AddOnResource();
                                    cloudServiceInstance.Resources.Add(resourceInstance);

                                    XElement resourceProviderNamespaceElement = resourcesElement.Element(XName.Get("ResourceProviderNamespace", "http://schemas.microsoft.com/windowsazure"));
                                    if (resourceProviderNamespaceElement != null)
                                    {
                                        string resourceProviderNamespaceInstance = resourceProviderNamespaceElement.Value;
                                        resourceInstance.Namespace = resourceProviderNamespaceInstance;
                                    }

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

                                    XElement nameElement2 = resourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                                    if (nameElement2 != null)
                                    {
                                        string nameInstance2 = nameElement2.Value;
                                        resourceInstance.Name = nameInstance2;
                                    }

                                    XElement planElement = resourcesElement.Element(XName.Get("Plan", "http://schemas.microsoft.com/windowsazure"));
                                    if (planElement != null)
                                    {
                                        string planInstance = planElement.Value;
                                        resourceInstance.Plan = planInstance;
                                    }

                                    XElement schemaVersionElement = resourcesElement.Element(XName.Get("SchemaVersion", "http://schemas.microsoft.com/windowsazure"));
                                    if (schemaVersionElement != null)
                                    {
                                        string schemaVersionInstance = schemaVersionElement.Value;
                                        resourceInstance.SchemaVersion = schemaVersionInstance;
                                    }

                                    XElement eTagElement = resourcesElement.Element(XName.Get("ETag", "http://schemas.microsoft.com/windowsazure"));
                                    if (eTagElement != null)
                                    {
                                        string eTagInstance = eTagElement.Value;
                                        resourceInstance.ETag = eTagInstance;
                                    }

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

                                    XElement usageMetersSequenceElement = resourcesElement.Element(XName.Get("UsageMeters", "http://schemas.microsoft.com/windowsazure"));
                                    if (usageMetersSequenceElement != null)
                                    {
                                        foreach (XElement usageMetersElement in usageMetersSequenceElement.Elements(XName.Get("UsageMeter", "http://schemas.microsoft.com/windowsazure")))
                                        {
                                            CloudServiceListResponse.CloudService.AddOnResource.UsageLimit usageMeterInstance = new CloudServiceListResponse.CloudService.AddOnResource.UsageLimit();
                                            resourceInstance.UsageLimits.Add(usageMeterInstance);

                                            XElement nameElement3 = usageMetersElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                                            if (nameElement3 != null)
                                            {
                                                string nameInstance3 = nameElement3.Value;
                                                usageMeterInstance.Name = nameInstance3;
                                            }

                                            XElement unitElement = usageMetersElement.Element(XName.Get("Unit", "http://schemas.microsoft.com/windowsazure"));
                                            if (unitElement != null)
                                            {
                                                string unitInstance = unitElement.Value;
                                                usageMeterInstance.Unit = unitInstance;
                                            }

                                            XElement includedElement = usageMetersElement.Element(XName.Get("Included", "http://schemas.microsoft.com/windowsazure"));
                                            if (includedElement != null)
                                            {
                                                long includedInstance = long.Parse(includedElement.Value, CultureInfo.InvariantCulture);
                                                usageMeterInstance.AmountIncluded = includedInstance;
                                            }

                                            XElement usedElement = usageMetersElement.Element(XName.Get("Used", "http://schemas.microsoft.com/windowsazure"));
                                            if (usedElement != null)
                                            {
                                                long usedInstance = long.Parse(usedElement.Value, CultureInfo.InvariantCulture);
                                                usageMeterInstance.AmountUsed = usedInstance;
                                            }
                                        }
                                    }

                                    XElement outputItemsSequenceElement = resourcesElement.Element(XName.Get("OutputItems", "http://schemas.microsoft.com/windowsazure"));
                                    if (outputItemsSequenceElement != null)
                                    {
                                        foreach (XElement outputItemsElement in outputItemsSequenceElement.Elements(XName.Get("OutputItem", "http://schemas.microsoft.com/windowsazure")))
                                        {
                                            string outputItemsKey   = outputItemsElement.Element(XName.Get("Key", "http://schemas.microsoft.com/windowsazure")).Value;
                                            string outputItemsValue = outputItemsElement.Element(XName.Get("Value", "http://schemas.microsoft.com/windowsazure")).Value;
                                            resourceInstance.OutputItems.Add(outputItemsKey, outputItemsValue);
                                        }
                                    }

                                    XElement operationStatusElement = resourcesElement.Element(XName.Get("OperationStatus", "http://schemas.microsoft.com/windowsazure"));
                                    if (operationStatusElement != null)
                                    {
                                        CloudServiceListResponse.CloudService.AddOnResource.OperationStatus operationStatusInstance = new CloudServiceListResponse.CloudService.AddOnResource.OperationStatus();
                                        resourceInstance.Status = operationStatusInstance;

                                        XElement typeElement2 = operationStatusElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                                        if (typeElement2 != null)
                                        {
                                            string typeInstance2 = typeElement2.Value;
                                            operationStatusInstance.Type = typeInstance2;
                                        }

                                        XElement resultElement = operationStatusElement.Element(XName.Get("Result", "http://schemas.microsoft.com/windowsazure"));
                                        if (resultElement != null)
                                        {
                                            string resultInstance = resultElement.Value;
                                            operationStatusInstance.Result = resultInstance;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    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();
                }
            }
        }