void EnableContainerReregistrationSetsReregisterFlag()
        {
            using (UndoContext undoContext = UndoContext.Current)
            {
                undoContext.Start();

                BackupVaultServicesManagementClient client = GetServiceClient <BackupVaultServicesManagementClient>();
                string containerId  = ConfigurationManager.AppSettings["ContainerId"];
                string friendlyName = ConfigurationManager.AppSettings["ContainerFriendlyName"];

                EnableReregistrationRequest request = new EnableReregistrationRequest()
                {
                    ContainerReregistrationState = new ContainerReregistrationState()
                    {
                        EnableReregistration = true,
                    },
                };

                OperationResponse response = client.Container.EnableMarsContainerReregistration(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, containerId, request, GetCustomRequestHeaders());
                // Response Validation
                Assert.NotNull(response);
                Assert.True(response.StatusCode == HttpStatusCode.NoContent, "Status code should be NoContent");

                // Basic Validation
                ListMarsContainerOperationResponse getResponse = client.Container.ListMarsContainersByTypeAndFriendlyName(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, MarsContainerType.Machine, friendlyName, GetCustomRequestHeaders());
                Assert.True(getResponse.ListMarsContainerResponse.Value.Any(marsContainer =>
                {
                    return(marsContainer.ContainerType == MarsContainerType.Machine.ToString() &&
                           marsContainer.Properties != null &&
                           string.Equals(marsContainer.Properties.FriendlyName, friendlyName, StringComparison.OrdinalIgnoreCase) &&
                           marsContainer.Properties.CanReRegister == true);
                }), "Reregistration doesn't appear to have been enabled for the input container");
            }
        }
        void UnregisterContainerDeletesContainer()
        {
            using (UndoContext undoContext = UndoContext.Current)
            {
                undoContext.Start();

                BackupVaultServicesManagementClient client = GetServiceClient <BackupVaultServicesManagementClient>();
                string containerId  = ConfigurationManager.AppSettings["ContainerId"];
                string friendlyName = ConfigurationManager.AppSettings["ContainerFriendlyName"];

                OperationResponse response = client.Container.UnregisterMarsContainer(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, containerId, GetCustomRequestHeaders());
                // Response Validation
                Assert.NotNull(response);
                Assert.True(response.StatusCode == HttpStatusCode.NoContent, "Status code should be NoContent");

                bool containerDeleted = false;
                try
                {
                    ListMarsContainerOperationResponse getResponse = client.Container.ListMarsContainersByTypeAndFriendlyName(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, MarsContainerType.Machine, friendlyName, GetCustomRequestHeaders());
                    if (getResponse.ListMarsContainerResponse.Value.Count == 0)
                    {
                        containerDeleted = true;
                    }
                }
                catch (Exception ex)
                {
                    if (ex.GetType() == typeof(Hyak.Common.CloudException))
                    {
                        Hyak.Common.CloudException cloudEx = ex as Hyak.Common.CloudException;
                        if (cloudEx != null &&
                            cloudEx.Error != null &&
                            !string.IsNullOrEmpty(cloudEx.Error.Code) &&
                            cloudEx.Error.Code == "ResourceNotFound")
                        {
                            containerDeleted = true;
                        }
                    }
                }

                Assert.True(containerDeleted, "Container still exists after unregistration");
            }
        }
        void ListMarsContainersByTypeReturnsNonZeroContainers()
        {
            using (UndoContext undoContext = UndoContext.Current)
            {
                undoContext.Start();

                BackupVaultServicesManagementClient client = GetServiceClient <BackupVaultServicesManagementClient>();

                string subscriptionId    = ConfigurationManager.AppSettings["SubscriptionId"];
                string resourceName      = ConfigurationManager.AppSettings["ResourceName"];
                string resourceId        = ConfigurationManager.AppSettings["ResourceId"];
                string containerId       = ConfigurationManager.AppSettings["ContainerId"];
                string containerStampId  = ConfigurationManager.AppSettings["ContainerStampId"];
                string containerStampUri = ConfigurationManager.AppSettings["ContainerStampUri"];
                string friendlyName      = ConfigurationManager.AppSettings["ContainerFriendlyName"];
                string uniqueName        = ConfigurationManager.AppSettings["ContainerUniqueName"];

                ListMarsContainerOperationResponse response = client.Container.ListMarsContainersByType(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, MarsContainerType.Machine, GetCustomRequestHeaders());

                // Response Validation
                Assert.NotNull(response);
                Assert.True(response.StatusCode == HttpStatusCode.OK, "Status code should be OK");
                Assert.NotNull(response.ListMarsContainerResponse);

                // Basic Validation
                Assert.True(response.ListMarsContainerResponse.Value.Any(marsContainer =>
                {
                    return(marsContainer.ContainerType == MarsContainerType.Machine.ToString() &&
                           marsContainer.Properties != null &&
                           marsContainer.Properties.ContainerId.ToString() == containerId &&
                           marsContainer.Properties.ContainerStampId.ToString() == containerStampId &&
                           marsContainer.Properties.ContainerStampUri == containerStampUri &&
                           marsContainer.Properties.CustomerType == CustomerType.OBS.ToString() &&
                           string.Equals(marsContainer.Properties.FriendlyName, friendlyName, StringComparison.OrdinalIgnoreCase) &&
                           string.Equals(marsContainer.UniqueName, uniqueName, StringComparison.OrdinalIgnoreCase));
                }), "Obtained container list doesn't contain the input container");
            }
        }
