Exemplo n.º 1
0
        public ActionResult <A2ARetrievableAccount> GetRetrievableAccountById([FromServices] ISafeguardLogic safeguard, [FromRoute] int accountId)
        {
            var retrievableAccount = safeguard.GetA2ARetrievableAccountById(A2ARegistrationType.Account, accountId);

            if (retrievableAccount == null)
            {
                return(NotFound());
            }

            return(Ok(retrievableAccount));
        }
        public IEnumerable <AccountMapping> SaveAccountMappings(ISafeguardConnection sgConnection, string name, IEnumerable <A2ARetrievableAccount> accounts)
        {
            if (_configDb.A2aRegistrationId == null)
            {
                var msg = "A2A registration not configured";
                _logger.Error(msg);
                throw new DevOpsException(msg);
            }

            if (_configDb.GetPluginByName(name) == null)
            {
                var msg = $"Plugin {name} not found";
                _logger.Error(msg);
                throw new DevOpsException(msg, HttpStatusCode.NotFound);
            }

            var retrievableAccounts = accounts.ToArray();

            if (retrievableAccounts.All(x => x.AccountId == 0))
            {
                var msg = "Invalid list of accounts. Expecting a list of retrievable accounts.";
                _logger.Error(msg);
                throw new DevOpsException(msg);
            }

            var allAccounts = _configDb.GetAccountMappings().ToArray();

            foreach (var account in retrievableAccounts)
            {
                if (!string.IsNullOrEmpty(account.AltAccountName))
                {
                    // Make sure that no other existing account has the same altAccountName
                    // Make sure that none of the accounts that are being added, have the same altAccountName
                    if (allAccounts.Any(x => x.AltAccountName != null &&
                                        x.AltAccountName.Equals(account.AltAccountName, StringComparison.OrdinalIgnoreCase) &&
                                        !x.VaultName.Equals(name, StringComparison.OrdinalIgnoreCase)) ||
                        retrievableAccounts.Any(x => x.AccountId != account.AccountId &&
                                                x.AltAccountName != null &&
                                                x.AltAccountName.Equals(account.AltAccountName, StringComparison.OrdinalIgnoreCase)))
                    {
                        var msg = $"Invalid alternate account name. The account name {account.AltAccountName} is already in use.";
                        _logger.Error(msg);
                        throw new DevOpsException(msg);
                    }
                }
            }

            var sg = sgConnection ?? _safeguardLogic.Connect();

            try
            {
                var newAccounts = new List <AccountMapping>();

                foreach (var account in retrievableAccounts)
                {
                    try
                    {
                        var retrievableAccount =
                            _safeguardLogic.GetA2ARetrievableAccountById(sg, A2ARegistrationType.Account, account.AccountId);

                        if (retrievableAccount != null)
                        {
                            var accountMapping = new AccountMapping()
                            {
                                AccountName    = retrievableAccount.AccountName,
                                AltAccountName = account.AltAccountName,
                                AccountId      = retrievableAccount.AccountId,
                                ApiKey         = retrievableAccount.ApiKey,
                                AssetName      = retrievableAccount.SystemName,
                                SystemId       = retrievableAccount.SystemId,
                                DomainName     = retrievableAccount.DomainName,
                                NetworkAddress = retrievableAccount.NetworkAddress,
                                VaultName      = name
                            };

                            newAccounts.Add(accountMapping);
                        }
                    }
                    catch (Exception ex)
                    {
                        var msg = $"Failed to add account {account.AccountId} - {account.AccountName}: {ex.Message}";
                        _logger.Error(ex, msg);
                    }
                }

                if (newAccounts.Count > 0)
                {
                    _configDb.SaveAccountMappings(newAccounts);
                }

                return(GetAccountMappings(name));
            }
            finally
            {
                if (sgConnection == null)
                {
                    sg.Dispose();
                }
            }
        }