Ejemplo n.º 1
0
        public void ConfigureDevOpsAddOn(string addonName)
        {
            var sg = _safeguardLogic.Connect();

            try
            {
                var addon = _configDb.GetAddonByName(addonName);
                if (addon == null)
                {
                    throw LogAndException($"Add-on {addonName} not found.");
                }

                if (!addon.VaultCredentials.Any())
                {
                    throw LogAndException($"Waiting for the Add-on {addonName} to post the vault credentials.  Try again later...");
                }

                if ((_configDb.AssetPartitionId ?? 0) == 0 || (_configDb.AssetId ?? 0) == 0)
                {
                    var assetPartition = _safeguardLogic.CreateAssetPartition(sg);
                    _safeguardLogic.CreateAsset(sg, assetPartition);
                    _safeguardLogic.CreateAssetAccountGroup(sg, addon);
                }

                var assetId = _configDb.AssetId;
                if ((assetId ?? 0) == 0)
                {
                    throw LogAndException($"Failed to configure the Add-on {addonName}. No associated asset found in Safeguard.");
                }

                if (addon.VaultCredentials.Any() && !SafeguardHasVaultCredentials(addon))
                {
                    var tasks = new List <Task>();
                    foreach (var vaultCredential in addon.VaultCredentials)
                    {
                        var newAccount = new AssetAccount()
                        {
                            Name    = vaultCredential.Key,
                            AssetId = assetId.Value
                        };
                        var p = addon.VaultCredentials[newAccount.Name];

                        tasks.Add(Task.Run(() =>
                        {
                            newAccount = _safeguardLogic.AddAssetAccount(sg, newAccount);
                            _safeguardLogic.SetAssetAccountPassword(sg, newAccount, p);
                        }));
                    }

                    if (tasks.Any())
                    {
                        Task.WaitAll(tasks.ToArray());
                    }
                }

                var asset = _safeguardLogic.GetAsset(sg);
                if (asset != null)
                {
                    addon.VaultAssetId   = asset.Id;
                    addon.VaultAssetName = asset.Name;

                    if (addon.VaultAccountName != null)
                    {
                        var account = _safeguardLogic.GetAssetAccounts(sg, asset.Id)
                                      .FirstOrDefault(x => x.Name.StartsWith(addon.VaultAccountName, StringComparison.OrdinalIgnoreCase));
                        addon.VaultAccountId   = account?.Id ?? 0;
                        addon.VaultAccountName = account?.Name ?? addon.VaultAccountName;
                    }

                    _configDb.SaveAddon(addon);
                }

                var plugin = _configDb.GetPluginByName(addon.Manifest.PluginName);
                if (plugin != null && plugin.VaultAccountId != addon.VaultAccountId)
                {
                    plugin.VaultAccountId = addon.VaultAccountId;
                    _pluginsLogic.SavePluginVaultAccount(sg, plugin.Name, new AssetAccount()
                    {
                        Id = addon.VaultAccountId.Value
                    });
                }
            }
            finally
            {
                sg.Dispose();
            }
        }