public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();
                Guid operationId = Guid.Empty;
                WriteDebug(Resources.MakingClientCall);

                if (!this.DeleteBackupData)
                {
                    //Calling update protection with policy Id as empty.
                    CSMUpdateProtectionRequest input = new CSMUpdateProtectionRequest()
                    {
                        Properties = new CSMUpdateProtectionRequestProperties(string.Empty)
                    };

                    operationId = AzureBackupClient.UpdateProtection(Item.ResourceGroupName, Item.ResourceName, Item.ContainerUniqueName, Item.ItemName, input);
                }

                else
                {
                    //Calling disable protection
                    operationId = AzureBackupClient.DisableProtection(Item.ResourceGroupName, Item.ResourceName, Item.ContainerUniqueName, Item.ItemName);
                }


                WriteDebug(Resources.DisableAzureBackupProtection);
                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 UpdateAzureBackupProtectionTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetServiceClient <BackupServicesManagementClient>();

                CSMUpdateProtectionRequest input = new CSMUpdateProtectionRequest();
                input.Properties = new CSMUpdateProtectionRequestProperties();
                string itemName      = ConfigurationManager.AppSettings["AzureBackupItemName"];
                string containerName = ConfigurationManager.AppSettings["ContainerName"];
                input.Properties.PolicyId = string.Empty;

                var response = client.DataSource.UpdateProtectionCSM(BackupServicesTestsBase.ResourceGroupName,
                                                                     BackupServicesTestsBase.ResourceName,
                                                                     GetCustomRequestHeaders(),
                                                                     containerName,
                                                                     itemName,
                                                                     input);

                Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
            }
        }
Beispiel #3
0
 /// <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> UpdateProtectionCSMAsync(this IDataSourceOperations operations, string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string containerName, string itemName, CSMUpdateProtectionRequest csmparameters)
 {
     return(operations.UpdateProtectionCSMAsync(resourceGroupName, resourceName, customRequestHeaders, containerName, itemName, csmparameters, CancellationToken.None));
 }
Beispiel #4
0
 /// <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 UpdateProtectionCSM(this IDataSourceOperations operations, string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string containerName, string itemName, CSMUpdateProtectionRequest csmparameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IDataSourceOperations)s).UpdateProtectionCSMAsync(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> UpdateProtectionCSMAsync(string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string containerName, string itemName, CSMUpdateProtectionRequest 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, "UpdateProtectionCSMAsync", 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     = new HttpMethod("PATCH");
                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 cSMUpdateProtectionRequestValue = new JObject();
                requestDoc = cSMUpdateProtectionRequestValue;

                JObject propertiesValue = new JObject();
                cSMUpdateProtectionRequestValue["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();
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Update Protection
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Guid UpdateProtection(string resourceGroupName, string resourceName, string containerName, string itemName, CSMUpdateProtectionRequest request)
        {
            var response = AzureBackupClient.DataSource.UpdateProtectionCSMAsync(resourceGroupName, resourceName, GetCustomRequestHeaders(), containerName, itemName, request, CmdletCancellationToken).Result;

            return(response.OperationId);
        }