Beispiel #1
0
        public async Task CreateUpdateGetDeleteBlobInventoryPolicy()
        {
            //create resource group and storage account
            string accountName = await CreateValidAccountNameAsync(namePrefix);

            _resourceGroup = await CreateResourceGroupAsync();

            StorageAccountContainer        storageAccountContainer = _resourceGroup.GetStorageAccounts();
            StorageAccountCreateParameters parameters = GetDefaultStorageAccountParameters(kind: Kind.StorageV2);
            StorageAccount account = (await storageAccountContainer.CreateOrUpdateAsync(accountName, parameters)).Value;
            BlobInventoryPolicyContainer blobInventoryPolicyContainer = account.GetBlobInventoryPolicies();

            //create a blob container
            string               containerName        = Recording.GenerateAssetName("testblob");
            BlobContainerData    data                 = new BlobContainerData();
            BlobServiceContainer blobServiceContainer = account.GetBlobServices();
            BlobService          blobService          = await blobServiceContainer.GetAsync("default");

            BlobContainerContainer blobContainerContainer = blobService.GetBlobContainers();
            BlobContainer          container = (await blobContainerContainer.CreateOrUpdateAsync(containerName, new BlobContainerData())).Value;

            //prepare schema fields
            string[] BlobSchemaField = new string[] { "Name", "Creation-Time", "Last-Modified", "Content-Length", "Content-MD5", "BlobType", "AccessTier", "AccessTierChangeTime",
                                                      "Snapshot", "VersionId", "IsCurrentVersion", "Metadata", "LastAccessTime" };
            string[] ContainerSchemaField = new string[] { "Name", "Last-Modified", "Metadata", "LeaseStatus", "LeaseState", "LeaseDuration", "PublicAccess", "HasImmutabilityPolicy", "HasLegalHold" };

            List <string> blobSchemaFields1 = new List <string>(BlobSchemaField);
            List <string> blobSchemaFields2 = new List <string>();

            blobSchemaFields2.Add("Name");
            List <string> containerSchemaFields1 = new List <string>(ContainerSchemaField);
            List <string> containerSchemaFields2 = new List <string>();

            containerSchemaFields2.Add("Name");

            // prepare policy objects,the type of policy rule should always be Inventory
            List <BlobInventoryPolicyRule> ruleList = new List <BlobInventoryPolicyRule>();
            BlobInventoryPolicyRule        rule1    = new BlobInventoryPolicyRule(true, "rule1", containerName,
                                                                                  new BlobInventoryPolicyDefinition(
                                                                                      filters: new BlobInventoryPolicyFilter(
                                                                                          blobTypes: new List <string>(new string[] { "blockBlob" }),
                                                                                          prefixMatch: new List <string>(new string[] { "prefix1", "prefix2" }),
                                                                                          includeBlobVersions: true,
                                                                                          includeSnapshots: true),
                                                                                      format: Format.Csv,
                                                                                      schedule: Schedule.Weekly,
                                                                                      objectType: ObjectType.Blob,
                                                                                      schemaFields: blobSchemaFields1));

            BlobInventoryPolicyRule rule2 = new BlobInventoryPolicyRule(true, "rule2", containerName,
                                                                        new BlobInventoryPolicyDefinition(
                                                                            format: Format.Csv,
                                                                            schedule: Schedule.Daily,
                                                                            objectType: ObjectType.Container,
                                                                            schemaFields: containerSchemaFields1));

            BlobInventoryPolicyRule rule3 = new BlobInventoryPolicyRule(true, "rule3", containerName,
                                                                        new BlobInventoryPolicyDefinition(
                                                                            format: Format.Parquet,
                                                                            schedule: Schedule.Weekly,
                                                                            objectType: ObjectType.Container,
                                                                            schemaFields: containerSchemaFields2));

            ruleList.Add(rule1);
            ruleList.Add(rule2);
            BlobInventoryPolicySchema policy    = new BlobInventoryPolicySchema(true, "Inventory", ruleList);
            BlobInventoryPolicyData   parameter = new BlobInventoryPolicyData()
            {
                Policy = policy
            };

            //create and get policy, the name of blob inventory policy should always be default
            BlobInventoryPolicy blobInventoryPolicy = (await blobInventoryPolicyContainer.CreateOrUpdateAsync("default", parameter)).Value;

            blobInventoryPolicy = await blobInventoryPolicyContainer.GetAsync("default");

            Assert.AreEqual(blobInventoryPolicy.Data.Policy.Rules.Count, 2);

            //update policy
            ruleList.Add(rule3);
            BlobInventoryPolicySchema policy2    = new BlobInventoryPolicySchema(true, "Inventory", ruleList);
            BlobInventoryPolicyData   parameter2 = new BlobInventoryPolicyData()
            {
                Policy = policy2
            };

            blobInventoryPolicy = (await blobInventoryPolicyContainer.CreateOrUpdateAsync("default", parameter2)).Value;
            Assert.AreEqual(blobInventoryPolicy.Data.Policy.Rules.Count, 3);

            //delete policy
            await blobInventoryPolicy.DeleteAsync();
        }