Skip to content

Commit

Permalink
Added service account login (#843)
Browse files Browse the repository at this point in the history
* Added core utils

* Added core utils

* Fixed days parameter

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Fixing script

* Added service account email

* Fixed doc
  • Loading branch information
jdesouza authored Nov 29, 2023
1 parent 726f717 commit 0ed4e49
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
3 changes: 3 additions & 0 deletions plugins/cloud-costs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog

## 0.1.1
* Added cloud costs service account support

## 0.1.0
* Initial release supporting AWS and GCP
10 changes: 1 addition & 9 deletions plugins/cloud-costs/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
FROM gcr.io/google.com/cloudsdktool/google-cloud-cli:455.0.0-alpine

RUN apk add --no-cache \
python3 \
py3-pip \
&& pip3 install --upgrade pip \
&& pip3 install --no-cache-dir \
awscli \
&& rm -rf /var/cache/apk/*
FROM gcr.io/google.com/cloudsdktool/google-cloud-cli:455.0.0

COPY cloud-costs.sh cloud-costs.sh

Expand Down
11 changes: 9 additions & 2 deletions plugins/cloud-costs/cloud-costs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ usage: cloud-costs \
--projectname <project name - required for GCP> \
--dataset <dataset name - required for GCP> \
--billingaccount <billing account - required for GCP> \
--serviceaccountemail <service account email - required for GCP> \
[--timeout <time in seconds>] \
[--days <number of days to query, default is 5>]
Expand All @@ -37,6 +38,7 @@ workgroup=''
projectname=''
dataset=''
billingaccount=''
serviceaccountemail=''
days=''
while [ ! $# -eq 0 ]; do
flag=${1##-}
Expand Down Expand Up @@ -82,6 +84,9 @@ while [ ! $# -eq 0 ]; do
billingaccount)
billingaccount=${2}
;;
serviceaccountemail)
serviceaccountemail=${2}
;;
*)
usage
exit
Expand All @@ -94,7 +99,7 @@ if [[ "$days" = "" && "$CLOUD_COSTS_DAYS" != "" ]]; then
days=$CLOUD_COSTS_DAYS
fi
if [[ "$days" = "" ]]; then
days='1'
days='5'
fi

initial_date_time=$(date -u -d $days+' day ago' +"%Y-%m-%d %H:00:00.000")
Expand Down Expand Up @@ -170,8 +175,10 @@ if [[ "$provider" == "gcp" ]]; then
table="$projectname.$dataset.gcp_billing_export_resource_v1_$billingaccount"

echo "Google bigquey is running......"
sql="SELECT main.* FROM \`$table\` AS main LEFT JOIN UNNEST(labels) as labels WHERE labels.key = '$tagkey' AND labels.value = '$tagvalue' and usage_start_time >= '$initial_date_time' AND usage_start_time < '$final_date_time' order by usage_start_time desc"

gcloud auth activate-service-account $serviceaccountemail --key-file=/.config/gcp-key.json --project=$projectname

sql="SELECT main.* FROM \`$table\` AS main LEFT JOIN UNNEST(labels) as labels WHERE labels.key = '$tagkey' AND labels.value = '$tagvalue' and usage_start_time >= '$initial_date_time' AND usage_start_time < '$final_date_time' order by usage_start_time desc"
bq --format=prettyjson query --max_rows=1000000 --nouse_legacy_sql "$sql" > /output/cloud-costs-tmp.json
echo "Google bigquey finished......"

Expand Down
2 changes: 1 addition & 1 deletion plugins/cloud-costs/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1

0 comments on commit 0ed4e49

Please sign in to comment.