|
| 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 | +namespace Microsoft.Azure.Commands.HPCCache.Test.Fixtures |
| 16 | +{ |
| 17 | + using System; |
| 18 | + using System.Collections.Generic; |
| 19 | + using System.Linq; |
| 20 | + using Microsoft.Azure.Commands.HPCCache.Test.Utilities; |
| 21 | + using Microsoft.Azure.Management.Authorization; |
| 22 | + using Microsoft.Azure.Management.Authorization.Models; |
| 23 | + using Microsoft.Azure.Management.Internal.Resources; |
| 24 | + using Microsoft.Azure.Management.Internal.Resources.Models; |
| 25 | + using Microsoft.Azure.Management.Network; |
| 26 | + using Microsoft.Azure.Management.Network.Models; |
| 27 | + using Microsoft.Azure.Management.StorageCache; |
| 28 | + using Microsoft.Azure.Management.StorageCache.Models; |
| 29 | + using Microsoft.Rest.Azure; |
| 30 | + using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Defines the <see cref="HpcCacheTestContext" />. |
| 34 | + /// </summary> |
| 35 | + public class HpcCacheTestContext : IDisposable |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// Defines the mockContext. |
| 39 | + /// </summary> |
| 40 | + private readonly MockContext mockContext; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Defines the serviceClientCache. |
| 44 | + /// </summary> |
| 45 | + private readonly Dictionary<Type, IDisposable> serviceClientCache = new Dictionary<Type, IDisposable>(); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Defines the disposedValue. |
| 49 | + /// </summary> |
| 50 | + private bool disposedValue = false; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Initializes a new instance of the <see cref="HpcCacheTestContext"/> class. |
| 54 | + /// </summary> |
| 55 | + /// <param name="suiteObject">Class object.</param> |
| 56 | + /// <param name="methodName">Method name of the calling method.</param> |
| 57 | + public HpcCacheTestContext( |
| 58 | + string suiteObject, |
| 59 | + [System.Runtime.CompilerServices.CallerMemberName] |
| 60 | + string methodName = ".ctor") |
| 61 | + { |
| 62 | + this.mockContext = MockContext.Start(suiteObject, methodName); |
| 63 | + this.RegisterSubscriptionForResource("Microsoft.StorageCache"); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Initializes a new instance of the <see cref="HpcCacheTestContext"/> class. |
| 68 | + /// </summary> |
| 69 | + /// <param name="type">Class type.</param> |
| 70 | + /// <param name="methodName">Method name of the calling method.</param> |
| 71 | + public HpcCacheTestContext( |
| 72 | + Type type, |
| 73 | + [System.Runtime.CompilerServices.CallerMemberName] |
| 74 | + string methodName = ".ctor") |
| 75 | + { |
| 76 | + this.mockContext = MockContext.Start(type.Name, methodName); |
| 77 | + this.RegisterSubscriptionForResource("Microsoft.StorageCache"); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Get service client. |
| 82 | + /// </summary> |
| 83 | + /// <param name="delegationHandler">HTTP delegation handler.</param> |
| 84 | + /// <typeparam name="TServiceClient">The type of the service client to return.</typeparam> |
| 85 | + /// <returns>A management client, created from the current context (environment variables).</returns> |
| 86 | + public TServiceClient GetClient<TServiceClient>() |
| 87 | + where TServiceClient : class, IDisposable |
| 88 | + { |
| 89 | + if (this.serviceClientCache.TryGetValue(typeof(TServiceClient), out IDisposable clientObject)) |
| 90 | + { |
| 91 | + return (TServiceClient)clientObject; |
| 92 | + } |
| 93 | + |
| 94 | + TServiceClient client = this.mockContext.GetServiceClient<TServiceClient>(); |
| 95 | + this.serviceClientCache.Add(typeof(TServiceClient), client); |
| 96 | + return client; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Get or create resource group. |
| 101 | + /// </summary> |
| 102 | + /// <param name="resourceGroupName">The name of the resource group.</param> |
| 103 | + /// <param name="location">Location where resource group is to be created.</param> |
| 104 | + /// <returns>ResourceGroup object.</returns> |
| 105 | + public ResourceGroup GetOrCreateResourceGroup(string resourceGroupName, string location) |
| 106 | + { |
| 107 | + ResourceManagementClient resourceClient = this.GetClient<ResourceManagementClient>(); |
| 108 | + ResourceGroup resourceGroup = this.GetResourceGroupIfExists(resourceGroupName); |
| 109 | + |
| 110 | + if (resourceGroup == null) |
| 111 | + { |
| 112 | + resourceGroup = resourceClient.ResourceGroups.CreateOrUpdate( |
| 113 | + resourceGroupName, |
| 114 | + new ResourceGroup |
| 115 | + { |
| 116 | + Location = location, |
| 117 | + Tags = new Dictionary<string, string>() { { resourceGroupName, DateTime.UtcNow.ToString("u") } }, |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + return resourceGroup; |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Get or create virtual network. |
| 126 | + /// </summary> |
| 127 | + /// <param name="resourceGroup">Object representing a resource group.</param> |
| 128 | + /// <param name="virtualNetworkName">The name of the virtual network.</param> |
| 129 | + /// <returns>VirtualNetwork object.</returns> |
| 130 | + public VirtualNetwork GetOrCreateVirtualNetwork(ResourceGroup resourceGroup, string virtualNetworkName) |
| 131 | + { |
| 132 | + NetworkManagementClient networkManagementClient = this.GetClient<NetworkManagementClient>(); |
| 133 | + VirtualNetwork virtualNetwork = null; |
| 134 | + try |
| 135 | + { |
| 136 | + virtualNetwork = networkManagementClient.VirtualNetworks.Get(resourceGroup.Name, virtualNetworkName); |
| 137 | + } |
| 138 | + catch (CloudException ex) |
| 139 | + { |
| 140 | + if (ex.Body.Code != "ResourceNotFound") |
| 141 | + { |
| 142 | + throw; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (virtualNetwork == null) |
| 147 | + { |
| 148 | + var vnet = new VirtualNetwork() |
| 149 | + { |
| 150 | + Location = resourceGroup.Location, |
| 151 | + AddressSpace = new AddressSpace() |
| 152 | + { |
| 153 | + AddressPrefixes = new List<string> { "10.1.0.0/16" }, |
| 154 | + }, |
| 155 | + }; |
| 156 | + virtualNetwork = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroup.Name, virtualNetworkName, vnet); |
| 157 | + } |
| 158 | + |
| 159 | + return virtualNetwork; |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Get Or create subnet. |
| 164 | + /// </summary> |
| 165 | + /// <param name="resourceGroup">Object representing a resource group.</param> |
| 166 | + /// <param name="virtualNetwork">Object representing a virtual network.</param> |
| 167 | + /// <param name="subnetName">The name of the subnet.</param> |
| 168 | + /// <returns>Subnet object.</returns> |
| 169 | + public Subnet GetOrCreateSubnet(ResourceGroup resourceGroup, VirtualNetwork virtualNetwork, string subnetName) |
| 170 | + { |
| 171 | + NetworkManagementClient networkManagementClient = this.GetClient<NetworkManagementClient>(); |
| 172 | + Subnet subNet = null; |
| 173 | + try |
| 174 | + { |
| 175 | + subNet = networkManagementClient.Subnets.Get(resourceGroup.Name, virtualNetwork.Name, subnetName); |
| 176 | + } |
| 177 | + catch (CloudException ex) |
| 178 | + { |
| 179 | + if (ex.Body.Code != "NotFound") |
| 180 | + { |
| 181 | + throw; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if (subNet == null) |
| 186 | + { |
| 187 | + var snet = new Subnet() |
| 188 | + { |
| 189 | + Name = subnetName, |
| 190 | + AddressPrefix = "10.1.0.0/24", |
| 191 | + }; |
| 192 | + |
| 193 | + subNet = networkManagementClient.Subnets.CreateOrUpdate(resourceGroup.Name, virtualNetwork.Name, subnetName, snet); |
| 194 | + } |
| 195 | + |
| 196 | + return subNet; |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Get cache if exists. |
| 201 | + /// </summary> |
| 202 | + /// <param name="resourceGroup">Object representing a resource group.</param> |
| 203 | + /// <param name="cacheName">The name of the cache.</param> |
| 204 | + /// <returns>Cache object if cache exists else null.</returns> |
| 205 | + public Cache GetCacheIfExists(ResourceGroup resourceGroup, string cacheName) |
| 206 | + { |
| 207 | + StorageCacheManagementClient storagecacheManagementClient = this.GetClient<StorageCacheManagementClient>(); |
| 208 | + storagecacheManagementClient.ApiVersion = Constants.DefaultAPIVersion; |
| 209 | + try |
| 210 | + { |
| 211 | + return storagecacheManagementClient.Caches.Get(resourceGroup.Name, cacheName); |
| 212 | + } |
| 213 | + catch (CloudErrorException ex) |
| 214 | + { |
| 215 | + if (ex.Body.Error.Code == "ResourceNotFound") |
| 216 | + { |
| 217 | + return null; |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + throw; |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + /// <summary> |
| 227 | + /// Get resource group if exists. |
| 228 | + /// </summary> |
| 229 | + /// <param name="resourceGroupName">The name of the resource group.</param> |
| 230 | + /// <returns>ResourceGroup object if resource group exists else null.</returns> |
| 231 | + public ResourceGroup GetResourceGroupIfExists(string resourceGroupName) |
| 232 | + { |
| 233 | + ResourceManagementClient resourceManagementClient = this.GetClient<ResourceManagementClient>(); |
| 234 | + try |
| 235 | + { |
| 236 | + return resourceManagementClient.ResourceGroups.Get(resourceGroupName); |
| 237 | + } |
| 238 | + catch (CloudException ex) |
| 239 | + { |
| 240 | + if (ex.Body.Code == "ResourceGroupNotFound") |
| 241 | + { |
| 242 | + return null; |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + throw; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + /// <summary> |
| 252 | + /// Register subscription for resource. |
| 253 | + /// </summary> |
| 254 | + /// <param name="providerName">Resource provider name.</param> |
| 255 | + public void RegisterSubscriptionForResource(string providerName) |
| 256 | + { |
| 257 | + ResourceManagementClient resourceManagementClient = this.GetClient<ResourceManagementClient>(); |
| 258 | + var reg = resourceManagementClient.Providers.Register(providerName); |
| 259 | + StorageCacheTestUtilities.ThrowIfTrue(reg == null, $"Failed to register provider {providerName}"); |
| 260 | + var result = resourceManagementClient.Providers.Get(providerName); |
| 261 | + StorageCacheTestUtilities.ThrowIfTrue(result == null, $"Failed to register provier {providerName}"); |
| 262 | + } |
| 263 | + |
| 264 | + /// <summary> |
| 265 | + /// Add role assignment by role name. |
| 266 | + /// </summary> |
| 267 | + /// <param name="context">Object representing a HpcCacheTestContext.</param> |
| 268 | + /// <param name="scope">The scope of the role assignment to create.</param> |
| 269 | + /// <param name="roleName">The role name.</param> |
| 270 | + /// <param name="assignmentName">The name of the role assignment to create.</param> |
| 271 | + public void AddRoleAssignment(HpcCacheTestContext context, string scope, string roleName, string assignmentName) |
| 272 | + { |
| 273 | + AuthorizationManagementClient authorizationManagementClient = context.GetClient<AuthorizationManagementClient>(); |
| 274 | + var roleDefinition = authorizationManagementClient.RoleDefinitions |
| 275 | + .List(scope) |
| 276 | + .First(role => role.RoleName.StartsWith(roleName)); |
| 277 | + |
| 278 | + var newRoleAssignment = new RoleAssignmentCreateParameters() |
| 279 | + { |
| 280 | + RoleDefinitionId = roleDefinition.Id, |
| 281 | + PrincipalId = Constants.StorageCacheResourceProviderPrincipalId, |
| 282 | + |
| 283 | + // The principal ID assigned to the role. |
| 284 | + // This maps to the ID inside the Active Directory. |
| 285 | + // It can point to a user, service principal, or security group. |
| 286 | + CanDelegate = false, |
| 287 | + }; |
| 288 | + |
| 289 | + authorizationManagementClient.RoleAssignments.Create(scope, assignmentName, newRoleAssignment); |
| 290 | + } |
| 291 | + |
| 292 | + /// <summary> |
| 293 | + /// This code added to correctly implement the disposable pattern. |
| 294 | + /// </summary> |
| 295 | + public void Dispose() |
| 296 | + { |
| 297 | + this.Dispose(true); |
| 298 | + } |
| 299 | + |
| 300 | + /// <summary> |
| 301 | + /// Dispose managed state. |
| 302 | + /// </summary> |
| 303 | + /// <param name="disposing">true if we should dispose managed state, otherwise false.</param> |
| 304 | + protected virtual void Dispose(bool disposing) |
| 305 | + { |
| 306 | + if (!this.disposedValue) |
| 307 | + { |
| 308 | + if (disposing) |
| 309 | + { |
| 310 | + // Dispose clients |
| 311 | + foreach (IDisposable client in this.serviceClientCache.Values) |
| 312 | + { |
| 313 | + client.Dispose(); |
| 314 | + } |
| 315 | + |
| 316 | + // Dispose context |
| 317 | + this.mockContext.Dispose(); |
| 318 | + } |
| 319 | + |
| 320 | + this.disposedValue = true; |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | +} |
0 commit comments