public List <PSCacheService> GetCacheServices(string cacheServiceName) { List <PSCacheService> services = new List <PSCacheService>(); CloudServiceListResponse listResponse = client.CloudServices.List(); foreach (CloudServiceListResponse.CloudService cloudService in listResponse) { foreach (CloudServiceResource resource in cloudService.Resources) { if (IsCachingResource(resource.Type)) { bool nameMatched = string.IsNullOrEmpty(cacheServiceName) || cacheServiceName.Equals(resource.Name, StringComparison.OrdinalIgnoreCase); //'unknown' is a bad caching entry due to service internal error, // that we should not display; otherwise, it will screw up the displaying for missing some important fields. if (nameMatched && string.Compare(resource.State, "Unknown", StringComparison.OrdinalIgnoreCase) != 0) { services.Add(new PSCacheService(resource)); } } } } return(services); }
public bool DeleteJobCollection(string jobCollection, string region = "") { if (!string.IsNullOrEmpty(region)) { if (!this.AvailableRegions.Contains(region, StringComparer.OrdinalIgnoreCase)) { throw new Exception(Resources.SchedulerInvalidLocation); } SchedulerOperationStatusResponse response = schedulerManagementClient.JobCollections.Delete(region.ToCloudServiceName(), jobCollection); return(response.StatusCode == System.Net.HttpStatusCode.OK ? true : false); } else if (string.IsNullOrEmpty(region)) { CloudServiceListResponse csList = csmClient.CloudServices.List(); foreach (CloudServiceListResponse.CloudService cs in csList.CloudServices) { foreach (CloudServiceGetResponse.Resource csRes in csmClient.CloudServices.Get(cs.Name).Resources) { if (csRes.Type.Contains(Constants.JobCollectionResource)) { JobCollectionGetResponse jcGetResponse = schedulerManagementClient.JobCollections.Get(cs.Name, csRes.Name); if (jcGetResponse.Name.Equals(jobCollection, StringComparison.OrdinalIgnoreCase)) { SchedulerOperationStatusResponse response = schedulerManagementClient.JobCollections.Delete(region.ToCloudServiceName(), jobCollection); return(response.StatusCode == System.Net.HttpStatusCode.OK ? true : false); } } } } } return(false); }
public CloudServiceResource UpdateCacheService(string cacheServiceName, CacheServiceSkuType sku, string memory, Action <bool, string, string, string, Action> ConfirmAction, bool force) { CloudServiceListResponse listResponse = client.CloudServices.List(); CloudServiceResource cacheResource = null; string cloudServiceName = null; foreach (CloudServiceListResponse.CloudService cloudService in listResponse) { cacheResource = cloudService.Resources.FirstOrDefault( p => { return(p.Name.Equals(cacheServiceName) && IsCachingResource(p.Type)); }); if (cacheResource != null) { cloudServiceName = cloudService.Name; break; } } if (cacheResource == null) { throw new ArgumentException(string.Format(Properties.Resources.CacheServiceNotExisting, cacheServiceName)); } CacheSkuCountConvert convert = new CacheSkuCountConvert(sku); CacheServiceSkuType existingSkuType = cacheResource.IntrinsicSettingsSection.CacheServiceInputSection.SkuType; int existingSkuCount = cacheResource.IntrinsicSettingsSection.CacheServiceInputSection.SkuCount; int newSkuCount = convert.ToSkuCount(memory); if (existingSkuType == sku && existingSkuCount == newSkuCount) { WriteProgress("No update is needed as there is no change"); return(cacheResource); } //We will prompt only if there is data loss string promptMessage = GetPromptMessgaeIfThereIsDataLoss(existingSkuType, sku, existingSkuCount, newSkuCount); if (string.IsNullOrEmpty(promptMessage)) { force = true; } ConfirmAction( force, string.Format(Properties.Resources.UpdatingCacheService), promptMessage, cacheServiceName, () => { cacheResource.IntrinsicSettingsSection.CacheServiceInputSection.SkuCount = convert.ToSkuCount(memory); cacheResource.IntrinsicSettingsSection.CacheServiceInputSection.SkuType = sku; CacheServiceCreateParameters param = new CacheServiceCreateParameters(); param.IntrinsicSettingsSection = cacheResource.IntrinsicSettingsSection; param.ETag = cacheResource.ETag; cacheResource = ProvisionCacheService(cloudServiceName, cacheResource.Name, param, false); }); return(cacheResource); }
/// <summary> /// Gets Site Recovery client. /// </summary> /// <returns>Site Recovery Management client</returns> private SiteRecoveryManagementClient GetSiteRecoveryClient() { CloudServiceListResponse services = this.recoveryServicesClient.CloudServices.List(); this.ValidateVaultSettings( asrVaultCreds.ResourceName, asrVaultCreds.CloudServiceName, services); CloudService selectedCloudService = null; Vault selectedResource = null; foreach (CloudService cloudService in services) { if (cloudService.Name == asrVaultCreds.CloudServiceName) { selectedCloudService = cloudService; } } if (null == selectedCloudService) { throw new ArgumentException(Properties.Resources.InvalidCloudService); } foreach (Vault vault in selectedCloudService.Resources) { if (vault.Name == asrVaultCreds.ResourceName) { selectedResource = vault; } } if (null == selectedResource) { throw new ArgumentException(Properties.Resources.InvalidResource); } SiteRecoveryManagementClient siteRecoveryClient = AzureSession.ClientFactory.CreateCustomClient <SiteRecoveryManagementClient>(asrVaultCreds.CloudServiceName, asrVaultCreds.ResourceName, recoveryServicesClient.Credentials, Profile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement)); if (null == siteRecoveryClient) { throw new InvalidOperationException(Properties.Resources.NullRecoveryServicesClient); } return(siteRecoveryClient); }
/// <summary> /// Validates current in-memory Vault Settings. /// </summary> /// <param name="resourceName">Resource Name</param> /// <param name="cloudServiceName">Cloud Service Name</param> /// <param name="services">Cloud Services</param> /// <returns>Whether Vault settings are valid or not</returns> public bool ValidateVaultSettings( string resourceName, string cloudServiceName, CloudServiceListResponse services = null) { if (string.IsNullOrEmpty(resourceName) || string.IsNullOrEmpty(cloudServiceName)) { throw new InvalidOperationException(Properties.Resources.MissingVaultSettings); } if (null == services) { services = this.recoveryServicesClient.CloudServices.List(); } CloudService selectedCloudService = null; Vault selectedResource = null; foreach (CloudService cloudService in services) { if (cloudService.Name == cloudServiceName) { selectedCloudService = cloudService; } } if (null == selectedCloudService) { throw new ArgumentException(Properties.Resources.InvalidCloudService); } foreach (Vault vault in selectedCloudService.Resources) { if (vault.Name == resourceName) { selectedResource = vault; } } if (null == selectedResource) { throw new ArgumentException(Properties.Resources.InvalidResource); } return(true); }
/// <summary> /// Gets the current vault location. /// </summary> /// <returns>The current vault location.</returns> protected string GetCurrentValutLocation() { string location = string.Empty; CloudServiceListResponse response = this.RecoveryServicesClient.GetRecoveryServicesClient.CloudServices.List(); foreach (var cloudService in response.CloudServices) { if (cloudService.Name == PSRecoveryServicesClient.asrVaultCreds.CloudServiceName) { location = cloudService.GeoRegion; break; } } return location; }
private string GetAssociatedCloudServiceName(string cacheServiceName) { CloudServiceListResponse listResponse = client.CloudServices.List(); foreach (CloudServiceListResponse.CloudService cloudService in listResponse) { CloudServiceResource matched = cloudService.Resources.FirstOrDefault( resource => { return(IsCachingResource(resource.Type) && cacheServiceName.Equals(resource.Name, StringComparison.OrdinalIgnoreCase)); }); if (matched != null) { return(cloudService.Name); } } return(null); }
public List <PSJobCollection> GetJobCollection(string region = "", string jobCollection = "") { List <PSJobCollection> lstSchedulerJobCollection = new List <PSJobCollection>(); CloudServiceListResponse csList = csmClient.CloudServices.List(); if (!string.IsNullOrEmpty(region)) { if (!this.AvailableRegions.Contains(region, StringComparer.OrdinalIgnoreCase)) { throw new Exception(Resources.SchedulerInvalidLocation); } string cloudService = region.ToCloudServiceName(); foreach (CloudServiceListResponse.CloudService cs in csList.CloudServices) { if (cs.Name.Equals(cloudService, StringComparison.OrdinalIgnoreCase)) { GetSchedulerJobCollection(cs, jobCollection).ForEach(x => lstSchedulerJobCollection.Add(x)); //If job collection parameter was passed and we found a matching job collection already, exit out of the loop and return the job collection if (!string.IsNullOrEmpty(jobCollection) && lstSchedulerJobCollection.Count > 0) { return(lstSchedulerJobCollection); } } } } else if (string.IsNullOrEmpty(region)) { foreach (CloudServiceListResponse.CloudService cs in csList.CloudServices) { if (cs.Name.Equals(Constants.CloudServiceNameFirst + cs.GeoRegion.Replace(" ", string.Empty) + Constants.CloudServiceNameSecond, StringComparison.OrdinalIgnoreCase)) { GetSchedulerJobCollection(cs, jobCollection).ForEach(x => lstSchedulerJobCollection.Add(x)); //If job collection parameter was passed and we found a matching job collection already, exit out of the loop and return the job collection if (!string.IsNullOrEmpty(jobCollection) && lstSchedulerJobCollection.Count > 0) { return(lstSchedulerJobCollection); } } } } return(lstSchedulerJobCollection); }
public List <PSCacheService> GetCacheServices(string cacheServiceName) { List <PSCacheService> services = new List <PSCacheService>(); CloudServiceListResponse listResponse = client.CloudServices.List(); foreach (CloudServiceListResponse.CloudService cloudService in listResponse) { foreach (CloudServiceResource resource in cloudService.Resources) { if (resource.Type == CacheResourceType) { bool nameMatched = string.IsNullOrEmpty(cacheServiceName) || cacheServiceName.Equals(resource.Name, StringComparison.OrdinalIgnoreCase); if (nameMatched) { services.Add(new PSCacheService(resource)); } } } } return(services); }
private CloudServiceResource FetchCloudServiceResource(string cacheServiceName, out string cloudServiceName) { CloudServiceListResponse listResponse = client.CloudServices.List(); CloudServiceResource cacheResource = null; cloudServiceName = null; foreach (CloudServiceListResponse.CloudService cloudService in listResponse) { cacheResource = cloudService.Resources.FirstOrDefault( p => { return(p.Name.Equals(cacheServiceName) && IsCachingResource(p.Type)); }); if (cacheResource != null) { cloudServiceName = cloudService.Name; break; } } if (cacheResource == null) { throw new ArgumentException(string.Format(Properties.Resources.CacheServiceNotExisting, cacheServiceName)); } return(cacheResource); }
public bool DeleteJob(string jobCollection, string jobName, string region = "") { if (!string.IsNullOrEmpty(region)) { SchedulerClient schedulerClient = new SchedulerClient(csmClient.Credentials, region.ToCloudServiceName(), jobCollection); OperationResponse response = schedulerClient.Jobs.Delete(jobName); return(response.StatusCode == System.Net.HttpStatusCode.OK ? true : false); } else if (string.IsNullOrEmpty(region)) { CloudServiceListResponse csList = csmClient.CloudServices.List(); foreach (CloudServiceListResponse.CloudService cs in csList.CloudServices) { foreach (CloudServiceGetResponse.Resource csRes in csmClient.CloudServices.Get(cs.Name).Resources) { if (csRes.Type.Contains(Constants.JobCollectionResource)) { JobCollectionGetResponse jcGetResponse = schedulerManagementClient.JobCollections.Get(cs.Name, csRes.Name); if (jcGetResponse.Name.Equals(jobCollection, StringComparison.OrdinalIgnoreCase)) { foreach (PSSchedulerJob job in GetSchedulerJobs(cs.Name, jobCollection)) { if (job.JobName.Equals(jobName, StringComparison.OrdinalIgnoreCase)) { SchedulerClient schedulerClient = new SchedulerClient(csmClient.Credentials, cs.Name, jobCollection); OperationResponse response = schedulerClient.Jobs.Delete(jobName); return(response.StatusCode == System.Net.HttpStatusCode.OK ? true : false); } } } } } } } return(false); }
/// <summary> /// Validates current in-memory Vault Settings. /// </summary> /// <param name="resourceName">Resource Name</param> /// <param name="cloudServiceName">Cloud Service Name</param> /// <param name="services">Cloud Services</param> /// <returns>Whether Vault settings are valid or not</returns> public bool ValidateVaultSettings( string resourceName, string cloudServiceName, CloudServiceListResponse services = null) { if (string.IsNullOrEmpty(resourceName) || string.IsNullOrEmpty(cloudServiceName)) { throw new InvalidOperationException(Properties.Resources.MissingVaultSettings); } if (null == services) { services = this.recoveryServicesClient.CloudServices.List(); } CloudService selectedCloudService = null; Vault selectedResource = null; foreach (CloudService cloudService in services) { if (cloudService.Name == cloudServiceName) { selectedCloudService = cloudService; } } if (null == selectedCloudService) { throw new ArgumentException(Properties.Resources.InvalidCloudService); } foreach (Vault vault in selectedCloudService.Resources) { if (vault.Name == resourceName) { selectedResource = vault; } } if (null == selectedResource) { throw new ArgumentException(Properties.Resources.InvalidResource); } return true; }
/// <summary> /// Retrieve a list of Cloud services (see /// http://msdn.microsoft.com/en-us/library/windowsazure/XXXXXXX.aspx /// for more information) /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response model for the list cloud service operation. /// </returns> public async Task <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 = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/cloudservices?"; url = url + "resourceType=AutomationAccount"; url = url + "&detailLevel=Full"; url = url + "&resourceProviderNamespace=automation"; 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", "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)); 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) { result.CloudServices = new List <CloudService>(); foreach (XElement cloudServicesElement in cloudServicesSequenceElement.Elements(XName.Get("CloudService", "http://schemas.microsoft.com/windowsazure"))) { CloudService cloudServiceInstance = new 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 = 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) { cloudServiceInstance.Resources = new List <AutomationResource>(); foreach (XElement resourcesElement in resourcesSequenceElement.Elements(XName.Get("Resource", "http://schemas.microsoft.com/windowsazure"))) { AutomationResource resourceInstance = new AutomationResource(); 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; } } } } } 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(); } } }
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))) { PSJobCollection jc = new PSJobCollection { CloudServiceName = cloudService.Name, JobCollectionName = jcGetResponse.Name, State = Enum.GetName(typeof(JobCollectionState), jcGetResponse.State), Location = cloudService.GeoRegion, Uri = csmClient.BaseUri.AbsoluteUri + csmClient.Credentials.SubscriptionId + "cloudservices/" + cloudService.Name + Constants.JobCollectionResourceURL + jcGetResponse.Name }; if (jcGetResponse.IntrinsicSettings != null) { jc.Plan = Enum.GetName(typeof(JobCollectionPlan), jcGetResponse.IntrinsicSettings.Plan); if (jcGetResponse.IntrinsicSettings.Quota != null) { jc.MaxJobCount = jcGetResponse.IntrinsicSettings.Quota.MaxJobCount == null ? string.Empty : jcGetResponse.IntrinsicSettings.Quota.MaxJobCount.ToString(); jc.MaxRecurrence = jcGetResponse.IntrinsicSettings.Quota.MaxRecurrence == null ? string.Empty : jcGetResponse.IntrinsicSettings.Quota.MaxRecurrence.Interval.ToString() + " per " + jcGetResponse.IntrinsicSettings.Quota.MaxRecurrence.Frequency.ToString(); } } lstSchedulerJobCollection.Add(jc); } } } 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(); } } }
/// <summary> /// Retrieve a list of Cloud services /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response model for the list cloud service operation. /// </returns> public async Task <CloudServiceListResponse> ListAsync(CancellationToken cancellationToken) { // Validate // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters); } // Construct URL string url = ""; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/cloudservices"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2015-04-10"); 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.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/xml"); httpRequest.Headers.Add("x-ms-version", "2013-03-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // 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) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result CloudServiceListResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { 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"))) { CloudService cloudServiceInstance = new CloudService(); result.CloudServices.Add(cloudServiceInstance); 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 labelElement = cloudServicesElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure")); if (labelElement != null) { string labelInstance = labelElement.Value; cloudServiceInstance.Label = labelInstance; } XElement nameElement = cloudServicesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; cloudServiceInstance.Name = nameInstance; } 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"))) { Vault resourceInstance = new Vault(); cloudServiceInstance.Resources.Add(resourceInstance); XElement resourceProviderNamespaceElement = resourcesElement.Element(XName.Get("ResourceProviderNamespace", "http://schemas.microsoft.com/windowsazure")); if (resourceProviderNamespaceElement != null) { string resourceProviderNamespaceInstance = resourceProviderNamespaceElement.Value; resourceInstance.ResourceProviderNamespace = resourceProviderNamespaceInstance; } XElement eTagElement = resourcesElement.Element(XName.Get("ETag", "http://schemas.microsoft.com/windowsazure")); if (eTagElement != null) { string eTagInstance = eTagElement.Value; resourceInstance.ETag = eTagInstance; } XElement labelElement2 = resourcesElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure")); if (labelElement2 != null) { string labelInstance2 = labelElement2.Value; resourceInstance.Label = labelInstance2; } XElement nameElement2 = resourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement2 != null) { string nameInstance2 = nameElement2.Value; resourceInstance.Name = nameInstance2; } XElement operationStatusElement = resourcesElement.Element(XName.Get("OperationStatus", "http://schemas.microsoft.com/windowsazure")); if (operationStatusElement != null) { ResourceOperationStatus operationStatusInstance = new ResourceOperationStatus(); resourceInstance.OperationStatus = operationStatusInstance; XElement resultElement = operationStatusElement.Element(XName.Get("Result", "http://schemas.microsoft.com/windowsazure")); if (resultElement != null) { string resultInstance = resultElement.Value; operationStatusInstance.Result = resultInstance; } XElement typeElement = operationStatusElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure")); if (typeElement != null) { string typeInstance = typeElement.Value; operationStatusInstance.Type = typeInstance; } XElement errorElement = operationStatusElement.Element(XName.Get("Error", "http://schemas.microsoft.com/windowsazure")); if (errorElement != null) { ResourceErrorInfo errorInstance = new ResourceErrorInfo(); operationStatusInstance.Error = errorInstance; XElement extendedCodeElement = errorElement.Element(XName.Get("ExtendedCode", "http://schemas.microsoft.com/windowsazure")); if (extendedCodeElement != null) { string extendedCodeInstance = extendedCodeElement.Value; errorInstance.ExtendedCode = extendedCodeInstance; } XElement httpCodeElement = errorElement.Element(XName.Get("HttpCode", "http://schemas.microsoft.com/windowsazure")); if (httpCodeElement != null) { int httpCodeInstance = int.Parse(httpCodeElement.Value, CultureInfo.InvariantCulture); errorInstance.HttpCode = httpCodeInstance; } XElement messageElement = errorElement.Element(XName.Get("Message", "http://schemas.microsoft.com/windowsazure")); if (messageElement != null) { string messageInstance = messageElement.Value; errorInstance.Message = messageInstance; } } } 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"))) { OutputItem outputItemInstance = new OutputItem(); resourceInstance.OutputItems.Add(outputItemInstance); XElement keyElement = outputItemsElement.Element(XName.Get("Key", "http://schemas.microsoft.com/windowsazure")); if (keyElement != null) { string keyInstance = keyElement.Value; outputItemInstance.Key = keyInstance; } XElement valueElement = outputItemsElement.Element(XName.Get("Value", "http://schemas.microsoft.com/windowsazure")); if (valueElement != null) { string valueInstance = valueElement.Value; outputItemInstance.Value = valueInstance; } } } 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 stateElement = resourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure")); if (stateElement != null) { string stateInstance = stateElement.Value; resourceInstance.State = stateInstance; } XElement subStateElement = resourcesElement.Element(XName.Get("SubState", "http://schemas.microsoft.com/windowsazure")); if (subStateElement != null) { string subStateInstance = subStateElement.Value; resourceInstance.SubState = subStateInstance; } XElement typeElement2 = resourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure")); if (typeElement2 != null) { string typeInstance2 = typeElement2.Value; resourceInstance.Type = typeInstance2; } } } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Method to retrieve cloud services list for the current subscription /// </summary> /// <returns>list of cloud services.</returns> public IEnumerable <CloudService> GetCloudServices() { CloudServiceListResponse response = this.GetRecoveryServicesClient.CloudServices.List(); return(response.CloudServices); }