/// <summary> /// Take a new snapshot /// </summary> /// <param name="bearerToken">Authorization bearer token</param> /// <param name="subscription">Guid of the subscription</param> /// <param name="resourceGroupName">Resource group (name) containing the virtual machine</param> /// <param name="virtualMachineName">Name of the virtual machine to create the snapshot into</param> /// <param name="snapshotName">Name of the snapshot</param> /// <param name="location">Azure data center geo location short/internal name (eg: "westus")</param> /// <param name="properties">Properties of the snapshot</param> /// <param name="tags">Tags (optional)</param> /// <returns>VMSnapshot object if successful, else NULL</returns> public static async Task <VMSnapshot?> Take(string bearerToken, Guid subscription, string resourceGroupName, string virtualMachineName, string snapshotName, string location, VMSnapshotProperties properties, Dictionary <string, string>?tags = null) { if (subscription == Guid.Empty) { throw new ArgumentNullException(nameof(subscription)); } if (string.IsNullOrWhiteSpace(resourceGroupName)) { throw new ArgumentNullException(nameof(resourceGroupName)); } if (string.IsNullOrWhiteSpace(virtualMachineName)) { throw new ArgumentNullException(nameof(virtualMachineName)); } if (string.IsNullOrWhiteSpace(snapshotName)) { throw new ArgumentNullException(nameof(snapshotName)); } if (string.IsNullOrWhiteSpace(location)) { throw new ArgumentNullException(nameof(location)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } string vmResourceUri = $"/{subscription.ToString("d")}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{virtualMachineName}/snapshots/{snapshotName}"; VMSnapshot snapshot = new VMSnapshot() { Location = location, ManagedBy = vmResourceUri, Name = snapshotName, ResourceId = $"/{vmResourceUri}/snapshots/{snapshotName}", Tags = tags, Type = "virtualMachines/snapshots", Properties = new VMSnapshotProperties() { CreationMetadata = properties.CreationMetadata, DiskSizeGB = properties.DiskSizeGB, ContentEncryptionProperties = properties.ContentEncryptionProperties, AzureDiskEncryptionSettings = properties.AzureDiskEncryptionSettings, HyperVGeneration = properties.HyperVGeneration, IsIncrementalSnapshot = properties.IsIncrementalSnapshot, OperatingSystem = properties.OperatingSystem } }; return(await Take(bearerToken, snapshot)); }
/// <summary> /// Take a new snapshot /// </summary> /// <param name="bearerToken">Authorization bearer token</param> /// <param name="snapshotName">Name of the snapshot</param> /// <param name="location">Azure data center geo location short/internal name (eg: "westus")</param> /// <param name="properties">Properties of the snapshot</param> /// <param name="tags">Tags (optional)</param> /// <returns>VMSnapshot object if successful, else NULL</returns> public static async Task <VMSnapshot?> Create(string bearerToken, string virtualMachineResourceUri, string snapshotName, string location, VMSnapshotProperties properties, Dictionary <string, string>?tags = null) { if (string.IsNullOrWhiteSpace(virtualMachineResourceUri)) { throw new ArgumentNullException(nameof(virtualMachineResourceUri)); } if (string.IsNullOrWhiteSpace(snapshotName)) { throw new ArgumentNullException(nameof(snapshotName)); } if (string.IsNullOrWhiteSpace(location)) { throw new ArgumentNullException(nameof(location)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } VMSnapshot snapshot = new VMSnapshot() { Location = location, ManagedBy = virtualMachineResourceUri, Name = snapshotName, ResourceId = $"{virtualMachineResourceUri}/snapshots/{snapshotName}", Tags = tags, Type = "virtualMachines/snapshots", Properties = new VMSnapshotProperties() { CreationMetadata = properties.CreationMetadata, DiskSizeGB = properties.DiskSizeGB, ContentEncryptionProperties = properties.ContentEncryptionProperties, AzureDiskEncryptionSettings = properties.AzureDiskEncryptionSettings, HyperVGeneration = properties.HyperVGeneration, IsIncrementalSnapshot = properties.IsIncrementalSnapshot, OperatingSystem = properties.OperatingSystem } }; return(await Take(bearerToken, snapshot)); }