/// <summary> /// Adds blob container. /// </summary> /// <param name="context">HpcCacheTestContext.</param> /// <param name="resourceGroup">Object representing a resource group.</param> /// <param name="storageAccount">Object representing a storage account.</param> /// <param name="containerName">containerName.</param> /// <param name="suffix">suffix.</param> /// <param name="testOutputHelper">testOutputHelper.</param> /// <returns>BlobContainer.</returns> public BlobContainer AddBlobContainer( HpcCacheTestContext context, ResourceGroup resourceGroup, StorageAccount storageAccount, string containerName = "cmdletcontnr", string suffix = null, ITestOutputHelper testOutputHelper = null) { if (this.blobContainersCache.TryGetValue(containerName + suffix, out BlobContainer blobContainer)) { if (testOutputHelper != null) { testOutputHelper.WriteLine($"Using existing blob container {containerName + suffix}"); } return(blobContainer); } StorageManagementClient storageManagementClient = context.GetClient <StorageManagementClient>(); StorageAccountsHelper storageAccountsHelper = new StorageAccountsHelper(storageManagementClient, resourceGroup); blobContainer = storageAccountsHelper.CreateBlobContainer(storageAccount.Name, containerName + suffix); this.blobContainersCache.Add(blobContainer.Name, blobContainer); return(blobContainer); }
/// <summary> /// Creates storage account, blob container and adds CLFS storage account to cache. /// </summary> /// <param name="context">HpcCacheTestContext.</param> /// <param name="suffix">suffix.</param> /// <param name="waitForStorageTarget">Whether to wait for storage target to deploy.</param> /// <param name="addPermissions">Whether to add storage account contributor roles.</param> /// <param name="testOutputHelper">testOutputHelper.</param> /// <param name="sleep">Sleep time for permissions to get propagated.</param> /// <param name="waitForPermissions">Whether to wait for permissions to be propagated.</param> /// <param name="maxRequestTries">Max retries.</param> /// <returns>StorageTarget.</returns> public StorageTarget AddClfsStorageTarget( HpcCacheTestContext context, string storageTargetName = "msazure", string storageAccountName = "cmdletsa", string containerName = "cmdletcontnr", string suffix = null, bool waitForStorageTarget = true, bool addPermissions = true, ITestOutputHelper testOutputHelper = null, int sleep = 300, bool waitForPermissions = true, int maxRequestTries = 25) { StorageTarget storageTarget; if (string.IsNullOrEmpty(HpcCacheTestEnvironmentUtilities.StorageTargetName)) { storageTargetName = string.IsNullOrEmpty(suffix) ? storageTargetName : storageTargetName + suffix; } else { storageTargetName = HpcCacheTestEnvironmentUtilities.StorageTargetName; } var client = context.GetClient <StorageCacheManagementClient>(); client.ApiVersion = Constants.DefaultAPIVersion; this.fixture.CacheHelper.StoragecacheManagementClient = client; storageTarget = this.fixture.CacheHelper.GetStorageTarget(this.fixture.Cache.Name, storageTargetName); if (storageTarget == null) { string junction = "/junction" + suffix; var storageAccount = this.AddStorageAccount( context, this.fixture.ResourceGroup, storageAccountName, suffix, addPermissions, testOutputHelper, sleep: sleep, waitForPermissions: waitForPermissions); var blobContainer = this.AddBlobContainer(context, this.fixture.ResourceGroup, storageAccount, containerName, suffix, testOutputHelper); StorageTarget storageTargetParameters = this.fixture.CacheHelper.CreateClfsStorageTargetParameters( storageAccount.Name, blobContainer.Name, junction); storageTarget = this.fixture.CacheHelper.CreateStorageTarget( this.fixture.Cache.Name, storageTargetName, storageTargetParameters, testOutputHelper, waitForStorageTarget, maxRequestTries); } return(storageTarget); }
/// <summary> /// Add role assignment by role name. /// </summary> /// <param name="context">Object representing a HpcCacheTestContext.</param> /// <param name="scope">The scope of the role assignment to create.</param> /// <param name="roleName">The role name.</param> /// <param name="assignmentName">The name of the role assignment to create.</param> public void AddRoleAssignment(HpcCacheTestContext context, string scope, string roleName, string assignmentName) { AuthorizationManagementClient authorizationManagementClient = context.GetClient <AuthorizationManagementClient>(); var roleDefinition = authorizationManagementClient.RoleDefinitions .List(scope) .First(role => role.RoleName.StartsWith(roleName)); var newRoleAssignment = new RoleAssignmentCreateParameters() { RoleDefinitionId = roleDefinition.Id, PrincipalId = Constants.StorageCacheResourceProviderPrincipalId, }; authorizationManagementClient.RoleAssignments.Create(scope, assignmentName, newRoleAssignment); }
/// <summary> /// Add role assignment by role name. /// </summary> /// <param name="context">Object representing a HpcCacheTestContext.</param> /// <param name="scope">The scope of the role assignment to create.</param> /// <param name="roleName">The role name.</param> /// <param name="assignmentName">The name of the role assignment to create.</param> public void AddRoleAssignment(HpcCacheTestContext context, string scope, string roleName, string assignmentName) { AuthorizationManagementClient authorizationManagementClient = context.GetClient <AuthorizationManagementClient>(); var roleDefinition = authorizationManagementClient.RoleDefinitions .List(scope) .First(role => role.RoleName.StartsWith(roleName)); var newRoleAssignment = new RoleAssignmentCreateParameters() { RoleDefinitionId = roleDefinition.Id, PrincipalId = Constants.StorageCacheResourceProviderPrincipalId, // The principal ID assigned to the role. // This maps to the ID inside the Active Directory. // It can point to a user, service principal, or security group. CanDelegate = false, }; authorizationManagementClient.RoleAssignments.Create(scope, assignmentName, newRoleAssignment); }
/// <summary> /// Adds storage account in given resource group and applies required roles. /// </summary> /// <param name="context">HpcCacheTestContext.</param> /// <param name="resourceGroup">Object representing a resource group.</param> /// <param name="storageAccountName">storage account name.</param> /// <param name="suffix">suffix.</param> /// <param name="addPermissions">Whether to add storage account contributor roles.</param> /// <param name="testOutputHelper">testOutputHelper.</param> /// <param name="sleep">Sleep time for permissions to get propagated.</param> /// <param name="waitForPermissions">Whether to wait for permissions to be propagated.</param> /// <returns>StorageAccount.</returns> public StorageAccount AddStorageAccount( HpcCacheTestContext context, ResourceGroup resourceGroup, string storageAccountName, string suffix = null, bool addPermissions = true, ITestOutputHelper testOutputHelper = null, int sleep = 300, bool waitForPermissions = true) { if (this.storageAccountsCache.TryGetValue(storageAccountName + suffix, out StorageAccount storageAccount)) { if (testOutputHelper != null) { testOutputHelper.WriteLine($"Using existing storage account {storageAccountName + suffix}"); } return(storageAccount); } StorageManagementClient storageManagementClient = context.GetClient <StorageManagementClient>(); StorageAccountsHelper storageAccountsHelper = new StorageAccountsHelper(storageManagementClient, resourceGroup); storageAccount = storageAccountsHelper.CreateStorageAccount(storageAccountName + suffix); if (addPermissions) { this.AddStorageAccountAccessRules(context, storageAccount); } if (waitForPermissions && HttpMockServer.Mode == HttpRecorderMode.Record) { if (testOutputHelper != null) { testOutputHelper.WriteLine($"Sleeping {sleep.ToString()} seconds while permissions propagates."); } TestUtilities.Wait(new TimeSpan(0, 0, sleep)); } this.storageAccountsCache.Add(storageAccount.Name, storageAccount); return(storageAccount); }
/// <summary> /// Adds storage account access roles. /// Storage Account Contributor or Storage blob Contributor. /// </summary> /// <param name="context">Object representing a HpcCacheTestContext.</param> /// <param name="storageAccount">Object representing a storage account.</param> /// <param name="testOutputHelper">Object representing a testOutputHelper.</param> private void AddStorageAccountAccessRules( HpcCacheTestContext context, StorageAccount storageAccount, ITestOutputHelper testOutputHelper = null) { try { string role1 = "Storage Account Contributor"; context.AddRoleAssignment(context, storageAccount.Id, role1, TestUtilities.GenerateGuid().ToString()); // string role2 = "Storage Blob Data Contributor"; // context.AddRoleAssignment(context, storageAccount.Id, role2, TestUtilities.GenerateGuid().ToString()); if (testOutputHelper != null) { testOutputHelper.WriteLine($"Added {role1} role to storage account {storageAccount.Name}."); } } catch (Exception) { throw; } }