/// <summary>
        /// Adds storage account access roles.
        /// Storage Account Contributor or Storage blob Contributor.
        /// </summary>
        /// <param name="context">Object representing a StorageCacheTestContext.</param>
        /// <param name="storageAccount">Object representing a storage account.</param>
        /// <param name="testOutputHelper">Object representing a testOutputHelper.</param>
        private void AddStorageAccountAccessRules(
            StorageCacheTestContext context,
            StorageAccount storageAccount,
            ITestOutputHelper testOutputHelper = null)
        {
            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine("Add Storage Account Rules");
            }

            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 ex)
            {
                if (ex.Message != "The role assignment already exists.")
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Adds blob container.
        /// </summary>
        /// <param name="context">StorageCacheTestContext.</param>
        /// <param name="resourceGroup">Object representing a resource group.</param>
        /// <param name="storageAccount">Object representing a storage account.</param>
        /// <param name="suffix">suffix.</param>
        /// <param name="testOutputHelper">testOutputHelper.</param>
        /// <returns>BlobContainer.</returns>
        public BlobContainer AddBlobContainer(
            StorageCacheTestContext context,
            ResourceGroup resourceGroup,
            StorageAccount storageAccount,
            string suffix = null,
            ITestOutputHelper testOutputHelper = null)
        {
            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine($"SAF Add blob container {resourceGroup.Name + suffix}");
            }

            if (this.blobContainersCache.TryGetValue(resourceGroup.Name + suffix, out BlobContainer blobContainer))
            {
                if (testOutputHelper != null)
                {
                    testOutputHelper.WriteLine($"Using existing blob container {resourceGroup.Name + suffix}");
                }

                return(blobContainer);
            }

            StorageManagementClient storageManagementClient = context.GetClient <StorageManagementClient>();
            StorageAccountsHelper   storageAccountsHelper   = new StorageAccountsHelper(storageManagementClient, resourceGroup);

            blobContainer = storageAccountsHelper.CreateBlobContainer(storageAccount.Name, resourceGroup.Name + suffix);
            this.blobContainersCache.Add(blobContainer.Name, blobContainer);
            return(blobContainer);
        }
        /// <summary>
        /// Creates storage account, blob container and adds BlobNfs storage account to cache.
        /// </summary>
        /// <param name="context">StorageCacheTestContext.</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 AddBlobNfsStorageAccount(
            StorageCacheTestContext context,
            string suffix                      = null,
            bool waitForStorageTarget          = true,
            bool addPermissions                = true,
            ITestOutputHelper testOutputHelper = null,
            int sleep = 300,
            bool waitForPermissions = true,
            int maxRequestTries     = 25,
            string usageModel       = "WRITE_WORKLOAD_15")
        {
            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine($"SAF Add blob nfs Storage Account");
            }

            string storageTargetName = string.IsNullOrEmpty(suffix) ? this.fixture.ResourceGroup.Name : this.fixture.ResourceGroup.Name + suffix;
            string junction          = "/junction" + suffix;
            var    storageAccount    = this.AddStorageAccount(
                context,
                this.fixture.ResourceGroup,
                suffix,
                addPermissions,
                testOutputHelper,
                sleep: sleep,
                waitForPermissions: waitForPermissions,
                blobNfs: true);
            var blobContainer = this.AddBlobContainer(context, this.fixture.ResourceGroup, storageAccount, suffix, testOutputHelper);

            testOutputHelper.WriteLine($"Add Storage Target Usage Model {usageModel}");
            StorageTarget storageTargetParameters = this.fixture.CacheHelper.CreateBlobNfsStorageTargetParameters(
                storageAccount.Name,
                blobContainer.Name,
                junction,
                usageModel: usageModel,
                testOutputHelper: testOutputHelper);

            testOutputHelper.WriteLine($"Storage Target Parameters BlobNfs {storageTargetParameters.BlobNfs.UsageModel}");
            testOutputHelper.WriteLine($"Storage Target Parameters BlobNfs {storageTargetParameters.BlobNfs.Target}");
            StorageTarget storageTarget = this.fixture.CacheHelper.CreateStorageTarget(
                this.fixture.Cache.Name,
                storageTargetName,
                storageTargetParameters,
                testOutputHelper,
                waitForStorageTarget,
                maxRequestTries);

            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine($"Storage target Name {storageTarget.Name}");
                testOutputHelper.WriteLine($"Storage target NamespacePath {storageTarget.Junctions[0].NamespacePath}");
                testOutputHelper.WriteLine($"Storage target TargetPath {storageTarget.Junctions[0].TargetPath}");
                testOutputHelper.WriteLine($"Storage target Id {storageTarget.Id}");
                testOutputHelper.WriteLine($"Storage target Target {storageTarget.BlobNfs.Target}");
                testOutputHelper.WriteLine($"Storage target UsageModel {storageTarget.BlobNfs.UsageModel}");
            }

            return(storageTarget);
        }
        /// <summary>
        /// Adds storage account in given resource group and applies required roles.
        /// </summary>
        /// <param name="context">StorageCacheTestContext.</param>
        /// <param name="resourceGroup">Object representing a resource group.</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(
            StorageCacheTestContext context,
            ResourceGroup resourceGroup,
            string suffix       = null,
            bool addPermissions = true,
            ITestOutputHelper testOutputHelper = null,
            int sleep = 300,
            bool waitForPermissions = true,
            bool blobNfs            = false)
        {
            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine($"SAF Add Storage Account {resourceGroup.Name + suffix}");
                testOutputHelper.WriteLine($"SAF Add Storage Account blobNfs {blobNfs}");
                testOutputHelper.WriteLine($"SAF Add Storage Account addpermissions {addPermissions}");
            }
            notes.Add("SAF AddStorageAccount");

            if (this.storageAccountsCache.TryGetValue(resourceGroup.Name + suffix, out StorageAccount storageAccount))
            {
                notes.Add("SAF AddStorageAccount Using Existing Storage Account.");

                if (testOutputHelper != null)
                {
                    testOutputHelper.WriteLine($"Using existing storage account {resourceGroup.Name + suffix}");
                }

                return(storageAccount);
            }

            notes.Add("SAF AddStorageAccount Need to create a storage account.");
            StorageManagementClient storageManagementClient = context.GetClient <StorageManagementClient>();
            StorageAccountsHelper   storageAccountsHelper   = new StorageAccountsHelper(storageManagementClient, resourceGroup);
            string subnetUri = $"/subscriptions/{fixture.SubscriptionID}/resourcegroups/{fixture.ResourceGroup.Name}/providers/Microsoft.Network/virtualNetworks/{fixture.VirtualNetwork.Name}/subnets/{fixture.SubNet.Name}";

            notes.Add($"SAF AddStroageAccount subnetUri: {subnetUri}");
            storageAccount = storageAccountsHelper.CreateStorageAccount(resourceGroup.Name + suffix, blobNfs: blobNfs, subnetUri: subnetUri);
            if (storageAccount == null)
            {
                testOutputHelper.WriteLine("Failed to create storageAccount");
                return(storageAccount);
            }
            if (addPermissions)
            {
                notes.Add("SAF AddStorageAccount Add SA Access Rules");
                this.AddStorageAccountAccessRules(context, storageAccount);
            }

            if (waitForPermissions && HttpMockServer.Mode == HttpRecorderMode.Record)
            {
                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>
        /// Creates storage account, blob container and adds CLFS storage account to cache.
        /// </summary>
        /// <param name="context">StorageCacheTestContext.</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 AddClfsStorageAccount(
            StorageCacheTestContext context,
            string suffix                      = null,
            bool waitForStorageTarget          = true,
            bool addPermissions                = true,
            ITestOutputHelper testOutputHelper = null,
            int sleep = 300,
            bool waitForPermissions = true,
            int maxRequestTries     = 25)
        {
            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine($"SAF Add clfs Storage Account with wait for ST {waitForStorageTarget}, add Permissions {addPermissions}, waitForPermissions {waitForPermissions}");
            }

            string storageTargetName = string.IsNullOrEmpty(suffix) ? this.fixture.ResourceGroup.Name : this.fixture.ResourceGroup.Name + suffix;
            string junction          = "/junction" + suffix;
            var    storageAccount    = this.AddStorageAccount(
                context,
                this.fixture.ResourceGroup,
                suffix,
                addPermissions,
                testOutputHelper,
                sleep: sleep,
                waitForPermissions: waitForPermissions);
            var           blobContainer           = this.AddBlobContainer(context, this.fixture.ResourceGroup, storageAccount, suffix, testOutputHelper);
            StorageTarget storageTargetParameters = this.fixture.CacheHelper.CreateClfsStorageTargetParameters(
                storageAccount.Name,
                blobContainer.Name,
                junction);
            StorageTarget storageTarget = this.fixture.CacheHelper.CreateStorageTarget(
                this.fixture.Cache.Name,
                storageTargetName,
                storageTargetParameters,
                testOutputHelper,
                waitForStorageTarget,
                maxRequestTries);

            if (testOutputHelper != null)
            {
                testOutputHelper.WriteLine($"Storage target Name {storageTarget.Name}");
                testOutputHelper.WriteLine($"Storage target NamespacePath {storageTarget.Junctions[0].NamespacePath}");
                testOutputHelper.WriteLine($"Storage target TargetPath {storageTarget.Junctions[0].TargetPath}");
                testOutputHelper.WriteLine($"Storage target Id {storageTarget.Id}");
                testOutputHelper.WriteLine($"Storage target Target {storageTarget.Clfs.Target}");
            }

            return(storageTarget);
        }
