aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentUpgradeViewVM.cs
blob: 3f353d54d089fd2238ec2c23ab16ef85e4234b72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.AzureUtils.Deployment;
using Tango.AzureUtils.Environment;
using Tango.Core.Commands;
using Tango.SharedUI;

namespace Tango.AzureUtils.UI.ViewModels
{
    public class EnvironmentUpgradeViewVM : AzureDashboardViewModel
    {
        private List<IWebAppBase> _apps;
        public List<IWebAppBase> Apps
        {
            get { return _apps; }
            set { _apps = value; RaisePropertyChangedAuto(); }
        }

        private IWebAppBase _selectedSourceApp;
        public IWebAppBase SelectedSourceApp
        {
            get { return _selectedSourceApp; }
            set { _selectedSourceApp = value; RaisePropertyChangedAuto(); }
        }

        private IWebAppBase _selectedTargetAPp;
        public IWebAppBase SelectedTargetApp
        {
            get { return _selectedTargetAPp; }
            set { _selectedTargetAPp = value; RaisePropertyChangedAuto(); }
        }

        private EnvironmentManager _environmentManager;
        public EnvironmentManager EnvironmentManager
        {
            get { return _environmentManager; }
            set { _environmentManager = value; RaisePropertyChangedAuto(); }
        }

        private bool _canUpgrade;
        public bool CanUpgrade
        {
            get { return _canUpgrade; }
            set { _canUpgrade = value; RaisePropertyChangedAuto(); }
        }

        private UpgradeEnvironmentConfiguration _config;
        public UpgradeEnvironmentConfiguration Config
        {
            get { return _config; }
            set { _config = value; RaisePropertyChangedAuto(); }
        }


        public RelayCommand ValidateUpgradeCommand { get; set; }

        public RelayCommand UpgradeEnvironmentCommand { get; set; }

        public EnvironmentUpgradeViewVM()
        {
            Config = new UpgradeEnvironmentConfiguration();
            ValidateUpgradeCommand = new RelayCommand(() => ValidateUpgrade());
            UpgradeEnvironmentCommand = new RelayCommand(() => UpgradeEnvironment());
        }

        public override void OnAuthenticated(IAzure azure, List<IWebAppBase> apps)
        {
            Apps = apps;

            SelectedSourceApp = Apps.FirstOrDefault();
            SelectedTargetApp = Apps.FirstOrDefault();

            EnvironmentManager = new EnvironmentManager(azure);
            _environmentManager.ConfirmationRequired += ConfirmationHandler;
            _environmentManager.Progress += ProgressHandler;
        }

        private async void ValidateUpgrade()
        {
            if (SelectedSourceApp == null || SelectedTargetApp == null)
            {
                return;
            }

            try
            {
                IsFree = false;
                StatusManager.UpdateStatus(AzureUtilsStage.Validating, "Validating configuration...", true);
                await EnvironmentManager.ValidateEnvironmentUpgrade(SelectedSourceApp, SelectedTargetApp, Config);
                CanUpgrade = true;
                StatusManager.UpdateStatus(AzureUtilsStage.Ready, "Configuration validated successfully.");
            }
            catch (Exception ex)
            {
                CanUpgrade = false;
                StatusManager.UpdateStatus(ex);
            }
            finally
            {
                IsFree = true;
            }
        }

        private async void UpgradeEnvironment()
        {
            try
            {
                IsFree = false;
                await EnvironmentManager.UpgradeEnvironment(SelectedSourceApp, SelectedTargetApp, Config);
            }
            catch (Exception ex)
            {
                StatusManager.UpdateStatus(ex);
            }
            finally
            {
                RequireRefresh();
                IsFree = true;
            }
        }

        protected override void OnRefreshRequired()
        {
            base.OnRefreshRequired();

            var oldSource = SelectedSourceApp;
            var oldTarget = SelectedTargetApp;
            SelectedSourceApp = null;
            SelectedTargetApp = null;
            SelectedSourceApp = oldSource;
            SelectedTargetApp = oldTarget;
        }
    }
}