/src/ServiceManagement/Profile/Commands.Profile/Models/PSAzureEnvironment.cs

https://gitlab.com/jslee1/azure-powershell · C# · 270 lines · 159 code · 24 blank · 87 comment · 47 complexity · d3d4bff44f3cda13d6f089eb33d09208 MD5 · raw file

  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using Microsoft.Azure.Commands.Common.Authentication.Models;
  15. namespace Microsoft.WindowsAzure.Commands.Profile.Models
  16. {
  17. /// <summary>
  18. /// Settings and endpoints for management of Azure or Azure Stack services.
  19. /// </summary>
  20. public class PSAzureEnvironment
  21. {
  22. /// <summary>
  23. /// Convert the PowerShell representation of environment to the internal representation.
  24. /// </summary>
  25. /// <param name="environment">The PowerShell environment to convert.</param>
  26. /// <returns>The internal representation of the Azure environment, as used by .Net authentication libraries.</returns>
  27. public static implicit operator AzureEnvironment(PSAzureEnvironment environment)
  28. {
  29. var newEnvironment = new AzureEnvironment
  30. {
  31. Name = environment.Name,
  32. OnPremise = environment.EnableAdfsAuthentication
  33. };
  34. newEnvironment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId] =
  35. environment.ActiveDirectoryServiceEndpointResourceId;
  36. newEnvironment.Endpoints[AzureEnvironment.Endpoint.AdTenant] = environment.AdTenant;
  37. newEnvironment.Endpoints[AzureEnvironment.Endpoint.Gallery] = environment.GalleryUrl;
  38. newEnvironment.Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl] = environment.ManagementPortalUrl;
  39. newEnvironment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = environment.ServiceManagementUrl;
  40. newEnvironment.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl] =
  41. environment.PublishSettingsFileUrl;
  42. newEnvironment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = environment.ResourceManagerUrl;
  43. newEnvironment.Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix] = environment.SqlDatabaseDnsSuffix;
  44. newEnvironment.Endpoints[AzureEnvironment.Endpoint.StorageEndpointSuffix] =
  45. environment.StorageEndpointSuffix;
  46. newEnvironment.Endpoints[AzureEnvironment.Endpoint.Graph] = environment.GraphUrl;
  47. newEnvironment.Endpoints[AzureEnvironment.Endpoint.TrafficManagerDnsSuffix] =
  48. environment.TrafficManagerDnsSuffix;
  49. newEnvironment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix] =
  50. environment.AzureKeyVaultDnsSuffix;
  51. newEnvironment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId] =
  52. environment.AzureKeyVaultServiceEndpointResourceId;
  53. return newEnvironment;
  54. }
  55. /// <summary>
  56. /// Convert the internal representation of Azure libraries to a representation that is more readable for PowerShell.
  57. /// </summary>
  58. /// <param name="environment">The internal representation fo the Azure environment.</param>
  59. /// <returns>The PowerShell;-friendly representation of the environment.</returns>
  60. public static implicit operator PSAzureEnvironment(AzureEnvironment environment)
  61. {
  62. return new PSAzureEnvironment(environment);
  63. }
  64. /// <summary>
  65. /// Initializes a new azure environment.
  66. /// </summary>
  67. public PSAzureEnvironment()
  68. {
  69. }
  70. /// <summary>
  71. /// Initializes a new Azure environment from the given internal representation.
  72. /// </summary>
  73. /// <param name="environment">The internal representation of the environment.</param>
  74. public PSAzureEnvironment(AzureEnvironment environment)
  75. {
  76. Name = environment.Name;
  77. EnableAdfsAuthentication = environment.OnPremise;
  78. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId))
  79. {
  80. ActiveDirectoryServiceEndpointResourceId =
  81. environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId];
  82. }
  83. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AdTenant))
  84. {
  85. AdTenant = environment.Endpoints[AzureEnvironment.Endpoint.AdTenant];
  86. }
  87. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.Gallery))
  88. {
  89. GalleryUrl =
  90. environment.Endpoints[AzureEnvironment.Endpoint.Gallery];
  91. }
  92. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ManagementPortalUrl))
  93. {
  94. ManagementPortalUrl =
  95. environment.Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl];
  96. }
  97. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ServiceManagement))
  98. {
  99. ServiceManagementUrl =
  100. environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement];
  101. }
  102. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.PublishSettingsFileUrl))
  103. {
  104. PublishSettingsFileUrl =
  105. environment.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl];
  106. }
  107. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ResourceManager))
  108. {
  109. ResourceManagerUrl =
  110. environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager];
  111. }
  112. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix))
  113. {
  114. SqlDatabaseDnsSuffix =
  115. environment.Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix];
  116. }
  117. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.StorageEndpointSuffix))
  118. {
  119. StorageEndpointSuffix =
  120. environment.Endpoints[AzureEnvironment.Endpoint.StorageEndpointSuffix];
  121. }
  122. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ActiveDirectory))
  123. {
  124. ActiveDirectoryAuthority =
  125. environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectory];
  126. }
  127. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.Graph))
  128. {
  129. GraphUrl =
  130. environment.Endpoints[AzureEnvironment.Endpoint.Graph];
  131. }
  132. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.TrafficManagerDnsSuffix))
  133. {
  134. TrafficManagerDnsSuffix =
  135. environment.Endpoints[AzureEnvironment.Endpoint.TrafficManagerDnsSuffix];
  136. }
  137. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix))
  138. {
  139. AzureKeyVaultDnsSuffix =
  140. environment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix];
  141. }
  142. if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId))
  143. {
  144. AzureKeyVaultServiceEndpointResourceId =
  145. environment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId];
  146. }
  147. }
  148. /// <summary>
  149. /// Gets or sets the name of the environment.
  150. /// </summary>
  151. public string Name { get; set; }
  152. /// <summary>
  153. /// Gets or sets a value indicating whther ADFS authentication should be allowed .
  154. /// Generally, this is only used in Azure Stack environments.
  155. /// </summary>
  156. public bool EnableAdfsAuthentication { get; set; }
  157. /// <summary>
  158. /// Gets or sets the expected token audience for authenticating management requests.
  159. /// </summary>
  160. public string ActiveDirectoryServiceEndpointResourceId { get; set; }
  161. /// <summary>
  162. /// Gets or sets the default tenant Id.
  163. /// </summary>
  164. public string AdTenant { get; set; }
  165. /// <summary>
  166. /// Gets or sets the Uri of the Template Gallery service.
  167. /// </summary>
  168. public string GalleryUrl { get; set; }
  169. /// <summary>
  170. /// Gets or sets the Uri of the management portal.
  171. /// </summary>
  172. public string ManagementPortalUrl { get; set; }
  173. /// <summary>
  174. /// Get or sets the Uri of the service management (RDFE) service.
  175. /// </summary>
  176. public string ServiceManagementUrl { get; set; }
  177. /// <summary>
  178. /// Gets or sets the endpoint of the publish settings download service.
  179. /// </summary>
  180. public string PublishSettingsFileUrl { get; set; }
  181. /// <summary>
  182. /// Gets or sets the Uri of the Azure Resource Manager (ARM) service.
  183. /// </summary>
  184. public string ResourceManagerUrl { get; set; }
  185. /// <summary>
  186. /// Gets or sets the Dns suffix used for Sql database servers.
  187. /// </summary>
  188. public string SqlDatabaseDnsSuffix { get; set; }
  189. /// <summary>
  190. /// Gets or sets the dns suffix of storage services.
  191. /// </summary>
  192. public string StorageEndpointSuffix { get; set; }
  193. /// <summary>
  194. /// Gets or sets the Uri of the Active Directory authentication endpoint.
  195. /// </summary>
  196. public string ActiveDirectoryAuthority { get; set; }
  197. /// <summary>
  198. /// Gets or sets the Uri of the Active Directory metadata (Graph) endpoint.
  199. /// </summary>
  200. public string GraphUrl { get; set; }
  201. /// <summary>
  202. /// Gets or sets the domain name suffix for traffig manager services.
  203. /// </summary>
  204. public string TrafficManagerDnsSuffix { get; set; }
  205. /// <summary>
  206. /// Gets or sets the domain name suffix for key vault services.
  207. /// </summary>
  208. public string AzureKeyVaultDnsSuffix { get; set; }
  209. /// <summary>
  210. /// Gets or sets the expected token audience for authenticating requests to the key vault service.
  211. /// </summary>
  212. public string AzureKeyVaultServiceEndpointResourceId { get; set; }
  213. /// <summary>
  214. /// Determine equality of two PSAzureEnvironment instances.
  215. /// </summary>
  216. /// <param name="obj">The instance to compare.</param>
  217. /// <returns>True if the instances are equivalent, false otherwise.</returns>
  218. public override bool Equals(object obj)
  219. {
  220. var other = obj as PSAzureEnvironment;
  221. if (other != null)
  222. {
  223. return Name == other.Name && EnableAdfsAuthentication == other.EnableAdfsAuthentication
  224. && ActiveDirectoryAuthority == other.ActiveDirectoryAuthority
  225. && ActiveDirectoryServiceEndpointResourceId == other.ActiveDirectoryServiceEndpointResourceId
  226. && AdTenant == other.AdTenant
  227. && AzureKeyVaultDnsSuffix == other.AzureKeyVaultDnsSuffix
  228. && AzureKeyVaultServiceEndpointResourceId == other.AzureKeyVaultServiceEndpointResourceId
  229. && GalleryUrl == other.GalleryUrl
  230. && GraphUrl == other.GraphUrl
  231. && ManagementPortalUrl == other.ManagementPortalUrl
  232. && PublishSettingsFileUrl == other.PublishSettingsFileUrl
  233. && ResourceManagerUrl == other.ResourceManagerUrl
  234. && ServiceManagementUrl == other.ServiceManagementUrl
  235. && StorageEndpointSuffix == other.StorageEndpointSuffix
  236. && SqlDatabaseDnsSuffix == other.SqlDatabaseDnsSuffix
  237. && TrafficManagerDnsSuffix == other.TrafficManagerDnsSuffix;
  238. }
  239. return false;
  240. }
  241. public override int GetHashCode()
  242. {
  243. return base.GetHashCode();
  244. }
  245. }
  246. }