Exemple #6
0
        /// <summary>
        /// Add role assignment by role name.
        /// </summary>
        /// <param name="context">Object representing a StorageCacheTestContext.</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(StorageCacheTestContext 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">StorageCacheTestContext.</param>
        /// <param name="resourceGroup">Object representing a resource group.</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(
            StorageCacheTestContext context,
            ResourceGroup resourceGroup,
            string suffix       = null,
            bool addPermissions = true,
            ITestOutputHelper testOutputHelper = null,
            int sleep = 300,
            bool waitForPermissions = true)
        {
            if (this.storageAccountsCache.TryGetValue(resourceGroup.Name + suffix, out StorageAccount storageAccount))
            {
                if (testOutputHelper != null)
                {
                    testOutputHelper.WriteLine($"Using existing storage account {resourceGroup.Name + suffix}");
                }

                return(storageAccount);
            }

            StorageManagementClient storageManagementClient = context.GetClient <StorageManagementClient>();
            StorageAccountsHelper   storageAccountsHelper   = new StorageAccountsHelper(storageManagementClient, resourceGroup);

            storageAccount = storageAccountsHelper.CreateStorageAccount(resourceGroup.Name + suffix);
            if (addPermissions)
            {
                this.AddStorageAccountAccessRules(context, storageAccount);
            }

            if (waitForPermissions && HttpMockServer.Mode == HttpRecorderMode.Record)
            {
                testOutputHelper.WriteLine($"Sleeping {sleep.ToString()} seconds while permissions propagates.");
                TestUtilities.Wait(new TimeSpan(0, 0, sleep));
            }

            this.storageAccountsCache.Add(storageAccount.Name, storageAccount);
            return(storageAccount);
        }