public async Task createStorageAccountAndGetBlobContainerCollection() { ArmClient armClient = new ArmClient(new DefaultAzureCredential()); SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync(); string rgName = "myRgName"; AzureLocation location = AzureLocation.WestUS2; ArmOperation <ResourceGroupResource> operation = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(location)); ResourceGroupResource resourceGroup = operation.Value; this.resourceGroup = resourceGroup; StorageSku sku = new StorageSku(StorageSkuName.StandardGRS); StorageKind kind = StorageKind.Storage; string locationStr = "westus2"; StorageAccountCreateParameters parameters = new StorageAccountCreateParameters(sku, kind, locationStr); StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts(); string accountName = "myAccount"; ArmOperation <StorageAccountResource> accountCreateOperation = await accountCollection.CreateOrUpdateAsync(WaitUntil.Started, accountName, parameters); storageAccount = await accountCreateOperation.WaitForCompletionAsync(); #region Snippet:Managing_BlobContainers_GetBlobService BlobServiceResource blobService = storageAccount.GetBlobService(); #endregion this.blobService = blobService; }
public async Task UpdateAccountIdentityFromNoneToSystem() { _resourceGroup = await CreateResourceGroupAsync(); StorageAccountCollection storageAccountCollection = _resourceGroup.GetStorageAccounts(); string accountName = await CreateValidAccountNameAsync(namePrefix); var param = GetDefaultStorageAccountParameters(sku: new StorageSku(StorageSkuName.StandardLRS)); StorageAccountResource account1 = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, param)).Value; Assert.AreEqual(accountName, account1.Id.Name); VerifyAccountProperties(account1, false); Assert.Null(account1.Data.Identity); StorageAccountPatch parameters = new StorageAccountPatch() { Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned) }; account1 = await account1.UpdateAsync(parameters); Assert.AreEqual(ManagedServiceIdentityType.SystemAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.IsEmpty(account1.Data.Identity.UserAssignedIdentities); Assert.NotNull(account1.Data.Identity.PrincipalId); Assert.NotNull(account1.Data.Identity.TenantId); // validate StorageAccountResource account2 = await storageAccountCollection.GetAsync(accountName); Assert.AreEqual(ManagedServiceIdentityType.SystemAssigned, account2.Data.Identity.ManagedServiceIdentityType); Assert.IsEmpty(account2.Data.Identity.UserAssignedIdentities); Assert.NotNull(account2.Data.Identity.PrincipalId); Assert.NotNull(account2.Data.Identity.TenantId); }
public static void VerifyAccountProperties(StorageAccountResource account, bool useDefaults) { Assert.NotNull(account); Assert.NotNull(account.Id); Assert.NotNull(account.Id.Name); Assert.NotNull(account.Data.Location); Assert.NotNull(account.Data); Assert.NotNull(account.Data.CreationOn); Assert.NotNull(account.Data.Sku); Assert.NotNull(account.Data.Sku.Name); Assert.NotNull(account.Data.Sku.Tier); Assert.NotNull(account.Data.PrimaryEndpoints); if (useDefaults) { Assert.AreEqual(DefaultLocation, account.Data.Location); Assert.AreEqual(DefaultSkuNameStandardGRS.Name, account.Data.Sku.Name); Assert.AreEqual(StorageSkuTier.Standard, account.Data.Sku.Tier); Assert.AreEqual(DefaultKindStorage, account.Data.Kind); Assert.NotNull(account.Data.Tags); Assert.AreEqual(2, account.Data.Tags.Count); Assert.AreEqual("value1", account.Data.Tags["key1"]); Assert.AreEqual("value2", account.Data.Tags["key2"]); } }
public async Task Get() { #region Snippet:Managing_StorageAccounts_GetStorageAccount StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts(); StorageAccountResource storageAccount = await accountCollection.GetAsync("myAccount"); Console.WriteLine(storageAccount.Id.Name); #endregion }
public static StorageAccountResource GetStorageAccountResource(this ArmClient client, ResourceIdentifier id) { return(client.GetResourceClient(() => { StorageAccountResource.ValidateResourceId(id); return new StorageAccountResource(client, id); } )); }
public async Task Delete() { #region Snippet:Managing_StorageAccounts_DeleteStorageAccount StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts(); StorageAccountResource storageAccount = await accountCollection.GetAsync("myAccount"); await storageAccount.DeleteAsync(WaitUntil.Completed); #endregion }
public async Task CreateEventhubWithParameter() { //prepare a storage account string accountName = Recording.GenerateAssetName("storage"); StorageSku sku = new StorageSku("Standard_LRS"); var storageAccountCreateParameters = new StorageAccountCreateOrUpdateContent(sku, StorageKind.StorageV2, "eastus2") { AccessTier = AccessTier.Hot }; StorageAccountResource account = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(WaitUntil.Completed, accountName, storageAccountCreateParameters)).Value; if (Mode != RecordedTestMode.Playback) { await Task.Delay(5000); } //create eventhub string eventHubName = Recording.GenerateAssetName("eventhub"); EventHubData parameter = new EventHubData() { MessageRetentionInDays = 4, PartitionCount = 4, Status = EntityStatus.Active, CaptureDescription = new CaptureDescription() { Enabled = true, Encoding = EncodingCaptureDescription.Avro, IntervalInSeconds = 120, SizeLimitInBytes = 10485763, Destination = new EventHubDestination() { Name = "EventHubArchive.AzureBlockBlob", BlobContainer = "container", ArchiveNameFormat = "{Namespace}/{EventHub}/{PartitionId}/{Year}/{Month}/{Day}/{Hour}/{Minute}/{Second}", StorageAccountResourceId = account.Id.ToString() } } }; EventHubResource eventHub = (await _eventHubCollection.CreateOrUpdateAsync(WaitUntil.Completed, eventHubName, parameter)).Value; //validate Assert.NotNull(eventHub); Assert.AreEqual(eventHub.Id.Name, eventHubName); Assert.AreEqual(eventHub.Data.Status, parameter.Status); Assert.AreEqual(eventHub.Data.MessageRetentionInDays, parameter.MessageRetentionInDays); Assert.AreEqual(eventHub.Data.PartitionCount, parameter.PartitionCount); Assert.AreEqual(eventHub.Data.CaptureDescription.IntervalInSeconds, parameter.CaptureDescription.IntervalInSeconds); Assert.AreEqual(eventHub.Data.CaptureDescription.SizeLimitInBytes, parameter.CaptureDescription.SizeLimitInBytes); Assert.AreEqual(eventHub.Data.CaptureDescription.Destination.Name, parameter.CaptureDescription.Destination.Name); Assert.AreEqual(eventHub.Data.CaptureDescription.Destination.BlobContainer, parameter.CaptureDescription.Destination.BlobContainer); Assert.AreEqual(eventHub.Data.CaptureDescription.Destination.StorageAccountResourceId, parameter.CaptureDescription.Destination.StorageAccountResourceId); Assert.AreEqual(eventHub.Data.CaptureDescription.Destination.ArchiveNameFormat, parameter.CaptureDescription.Destination.ArchiveNameFormat); await account.DeleteAsync(WaitUntil.Completed); }
public async Task AddTag() { #region Snippet:Managing_StorageAccounts_AddTagStorageAccount StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts(); StorageAccountResource storageAccount = await accountCollection.GetAsync("myAccount"); // add a tag on this storage account await storageAccount.AddTagAsync("key", "value"); #endregion }
public async Task ExtendImmutabilityPolicy() { //update storage account to v2 PatchableStorageAccountData updateParameters = new PatchableStorageAccountData() { Kind = StorageKind.StorageV2 }; _storageAccount = await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); // create a blob container string containerName = Recording.GenerateAssetName("testblob"); BlobContainerData data = new BlobContainerData(); BlobContainerResource container = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName, new BlobContainerData())).Value; //create immutability policy ImmutabilityPolicyData immutabilityPolicyModel = new ImmutabilityPolicyData() { ImmutabilityPeriodSinceCreationInDays = 3 }; ImmutabilityPolicyResource immutabilityPolicy = (await container.GetImmutabilityPolicy().CreateOrUpdateAsync(WaitUntil.Completed, data: immutabilityPolicyModel)).Value; //validate Assert.NotNull(immutabilityPolicy.Data.Id); Assert.NotNull(immutabilityPolicy.Data.ResourceType); Assert.NotNull(immutabilityPolicy.Data.Name); Assert.AreEqual(3, immutabilityPolicy.Data.ImmutabilityPeriodSinceCreationInDays); Assert.AreEqual(ImmutabilityPolicyState.Unlocked, immutabilityPolicy.Data.State); //lock immutability policy immutabilityPolicy = await container.GetImmutabilityPolicy().LockImmutabilityPolicyAsync(ifMatch: immutabilityPolicy.Data.Etag); Assert.NotNull(immutabilityPolicy.Data.Id); Assert.NotNull(immutabilityPolicy.Data.ResourceType); Assert.NotNull(immutabilityPolicy.Data.Name); Assert.AreEqual(3, immutabilityPolicy.Data.ImmutabilityPeriodSinceCreationInDays); Assert.AreEqual(ImmutabilityPolicyState.Locked, immutabilityPolicy.Data.State); //extend immutability policy immutabilityPolicyModel = new ImmutabilityPolicyData() { ImmutabilityPeriodSinceCreationInDays = 100 }; immutabilityPolicy = await container.GetImmutabilityPolicy().ExtendImmutabilityPolicyAsync(ifMatch: immutabilityPolicy.Data.Etag, data: immutabilityPolicyModel); Assert.NotNull(immutabilityPolicy.Data.Id); Assert.NotNull(immutabilityPolicy.Data.ResourceType); Assert.NotNull(immutabilityPolicy.Data.Name); Assert.AreEqual(100, immutabilityPolicy.Data.ImmutabilityPeriodSinceCreationInDays); Assert.AreEqual(ImmutabilityPolicyState.Locked, immutabilityPolicy.Data.State); await container.DeleteAsync(WaitUntil.Completed); }
public async Task ClearStorageAccount() { if (_resourceGroup != null) { var storageAccountCollection = _resourceGroup.GetStorageAccounts(); await foreach (StorageAccountResource account in storageAccountCollection.GetAllAsync()) { await account.DeleteAsync(WaitUntil.Completed); } _resourceGroup = null; _storageAccount = null; } }
public async Task CreateStorageAccountAndGetQueueCollection() { _resourceGroup = await CreateResourceGroupAsync(); string accountName = await CreateValidAccountNameAsync("teststoragemgmt"); StorageAccountCollection storageAccountCollection = _resourceGroup.GetStorageAccounts(); _storageAccount = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, GetDefaultStorageAccountParameters())).Value; _queueService = _storageAccount.GetQueueService(); _queueService = await _queueService.GetAsync(); _storageQueueCollection = _queueService.GetStorageQueues(); }
public async Task BlobContainersVLW() { //update storage account to v2 PatchableStorageAccountData updateParameters = new PatchableStorageAccountData() { Kind = StorageKind.StorageV2 }; _storageAccount = await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); //enable blob versioning BlobServiceData properties = _blobService.Data; properties.IsVersioningEnabled = true; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties)).Value; Assert.IsTrue(properties.IsVersioningEnabled); //create container with VLW string containerName1 = Recording.GenerateAssetName("testblob1"); BlobContainerData parameters1 = new BlobContainerData() { ImmutableStorageWithVersioning = new ImmutableStorageWithVersioning() { Enabled = true } }; BlobContainerResource container1 = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName1, parameters1)).Value; Assert.IsTrue(container1.Data.ImmutableStorageWithVersioning.Enabled); Assert.IsNull(container1.Data.ImmutableStorageWithVersioning.MigrationState); //update container to enabled Immutability Policy string containerName2 = Recording.GenerateAssetName("testblob2"); BlobContainerData parameters2 = new BlobContainerData(); BlobContainerResource container2 = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName2, parameters2)).Value; await container2.GetImmutabilityPolicy().CreateOrUpdateAsync(WaitUntil.Completed, data: new ImmutabilityPolicyData() { ImmutabilityPeriodSinceCreationInDays = 1 }); await container2.ObjectLevelWormAsync(WaitUntil.Completed); container2 = await container2.GetAsync(); Assert.IsTrue(container2.Data.ImmutableStorageWithVersioning.Enabled); Assert.AreEqual("Completed", container2.Data.ImmutableStorageWithVersioning.MigrationState); }
public async Task UpdateAccountIdentityFromUserToTwoUsers() { _resourceGroup = await CreateResourceGroupAsync(); StorageAccountCollection storageAccountCollection = _resourceGroup.GetStorageAccounts(); string accountName = await CreateValidAccountNameAsync(namePrefix); var identity = new ManagedServiceIdentity(ManagedServiceIdentityType.UserAssigned); var userAssignedIdentity = await CreateUserAssignedIdentityAsync(); identity.UserAssignedIdentities.Add(userAssignedIdentity.Id, new UserAssignedIdentity()); var param = GetDefaultStorageAccountParameters(sku: new StorageSku(StorageSkuName.StandardLRS), identity: identity); StorageAccountResource account1 = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, param)).Value; Assert.AreEqual(accountName, account1.Id.Name); VerifyAccountProperties(account1, false); Assert.AreEqual(ManagedServiceIdentityType.UserAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account1.Data.Identity.UserAssignedIdentities.Count, 1); Assert.Null(account1.Data.Identity.PrincipalId); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity.Id].PrincipalId); // With JSON Merge Patch, we only need to put the identity to add in the dictionary for update operation. var identity2 = new ManagedServiceIdentity(ManagedServiceIdentityType.UserAssigned); var userAssignedIdentity2 = await CreateUserAssignedIdentityAsync(); identity2.UserAssignedIdentities.Add(userAssignedIdentity2.Id, new UserAssignedIdentity()); StorageAccountPatch parameters = new StorageAccountPatch() { Identity = identity2 }; account1 = await account1.UpdateAsync(parameters); Assert.AreEqual(ManagedServiceIdentityType.UserAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account1.Data.Identity.UserAssignedIdentities.Count, 2); Assert.Null(account1.Data.Identity.PrincipalId); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity.Id].PrincipalId); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity2.Id].PrincipalId); // validate StorageAccountResource account2 = await storageAccountCollection.GetAsync(accountName); Assert.AreEqual(ManagedServiceIdentityType.UserAssigned, account2.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account2.Data.Identity.UserAssignedIdentities.Count, 2); Assert.Null(account2.Data.Identity.PrincipalId); Assert.NotNull(account2.Data.Identity.UserAssignedIdentities[userAssignedIdentity.Id].PrincipalId); Assert.NotNull(account2.Data.Identity.UserAssignedIdentities[userAssignedIdentity2.Id].PrincipalId); }
public async Task UpdateAccountIdentityFromTwoUsersToOneUser() { _resourceGroup = await CreateResourceGroupAsync(); StorageAccountCollection storageAccountCollection = _resourceGroup.GetStorageAccounts(); string accountName = await CreateValidAccountNameAsync(namePrefix); var identity = new ManagedServiceIdentity(ManagedServiceIdentityType.UserAssigned); var userAssignedIdentity1 = await CreateUserAssignedIdentityAsync(); identity.UserAssignedIdentities.Add(userAssignedIdentity1.Id, new UserAssignedIdentity()); var userAssignedIdentity2 = await CreateUserAssignedIdentityAsync(); identity.UserAssignedIdentities.Add(userAssignedIdentity2.Id, new UserAssignedIdentity()); var param = GetDefaultStorageAccountParameters(sku: new StorageSku(StorageSkuName.StandardLRS), identity: identity); StorageAccountResource account1 = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, param)).Value; Assert.AreEqual(accountName, account1.Id.Name); VerifyAccountProperties(account1, false); Assert.AreEqual(ManagedServiceIdentityType.UserAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account1.Data.Identity.UserAssignedIdentities.Count, 2); Assert.Null(account1.Data.Identity.PrincipalId); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity1.Id].PrincipalId); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity2.Id].PrincipalId); account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity1.Id] = null; StorageAccountPatch parameters = new StorageAccountPatch() { Identity = account1.Data.Identity }; account1 = await account1.UpdateAsync(parameters); Assert.AreEqual(ManagedServiceIdentityType.UserAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account1.Data.Identity.UserAssignedIdentities.Count, 1); Assert.Null(account1.Data.Identity.PrincipalId); Assert.Null(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity1.Id]); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity2.Id].PrincipalId); // validate StorageAccountResource account2 = await storageAccountCollection.GetAsync(accountName); Assert.AreEqual(ManagedServiceIdentityType.UserAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account1.Data.Identity.UserAssignedIdentities.Count, 1); Assert.Null(account1.Data.Identity.PrincipalId); Assert.IsFalse(account1.Data.Identity.UserAssignedIdentities.ContainsKey(userAssignedIdentity1.Id)); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity2.Id].PrincipalId); }
public async Task PITR() { //update storage account to v2 StorageAccountPatch updateParameters = new StorageAccountPatch() { Kind = StorageKind.StorageV2 }; _storageAccount = await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); BlobServiceData properties = _blobService.Data; properties.DeleteRetentionPolicy = new DeleteRetentionPolicy(); properties.DeleteRetentionPolicy.Enabled = true; properties.DeleteRetentionPolicy.Days = 30; properties.ChangeFeed = new ChangeFeed(); properties.ChangeFeed.Enabled = true; properties.IsVersioningEnabled = true; properties.RestorePolicy = new RestorePolicyProperties(true) { Days = 5 }; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties)).Value; if (Mode != RecordedTestMode.Playback) { await Task.Delay(10000); } //create restore ranges //start restore BlobRestoreContent restoreContent = new BlobRestoreContent( Recording.Now.AddSeconds(-1).ToUniversalTime(), new List <BlobRestoreRange>() { new BlobRestoreRange("", "container1/blob1"), new BlobRestoreRange("container1/blob2", "container2/blob3"), new BlobRestoreRange("container3/blob3", "") }); ArmOperation <BlobRestoreStatus> restoreOperation = await _storageAccount.RestoreBlobRangesAsync(WaitUntil.Started, restoreContent); //wait for restore completion BlobRestoreStatus restoreStatus = await restoreOperation.WaitForCompletionAsync(); Assert.IsTrue(restoreStatus.Status == BlobRestoreProgressStatus.Complete || restoreStatus.Status == BlobRestoreProgressStatus.InProgress); }
public void CreateStorageAccount() { #endif string accountName = "myaccount"; string resourceGroupName = "myResourceGroup"; ArmClient client = new ArmClient(new DefaultAzureCredential()); ResourceGroupResource resourceGroup = client.GetDefaultSubscription().GetResourceGroups().Get(resourceGroupName); StorageAccountCollection storageAccountCollection = resourceGroup.GetStorageAccounts(); StorageSku sku = new StorageSku(StorageSkuName.PremiumLRS); StorageAccountCreateOrUpdateContent parameters = new StorageAccountCreateOrUpdateContent(new StorageSku(StorageSkuName.StandardGRS), StorageKind.Storage, AzureLocation.WestUS); parameters.Tags.Add("key1", "value1"); parameters.Tags.Add("key2", "value2"); StorageAccountResource account = storageAccountCollection.CreateOrUpdate(WaitUntil.Completed, accountName, parameters).Value; #endregion }
public async Task CreateOrUpdate() { #region Snippet:Managing_StorageAccounts_CreateStorageAccount //first we need to define the StorageAccountCreateParameters StorageSku sku = new StorageSku(StorageSkuName.StandardGRS); StorageKind kind = StorageKind.Storage; string location = "westus2"; StorageAccountCreateOrUpdateContent parameters = new StorageAccountCreateOrUpdateContent(sku, kind, location); //now we can create a storage account with defined account name and parameters StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts(); string accountName = "myAccount"; ArmOperation <StorageAccountResource> accountCreateOperation = await accountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, parameters); StorageAccountResource storageAccount = accountCreateOperation.Value; #endregion }
public async Task CreateAccountWithSystemAndUserAssignedIdentity() { _resourceGroup = await CreateResourceGroupAsync(); StorageAccountCollection storageAccountCollection = _resourceGroup.GetStorageAccounts(); string accountName = await CreateValidAccountNameAsync(namePrefix); var identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssignedUserAssigned); var userAssignedIdentity = await CreateUserAssignedIdentityAsync(); identity.UserAssignedIdentities.Add(userAssignedIdentity.Id, new UserAssignedIdentity()); var param = GetDefaultStorageAccountParameters(sku: new StorageSku(StorageSkuName.StandardLRS), identity: identity); StorageAccountResource account1 = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, param)).Value; Assert.AreEqual(accountName, account1.Id.Name); VerifyAccountProperties(account1, false); Assert.AreEqual(ManagedServiceIdentityType.SystemAssignedUserAssigned, account1.Data.Identity.ManagedServiceIdentityType); Assert.AreEqual(account1.Data.Identity.UserAssignedIdentities.Count, 1); Assert.NotNull(account1.Data.Identity.PrincipalId); Assert.NotNull(account1.Data.Identity.UserAssignedIdentities[userAssignedIdentity.Id].PrincipalId); }
public async Task CreateGetDeleteObjectReplicationPolicy() { //create 2 storage accounts string accountName1 = await CreateValidAccountNameAsync("teststoragemgmt"); string accountName2 = await CreateValidAccountNameAsync("teststoragemgmt"); StorageAccountCreateParameters createParameters = GetDefaultStorageAccountParameters(kind: StorageKind.StorageV2); StorageAccountResource sourceAccount = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(WaitUntil.Completed, accountName1, createParameters)).Value; StorageAccountResource destAccount = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(WaitUntil.Completed, accountName2, createParameters)).Value; //update 2 accounts properties var updateparameter = new PatchableStorageAccountData { AllowCrossTenantReplication = true, EnableHttpsTrafficOnly = true }; destAccount = await destAccount.UpdateAsync(updateparameter); sourceAccount = await sourceAccount.UpdateAsync(updateparameter); BlobServiceResource blobService1 = await destAccount.GetBlobService().GetAsync(); BlobContainerCollection blobContainerCollection1 = blobService1.GetBlobContainers(); BlobServiceResource blobService2 = await destAccount.GetBlobService().GetAsync(); BlobContainerCollection blobContainerCollection2 = blobService2.GetBlobContainers(); //enable changefeed and versoning blobService1.Data.IsVersioningEnabled = true; await blobService1.CreateOrUpdateAsync(WaitUntil.Completed, blobService1.Data); //create 2 pairs of source and dest blob containers string containerName1 = Recording.GenerateAssetName("testblob1"); string containerName2 = Recording.GenerateAssetName("testblob2"); string containerName3 = Recording.GenerateAssetName("testblob3"); string containerName4 = Recording.GenerateAssetName("testblob4"); BlobContainerResource container1 = (await blobContainerCollection1.CreateOrUpdateAsync(WaitUntil.Completed, containerName1, new BlobContainerData())).Value; BlobContainerResource container2 = (await blobContainerCollection2.CreateOrUpdateAsync(WaitUntil.Completed, containerName2, new BlobContainerData())).Value; BlobContainerResource container3 = (await blobContainerCollection1.CreateOrUpdateAsync(WaitUntil.Completed, containerName3, new BlobContainerData())).Value; BlobContainerResource container4 = (await blobContainerCollection2.CreateOrUpdateAsync(WaitUntil.Completed, containerName4, new BlobContainerData())).Value; //prepare rules and policy ObjectReplicationPolicyData parameter = new ObjectReplicationPolicyData() { SourceAccount = sourceAccount.Id.Name, DestinationAccount = destAccount.Id.Name }; List <string> prefix = new List <string>(); prefix.Add("aa"); prefix.Add("bc d"); prefix.Add("123"); string minCreationTime = "2021-03-19T16:06:00Z"; List <ObjectReplicationPolicyRule> rules = new List <ObjectReplicationPolicyRule>(); parameter.Rules.Add( new ObjectReplicationPolicyRule(containerName1, containerName2) { Filters = new ObjectReplicationPolicyFilter(prefix, minCreationTime), } ); parameter.Rules.Add( new ObjectReplicationPolicyRule(containerName3, containerName4) ); //create policy ObjectReplicationPolicyCollection objectReplicationPolicyCollection = destAccount.GetObjectReplicationPolicies(); ObjectReplicationPolicyResource objectReplicationPolicy = (await objectReplicationPolicyCollection.CreateOrUpdateAsync(WaitUntil.Completed, "default", parameter)).Value; Assert.NotNull(objectReplicationPolicy); Assert.AreEqual(objectReplicationPolicy.Data.DestinationAccount, destAccount.Id.Name); Assert.AreEqual(objectReplicationPolicy.Data.SourceAccount, sourceAccount.Id.Name); //get policy List <ObjectReplicationPolicyResource> policies = await objectReplicationPolicyCollection.GetAllAsync().ToEnumerableAsync(); objectReplicationPolicy = policies[0]; Assert.NotNull(objectReplicationPolicy); Assert.AreEqual(objectReplicationPolicy.Data.DestinationAccount, destAccount.Id.Name); Assert.AreEqual(objectReplicationPolicy.Data.SourceAccount, sourceAccount.Id.Name); //delete policy await objectReplicationPolicy.DeleteAsync(WaitUntil.Completed); }
public static void AssertStorageAccountEqual(StorageAccountResource account1, StorageAccountResource account2) { Assert.AreEqual(account1.Id.Name, account2.Id.Name); Assert.AreEqual(account1.Id.Location, account2.Id.Location); }
public async Task PrivateDnsZoneGroupTest() { virtualNetwork = (await createVirtualNetwork()).Value; storageAccount = await createStorageAccount(); // create var privateEndpointCollection = resourceGroup.GetPrivateEndpoints(); var name = Recording.GenerateAssetName("pe"); System.Console.WriteLine($"SubnetResource ID: {virtualNetwork.Data.Subnets[0].Id}"); var privateEndpointData = new PrivateEndpointData { Location = TestEnvironment.Location, Subnet = virtualNetwork.Data.Subnets[0], PrivateLinkServiceConnections = { new PrivateLinkServiceConnection { Name = Recording.GenerateAssetName("pec"), // TODO: externalize or create the service on-demand, like virtual network //PrivateLinkServiceId = "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourceGroups/sdktest7669/providers/Microsoft.KeyVault/vaults/TierRuanKeyVaultJustTest", PrivateLinkServiceId = storageAccount.Id, RequestMessage = "SDK test", GroupIds = { "storage" } } }, }; var privateEndpoint = (await privateEndpointCollection.CreateOrUpdateAsync(WaitUntil.Completed, name, privateEndpointData)).Value; var privateDnsZoneName = Recording.GenerateAssetName("private_dns_zone"); var privateDnsZoneResourceId = new ResourceIdentifier($"/subscriptions/{TestEnvironment.SubscriptionId}/resourceGroups/{resourceGroup.Data.Name}/Microsoft.Network/privateDnsZones/{privateDnsZoneName}"); privateDnsZone = ArmClient.GetGenericResources().CreateOrUpdate(WaitUntil.Completed, privateDnsZoneResourceId, new GenericResourceData(TestEnvironment.Location)).Value; var privateDnsZoneGroupName = Recording.GenerateAssetName("private_dns_zone_group"); var privateDnsZoneGroupCollection = privateEndpoint.GetPrivateDnsZoneGroups(); var privateDnsZoneGroup = (await privateDnsZoneGroupCollection.CreateOrUpdateAsync(WaitUntil.Completed, privateDnsZoneGroupName, new PrivateDnsZoneGroupData { PrivateDnsZoneConfigs = { new PrivateDnsZoneConfig { Name = privateDnsZoneName, PrivateDnsZoneId = privateDnsZone.Id, } } })).Value; Assert.IsNotEmpty(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs); Assert.That(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs, Has.Count.EqualTo(1)); Assert.AreEqual(privateDnsZoneName, privateDnsZoneGroup.Data.PrivateDnsZoneConfigs[0].Name); Assert.AreEqual(privateDnsZone.Id, privateDnsZoneGroup.Data.PrivateDnsZoneConfigs[0].PrivateDnsZoneId); // list var groups = (await privateDnsZoneGroupCollection.GetAllAsync().ToEnumerableAsync()); Assert.That(groups, Has.Count.EqualTo(1)); privateDnsZoneGroup = groups[0]; Assert.IsNotEmpty(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs); Assert.That(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs, Has.Count.EqualTo(1)); Assert.AreEqual(privateDnsZoneName, privateDnsZoneGroup.Data.PrivateDnsZoneConfigs[0].Name); Assert.AreEqual(privateDnsZone.Id, privateDnsZoneGroup.Data.PrivateDnsZoneConfigs[0].PrivateDnsZoneId); // get privateDnsZoneGroup = (await privateDnsZoneGroupCollection.GetAsync(privateDnsZoneGroupName)).Value; Assert.IsNotEmpty(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs); Assert.That(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs, Has.Count.EqualTo(1)); Assert.AreEqual(privateDnsZoneName, privateDnsZoneGroup.Data.PrivateDnsZoneConfigs[0].Name); Assert.AreEqual(privateDnsZone.Id, privateDnsZoneGroup.Data.PrivateDnsZoneConfigs[0].PrivateDnsZoneId); // update privateDnsZoneGroup = (await privateDnsZoneGroupCollection.CreateOrUpdateAsync(WaitUntil.Completed, privateDnsZoneGroupName, new PrivateDnsZoneGroupData { })).Value; Assert.IsEmpty(privateDnsZoneGroup.Data.PrivateDnsZoneConfigs); // delete await privateDnsZoneGroup.DeleteAsync(WaitUntil.Completed); // list again groups = (await privateDnsZoneGroupCollection.GetAllAsync().ToEnumerableAsync()); Assert.IsEmpty(groups); await privateEndpoint.DeleteAsync(WaitUntil.Completed); }
public async Task PrivateEndpointTest() { virtualNetwork = (await createVirtualNetwork()).Value; storageAccount = await createStorageAccount(); // create var privateEndpointCollection = resourceGroup.GetPrivateEndpoints(); var name = Recording.GenerateAssetName("pe"); System.Console.WriteLine($"SubnetResource ID: {virtualNetwork.Data.Subnets[0].Id}"); var privateEndpointData = new PrivateEndpointData { Location = TestEnvironment.Location, Subnet = virtualNetwork.Data.Subnets[0], PrivateLinkServiceConnections = { new PrivateLinkServiceConnection { Name = Recording.GenerateAssetName("pec"), // TODO: externalize or create the service on-demand, like virtual network //PrivateLinkServiceId = $"/subscriptions/{TestEnvironment.SubscriptionId}/resourceGroups/{resourceGroup.Data.Name}/providers/Microsoft.Storage/storageAccounts/{storageAccount.Name}", PrivateLinkServiceId = storageAccount.Id, RequestMessage = "SDK test", GroupIds = { "storage" } } }, }; var privateEndpoint = (await privateEndpointCollection.CreateOrUpdateAsync(WaitUntil.Completed, name, privateEndpointData)).Value; Assert.AreEqual(name, privateEndpoint.Data.Name); Assert.AreEqual(TestEnvironment.Location, privateEndpoint.Data.Location); Assert.IsEmpty(privateEndpoint.Data.Tags); // get privateEndpoint = (await privateEndpointCollection.GetAsync(name)).Value; Assert.AreEqual(name, privateEndpoint.Data.Name); Assert.AreEqual(TestEnvironment.Location, privateEndpoint.Data.Location); Assert.IsEmpty(privateEndpoint.Data.Tags); // update privateEndpointData.Tags.Add("test", "test"); privateEndpoint = (await privateEndpointCollection.CreateOrUpdateAsync(WaitUntil.Completed, name, privateEndpointData)).Value; Assert.AreEqual(name, privateEndpoint.Data.Name); Assert.AreEqual(TestEnvironment.Location, privateEndpoint.Data.Location); Assert.That(privateEndpoint.Data.Tags, Has.Count.EqualTo(1)); Assert.That(privateEndpoint.Data.Tags, Does.ContainKey("test").WithValue("test")); // list var privateEndpoints = (await privateEndpointCollection.GetAllAsync().ToEnumerableAsync()); Assert.That(privateEndpoints, Has.Count.EqualTo(1)); Assert.AreEqual(name, privateEndpoint.Data.Name); // delete await privateEndpoint.DeleteAsync(WaitUntil.Completed); // list all privateEndpoints = (await _subscription.GetPrivateEndpointsAsync().ToEnumerableAsync()); Assert.That(privateEndpoints, Has.None.Matches <PrivateEndpointResource>(p => p.Data.Name == name)); }
private object DeployStorageAccessPoliciesAsync(OperationRunner context) { AzureClient client = new AzureClient(WizardContext.TokenProvider); client.SetLogger(context.Logger); // Create shared access signatures StorageAccountResource result = client.GetResourceAsync <StorageAccountResource>( DataModel.InstallationConfiguration.Azure.SelectedSubscription.Id, DataModel.InstallationConfiguration.Azure.ResourceGroupName, "Microsoft.Storage", null, "storageAccounts", DataModel.InstallationConfiguration.Azure.StorageAccount.StorageAccountName, "2019-04-01").Result; if (result == null) { throw new Exception("Could not acquire storage account!"); } ListKeysResponse accessKeys = client.InvokeResourceAction2Async <ListKeysResponse>( result.Id, "listkeys", string.Empty, "2019-04-01").Result; if (accessKeys == null || accessKeys.Keys.Count() == 0) { throw new Exception("Could not acquire storage account access key!"); } NetworkCredential accessKey = new NetworkCredential(string.Empty, accessKeys.Keys[0].Value); DataModel.InstallationConfiguration.Azure.StorageAccount.FullAccessPolicyId = Convert.ToBase64String( Encoding.UTF8.GetBytes( Guid.NewGuid().ToString())); DataModel.InstallationConfiguration.Azure.StorageAccount.ReadAccessPolicyId = Convert.ToBase64String( Encoding.UTF8.GetBytes( Guid.NewGuid().ToString())); string accessPolicy = client.CreateBlobStoredAccessPolicyAsync( DataModel.InstallationConfiguration.Azure.StorageAccount.StorageAccountName, StorageAccountConfiguration.RecordingsContainerName, new SignedIdentifiers() { SignedIdentifier = new SignedIdentifier[] { new SignedIdentifier() { Id = DataModel.InstallationConfiguration.Azure.StorageAccount.FullAccessPolicyId, AccessPolicy = new AccessPolicy() { Start = DateTime.UtcNow.ToString("o"), Expiry = DateTime.UtcNow.AddYears(1).ToString("o"), Permission = "rwd", }, }, new SignedIdentifier() { Id = DataModel.InstallationConfiguration.Azure.StorageAccount.ReadAccessPolicyId, AccessPolicy = new AccessPolicy() { Start = DateTime.UtcNow.ToString("o"), Expiry = DateTime.UtcNow.AddYears(1).ToString("o"), Permission = "r", }, }, }, }).Result; if (accessPolicy == null) { throw new Exception("Could not create stored access policies on Azure Blob container!"); } string fullSharedAccessSignature = client.CreateSharedAccessSignature( DataModel.InstallationConfiguration.Azure.StorageAccount.StorageAccountName, StorageAccountConfiguration.RecordingsContainerName, accessKey, DataModel.InstallationConfiguration.Azure.StorageAccount.FullAccessPolicyId, new SharedAccessBlobPolicy()); if (fullSharedAccessSignature.StartsWith("?")) { fullSharedAccessSignature = fullSharedAccessSignature.Substring(1); } AzureKeyVaultSecret fullAccessSecret = client.UpdateKeyVaultSecretAsync( DataModel.InstallationConfiguration.Azure.KeyVault.KeyVaultName, DataModel.InstallationConfiguration.Azure.KeyVault.StorageAccessKeySecretName, new NetworkCredential("full", fullSharedAccessSignature)).Result; string readSharedAccessSignature = client.CreateSharedAccessSignature( DataModel.InstallationConfiguration.Azure.StorageAccount.StorageAccountName, StorageAccountConfiguration.RecordingsContainerName, accessKey, DataModel.InstallationConfiguration.Azure.StorageAccount.ReadAccessPolicyId, new SharedAccessBlobPolicy()); if (readSharedAccessSignature.StartsWith("?")) { readSharedAccessSignature = readSharedAccessSignature.Substring(1); } AzureKeyVaultSecret readAccessSecret = client.UpdateKeyVaultSecretAsync( DataModel.InstallationConfiguration.Azure.KeyVault.KeyVaultName, DataModel.InstallationConfiguration.Azure.KeyVault.StorageReadAccessKeySecretName, new NetworkCredential("read", readSharedAccessSignature)).Result; return(null); }