public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); WriteDebug(Resources.MakingClientCall); string itemName = string.Empty; CSMSetProtectionRequest input = new CSMSetProtectionRequest(); input.Properties = new CSMSetProtectionRequestProperties(); input.Properties.PolicyId = Policy.PolicyId; if (Item.GetType() == typeof(AzureRMBackupItem)) { itemName = (Item as AzureRMBackupItem).ItemName; } else if (Item.GetType() == typeof(AzureRMBackupContainer)) { WriteDebug(String.Format(Resources.ContainerTypeInput, Item.GetType())); if ((Item as AzureRMBackupContainer).ContainerType == AzureBackupContainerType.AzureVM.ToString()) { itemName = (Item as AzureRMBackupContainer).ContainerUniqueName; } else { throw new Exception(Resources.UnknownItemType); } } else { throw new Exception(Resources.UnknownItemType); } var operationId = AzureBackupClient.EnableProtection(Item.ResourceGroupName, Item.ResourceName, Item.ContainerUniqueName, itemName, input); WriteDebug(Resources.EnableAzureBackupProtection); var operationStatus = TrackOperation(Item.ResourceGroupName, Item.ResourceName, operationId); this.WriteObject(GetCreatedJobs(Item.ResourceGroupName, Item.ResourceName, new Models.AzureRMBackupVault(Item.ResourceGroupName, Item.ResourceName, Item.Location), operationStatus.JobList).FirstOrDefault()); }); }
//[Fact] public void EnableAzureBackupProtectionTest() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = GetServiceClient <BackupServicesManagementClient>(); CSMSetProtectionRequest input = new CSMSetProtectionRequest(); input.Properties = new CSMSetProtectionRequestProperties(); input.Properties.PolicyId = ConfigurationManager.AppSettings["PolicyId"]; string itemName = ConfigurationManager.AppSettings["AzureBackupItemName"]; string containerName = ConfigurationManager.AppSettings["ContainerName"]; var response = client.DataSource.EnableProtectionCSM(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, GetCustomRequestHeaders(), containerName, itemName, input); Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); } }
/// <summary> /// Enable protection for given item. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.BackupServices.IDataSourceOperations. /// </param> /// <param name='resourceGroupName'> /// Required. /// </param> /// <param name='resourceName'> /// Required. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='containerName'> /// Required. containerName. /// </param> /// <param name='itemName'> /// Required. itemName. /// </param> /// <param name='csmparameters'> /// Required. Set protection request input. /// </param> /// <returns> /// The definition of a Operation Response. /// </returns> public static Task <OperationResponse> EnableProtectionCSMAsync(this IDataSourceOperations operations, string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string containerName, string itemName, CSMSetProtectionRequest csmparameters) { return(operations.EnableProtectionCSMAsync(resourceGroupName, resourceName, customRequestHeaders, containerName, itemName, csmparameters, CancellationToken.None)); }
/// <summary> /// Enable protection for given item. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.BackupServices.IDataSourceOperations. /// </param> /// <param name='resourceGroupName'> /// Required. /// </param> /// <param name='resourceName'> /// Required. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='containerName'> /// Required. containerName. /// </param> /// <param name='itemName'> /// Required. itemName. /// </param> /// <param name='csmparameters'> /// Required. Set protection request input. /// </param> /// <returns> /// The definition of a Operation Response. /// </returns> public static OperationResponse EnableProtectionCSM(this IDataSourceOperations operations, string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string containerName, string itemName, CSMSetProtectionRequest csmparameters) { return(Task.Factory.StartNew((object s) => { return ((IDataSourceOperations)s).EnableProtectionCSMAsync(resourceGroupName, resourceName, customRequestHeaders, containerName, itemName, csmparameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// Enable protection for given item. /// </summary> /// <param name='resourceGroupName'> /// Required. /// </param> /// <param name='resourceName'> /// Required. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='containerName'> /// Required. containerName. /// </param> /// <param name='itemName'> /// Required. itemName. /// </param> /// <param name='csmparameters'> /// Required. Set protection request input. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The definition of a Operation Response. /// </returns> public async Task <OperationResponse> EnableProtectionCSMAsync(string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string containerName, string itemName, CSMSetProtectionRequest csmparameters, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (resourceName == null) { throw new ArgumentNullException("resourceName"); } if (containerName == null) { throw new ArgumentNullException("containerName"); } if (itemName == null) { throw new ArgumentNullException("itemName"); } if (csmparameters == null) { throw new ArgumentNullException("csmparameters"); } if (csmparameters.Properties == null) { throw new ArgumentNullException("csmparameters.Properties"); } if (csmparameters.Properties.PolicyId == null) { throw new ArgumentNullException("csmparameters.Properties.PolicyId"); } // 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("customRequestHeaders", customRequestHeaders); tracingParameters.Add("containerName", containerName); tracingParameters.Add("itemName", itemName); tracingParameters.Add("csmparameters", csmparameters); TracingAdapter.Enter(invocationId, this, "EnableProtectionCSMAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/resourceGroups/"; url = url + Uri.EscapeDataString(resourceGroupName); url = url + "/providers/"; url = url + "Microsoft.Backup"; url = url + "/"; url = url + "BackupVault"; url = url + "/"; url = url + Uri.EscapeDataString(resourceName); url = url + "/registeredContainers/"; url = url + Uri.EscapeDataString(containerName); url = url + "/protectedItems/"; url = url + Uri.EscapeDataString(itemName); List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2014-09-01"); 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 httpRequest.Headers.Add("Accept-Language", "en-us"); httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject cSMSetProtectionRequestValue = new JObject(); requestDoc = cSMSetProtectionRequestValue; JObject propertiesValue = new JObject(); cSMSetProtectionRequestValue["properties"] = propertiesValue; propertiesValue["policyId"] = csmparameters.Properties.PolicyId; requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); // 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.Accepted) { 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 OperationResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.Accepted) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new OperationResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { Guid operationIdInstance = Guid.Parse(((string)responseDoc)); result.OperationId = operationIdInstance; } } result.StatusCode = statusCode; if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Enable Protection /// </summary> /// <param name="request"></param> /// <returns></returns> public Guid EnableProtection(string resourceGroupName, string resourceName, string containerName, string itemName, CSMSetProtectionRequest request) { var response = AzureBackupClient.DataSource.EnableProtectionCSMAsync(resourceGroupName, resourceName, GetCustomRequestHeaders(), containerName, itemName, request, CmdletCancellationToken).Result; return(response.OperationId); }