/src/ResourceManager/ApiManagement/Commands.ApiManagement/Models/PsApiManagement.cs

https://gitlab.com/jslee1/azure-powershell · C# · 246 lines · 192 code · 41 blank · 13 comment · 39 complexity · b99a7e7afb2bcbaad52b119795c485c8 MD5 · raw file

  1. //
  2. // Copyright (c) Microsoft. All rights reserved.
  3. //
  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. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. namespace Microsoft.Azure.Commands.ApiManagement.Models
  15. {
  16. using AutoMapper;
  17. using Microsoft.Azure.Commands.ApiManagement.Properties;
  18. using Microsoft.Azure.Management.ApiManagement.Models;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text.RegularExpressions;
  23. public class PsApiManagement
  24. {
  25. private static readonly Regex ResourceGroupRegex =
  26. new Regex(@"/resourceGroups/(?<resourceGroupName>.+)/providers/", RegexOptions.Compiled);
  27. public PsApiManagement()
  28. {
  29. Tags = new Dictionary<string, string>();
  30. AdditionalRegions = new List<PsApiManagementRegion>();
  31. }
  32. public PsApiManagement(ApiServiceResource apiServiceResource)
  33. : this()
  34. {
  35. if (apiServiceResource == null)
  36. {
  37. throw new ArgumentNullException("apiServiceResource");
  38. }
  39. Id = apiServiceResource.Id;
  40. Name = apiServiceResource.Name;
  41. Location = apiServiceResource.Location;
  42. Sku = Mapper.Map<SkuType, PsApiManagementSku>(apiServiceResource.Properties.SkuProperties.SkuType);
  43. Capacity = apiServiceResource.Properties.SkuProperties.Capacity ?? 1;
  44. ProvisioningState = apiServiceResource.Properties.ProvisioningState;
  45. RuntimeUrl = apiServiceResource.Properties.ProxyEndpoint;
  46. PortalUrl = apiServiceResource.Properties.ManagementPortalEndpoint;
  47. StaticIPs = apiServiceResource.Properties.StaticIPs.ToArray();
  48. if (apiServiceResource.Properties.AdditionalRegions != null)
  49. {
  50. AdditionalRegions =
  51. apiServiceResource.Properties.AdditionalRegions
  52. .Select(region => new PsApiManagementRegion(region))
  53. .ToList();
  54. }
  55. if (apiServiceResource.Properties.VirtualNetworkConfiguration != null)
  56. {
  57. VirtualNetwork = new PsApiManagementVirtualNetwork(apiServiceResource.Properties.VirtualNetworkConfiguration);
  58. }
  59. if (apiServiceResource.Properties.HostnameConfigurations != null)
  60. {
  61. var portalHostnameResource = apiServiceResource.Properties.HostnameConfigurations.FirstOrDefault(conf => conf.Type == HostnameType.Portal);
  62. if (portalHostnameResource != null)
  63. {
  64. PortalHostnameConfiguration = new PsApiManagementHostnameConfiguration(portalHostnameResource);
  65. }
  66. var proxyHostnameResource = apiServiceResource.Properties.HostnameConfigurations.FirstOrDefault(conf => conf.Type == HostnameType.Proxy);
  67. if (proxyHostnameResource != null)
  68. {
  69. ProxyHostnameConfiguration = new PsApiManagementHostnameConfiguration(proxyHostnameResource);
  70. }
  71. }
  72. if (apiServiceResource.Tags != null)
  73. {
  74. Tags = apiServiceResource.Tags;
  75. }
  76. }
  77. public string[] StaticIPs { get; private set; }
  78. public string Id { get; private set; }
  79. public string Name { get; private set; }
  80. public string Location { get; private set; }
  81. public PsApiManagementSku Sku { get; set; }
  82. public int Capacity { get; set; }
  83. public string ProvisioningState { get; private set; }
  84. public string RuntimeUrl { get; private set; }
  85. public string PortalUrl { get; private set; }
  86. public PsApiManagementVirtualNetwork VirtualNetwork { get; set; }
  87. public PsApiManagementHostnameConfiguration PortalHostnameConfiguration { get; set; }
  88. public PsApiManagementHostnameConfiguration ProxyHostnameConfiguration { get; set; }
  89. public IDictionary<string, string> Tags { get; set; }
  90. public IList<PsApiManagementRegion> AdditionalRegions { get; private set; }
  91. public string ResourceGroupName
  92. {
  93. get
  94. {
  95. if (string.IsNullOrWhiteSpace(Id))
  96. {
  97. return null;
  98. }
  99. var match = ResourceGroupRegex.Match(Id);
  100. if (match.Success)
  101. {
  102. var resourceGroupNameGroup = match.Groups["resourceGroupName"];
  103. if (resourceGroupNameGroup != null && resourceGroupNameGroup.Success)
  104. {
  105. return resourceGroupNameGroup.Value;
  106. }
  107. }
  108. return null;
  109. }
  110. }
  111. public PsApiManagementRegion AddRegion(
  112. string location,
  113. PsApiManagementSku sku = PsApiManagementSku.Developer,
  114. int capacity = 1,
  115. PsApiManagementVirtualNetwork virtualNetwork = null)
  116. {
  117. if (location == null)
  118. {
  119. throw new ArgumentNullException("location");
  120. }
  121. if (!CommonConstants.ValidLocationsSet.Contains(location))
  122. {
  123. throw new ArgumentException(
  124. string.Format(
  125. Resources.InvalidLocation,
  126. location,
  127. string.Join(",", CommonConstants.ValidLocationsSet)
  128. ),
  129. "location");
  130. }
  131. if (location.Equals(Location) || AdditionalRegions.Any(r => location.Equals(r.Location)))
  132. {
  133. throw new ArgumentException(string.Format(Resources.AddRegionExistsMessage, location), "location");
  134. }
  135. var newRegion = new PsApiManagementRegion
  136. {
  137. Location = location,
  138. Sku = sku,
  139. Capacity = capacity,
  140. VirtualNetwork = virtualNetwork
  141. };
  142. AdditionalRegions.Add(newRegion);
  143. return newRegion;
  144. }
  145. public bool RemoveRegion(string location)
  146. {
  147. if (location == null)
  148. {
  149. throw new ArgumentNullException("location");
  150. }
  151. if (!CommonConstants.ValidLocationsSet.Contains(location))
  152. {
  153. throw new ArgumentException(
  154. string.Format(
  155. Resources.InvalidLocation,
  156. location,
  157. string.Join(",", CommonConstants.ValidLocationsSet)
  158. ),
  159. "location");
  160. }
  161. if (location.Equals(Location))
  162. {
  163. throw new ArgumentException(
  164. string.Format(Resources.RemoveRegionCannotRemoveMasterRegion, location),
  165. "location");
  166. }
  167. var regionToRemove = AdditionalRegions.FirstOrDefault(r => location.Equals(r.Location));
  168. return regionToRemove != null && AdditionalRegions.Remove(regionToRemove);
  169. }
  170. public void UpdateRegion(string location, PsApiManagementSku sku, int capacity, PsApiManagementVirtualNetwork virtualNetwork)
  171. {
  172. if (location == null)
  173. {
  174. throw new ArgumentNullException("location");
  175. }
  176. if (!CommonConstants.ValidLocationsSet.Contains(location))
  177. {
  178. throw new ArgumentException(
  179. string.Format(
  180. Resources.InvalidLocation,
  181. location,
  182. string.Join(",", CommonConstants.ValidLocationsSet)
  183. ),
  184. "location");
  185. }
  186. var regionToUpdate = AdditionalRegions.FirstOrDefault(r => location.Equals(r.Location));
  187. if (regionToUpdate != null)
  188. {
  189. regionToUpdate.Sku = sku;
  190. regionToUpdate.Capacity = capacity;
  191. regionToUpdate.VirtualNetwork = virtualNetwork;
  192. }
  193. else if (location.Equals(Location))
  194. {
  195. Sku = sku;
  196. Capacity = capacity;
  197. VirtualNetwork = virtualNetwork;
  198. }
  199. else
  200. {
  201. throw new ArgumentException(string.Format(Resources.UpdateRegionDoesNotExistsMessage, location), "location");
  202. }
  203. }
  204. }
  205. }