示例#4
0
        /// <summary>
        /// Get the list of all container based on the given query filter
        /// string.
        /// </summary>
        /// <param name='containerType'>
        /// Required. Type of container.
        /// </param>
        /// <param name='friendlyName'>
        /// Required. Friendly name of container.
        /// </param>
        /// <param name='customRequestHeaders'>
        /// Optional. Request header parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List of Microsoft Azure Recovery Services (MARS) containers.
        /// </returns>
        public async Task <ListMarsContainerOperationResponse> ListMarsContainersByTypeAndFriendlyNameAsync(MarsContainerType containerType, string friendlyName, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken)
        {
            // Validate
            if (friendlyName == null)
            {
                throw new ArgumentNullException("friendlyName");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("containerType", containerType);
                tracingParameters.Add("friendlyName", friendlyName);
                tracingParameters.Add("customRequestHeaders", customRequestHeaders);
                TracingAdapter.Enter(invocationId, this, "ListMarsContainersByTypeAndFriendlyNameAsync", 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(this.Client.ResourceGroupName);
            url = url + "/providers/";
            url = url + "Microsoft.Backup";
            url = url + "/";
            url = url + "BackupVault";
            url = url + "/";
            url = url + Uri.EscapeDataString(this.Client.ResourceName);
            url = url + "/backupContainers";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-03-15");
            List <string> odataFilter = new List <string>();

            odataFilter.Add("type eq '" + Uri.EscapeDataString(containerType.ToString()) + "'");
            odataFilter.Add("friendlyName eq '" + Uri.EscapeDataString(friendlyName) + "'");
            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");

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

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

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

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

                                    JToken uniqueNameValue = valueValue["uniqueName"];
                                    if (uniqueNameValue != null && uniqueNameValue.Type != JTokenType.Null)
                                    {
                                        string uniqueNameInstance = ((string)uniqueNameValue);
                                        marsContainerResponseInstance.UniqueName = uniqueNameInstance;
                                    }

                                    JToken containerTypeValue = valueValue["containerType"];
                                    if (containerTypeValue != null && containerTypeValue.Type != JTokenType.Null)
                                    {
                                        string containerTypeInstance = ((string)containerTypeValue);
                                        marsContainerResponseInstance.ContainerType = containerTypeInstance;
                                    }

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

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

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

                                        JToken containerStampIdValue = propertiesValue["containerStampId"];
                                        if (containerStampIdValue != null && containerStampIdValue.Type != JTokenType.Null)
                                        {
                                            Guid containerStampIdInstance = Guid.Parse(((string)containerStampIdValue));
                                            propertiesInstance.ContainerStampId = containerStampIdInstance;
                                        }

                                        JToken containerStampUriValue = propertiesValue["containerStampUri"];
                                        if (containerStampUriValue != null && containerStampUriValue.Type != JTokenType.Null)
                                        {
                                            string containerStampUriInstance = ((string)containerStampUriValue);
                                            propertiesInstance.ContainerStampUri = containerStampUriInstance;
                                        }

                                        JToken canReRegisterValue = propertiesValue["canReRegister"];
                                        if (canReRegisterValue != null && canReRegisterValue.Type != JTokenType.Null)
                                        {
                                            bool canReRegisterInstance = ((bool)canReRegisterValue);
                                            propertiesInstance.CanReRegister = canReRegisterInstance;
                                        }

                                        JToken customerTypeValue = propertiesValue["customerType"];
                                        if (customerTypeValue != null && customerTypeValue.Type != JTokenType.Null)
                                        {
                                            string customerTypeInstance = ((string)customerTypeValue);
                                            propertiesInstance.CustomerType = customerTypeInstance;
                                        }
                                    }

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

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

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

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

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

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

                            JToken typeValue2 = responseDoc["type"];
                            if (typeValue2 != null && typeValue2.Type != JTokenType.Null)
                            {
                                string typeInstance2 = ((string)typeValue2);
                                listMarsContainerResponseInstance.Type = typeInstance2;
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-client-request-id"))
                    {
                        customRequestHeaders.ClientRequestId = httpResponse.Headers.GetValues("x-ms-client-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }