|
| 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 | + |
| 15 | +using System; |
| 16 | +using Microsoft.Azure.Commands.Network.Models; |
| 17 | +using MNM = Microsoft.Azure.Management.Network.Models; |
| 18 | + |
| 19 | +namespace Microsoft.Azure.Commands.Network |
| 20 | +{ |
| 21 | + public static class ApplicationGatewayChildResourceHelper |
| 22 | + { |
| 23 | + public static string GetResourceId( |
| 24 | + string subscriptionId, |
| 25 | + string resourceGroupName, |
| 26 | + string applicationGatewayName, |
| 27 | + string resource, |
| 28 | + string resourceName) |
| 29 | + { |
| 30 | + return string.Format( |
| 31 | + Microsoft.Azure.Commands.Network.Properties.Resources.ApplicationGatewayChildResourceId, |
| 32 | + subscriptionId, |
| 33 | + resourceGroupName, |
| 34 | + applicationGatewayName, |
| 35 | + resource, |
| 36 | + resourceName); |
| 37 | + } |
| 38 | + |
| 39 | + public static string GetResourceNotSetId(string subscriptionId, string resource, string resourceName) |
| 40 | + { |
| 41 | + return string.Format( |
| 42 | + Microsoft.Azure.Commands.Network.Properties.Resources.ApplicationGatewayChildResourceId, |
| 43 | + subscriptionId, |
| 44 | + Microsoft.Azure.Commands.Network.Properties.Resources.ResourceGroupNotSet, |
| 45 | + Microsoft.Azure.Commands.Network.Properties.Resources.ApplicationGatewayNameNotSet, |
| 46 | + resource, |
| 47 | + resourceName); |
| 48 | + } |
| 49 | + |
| 50 | + private static string NormalizeApplicationGatewayNameChildResourceIds(string id, string resourceGroupName, string applicationGatewayName) |
| 51 | + { |
| 52 | + id = NormalizeId(id, "resourceGroups", resourceGroupName); |
| 53 | + id = NormalizeId(id, "applicationGateways", applicationGatewayName); |
| 54 | + |
| 55 | + return id; |
| 56 | + } |
| 57 | + |
| 58 | + private static string NormalizeId(string id, string resourceName, string resourceValue) |
| 59 | + { |
| 60 | + int startIndex = id.IndexOf(resourceName, StringComparison.OrdinalIgnoreCase) + resourceName.Length + 1; |
| 61 | + int endIndex = id.IndexOf("/", startIndex, StringComparison.OrdinalIgnoreCase); |
| 62 | + |
| 63 | + // Replace the following string '/{value}/' |
| 64 | + startIndex--; |
| 65 | + string orignalString = id.Substring(startIndex, endIndex - startIndex + 1); |
| 66 | + |
| 67 | + return id.Replace(orignalString, string.Format("/{0}/", resourceValue)); |
| 68 | + } |
| 69 | + |
| 70 | + public static void NormalizeChildResourcesId(PSApplicationGateway applicationGateway) |
| 71 | + { |
| 72 | + // Normalize GatewayIpConfiguration |
| 73 | + if (applicationGateway.GatewayIPConfigurations != null) |
| 74 | + { |
| 75 | + foreach (var gatewayIpConfig in applicationGateway.GatewayIPConfigurations) |
| 76 | + { |
| 77 | + gatewayIpConfig.Id = string.Empty; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Normalize SslCertificates |
| 82 | + if (applicationGateway.SslCertificates != null) |
| 83 | + { |
| 84 | + foreach (var sslCertificate in applicationGateway.SslCertificates) |
| 85 | + { |
| 86 | + sslCertificate.Id = string.Empty; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Normalize FrontendIpConfiguration |
| 91 | + if (applicationGateway.FrontendIPConfigurations != null) |
| 92 | + { |
| 93 | + foreach (var frontendIpConfiguration in applicationGateway.FrontendIPConfigurations) |
| 94 | + { |
| 95 | + frontendIpConfiguration.Id = string.Empty; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Normalize FrontendPort |
| 100 | + if (applicationGateway.FrontendPorts != null) |
| 101 | + { |
| 102 | + foreach (var frontendPort in applicationGateway.FrontendPorts) |
| 103 | + { |
| 104 | + frontendPort.Id = string.Empty; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Normalize BackendAddressPool |
| 109 | + if (applicationGateway.BackendAddressPools != null) |
| 110 | + { |
| 111 | + foreach (var backendAddressPool in applicationGateway.BackendAddressPools) |
| 112 | + { |
| 113 | + backendAddressPool.Id = string.Empty; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Normalize Probe |
| 118 | + if (applicationGateway.Probes != null) |
| 119 | + { |
| 120 | + foreach (var probe in applicationGateway.Probes) |
| 121 | + { |
| 122 | + probe.Id = string.Empty; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Normalize BackendHttpSettings |
| 127 | + if (applicationGateway.BackendHttpSettingsCollection != null) |
| 128 | + { |
| 129 | + foreach (var backendHttpSettings in applicationGateway.BackendHttpSettingsCollection) |
| 130 | + { |
| 131 | + backendHttpSettings.Id = string.Empty; |
| 132 | + |
| 133 | + if (null != backendHttpSettings.Probe) |
| 134 | + { |
| 135 | + backendHttpSettings.Probe.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 136 | + backendHttpSettings.Probe.Id, |
| 137 | + applicationGateway.ResourceGroupName, |
| 138 | + applicationGateway.Name); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Normalize HttpListener |
| 144 | + if (applicationGateway.HttpListeners != null) |
| 145 | + { |
| 146 | + foreach (var httpListener in applicationGateway.HttpListeners) |
| 147 | + { |
| 148 | + httpListener.Id = string.Empty; |
| 149 | + |
| 150 | + httpListener.FrontendPort.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 151 | + httpListener.FrontendPort.Id, |
| 152 | + applicationGateway.ResourceGroupName, |
| 153 | + applicationGateway.Name); |
| 154 | + |
| 155 | + if (null != httpListener.FrontendIpConfiguration) |
| 156 | + { |
| 157 | + httpListener.FrontendIpConfiguration.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 158 | + httpListener.FrontendIpConfiguration.Id, |
| 159 | + applicationGateway.ResourceGroupName, |
| 160 | + applicationGateway.Name); |
| 161 | + } |
| 162 | + |
| 163 | + if (null != httpListener.SslCertificate) |
| 164 | + { |
| 165 | + httpListener.SslCertificate.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 166 | + httpListener.SslCertificate.Id, |
| 167 | + applicationGateway.ResourceGroupName, |
| 168 | + applicationGateway.Name); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Normalize UrlPathMap |
| 174 | + if (applicationGateway.UrlPathMaps != null) |
| 175 | + { |
| 176 | + foreach (var urlPathMap in applicationGateway.UrlPathMaps) |
| 177 | + { |
| 178 | + urlPathMap.Id = string.Empty; |
| 179 | + |
| 180 | + urlPathMap.DefaultBackendAddressPool.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 181 | + urlPathMap.DefaultBackendAddressPool.Id, |
| 182 | + applicationGateway.ResourceGroupName, |
| 183 | + applicationGateway.Name); |
| 184 | + |
| 185 | + urlPathMap.DefaultBackendHttpSettings.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 186 | + urlPathMap.DefaultBackendHttpSettings.Id, |
| 187 | + applicationGateway.ResourceGroupName, |
| 188 | + applicationGateway.Name); |
| 189 | + |
| 190 | + foreach (var pathRule in urlPathMap.PathRules) |
| 191 | + { |
| 192 | + pathRule.BackendAddressPool.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 193 | + pathRule.BackendAddressPool.Id, |
| 194 | + applicationGateway.ResourceGroupName, |
| 195 | + applicationGateway.Name); |
| 196 | + |
| 197 | + pathRule.BackendHttpSettings.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 198 | + pathRule.BackendHttpSettings.Id, |
| 199 | + applicationGateway.ResourceGroupName, |
| 200 | + applicationGateway.Name); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // Normalize RequestRoutingRule |
| 206 | + if (applicationGateway.RequestRoutingRules != null) |
| 207 | + { |
| 208 | + foreach (var requestRoutingRule in applicationGateway.RequestRoutingRules) |
| 209 | + { |
| 210 | + requestRoutingRule.Id = string.Empty; |
| 211 | + |
| 212 | + requestRoutingRule.HttpListener.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 213 | + requestRoutingRule.HttpListener.Id, |
| 214 | + applicationGateway.ResourceGroupName, |
| 215 | + applicationGateway.Name); |
| 216 | + |
| 217 | + if (null != requestRoutingRule.BackendAddressPool) |
| 218 | + { |
| 219 | + requestRoutingRule.BackendAddressPool.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 220 | + requestRoutingRule.BackendAddressPool.Id, |
| 221 | + applicationGateway.ResourceGroupName, |
| 222 | + applicationGateway.Name); |
| 223 | + } |
| 224 | + |
| 225 | + if (null != requestRoutingRule.BackendHttpSettings) |
| 226 | + { |
| 227 | + requestRoutingRule.BackendHttpSettings.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 228 | + requestRoutingRule.BackendHttpSettings.Id, |
| 229 | + applicationGateway.ResourceGroupName, |
| 230 | + applicationGateway.Name); |
| 231 | + } |
| 232 | + |
| 233 | + if (null != requestRoutingRule.UrlPathMap) |
| 234 | + { |
| 235 | + requestRoutingRule.UrlPathMap.Id = NormalizeApplicationGatewayNameChildResourceIds( |
| 236 | + requestRoutingRule.UrlPathMap.Id, |
| 237 | + applicationGateway.ResourceGroupName, |
| 238 | + applicationGateway.Name); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments