示例#1
0
 public void ApplySnapshot(VMSnapshot snapshot)
 {
     if (MessageBox.Show("Are you shure you want to apply this snapshot? Current settings will be lost", snapshot.ElementName,
                         MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes) == MessageBoxResult.Yes)
     {
         model.ApplySnapshot(snapshot, ActiveVMList);
     }
 }
示例#2
0
 private void RemoveSnapshotTree(VMSnapshot snapshot)
 {
     if (MessageBox.Show("Are you shure you want to remove this snapshot and all it's child snapshots?", snapshot.ElementName,
                         MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes)
     {
         model.RemoveSnapshotTree(snapshot, ActiveVMList);
     }
 }
示例#3
0
 public void RemoveSnapshot(VMSnapshot snapshot)
 {
     if (MessageBox.Show("Are you shure you want to remove this snapshot?", snapshot.ElementName,
                         MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes)
     {
         model.RemoveSnapshot(snapshot, ActiveVMList);
     }
 }
        /// <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 or update a snapshot. If the snapshot already exists, it is replaced.
        /// </summary>
        /// <param name="bearerToken">Authorization bearer token</param>
        /// <param name="virtualMachineSnapshot">The prepopulated Virtual Machine Snapshot object</param>
        /// <returns>VMSnapshot object if successful, else NULL</returns>
        public static async Task <VMSnapshot?> Take(string bearerToken, VMSnapshot virtualMachineSnapshot)
        {
            if (string.IsNullOrWhiteSpace(bearerToken))
            {
                throw new ArgumentNullException(nameof(virtualMachineSnapshot));
            }
            if (virtualMachineSnapshot == null)
            {
                throw new ArgumentNullException(nameof(virtualMachineSnapshot));
            }

            RestApiResponse response = await RestApiClient.PUT(
                bearerToken,
                $"https://management.azure.com/{virtualMachineSnapshot.ResourceId[1..]}",
        /// <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));
        }
示例#7
0
        public VMSnapshotViewModel(VMSnapshot _snapshot, VM vm, VMModel _model)
            : base()
        {
            this.model            = _model;
            this.snapshotInstance = _snapshot;
            this.ElementName      = _snapshot.ElementName;
            this.children         = new ObservableCollection <VMSnapshotViewModel>();
            foreach (VMSnapshot child in this.snapshotInstance.ChildSnapshots)
            {
                this.AddChild(new VMSnapshotViewModel(child, vm, model));
            }
            isSelected = false;
            //check if this snapshot is last applied
            VMSnapshot lastAppliedSnapshot = new VMSnapshot(Utility.GetLastAppliedVirtualSystemSnapshot(vm.Instance));

            if (lastAppliedSnapshot.ElementName == this.snapshotInstance.ElementName)
            {
                this.IsLastApplied = true;
            }
            else
            {
                this.IsLastApplied = false;
            }
        }
示例#8
0
 private bool CanRemoveSnapshotTree(VMSnapshot snapshot)
 {
     return(model.CanRemoveSnapshotTree(snapshot, ActiveVMList));
 }
示例#9
0
 public bool CanRemoveSnapshot(VMSnapshot snapshot)
 {
     return(model.CanRemoveSnapshot(snapshot, ActiveVMList));
 }
示例#10
0
        public VMCommandsViewModel(VMModel model)
        {
            this.model           = model;
            this.ActiveVMList    = model.ActiveVMList;
            VMListStart          = new DelegateCommand <ObservableCollection <VM> >(StartVMList, CanStartVMList);
            VMListStop           = new DelegateCommand <ObservableCollection <VM> >(StopVMList, CanStopVMList);
            VMListShutdown       = new DelegateCommand <ObservableCollection <VM> >(ShutdownVMList, CanShutdownVMList);
            VMListSuspend        = new DelegateCommand <ObservableCollection <VM> >(SuspendVMList, CanSuspendVMList);
            VMListPause          = new DelegateCommand <ObservableCollection <VM> >(PauseVMList, CanPauseVMList);
            VMListReboot         = new DelegateCommand <ObservableCollection <VM> >(RebootVMList, CanRebootVMList);
            VMConnect            = new DelegateCommand <ObservableCollection <VM> >(ConnectToVM, CanConnectToVM);
            VMListCreateSnapshot = new DelegateCommand <ObservableCollection <VM> >(CreateSnapshot, CanCreateSnapshot);
            VMSnapshotApply      = new DelegateCommand <VMSnapshot>(ApplySnapshot, CanApplySnapshot);
            VMSnapshotRemove     = new DelegateCommand <VMSnapshot>(RemoveSnapshot, CanRemoveSnapshot);
            VMSnapshotTreeRemove = new DelegateCommand <VMSnapshot>(RemoveSnapshotTree, CanRemoveSnapshotTree);
            MoveToVMGroup        = new DelegateCommand <object>(MoveToGroup, CanMoveToGroup);
            CanExecuteRaisers    = new List <Action> {
                vmListStart.RaiseCanExecuteChanged,
                vmListStop.RaiseCanExecuteChanged,
                vmListShutdown.RaiseCanExecuteChanged,
                vmListPause.RaiseCanExecuteChanged,
                vmListReboot.RaiseCanExecuteChanged,
                vmListSuspend.RaiseCanExecuteChanged,
                vmConnect.RaiseCanExecuteChanged,
                vmListCreateSnapshot.RaiseCanExecuteChanged,
                vmSnapshotApply.RaiseCanExecuteChanged,
                vmSnapshotRemove.RaiseCanExecuteChanged,
                vmSnapshotTreeRemove.RaiseCanExecuteChanged,
                moveToVMGroup.RaiseCanExecuteChanged,
            };
            model.SelectedTreeItemChanged +=
                (o, e) =>
            {
                this.ActiveItem = model.SelectedTreeItem;
            };
            model.ActiveVMGroupChanged +=
                (o, e) =>
            {
                this.ActiveVMGroup = model.ActiveVMGroup;
                RefreshCommands();
            };
            model.ActiveVMListChanged +=
                (o, e) =>
            {
                if (e.NewItems != null)
                {
                    this.ActiveVMList = model.ActiveVMList;
                    foreach (VM vm in e.NewItems)
                    {
                        vm.VMStatusChanged += new VMStatusChangedEventHandler(vm_VMStatusChanged);
                    }
                    if (e.OldItems != null)
                    {
                        foreach (VM vm in e.OldItems)
                        {
                            vm.VMStatusChanged -= vm_VMStatusChanged;
                        }
                    }
                }
                RefreshCommands();
            };

            model.SelectedSnapshotItemChanged +=
                (o, e) =>
            {
                ActiveVMSnapshot = (VMSnapshot)model.SelectedSnapshotItem;
                RefreshCommands();
            };
            View = new VMCommandsView();
            View.SetViewModel(this);
        }