예제 #1
0
        public AzureRMBackupItem(CSMProtectedItemResponse datasource, AzureRMBackupContainer azureBackupContainer)
            : base(datasource, azureBackupContainer)
        {
            DataSourceStatus = datasource.Properties.ProtectionStatus;
            ProtectionStatus = datasource.Properties.Status;
            ItemName         = datasource.Name;
            Name             = datasource.Properties.FriendlyName;

            if (datasource.Properties.ProtectionPolicyId != null)
            {
                ProtectionPolicyName = datasource.Properties.ProtectionPolicyId.Split('/').Last();
            }

            ProtectionPolicyId  = datasource.Properties.ProtectionPolicyId;
            RecoveryPointsCount = datasource.Properties.RecoveryPointsCount;
            Type = ItemHelpers.GetTypeForItem(datasource.Properties.ItemType);
        }
 public void WriteAzureBackupItem(CSMProtectedItemResponse sourceItem, AzureRMBackupContainer azureBackupItem)
 {
     this.WriteObject(new AzureRMBackupItem(sourceItem, azureBackupItem));
 }
        /// <summary>
        /// Get the list of all Datasources.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required.
        /// </param>
        /// <param name='resourceName'>
        /// Required.
        /// </param>
        /// <param name='csmparameters'>
        /// Optional. DataSource query parameter.
        /// </param>
        /// <param name='customRequestHeaders'>
        /// Optional. Request header parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The definition of a CSMProtectedItemListOperationResponse.
        /// </returns>
        public async Task <CSMProtectedItemListOperationResponse> ListCSMAsync(string resourceGroupName, string resourceName, CSMProtectedItemQueryObject csmparameters, 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("csmparameters", csmparameters);
                tracingParameters.Add("customRequestHeaders", customRequestHeaders);
                TracingAdapter.Enter(invocationId, this, "ListCSMAsync", 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 + "/protectedItems";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2014-09-01");
            List <string> odataFilter = new List <string>();

            if (csmparameters != null && csmparameters.ProtectionStatus != null)
            {
                odataFilter.Add("protectionStatus eq '" + Uri.EscapeDataString(csmparameters.ProtectionStatus) + "'");
            }
            if (csmparameters != null && csmparameters.Status != null)
            {
                odataFilter.Add("status eq '" + Uri.EscapeDataString(csmparameters.Status) + "'");
            }
            if (csmparameters != null && csmparameters.Type != null)
            {
                odataFilter.Add("itemType eq '" + Uri.EscapeDataString(csmparameters.Type) + "'");
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(" and ", odataFilter));
            }
            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", "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);

                // 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
                    CSMProtectedItemListOperationResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new CSMProtectedItemListOperationResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            CSMProtectedItemListResponse cSMProtectedItemListResponseInstance = new CSMProtectedItemListResponse();
                            result.CSMProtectedItemListResponse = cSMProtectedItemListResponseInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    CSMProtectedItemResponse cSMProtectedItemResponseInstance = new CSMProtectedItemResponse();
                                    cSMProtectedItemListResponseInstance.Value.Add(cSMProtectedItemResponseInstance);

                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        CSMProtectedItemProperties propertiesInstance = new CSMProtectedItemProperties();
                                        cSMProtectedItemResponseInstance.Properties = propertiesInstance;

                                        JToken protectionStatusValue = propertiesValue["protectionStatus"];
                                        if (protectionStatusValue != null && protectionStatusValue.Type != JTokenType.Null)
                                        {
                                            string protectionStatusInstance = ((string)protectionStatusValue);
                                            propertiesInstance.ProtectionStatus = protectionStatusInstance;
                                        }

                                        JToken protectionPolicyIdValue = propertiesValue["protectionPolicyId"];
                                        if (protectionPolicyIdValue != null && protectionPolicyIdValue.Type != JTokenType.Null)
                                        {
                                            string protectionPolicyIdInstance = ((string)protectionPolicyIdValue);
                                            propertiesInstance.ProtectionPolicyId = protectionPolicyIdInstance;
                                        }

                                        JToken policyInconsistentValue = propertiesValue["policyInconsistent"];
                                        if (policyInconsistentValue != null && policyInconsistentValue.Type != JTokenType.Null)
                                        {
                                            bool policyInconsistentInstance = ((bool)policyInconsistentValue);
                                            propertiesInstance.PolicyInconsistent = policyInconsistentInstance;
                                        }

                                        JToken recoveryPointsCountValue = propertiesValue["recoveryPointsCount"];
                                        if (recoveryPointsCountValue != null && recoveryPointsCountValue.Type != JTokenType.Null)
                                        {
                                            int recoveryPointsCountInstance = ((int)recoveryPointsCountValue);
                                            propertiesInstance.RecoveryPointsCount = recoveryPointsCountInstance;
                                        }

                                        JToken lastRecoveryPointValue = propertiesValue["lastRecoveryPoint"];
                                        if (lastRecoveryPointValue != null && lastRecoveryPointValue.Type != JTokenType.Null)
                                        {
                                            DateTime lastRecoveryPointInstance = ((DateTime)lastRecoveryPointValue);
                                            propertiesInstance.LastRecoveryPoint = lastRecoveryPointInstance;
                                        }

                                        JToken lastBackupTimeValue = propertiesValue["lastBackupTime"];
                                        if (lastBackupTimeValue != null && lastBackupTimeValue.Type != JTokenType.Null)
                                        {
                                            DateTime lastBackupTimeInstance = ((DateTime)lastBackupTimeValue);
                                            propertiesInstance.LastBackupTime = lastBackupTimeInstance;
                                        }

                                        JToken lastBackupStatusValue = propertiesValue["lastBackupStatus"];
                                        if (lastBackupStatusValue != null && lastBackupStatusValue.Type != JTokenType.Null)
                                        {
                                            string lastBackupStatusInstance = ((string)lastBackupStatusValue);
                                            propertiesInstance.LastBackupStatus = lastBackupStatusInstance;
                                        }

                                        JToken lastBackupJobIdValue = propertiesValue["lastBackupJobId"];
                                        if (lastBackupJobIdValue != null && lastBackupJobIdValue.Type != JTokenType.Null)
                                        {
                                            string lastBackupJobIdInstance = ((string)lastBackupJobIdValue);
                                            propertiesInstance.LastBackupJobId = lastBackupJobIdInstance;
                                        }

                                        JToken friendlyNameValue = propertiesValue["friendlyName"];
                                        if (friendlyNameValue != null && friendlyNameValue.Type != JTokenType.Null)
                                        {
                                            string friendlyNameInstance = ((string)friendlyNameValue);
                                            propertiesInstance.FriendlyName = friendlyNameInstance;
                                        }

                                        JToken itemTypeValue = propertiesValue["itemType"];
                                        if (itemTypeValue != null && itemTypeValue.Type != JTokenType.Null)
                                        {
                                            string itemTypeInstance = ((string)itemTypeValue);
                                            propertiesInstance.ItemType = itemTypeInstance;
                                        }

                                        JToken statusValue = propertiesValue["status"];
                                        if (statusValue != null && statusValue.Type != JTokenType.Null)
                                        {
                                            string statusInstance = ((string)statusValue);
                                            propertiesInstance.Status = statusInstance;
                                        }

                                        JToken containerIdValue = propertiesValue["containerId"];
                                        if (containerIdValue != null && containerIdValue.Type != JTokenType.Null)
                                        {
                                            string containerIdInstance = ((string)containerIdValue);
                                            propertiesInstance.ContainerId = containerIdInstance;
                                        }
                                    }

                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        cSMProtectedItemResponseInstance.Id = idInstance;
                                    }

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        cSMProtectedItemResponseInstance.Name = nameInstance;
                                    }

                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        cSMProtectedItemResponseInstance.Type = typeInstance;
                                    }
                                }
                            }

                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                cSMProtectedItemListResponseInstance.NextLink = nextLinkInstance;
                            }

                            JToken idValue2 = responseDoc["id"];
                            if (idValue2 != null && idValue2.Type != JTokenType.Null)
                            {
                                string idInstance2 = ((string)idValue2);
                                cSMProtectedItemListResponseInstance.Id = idInstance2;
                            }

                            JToken nameValue2 = responseDoc["name"];
                            if (nameValue2 != null && nameValue2.Type != JTokenType.Null)
                            {
                                string nameInstance2 = ((string)nameValue2);
                                cSMProtectedItemListResponseInstance.Name = nameInstance2;
                            }

                            JToken typeValue2 = responseDoc["type"];
                            if (typeValue2 != null && typeValue2.Type != JTokenType.Null)
                            {
                                string typeInstance2 = ((string)typeValue2);
                                cSMProtectedItemListResponseInstance.Type = typeInstance2;
                            }
                        }
                    }
                    result.StatusCode = statusCode;

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
 public AzureRMBackupItemContextObject(CSMProtectedItemResponse item, AzureRMBackupContainer azureBackupContainer)
     : base(azureBackupContainer)
 {
     ItemName = item.Name;
 }