aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsComponentBase.cs
blob: 813a990116d7f303a876e3e9e6b8baf231eb28ce (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
using Microsoft.Azure.Management.Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Logging;

namespace Tango.AzureUtils
{
    public abstract class AzureUtilsComponentBase : ExtendedObject
    {
        protected IAzure Azure { get; private set; }

        public event EventHandler<AzureUtilsProgressEventArgs> Progress;
        public event EventHandler<AzureUtilsConfirmationEventArgs> ConfirmationRequired;

        public AzureUtilsComponentBase(IAzure azure)
        {
            Azure = azure;
        }

        protected virtual void OnProgress(AzureUtilsStage stage, String message = null, double progress = 0, double maximum = 100, bool indeterminate = true)
        {
            LogManager.Log($"{stage}: {message}");

            Progress?.Invoke(this, new AzureUtilsProgressEventArgs()
            {
                Stage = stage,
                Message = message,
                Progress = progress,
                Maximum = maximum,
                IsIndeterminate = indeterminate,
            });
        }

        protected virtual void OnCompleted(String message)
        {
            OnProgress(AzureUtilsStage.Ready, message, 0, 100, false);
        }

        protected virtual void OnProgress(AzureUtilsProgressEventArgs e)
        {
            Progress?.Invoke(this, e);
        }

        protected async Task<bool> RequestConfirmation(String message)
        {
            LogManager.Log($"Confirmation Required: {message}");

            AzureUtilsConfirmationEventArgs e = new AzureUtilsConfirmationEventArgs(message);
            if (ConfirmationRequired != null)
            {
                OnProgress(AzureUtilsStage.ConfirmationRequired, "Waiting for user confirmation...");

                OnRequestConfirmation(e);
                var result = await e.GetAwaiter();

                if (!result)
                {
                    throw new OperationCanceledException("Operation canceled.");
                }

                return result;
            }
            else
            {
                throw new OperationCanceledException($"Confirmation required no event handler found. ({message})");
            }
        }

        protected void OnRequestConfirmation(AzureUtilsConfirmationEventArgs e)
        {
            ConfirmationRequired?.Invoke(this, e);
        }

        protected T CreateManager<T>() where T : AzureUtilsComponentBase
        {
            T manager = Activator.CreateInstance(typeof(T), new object[] { Azure }) as T;
            manager.ConfirmationRequired += (x, e) => OnRequestConfirmation(e);
            manager.Progress += (x, e) => OnProgress(e);
            return manager;
        }
    }
}