Exemple #1
0
        public async Task <decimal> AddAccount(CloudAccountModel model)
        {
            if (model.AccountType == CloudAccountType.Aws)
            {
                var accountExists = await AwsAccountExists(model.AwsAccountNumber);

                if (accountExists)
                {
                    throw new ArgumentException("An AWS Account with this AWS Account Number already exists.");
                }
            }

            if (model.AccountType == CloudAccountType.GoogleCloud)
            {
                var keyObject     = JsonConvert.DeserializeObject <GoogleCloudKey>(model.GcJsonBody);
                var accountExists = await GoogleCloudAccountExists(keyObject.ClientId);

                if (accountExists)
                {
                    throw new ArgumentException("This Client Id is already registered.");
                }
            }

            await using var command = _connection.CreateCommand();
            command.CommandType     = CommandType.StoredProcedure;
            command.CommandText     = "CreateAccount";
            command.Parameters.AddWithValue("@CreatorUserID", model.CreatorUserId);
            command.Parameters.AddWithValue("@AccountType", model.AccountType);
            command.Parameters.AddWithValue("@AWSRoleArn", model.AwsRoleArn);
            command.Parameters.AddWithValue("@AWSAccountNumber", model.AwsAccountNumber);
            command.Parameters.AddWithValue("@AccountName", model.AccountName);
            command.Parameters.AddWithValue("@AWSRegionName", model.AwsRegionName);
            command.Parameters.AddWithValue("@SourceAccountNumber", model.SourceAccountNumber);
            command.Parameters.AddWithValue("@ExternalID", model.AwsExternalId);

            if (_connection.State != ConnectionState.Open)
            {
                await _connection.OpenAsync();
            }
            var reader = await command.ExecuteReaderAsync();

            await reader.ReadAsync();

            return(reader.GetDecimal(0));
        }
        public async Task <IActionResult> TestAccount(CloudAccountModel account)
        {
            try
            {
                ICloudAccount cloudAccount = null;

                switch (account.AccountType)
                {
                case CloudAccountType.Aws: cloudAccount = new AwsAccount(account.AwsRoleArn, account.AwsExternalId, null); break;

                case CloudAccountType.Azure: cloudAccount = new AzureAccount(account.AzureSubscriptionId, account.AzureTenantId, account.AzureClientId, account.AzureClientSecret); break;

                case CloudAccountType.GoogleCloud:
                    ValidateModelForGoogleCloud(account.GcJsonBody); cloudAccount = new GoogleCloudAccount(account.GcJsonBody, account.GcProjectId, null); break;
                }

                if (cloudAccount != null)
                {
                    var creds = await cloudAccount.GetTemporaryCredentials <dynamic>();

                    if (creds?.CredentialObject == null)
                    {
                        throw new Exception("Sorry! We could not connect to your cloud service provider. Please check your details and try again.");
                    }
                    return(Ok());
                }

                return(Unauthorized(new { Message = "The Account Type  value is not valid." }));
            }
            catch (Exception ex)
            {
                return(Unauthorized(new
                {
                    Message = ex.Message
                }));
            }
        }
        public async Task <IActionResult> AddAccount(CloudAccountModel account)
        {
            try
            {
                var user = await TryGetUser(User);

                account.CreatorUserId = user.Id;

                var newAccountId = await _dataAccess.AddAccount(account);

                var getLink = Url.Action("GetAccountById", new { accountId = newAccountId });
                return(Created(getLink, null));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "AddAccount");
                if (_hostEnvironment.IsDevelopment())
                {
                    throw;
                }

                return(BadRequest(new { Error = e.Message }));
            }
        }