summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Plugins/InstallationPlugins/IISWeb/IISWebOptionsFactory.cs
blob: 4a0dece7f3ee76334c580fee1985a9191683290a (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
using PKISharp.WACS.Clients.IIS;
using PKISharp.WACS.DomainObjects;
using PKISharp.WACS.Plugins.Base.Factories;
using PKISharp.WACS.Plugins.StorePlugins;
using PKISharp.WACS.Services;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PKISharp.WACS.Plugins.InstallationPlugins
{
    internal class IISWebOptionsFactory : InstallationPluginFactory<IISWeb, IISWebOptions>
    {
        public override int Order => 5;
        private readonly IIISClient _iisClient;
        private IArgumentsService _arguments;

        public IISWebOptionsFactory(IIISClient iisClient, IArgumentsService arguments)
        {
            _iisClient = iisClient;
            _arguments = arguments;
        }

        public override bool CanInstall(IEnumerable<Type> storeTypes)
        {
            return _iisClient.HasWebSites && 
                (storeTypes.Contains(typeof(CertificateStore)) ||
                 storeTypes.Contains(typeof(CentralSsl)));
        }

        public override IISWebOptions Aquire(Target target, IInputService inputService, RunLevel runLevel)
        {
            var args = _arguments.GetArguments<IISWebArguments>();
            var ret = new IISWebOptions(args);
            var ask = true;
            if (target.IIS)
            {
                if (runLevel.HasFlag(RunLevel.Advanced))
                {
                    ask = inputService.PromptYesNo("Use different site for installation?", false);
                }
                else
                {
                    ask = false;
                }
            }
            if (ask)
            {
                var chosen = inputService.ChooseFromList("Choose site to create new bindings",
                   _iisClient.WebSites,
                   x => Choice.Create(x.Id, x.Name, x.Id.ToString()));
                ret.SiteId = chosen;
            }
            return ret;
        }

        public override IISWebOptions Default(Target target)
        {
            var args = _arguments.GetArguments<IISWebArguments>();
            var ret = new IISWebOptions(args);
            if (args.InstallationSiteId != null)
            {
                // Throws exception when not found
                var site = _iisClient.GetWebSite(args.InstallationSiteId.Value); 
                ret.SiteId = site.Id;
            }
            else if (!target.IIS)
            {
                throw new Exception($"Missing parameter --{nameof(args.InstallationSiteId).ToLower()}");
            }
            return ret;
        }
    }
}