Skip to content

Commit

Permalink
#13 add cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ma91n committed Aug 28, 2019
1 parent 1386dfa commit 7fe7d89
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.out

# test output png
*.png
92 changes: 64 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,88 @@
# gbilling-plot
Create graphed invoice for Google Cloud Platform. You can see billing amount per GCP project.

<img src="https://img.shields.io/badge/go-v1.12-green.svg" />

Create graphed invoice for GCP. You can see billing amount per GCP project.
Create graphed invoice for Google Cloud Platform. You can see billing amount per GCP project.

## Usage

This package uses below services.
This package uses below great services.

- Google Cloud Billing(BigQuery)
- Google Cloud Functions
- Google Cloud Pub/Sub
- Google Cloud Scheduler
- Slack API

## Requirements
## QuickStart

1. Install
```console
go get -u go get -u github.com/future-architect/gbilling-plot/cmd/gbplot
```
2. Obtain GCP Service credentials that must have `bigquery.jobs.create` permission
```bash
export GOOGLE_APPLICATION_CREDENTIALS=<credentials path>
```
3. Export your GCP billing to BigQuery
* https://cloud.google.com/billing/docs/how-to/export-data-bigquery
4. Run command
```bash
gbplot -project <your project name> -table <your billing table name on bigquery> -out out.png
```
5. You can confirm out.png file

## Options

```console
$ gbplot --help
Usage of gbplot:
-o string
Output file name (default "out.png")
-out string
Output file name (default "out.png")
-p string
GCP project name
-project string
GCP project name
-t string
BigQuery billing table name
-table string
BigQuery billing table name
```

## Deploy Google Cloud Function

*Notify GCP Billing to Slack* requires the following to run:
### Requirements

* [Go](https://golang.org/dl/) more than 1.11
* [Cloud SDK](https://cloud.google.com/sdk/install/)

* [Cloud SDK](https://cloud.google.com/sdk/install/)

## Steps
### Steps

1. [Get Slack API Token](https://get.slack.help/hc/en-us/articles/215770388-Create-and-regenerate-API-tokens)
2. Export your GCP billing to BigQuery ([reference](https://cloud.google.com/billing/docs/how-to/export-data-bigquery))
2. [Export your GCP billing to BigQuery](https://cloud.google.com/billing/docs/how-to/export-data-bigquery)
3. Create Cloud Scheduler
```sh
gcloud beta scheduler jobs create pubsub graph-billing --project "<your project name>" \
--schedule "50 23 * * *" \
--topic graph-billing \
--message-body="execute" \
--time-zone "Asia/Tokyo" \
--description "This is scheduler for graph billing."
```
```sh
gcloud beta scheduler jobs create pubsub graph-billing --project "<your project name>" \
--schedule "50 23 * * *" \
--topic graph-billing \
--message-body="execute" \
--time-zone "Asia/Tokyo" \
--description "This is scheduler for graph billing."
```
4. Deploy to Cloud Function
```sh
gcloud functions deploy graphBilling --project "<your project name>" \
--entry-point GraphedBilling \
--trigger-resource graph-billing \
--trigger-event google.pubsub.topic.publish \
--runtime go111 \
--set-env-vars TABLE_NAME="<your billing table name on bigquery>" \
--set-env-vars SLACK_API_TOKEN="<your slack api token>" \
--set-env-vars SLACK_CHANNEL="<your slack channel name>"
```
```sh
git clone https://github.com/future-architect/gbilling-plot.git
cd gbilling-plot
gcloud functions deploy graphBilling --project "<your project name>" \
--entry-point GraphedBilling \
--triggerz-resource graph-billing \
--trigger-event google.pubsub.topic.publish \
--runtime go111 \
--set-env-vars TABLE_NAME="<your billing table name on bigquery>" \
--set-env-vars SLACK_API_TOKEN="<your slack api token>" \
--set-env-vars SLACK_CHANNEL="<your slack channel name>"
```
5. Go to the [Cloud Scheduler page](https://cloud.google.com/scheduler/docs/tut-pub-sub) and click the *run now* button of *graphBilling*

## Example
Expand Down
8 changes: 5 additions & 3 deletions cmd/ginvoice/gbillingplot.go → cmd/gbplot/gbplot.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019-present Future Corporation
* Copyright (c) 20present Future Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,13 +27,14 @@ import (

const period = 30

// export GOOGLE_APPLICATION_CREDENTIALS=key.json
func main() {

projectID := flag.String("p", os.Getenv("GCP_PROJECT"), "GCP project name")
tableName := flag.String("t", os.Getenv("TABLE_NAME"), "BigQuery billing table name")
outFileName := flag.String("o", "out.png", "Output file name")
flag.StringVar(projectID, "project", "", "GCP project name")
flag.StringVar(tableName, "table", "", "BigQuery billing table name")
flag.StringVar(outFileName, "out", "out.png", "Output file name")
flag.Parse()

if *projectID == "" || *tableName == "" {
Expand Down Expand Up @@ -63,7 +64,8 @@ func main() {
log.Fatal(err)
}

if err := ioutil.WriteFile("example.png", plotBytes, 0644); err != nil {

if err := ioutil.WriteFile(*outFileName, plotBytes, 0644); err != nil {
log.Fatal(err)
}

Expand Down
Binary file removed example.png
Binary file not shown.

0 comments on commit 7fe7d89

Please sign in to comment.