Exemplo n.º 1
0
        public override async Task SetUp()
        {
            if (Environment.Mode != RecordedTestMode.Playback)
            {
                _config = TestConfigurations.DefaultTargetManagedDisk;

                TokenCredential tokenCredentials = new Identity.ClientSecretCredential(
                    _config.ActiveDirectoryTenantId, _config.ActiveDirectoryApplicationId, _config.ActiveDirectoryApplicationSecret);

                ArmClient            client       = new ArmClient(tokenCredentials, _config.SubsriptionId);
                SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();

                _resourceGroup = await subscription.GetResourceGroups().GetAsync(_config.ResourceGroupName);

                var disks = await _resourceGroup.GetDisks().GetAllAsync().ToListAsync();

                var disk = disks.Where(d => d.Data.Name.Contains(_config.DiskNamePrefix)).First();

                Snapshot1 = await CreateSnapshot(disk, _config.DiskNamePrefix + Guid.NewGuid().ToString().Replace("-", ""));

                // The disk is attached to VM, wait some time to let OS background jobs write something to disk to create delta.
                await Task.Delay(TimeSpan.FromSeconds(60));

                Snapshot2 = await CreateSnapshot(disk, _config.DiskNamePrefix + Guid.NewGuid().ToString().Replace("-", ""));

                Snapshot1SASUri = await GrantAccess(Snapshot1);

                Snapshot2SASUri = await GrantAccess(Snapshot2);
            }

            Instance = this;
        }
        public async Task CreateDisk()
        {
            #region Snippet:Managing_Disks_CreateADisk
            ArmClient            armClient    = new ArmClient(new DefaultAzureCredential());
            SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();

            // first we need to get the resource group
            string rgName = "myRgName";
            ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);

            // Now we get the disk collection from the resource group
            DiskCollection diskCollection = resourceGroup.GetDisks();
            // Use the same location as the resource group
            string diskName = "myDisk";
            var    input    = new DiskData(resourceGroup.Data.Location)
            {
                Sku = new DiskSku()
                {
                    Name = DiskStorageAccountTypes.StandardLRS
                },
                CreationData = new CreationData(DiskCreateOption.Empty),
                DiskSizeGB   = 1,
            };
            ArmOperation <DiskResource> lro = await diskCollection.CreateOrUpdateAsync(WaitUntil.Completed, diskName, input);

            DiskResource disk = lro.Value;
            #endregion Snippet:Managing_Disks_CreateADisk
        }
        public async Task DeleteDisk()
        {
            #region Snippet:Managing_Disks_DeleteDisk
            ArmClient            armClient    = new ArmClient(new DefaultAzureCredential());
            SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();

            // first we need to get the resource group
            string rgName = "myRgName";
            ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);

            // Now we get the disk collection from the resource group
            DiskCollection diskCollection = resourceGroup.GetDisks();
            string         diskName       = "myDisk";
            DiskResource   disk           = await diskCollection.GetAsync(diskName);

            await disk.DeleteAsync(WaitUntil.Completed);

            #endregion Snippet:Managing_Disks_DeleteDisk
        }
        public async Task ListDisks()
        {
            #region Snippet:Managing_Disks_ListAllDisks
            ArmClient            armClient    = new ArmClient(new DefaultAzureCredential());
            SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();

            // first we need to get the resource group
            string rgName = "myRgName";
            ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);

            // Now we get the disk collection from the resource group
            DiskCollection diskCollection = resourceGroup.GetDisks();
            // With ListAsync(), we can get a list of the disks
            AsyncPageable <DiskResource> response = diskCollection.GetAllAsync();
            await foreach (DiskResource disk in response)
            {
                Console.WriteLine(disk.Data.Name);
            }
            #endregion Snippet:Managing_Disks_ListAllDisks
        }