public void ListProtectableObjectTest() { using (UndoContext context = UndoContext.Current) { context.Start(); string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"]; var client = GetServiceClient <RecoveryServicesBackupManagementClient>(resourceNamespace); ProtectableObjectListQueryParameters queryParams = new ProtectableObjectListQueryParameters(); PaginationRequest paginationParam = new PaginationRequest(); ProtectableObjectTestHelper poTestHelper = new ProtectableObjectTestHelper(client); ProtectableObjectListResponse response = poTestHelper.ListProtectableObjects(queryParams, paginationParam); string protectableObjectName = ConfigurationManager.AppSettings["RsVaultIaasV1ContainerUniqueName"]; Assert.True( response.ItemList.ProtectableObjects.Any( protectableObject => { return(protectableObject.Properties.GetType() == typeof(AzureIaaSClassicComputeVMProtectableItem) && protectableObject.Name == protectableObjectName); }), "Retrieved list of containers doesn't contain AzureIaaSClassicComputeVMProtectable Item"); } }
public ProtectableObjectListResponse ListProtectableObjects(string rsVaultRgName, string rsVaultName, ProtectableObjectListQueryParameters queryParams, PaginationRequest paginationParams) { ProtectableObjectListResponse response = Client.ProtectableObjects.List( rsVaultRgName, rsVaultName, queryParams, paginationParams, CommonTestHelper.GetCustomRequestHeaders()); Assert.NotNull(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.ItemList); return(response); }
public void ListProtectableObjectTest() { using (UndoContext context = UndoContext.Current) { context.Start(); string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"]; string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"]; string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"]; string location = ConfigurationManager.AppSettings["vaultLocationRP"]; // TODO: Create VM instead of taking these parameters from config string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"]; string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"]; string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"]; string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"]; string containerUri = containeType + ";" + containerUniqueName; string itemUri = itemType + ";" + itemUniqueName; var client = GetServiceClient <RecoveryServicesBackupManagementClient>(resourceNamespace); // 1. Create vault VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client); vaultTestHelper.CreateVault(resourceGroupName, resourceName, location); // 2. Get default policy PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client); string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName); // 3. Enable protection ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client); protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri); // ACTION: List protectable objects ProtectableObjectListQueryParameters queryParams = new ProtectableObjectListQueryParameters(); queryParams.BackupManagementType = BackupManagementType.AzureIaasVM.ToString(); ProtectableObjectTestHelper poTestHelper = new ProtectableObjectTestHelper(client); ProtectableObjectListResponse response = poTestHelper.ListProtectableObjects( resourceGroupName, resourceName, queryParams, new PaginationRequest()); // VALIDATION: VM should not be found in the list Assert.False(response.ItemList.ProtectableObjects.Any( protectableObject => { return(protectableObject.Properties.GetType().IsSubclassOf(typeof(AzureIaaSVMProtectableItem)) && protectableObject.Name.Contains(containerUniqueName)); }), "Retrieved list of containers doesn't contain AzureIaaSVMProtectableItem Item"); } }
/// <summary> /// Lists all the protectable objects within your subscription /// according to the query filter and the pagination parameters. /// </summary> /// <param name='resourceGroupName'> /// Required. Resource group name of your recovery services vault. /// </param> /// <param name='resourceName'> /// Required. Name of your recovery services vault. /// </param> /// <param name='queryFilter'> /// Optional. /// </param> /// <param name='paginationParams'> /// Optional. Pagination parameters for controlling the response. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// List of protectable object resonses as returned by the list /// protectable objects API. /// </returns> public async Task <ProtectableObjectListResponse> ListAsync(string resourceGroupName, string resourceName, ProtectableObjectListQueryParameters queryFilter, PaginationRequest paginationParams, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (resourceName == null) { throw new ArgumentNullException("resourceName"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("resourceGroupName", resourceGroupName); tracingParameters.Add("resourceName", resourceName); tracingParameters.Add("queryFilter", queryFilter); tracingParameters.Add("paginationParams", paginationParams); tracingParameters.Add("customRequestHeaders", customRequestHeaders); TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/Subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId.ToString()); } url = url + "/resourceGroups/"; url = url + Uri.EscapeDataString(resourceGroupName); url = url + "/providers/"; if (this.Client.ResourceNamespace != null) { url = url + Uri.EscapeDataString(this.Client.ResourceNamespace); } url = url + "/"; url = url + "vaults"; url = url + "/"; url = url + Uri.EscapeDataString(resourceName); url = url + "/backupProtectableItems"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2016-05-01"); List <string> odataFilter = new List <string>(); if (queryFilter != null && queryFilter.BackupManagementType != null) { odataFilter.Add("backupManagementType eq '" + Uri.EscapeDataString(queryFilter.BackupManagementType) + "'"); } if (queryFilter != null && queryFilter.Status != null) { odataFilter.Add("status eq '" + Uri.EscapeDataString(queryFilter.Status) + "'"); } if (queryFilter != null && queryFilter.FriendlyName != null) { odataFilter.Add("friendlyName eq '" + Uri.EscapeDataString(queryFilter.FriendlyName) + "'"); } if (odataFilter.Count > 0) { queryParameters.Add("$filter=" + string.Join(" and ", odataFilter)); } if (paginationParams != null && paginationParams.SkipToken != null) { queryParameters.Add("$skiptoken=" + Uri.EscapeDataString(paginationParams.SkipToken)); } if (paginationParams != null && paginationParams.Top != null) { queryParameters.Add("$top=" + Uri.EscapeDataString(paginationParams.Top)); } 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-Language", customRequestHeaders.Culture); httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId); // 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 ProtectableObjectListResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new ProtectableObjectListResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { ProtectableObjectResourceList itemListInstance = new ProtectableObjectResourceList(); result.ItemList = itemListInstance; JToken valueArray = responseDoc["value"]; if (valueArray != null && valueArray.Type != JTokenType.Null) { foreach (JToken valueValue in ((JArray)valueArray)) { ProtectableObjectResource protectableObjectResourceInstance = new ProtectableObjectResource(); itemListInstance.ProtectableObjects.Add(protectableObjectResourceInstance); JToken propertiesValue = valueValue["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { string typeName = ((string)propertiesValue["protectableItemType"]); if (typeName == "ProtectableItem") { ProtectableItem protectableItemInstance = new ProtectableItem(); JToken friendlyNameValue = propertiesValue["friendlyName"]; if (friendlyNameValue != null && friendlyNameValue.Type != JTokenType.Null) { string friendlyNameInstance = ((string)friendlyNameValue); protectableItemInstance.FriendlyName = friendlyNameInstance; } JToken protectionStateValue = propertiesValue["protectionState"]; if (protectionStateValue != null && protectionStateValue.Type != JTokenType.Null) { string protectionStateInstance = ((string)protectionStateValue); protectableItemInstance.ProtectionState = protectionStateInstance; } JToken protectableItemTypeValue = propertiesValue["protectableItemType"]; if (protectableItemTypeValue != null && protectableItemTypeValue.Type != JTokenType.Null) { string protectableItemTypeInstance = ((string)protectableItemTypeValue); protectableItemInstance.ProtectableItemType = protectableItemTypeInstance; } JToken backupManagementTypeValue = propertiesValue["backupManagementType"]; if (backupManagementTypeValue != null && backupManagementTypeValue.Type != JTokenType.Null) { string backupManagementTypeInstance = ((string)backupManagementTypeValue); protectableItemInstance.BackupManagementType = backupManagementTypeInstance; } protectableObjectResourceInstance.Properties = protectableItemInstance; } if (typeName == "IaaSVMProtectableItem") { AzureIaaSVMProtectableItem azureIaaSVMProtectableItemInstance = new AzureIaaSVMProtectableItem(); JToken virtualMachineIdValue = propertiesValue["virtualMachineId"]; if (virtualMachineIdValue != null && virtualMachineIdValue.Type != JTokenType.Null) { string virtualMachineIdInstance = ((string)virtualMachineIdValue); azureIaaSVMProtectableItemInstance.VirtualMachineId = virtualMachineIdInstance; } JToken friendlyNameValue2 = propertiesValue["friendlyName"]; if (friendlyNameValue2 != null && friendlyNameValue2.Type != JTokenType.Null) { string friendlyNameInstance2 = ((string)friendlyNameValue2); azureIaaSVMProtectableItemInstance.FriendlyName = friendlyNameInstance2; } JToken protectionStateValue2 = propertiesValue["protectionState"]; if (protectionStateValue2 != null && protectionStateValue2.Type != JTokenType.Null) { string protectionStateInstance2 = ((string)protectionStateValue2); azureIaaSVMProtectableItemInstance.ProtectionState = protectionStateInstance2; } JToken protectableItemTypeValue2 = propertiesValue["protectableItemType"]; if (protectableItemTypeValue2 != null && protectableItemTypeValue2.Type != JTokenType.Null) { string protectableItemTypeInstance2 = ((string)protectableItemTypeValue2); azureIaaSVMProtectableItemInstance.ProtectableItemType = protectableItemTypeInstance2; } JToken backupManagementTypeValue2 = propertiesValue["backupManagementType"]; if (backupManagementTypeValue2 != null && backupManagementTypeValue2.Type != JTokenType.Null) { string backupManagementTypeInstance2 = ((string)backupManagementTypeValue2); azureIaaSVMProtectableItemInstance.BackupManagementType = backupManagementTypeInstance2; } protectableObjectResourceInstance.Properties = azureIaaSVMProtectableItemInstance; } if (typeName == "Microsoft.ClassicCompute/virtualMachines") { AzureIaaSClassicComputeVMProtectableItem azureIaaSClassicComputeVMProtectableItemInstance = new AzureIaaSClassicComputeVMProtectableItem(); JToken virtualMachineIdValue2 = propertiesValue["virtualMachineId"]; if (virtualMachineIdValue2 != null && virtualMachineIdValue2.Type != JTokenType.Null) { string virtualMachineIdInstance2 = ((string)virtualMachineIdValue2); azureIaaSClassicComputeVMProtectableItemInstance.VirtualMachineId = virtualMachineIdInstance2; } JToken friendlyNameValue3 = propertiesValue["friendlyName"]; if (friendlyNameValue3 != null && friendlyNameValue3.Type != JTokenType.Null) { string friendlyNameInstance3 = ((string)friendlyNameValue3); azureIaaSClassicComputeVMProtectableItemInstance.FriendlyName = friendlyNameInstance3; } JToken protectionStateValue3 = propertiesValue["protectionState"]; if (protectionStateValue3 != null && protectionStateValue3.Type != JTokenType.Null) { string protectionStateInstance3 = ((string)protectionStateValue3); azureIaaSClassicComputeVMProtectableItemInstance.ProtectionState = protectionStateInstance3; } JToken protectableItemTypeValue3 = propertiesValue["protectableItemType"]; if (protectableItemTypeValue3 != null && protectableItemTypeValue3.Type != JTokenType.Null) { string protectableItemTypeInstance3 = ((string)protectableItemTypeValue3); azureIaaSClassicComputeVMProtectableItemInstance.ProtectableItemType = protectableItemTypeInstance3; } JToken backupManagementTypeValue3 = propertiesValue["backupManagementType"]; if (backupManagementTypeValue3 != null && backupManagementTypeValue3.Type != JTokenType.Null) { string backupManagementTypeInstance3 = ((string)backupManagementTypeValue3); azureIaaSClassicComputeVMProtectableItemInstance.BackupManagementType = backupManagementTypeInstance3; } protectableObjectResourceInstance.Properties = azureIaaSClassicComputeVMProtectableItemInstance; } if (typeName == "Microsoft.Compute/virtualMachines") { AzureIaaSComputeVMProtectableItem azureIaaSComputeVMProtectableItemInstance = new AzureIaaSComputeVMProtectableItem(); JToken virtualMachineIdValue3 = propertiesValue["virtualMachineId"]; if (virtualMachineIdValue3 != null && virtualMachineIdValue3.Type != JTokenType.Null) { string virtualMachineIdInstance3 = ((string)virtualMachineIdValue3); azureIaaSComputeVMProtectableItemInstance.VirtualMachineId = virtualMachineIdInstance3; } JToken friendlyNameValue4 = propertiesValue["friendlyName"]; if (friendlyNameValue4 != null && friendlyNameValue4.Type != JTokenType.Null) { string friendlyNameInstance4 = ((string)friendlyNameValue4); azureIaaSComputeVMProtectableItemInstance.FriendlyName = friendlyNameInstance4; } JToken protectionStateValue4 = propertiesValue["protectionState"]; if (protectionStateValue4 != null && protectionStateValue4.Type != JTokenType.Null) { string protectionStateInstance4 = ((string)protectionStateValue4); azureIaaSComputeVMProtectableItemInstance.ProtectionState = protectionStateInstance4; } JToken protectableItemTypeValue4 = propertiesValue["protectableItemType"]; if (protectableItemTypeValue4 != null && protectableItemTypeValue4.Type != JTokenType.Null) { string protectableItemTypeInstance4 = ((string)protectableItemTypeValue4); azureIaaSComputeVMProtectableItemInstance.ProtectableItemType = protectableItemTypeInstance4; } JToken backupManagementTypeValue4 = propertiesValue["backupManagementType"]; if (backupManagementTypeValue4 != null && backupManagementTypeValue4.Type != JTokenType.Null) { string backupManagementTypeInstance4 = ((string)backupManagementTypeValue4); azureIaaSComputeVMProtectableItemInstance.BackupManagementType = backupManagementTypeInstance4; } protectableObjectResourceInstance.Properties = azureIaaSComputeVMProtectableItemInstance; } } JToken idValue = valueValue["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); protectableObjectResourceInstance.Id = idInstance; } JToken nameValue = valueValue["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); protectableObjectResourceInstance.Name = nameInstance; } JToken typeValue = valueValue["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); protectableObjectResourceInstance.Type = typeInstance; } JToken locationValue = valueValue["location"]; if (locationValue != null && locationValue.Type != JTokenType.Null) { string locationInstance = ((string)locationValue); protectableObjectResourceInstance.Location = locationInstance; } JToken tagsSequenceElement = ((JToken)valueValue["tags"]); if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null) { foreach (JProperty property in tagsSequenceElement) { string tagsKey = ((string)property.Name); string tagsValue = ((string)property.Value); protectableObjectResourceInstance.Tags.Add(tagsKey, tagsValue); } } JToken eTagValue = valueValue["eTag"]; if (eTagValue != null && eTagValue.Type != JTokenType.Null) { string eTagInstance = ((string)eTagValue); protectableObjectResourceInstance.ETag = eTagInstance; } } } JToken nextLinkValue = responseDoc["nextLink"]; if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null) { string nextLinkInstance = ((string)nextLinkValue); itemListInstance.NextLink = nextLinkInstance; } } } result.StatusCode = statusCode; if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }