/// <summary>
        /// Function used to wait for a specific ANF resource complete its deletion and ARM caching gets cleared
        /// </summary>
        /// <typeparam name="T">Resource Types as Snapshot, Volume, CapacityPool, and NetAppAccount</typeparam>
        /// <param name="client">ANF Client</param>
        /// <param name="resourceId">Resource Id of the resource being waited for being deleted</param>
        /// <param name="intervalInSec">Time in seconds that the sample will poll to check if the resource got deleted or not. Defaults to 10 seconds.</param>
        /// <param name="retries">How many retries before exting the wait for no resource function. Defaults to 60 retries.</param>
        /// <returns></returns>
        static public async Task WaitForNoAnfResource <T>(AzureNetAppFilesManagementClient client, string resourceId, int intervalInSec = 10, int retries = 60)
        {
            for (int i = 0; i < retries; i++)
            {
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(intervalInSec));

                try
                {
                    if (typeof(T) == typeof(Snapshot))
                    {
                        var resource = await client.Snapshots.GetAsync(ResourceUriUtils.GetResourceGroup(resourceId),
                                                                       ResourceUriUtils.GetAnfAccount(resourceId),
                                                                       ResourceUriUtils.GetAnfCapacityPool(resourceId),
                                                                       ResourceUriUtils.GetAnfVolume(resourceId),
                                                                       ResourceUriUtils.GetAnfSnapshot(resourceId));
                    }
                    else if (typeof(T) == typeof(Volume))
                    {
                        var resource = await client.Volumes.GetAsync(ResourceUriUtils.GetResourceGroup(resourceId),
                                                                     ResourceUriUtils.GetAnfAccount(resourceId),
                                                                     ResourceUriUtils.GetAnfCapacityPool(resourceId),
                                                                     ResourceUriUtils.GetAnfVolume(resourceId));
                    }
                    else if (typeof(T) == typeof(CapacityPool))
                    {
                        var resource = await client.Pools.GetAsync(ResourceUriUtils.GetResourceGroup(resourceId),
                                                                   ResourceUriUtils.GetAnfAccount(resourceId),
                                                                   ResourceUriUtils.GetAnfCapacityPool(resourceId));
                    }
                    else if (typeof(T) == typeof(NetAppAccount))
                    {
                        var resource = await client.Accounts.GetAsync(ResourceUriUtils.GetResourceGroup(resourceId),
                                                                      ResourceUriUtils.GetAnfAccount(resourceId));
                    }
                }
                catch (Exception ex)
                {
                    // The following HResult is thrown if no resource is found
                    if (ex.HResult == -2146233088)
                    {
                        break;
                    }
                    throw;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Executes Snapshot related operations
        /// </summary>
        /// <returns></returns>
        public static async Task RunSnapshotOperationsSampleAsync(ProjectConfiguration config, AzureNetAppFilesManagementClient anfClient)
        {
            //
            // Creating snapshot from first volume of the first capacity pool
            //
            Utils.WriteConsoleMessage("Performing snapshot operations");

            Utils.WriteConsoleMessage("\tCreating snapshot...");

            string snapshotName = $"Snapshot-{Guid.NewGuid()}";

            Snapshot snapshotBody = new Snapshot()
            {
                Location = config.Accounts[0].Location
            };

            Snapshot snapshot = null;

            try
            {
                snapshot = await anfClient.Snapshots.CreateAsync(
                    snapshotBody,
                    config.ResourceGroup,
                    config.Accounts[0].Name,
                    config.Accounts[0].CapacityPools[0].Name,
                    config.Accounts[0].CapacityPools[0].Volumes[0].Name,
                    snapshotName);

                Utils.WriteConsoleMessage($"Snapshot successfully created. Snapshot resource id: {snapshot.Id}");
            }
            catch (Exception ex)
            {
                Utils.WriteErrorMessage($"An error occured while creating a snapshot of volume {config.Accounts[0].CapacityPools[0].Volumes[0].Name}.\nError message: {ex.Message}");
                throw;
            }

            //
            // Creating a volume from snapshot
            //
            Utils.WriteConsoleMessage("\tCreating new volume from snapshot...");

            string newVolumeName = $"Vol-{ResourceUriUtils.GetAnfSnapshot(snapshot.Id)}";

            Volume snapshotVolume = null;

            try
            {
                snapshotVolume = await anfClient.Volumes.GetAsync(
                    ResourceUriUtils.GetResourceGroup(snapshot.Id),
                    ResourceUriUtils.GetAnfAccount(snapshot.Id),
                    ResourceUriUtils.GetAnfCapacityPool(snapshot.Id),
                    ResourceUriUtils.GetAnfVolume(snapshot.Id));
            }
            catch (Exception ex)
            {
                Utils.WriteErrorMessage($"An error occured trying to obtain information about volume ({ResourceUriUtils.GetAnfVolume(snapshot.Id)}) from snapshot {snapshot.Id}.\nError message: {ex.Message}");
                throw;
            }

            Volume newVolumeFromSnapshot = null;

            try
            {
                // Notice that SnapshotId is not the actual resource Id of the snapshot, this value is the unique identifier (guid) of
                // the snapshot, represented by the SnapshotId instead.
                Volume volumeFromSnapshotBody = new Volume()
                {
                    SnapshotId     = snapshot.SnapshotId,
                    ExportPolicy   = snapshotVolume.ExportPolicy,
                    Location       = snapshotVolume.Location,
                    ProtocolTypes  = snapshotVolume.ProtocolTypes,
                    ServiceLevel   = snapshotVolume.ServiceLevel,
                    UsageThreshold = snapshotVolume.UsageThreshold,
                    SubnetId       = snapshotVolume.SubnetId,
                    CreationToken  = newVolumeName
                };

                newVolumeFromSnapshot = await anfClient.Volumes.CreateOrUpdateAsync(
                    volumeFromSnapshotBody,
                    config.ResourceGroup,
                    config.Accounts[0].Name,
                    config.Accounts[0].CapacityPools[0].Name,
                    newVolumeName);

                Utils.WriteConsoleMessage($"Volume successfully created from snapshot. Volume resource id: {newVolumeFromSnapshot.Id}");
            }
            catch (Exception ex)
            {
                Utils.WriteErrorMessage($"An error occured while creating a volume ({newVolumeName}) from snapshot {snapshot.Id}.\nError message: {ex.Message}");
                throw;
            }
        }