#!/bin/env python # -*- coding: utf-8 -*- # need to install requests # this script is used to get billing info for a project import json # USAGE: python suspend_cluster.py [YYYY-MM] import os import sys import requests from requests.auth import HTTPDigestAuth if len(sys.argv) != 2: print("USAGE: python get_billing.py [YYYY-MM]") exit() # env variables tidb_pub = os.environ.get('TIDB_PUB') tidb_priv = os.environ.get('TIDB_PRIV') dry_run = os.environ.get('DRY_RUN') or False # arguments bill_month = sys.argv[1] # get projects def get_bills(bill_month): project_url = f'https://billing.tidbapi.com/v1beta1/bills/{bill_month}' headers = {'Content-type': 'application/json'} # get all project and crate dict for looking-up later(project_name: project_id) response = requests.get(project_url, headers=headers, auth=HTTPDigestAuth(tidb_pub,tidb_priv)) if response.status_code != 200: print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.text) exit() return response.json() # main bills = get_bills(bill_month) print(json.dumps(bills, indent=4, sort_keys=True))