aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs
blob: 4e37be13f10810a90322c331c8020e9bf8944aad (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
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.AzureUtils
{
    public class AzureUtilsAuthenticationFactory
    {
        private static IAzure _azure;

        public static Task<IAzure> AuthenticateOrGetAsync(AzureUtilsCredentials creds)
        {
            if (_azure == null)
            {
                return Task.Factory.StartNew<IAzure>(() =>
                {
                    var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                        creds.ClientID,
                        creds.ClientSecret,
                        creds.TenantID,
                        AzureEnvironment.AzureGlobalCloud);

                    _azure = Azure.Authenticate(credentials).WithSubscription(creds.SubscriptionID);
                    return _azure;
                });
            }
            else
            {
                return Task.FromResult(_azure);
            }
        }
    }
}