Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const recruitment = {
organization: 'AWS-SRB-Recruitment'
};

const stage = process.env.NODE_ENV === 'production' ? recruitment : recruitment;
const stage = process.env.NODE_ENV === 'production' ? serbia : serbia;

export default {
...stage
Expand Down
8 changes: 4 additions & 4 deletions client/src/containers/AccountsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ const AccountsView = () => {
account={account}
refreshList={refreshList}
apiFunction={
show === 'Create new AWS account' || 'Create new GCP account' ?
serviceHandler.createAccount : serviceHandler.updateAccount
show === 'Create new AWS account' ?
awsServices.createAccount : awsServices.updateAccount
}
handleViewChange={handleViewChange} />
</div>
Expand Down Expand Up @@ -160,8 +160,8 @@ const AccountsView = () => {
account={account}
refreshList={refreshList}
apiFunction={
show === 'Create new AWS account' || 'Create new GCP account' ?
serviceHandler.createAccount : serviceHandler.updateAccount
show === 'Create new GCP account' ?
gcpServices.createAccount : gcpServices.updateAccount
}
handleViewChange={handleViewChange} />
</div>
Expand Down
10 changes: 10 additions & 0 deletions server/serverless-gcp-account-crud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ functions:
cors: true
authorizer: ${self:custom.authorizer}
- schedule: cron(2,32 8-18 * * ? *)
gcpResetBudgets:
handler: src/handler/gcp-account-handler.resetBudgets
timeout: 900
events:
- http:
path: gcp/reset-budgets
method: post # TODO: use node v15.14.0 (otherwise post api does not work)
cors: true
authorizer: ${self:custom.authorizer}
- schedule: cron(0 6 1 * ? *) # Run at 6:00 am (UTC+0) every 1st day of the month
16 changes: 14 additions & 2 deletions server/src/handler/gcp-account-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const updateAccount = async (event) => {
if (oldStateAccount.budget != account.budget) {
const budgets = await getBudgetList();
const budgetsForUpdate = budgets.filter(budget => budget.displayName === `${account.name}${budgetNameSuffix.EMAIL}` || budget.displayName === `${account.name}${budgetNameSuffix.PUBSUB}`);

await Promise.all(budgetsForUpdate.map(async (budget) => {
await updateBudget(budget, account.budget);
}));
Expand Down Expand Up @@ -80,6 +79,18 @@ const syncBudgets = async () => {
}
};


const resetBudgets = async () => {
try {
await accountSyncService.resetBudget();
return okResponse({ success: true, message: 'Budget reset is done' });
} catch (error) {
console.log('Budget sync failed', error);
return errorResponse({ statusCode: 500, message: 'Budget reset failed'});
}
};


const syncOwners = async () => {
try {
const result = await accountSyncService.syncOwners();
Expand Down Expand Up @@ -107,5 +118,6 @@ module.exports = {
updateAccount,
syncBudgets,
syncOwners,
syncAccounts
syncAccounts,
resetBudgets
};
18 changes: 17 additions & 1 deletion server/src/service/gcp-account-sync-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,25 @@ const syncBudgets = async () => {
return { success: true, message: 'Budget sync is done' };
}


const resetBudget = async () => {

// get existing accounts from dynamodb
const accounts = await accountService.getAccounts(tableName);

// update budgets to 0
for (const account of accounts) {
await accountService.updateAccount({ id: account.id, actualSpend: `0` }, tableName);
console.log(`Account with id ${account.id} has reset budget`)
}

return { success: true, message: 'Budget reset is done' };
}

module.exports = {
syncAccounts,
syncAccountsMembers,
syncOwners,
syncBudgets
syncBudgets,
resetBudget
};
4 changes: 2 additions & 2 deletions server/src/service/gcp-budget-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const getBudgetList = async () => {
// use next token to get all pages/budgets
do {
response = await gcpClient.request({ method: 'GET', url: !nextToken ? url : `${url}?pageToken=${nextToken}` });
budgets.push(response.budgets);
budgets = budgets.concat(response.data.budgets);
nextToken = response.nextPageToken ? response.nextPageToken : null;
} while (nextToken);

Expand All @@ -129,7 +129,7 @@ const updateBudget = async (budget, newAmount) => {
console.log(`Updating GCP project budget amount, new amount: ${newAmount}, budget: ${JSON.stringify(budget)}`);
try {
const gcpClient = await getGcpAuthClient();
const url = `https://billingbudgets.googleapis.com/v1/billingAccounts/${process.env.GCP_BILLING_ACCOUNT_ID}/budgets/${budget.name}`;
const url = `https://billingbudgets.googleapis.com/v1/${budget.name}`;
const body = {
amount: budget.amount
};
Expand Down