| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Rest.ClientRuntime.Azure.TestFramework |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Extensions to the TestEnvironment class. |
| | 12 | | /// </summary> |
| | 13 | | public static class TestEnvironmentExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Gets the fully-qualified domain name suffix of an Azure Search service for the given test environment and se |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="environment">The test environment.</param> |
| | 19 | | /// <param name="searchServiceName">The name of the search service.</param> |
| | 20 | | /// <returns>The fully-qualified domain name of the search service in the given environment.</returns> |
| | 21 | | public static string GetSearchDnsSuffix(this TestEnvironment environment, string searchServiceName) |
| | 22 | | { |
| 1394 | 23 | | EnvironmentNames envName = LookupEnvironmentFromBaseUri(environment.BaseUri.AbsoluteUri); |
| | 24 | |
|
| | 25 | | switch (envName) |
| | 26 | | { |
| | 27 | | case EnvironmentNames.Dogfood: |
| 0 | 28 | | return "search-dogfood.windows-int.net"; |
| | 29 | |
|
| | 30 | | case EnvironmentNames.Next: |
| 0 | 31 | | return "search-next.windows-int.net"; |
| | 32 | |
|
| | 33 | | case EnvironmentNames.Current: |
| 0 | 34 | | return "search-current.windows-int.net"; |
| | 35 | |
|
| | 36 | | case EnvironmentNames.Prod: |
| | 37 | | default: |
| | 38 | | // Assume PROD if all else fails. |
| 1394 | 39 | | return "search.windows.net"; |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the base URI of an Azure Search service for the given test environment and service name. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="environment">The test environment.</param> |
| | 47 | | /// <param name="searchServiceName">The name of the search service.</param> |
| | 48 | | /// <returns>The correct base URI of the search service in the given environment.</returns> |
| | 49 | | public static Uri GetBaseSearchUri(this TestEnvironment environment, string searchServiceName) |
| | 50 | | { |
| | 51 | | const string UriFormat = "https://{0}.{1}/"; |
| 0 | 52 | | string dnsSuffix = environment.GetSearchDnsSuffix(searchServiceName); |
| 0 | 53 | | return new Uri(String.Format(UriFormat, searchServiceName, dnsSuffix)); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | private static EnvironmentNames LookupEnvironmentFromBaseUri(string resourceManagementUri) |
| | 57 | | { |
| 1394 | 58 | | Dictionary<Uri, EnvironmentNames> envEndpoints = new Dictionary<Uri, EnvironmentNames>(); |
| | 59 | |
|
| 1394 | 60 | | envEndpoints.Add( |
| 1394 | 61 | | new Uri("https://management.azure.com/"), |
| 1394 | 62 | | EnvironmentNames.Prod); |
| 1394 | 63 | | envEndpoints.Add( |
| 1394 | 64 | | new Uri("https://api-dogfood.resources.windows-int.net/"), |
| 1394 | 65 | | EnvironmentNames.Dogfood); |
| 1394 | 66 | | envEndpoints.Add( |
| 1394 | 67 | | new Uri("https://api-next.resources.windows-int.net/"), |
| 1394 | 68 | | EnvironmentNames.Next); |
| 1394 | 69 | | envEndpoints.Add( |
| 1394 | 70 | | new Uri("https://api-current.resources.windows-int.net/"), |
| 1394 | 71 | | EnvironmentNames.Current); |
| | 72 | |
|
| 4182 | 73 | | foreach (Uri testUri in envEndpoints.Keys) |
| | 74 | | { |
| 1394 | 75 | | if (MatchEnvironmentBaseUri(testUri, resourceManagementUri)) |
| | 76 | | { |
| 1394 | 77 | | return envEndpoints[testUri]; |
| | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | return EnvironmentNames.Prod; |
| 1394 | 82 | | } |
| | 83 | |
|
| | 84 | | private static bool MatchEnvironmentBaseUri(Uri testUri, string endpointValue) |
| | 85 | | { |
| 1394 | 86 | | endpointValue = EnsureTrailingSlash(endpointValue); |
| 1394 | 87 | | return string.Equals(testUri.ToString(), endpointValue, StringComparison.OrdinalIgnoreCase); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private static string EnsureTrailingSlash(string uri) |
| | 91 | | { |
| 1394 | 92 | | if (uri.EndsWith("/")) |
| | 93 | | { |
| 1394 | 94 | | return uri; |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | return string.Format("{0}/", uri); |
| | 98 | | } |
| | 99 | | } |
| | 100 | | } |