aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentCreationViewVM.cs
blob: 74995c16d8e5803f45d7eb12f02469dc2b05b53b (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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using Tango.AzureUtils.ActiveDirectory;
using Tango.AzureUtils.Deployment;
using Tango.AzureUtils.Environment;
using Tango.Core.Commands;

namespace Tango.AzureUtils.UI.ViewModels
{
    public class EnvironmentCreationViewVM : AzureDashboardViewModel
    {
        private IWebAppBase _machineServiceApp;
        private EnvironmentManager _environmentManager;

        private List<IDeploymentSlot> _deploymentSlots;
        public List<IDeploymentSlot> DeploymentSlots
        {
            get { return _deploymentSlots; }
            set { _deploymentSlots = value; RaisePropertyChangedAuto(); }
        }

        private IDeploymentSlot _selectedDeploymentSlot;
        public IDeploymentSlot SelectedDeploymentSlot
        {
            get { return _selectedDeploymentSlot; }
            set { _selectedDeploymentSlot = value; RaisePropertyChangedAuto(); }
        }

        private String _slotName;
        [Required(ErrorMessage = "Deployment slot name is required.")]
        public String SlotName
        {
            get { return _slotName; }
            set { _slotName = value; RaisePropertyChangedAuto(); }
        }

        private String _email;
        [Required(ErrorMessage = "Active directory email is required.")]
        [EmailAddress(ErrorMessage = "Please enter a valid email.")]
        public String Email
        {
            get { return _email; }
            set { _email = value; RaisePropertyChangedAuto(); }
        }

        private String _password;
        [Required(ErrorMessage = "Password is required.")]
        public String Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand CreateDeploymentSlotCommand { get; set; }

        public EnvironmentCreationViewVM()
        {
            CreateDeploymentSlotCommand = new RelayCommand(CreateDeploymentSlot);
        }

        public override void OnApplicationReady()
        {
            Email = "roy@twine-s.com";
        }

        public override void OnAuthenticated(IAzure azure, List<IWebAppBase> apps)
        {
            _machineServiceApp = apps.SingleOrDefault(x => x.Name == "MachineService");
            DeploymentSlots = apps.OfType<IDeploymentSlot>().Where(x => x.Parent == _machineServiceApp).ToList();
            SelectedDeploymentSlot = DeploymentSlots.FirstOrDefault();

            _environmentManager = new EnvironmentManager(azure);
            _environmentManager.ConfirmationRequired += _environmentManager_ConfirmationRequired;
            _environmentManager.Progress += (x, e) => StatusManager.UpdateStatus(e);
        }

        private void _environmentManager_ConfirmationRequired(object sender, AzureUtilsConfirmationEventArgs e)
        {
            if (MessageBox.Show(e.Message, "Confirmation Required", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                e.Confirm();
            }
            else
            {
                e.Cancel();
            }
        }

        private async void CreateDeploymentSlot()
        {
            try
            {
                if (!Validate()) return;

                IsFree = false;
                await _environmentManager.CreateDeploymentSlot(_machineServiceApp as IWebApp, SelectedDeploymentSlot, SlotName, Email, Password);
            }
            catch (Exception ex)
            {
                StatusManager.UpdateStatus(ex);
            }
            finally
            {
                IsFree = true;
            }
        }
    }
}