Skip to content

Commit

Permalink
feat: bring budget (#102)
Browse files Browse the repository at this point in the history
* budget lib

* remove my email

* added clarification

* small fixes

* add dummy sim

* fix

* change the order

* create the lib with the script

* add peerDependencies

* change the name of the class to Alert

* update todo

---------

Co-authored-by: Elad Ben-Israel <[email protected]>
  • Loading branch information
meirdev and eladb authored Mar 12, 2024
1 parent 29485bb commit ee1afe6
Show file tree
Hide file tree
Showing 14 changed files with 10,341 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/budget-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: budget-pull
on:
pull_request:
paths:
- budget/**
jobs:
build-budget:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: budget
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: budget
- name: Test
run: wing test
working-directory: budget
- name: Pack
run: wing pack
working-directory: budget
54 changes: 54 additions & 0 deletions .github/workflows/budget-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: budget-release
on:
push:
branches:
- main
paths:
- budget/**
- "!budget/package-lock.json"
jobs:
build-budget:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: budget
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: budget
- name: Test
run: wing test
working-directory: budget
- name: Pack
run: wing pack
working-directory: budget
- name: Get package version
run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >>
"$GITHUB_ENV"
working-directory: budget
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: budget
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Tag commit
uses: tvdias/[email protected]
with:
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
tag: budget-v${{ env.WINGLIB_VERSION }}
- name: GitHub release
uses: softprops/action-gh-release@v1
with:
name: budget v${{ env.WINGLIB_VERSION }}
tag_name: budget-v${{ env.WINGLIB_VERSION }}
files: "*.tgz"
token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
21 changes: 21 additions & 0 deletions .github/workflows/canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ jobs:
- name: Test
run: wing test
working-directory: bedrock
canary-budget:
name: Test budget
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: budget
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: budget
- name: Test
run: wing test
working-directory: budget
canary-checks:
name: Test checks
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pull_request_rules:
- -check-failure=build-bedrock
- -check-pending=build-bedrock
- -check-stale=build-bedrock
- -check-failure=build-budget
- -check-pending=build-budget
- -check-stale=build-budget
- -check-failure=build-checks
- -check-pending=build-checks
- -check-stale=build-checks
Expand Down Expand Up @@ -83,6 +86,9 @@ pull_request_rules:
- -check-failure=build-bedrock
- -check-pending=build-bedrock
- -check-stale=build-bedrock
- -check-failure=build-budget
- -check-pending=build-budget
- -check-stale=build-budget
- -check-failure=build-checks
- -check-pending=build-checks
- -check-stale=build-checks
Expand Down
2 changes: 2 additions & 0 deletions budget/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions budget/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions budget/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# budget

A Wing library for working with [AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html)

## Prerequisites

* [winglang](https://winglang.io).

## Installation

```sh
npm i @winglibs/budget
```

## Usage

**⚠️ The budget refers to the entire account and not just for the current project!**

Add your budget alert to the code:

```wing
bring budget;
new budget.Alert(
name: "Test",
amount: 10,
emailAddresses: ["[email protected]"],
);
```

*Note: ​The budget amount is in USD.*

You get an alert when your monthly payment goes over your budget.

## TODO

- [ ] Set a budget alert only for resources with certain tags.
- [ ] Allow to perform automatic actions when the budget runs out.

## License

This library is licensed under the [MIT License](./LICENSE).
27 changes: 27 additions & 0 deletions budget/budget-shared.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub enum TimeUnit {
DAILY,
MONTHLY,
ANNUALLY,
}

pub struct AlertProps {
name: str;
amount: num; // USD
emailAddresses: Array<str>;
timeUnit: TimeUnit?; // default: MONTHLY
}

pub interface IAlert {
}

pub class Util {
pub static timeUnitToStr(timeUnit: TimeUnit): str {
if timeUnit == TimeUnit.DAILY {
return "DAILY";
} elif timeUnit == TimeUnit.MONTHLY {
return "MONTHLY";
} elif timeUnit == TimeUnit.ANNUALLY {
return "ANNUALLY";
}
}
}
6 changes: 6 additions & 0 deletions budget/budget-sim.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bring "./budget-shared.w" as shared;

pub class AlertSim impl shared.IAlert {
new(props: shared.AlertProps) {
}
}
25 changes: 25 additions & 0 deletions budget/budget-tfaws.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
bring "cdktf" as cdktf;
bring "@cdktf/provider-aws" as tfaws;

bring "./budget-shared.w" as shared;

pub class AlertTfAws impl shared.IAlert {
budget: tfaws.budgetsBudget.BudgetsBudget;

new(props: shared.AlertProps) {
this.budget = new tfaws.budgetsBudget.BudgetsBudget(
name: props.name,
budgetType: "COST",
limitUnit: "USD",
limitAmount: "{props.amount}",
timeUnit: shared.Util.timeUnitToStr(props.timeUnit ?? shared.TimeUnit.MONTHLY),
notification: {
comparison_operator: "GREATER_THAN",
threshold: 100,
threshold_type: "PERCENTAGE",
notification_type: "ACTUAL",
subscriber_email_addresses: props.emailAddresses,
},
);
}
}
9 changes: 9 additions & 0 deletions budget/budget.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bring "./budget.w" as budget;

// use budget of 10 USD

new budget.Alert(
name: "test",
amount: 10,
emailAddresses: ["[email protected]"],
);
19 changes: 19 additions & 0 deletions budget/budget.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
bring util;

bring "./budget-sim.w" as sim;
bring "./budget-tfaws.w" as tfaws;
bring "./budget-shared.w" as shared;

pub class Alert {
platform: shared.IAlert;

new(props: shared.AlertProps) {
if util.env("WING_TARGET") == "tf-aws" {
this.platform = new tfaws.AlertTfAws(props);
} elif util.env("WING_TARGET") == "sim" {
this.platform = new sim.AlertSim(props);
} else {
throw "unknown platform";
}
}
}
Loading

0 comments on commit ee1afe6

Please sign in to comment.