import json import os import requests from google.cloud import bigquery ACTION_RUN_URL = os.getenv("ACTION_RUN_URL", "test") AMOUNT_THRESHOLD = os.getenv("AMOUNT_THRESHOLD", 30) QUERY_DAYS = os.getenv("QUERY_DAYS", 5) SQL = """ SELECT DATE(line_item_usage_start_date) as d, account_id, line_item_product_code, line_item_usage_type, sum(line_item_unblended_cost) as total FROM `dbaas-eng-bi.finops.aws_prod_billing` WHERE line_item_product_code not in ("AmazonGuardDuty", "AWSConfig", "AWSCloudTrail", "AmazonCloudWatch", "ComputeSavingsPlans", "AWSCertificateManager","AmazonRDS") AND line_item_usage_type NOT LIKE '%-NatGateway-%' AND line_item_usage_type NOT LIKE '%-PublicIPv4:InUseAddress' AND resource_tags_user_usedby="" AND DATE(line_item_usage_start_date) >= DATE_ADD(CURRENT_DATE(), INTERVAL -{QUERY_DAYS} DAY) GROUP BY d, account_id,line_item_product_code, line_item_usage_type HAVING total > {AMOUNT_THRESHOLD} ORDER BY d LIMIT 1000 """.format(AMOUNT_THRESHOLD=AMOUNT_THRESHOLD, QUERY_DAYS=QUERY_DAYS) project = 'dbaas-eng-bi' location = 'US' client = bigquery.Client(project=project, location=location) # Define the jira mappings jira_project = { "dev": "O11YDEV", "staging": "O11YSTAG", "prod": "O11Y" } jira_tag = { "dev": "DEV", "staging": "STAGING", "prod": "PROD" } class ConfigInfo: def __init__(self, env, jira_url, jira_token): self.env = env self.jira_url = jira_url self.jira_token = jira_token def get_config_info(): return ConfigInfo(env="prod", jira_url="https://tidb.atlassian.net/rest/api/2/issue", jira_token=os.getenv("JIRA_TOKEN")) def post_to_jira(description, title, priority): config = get_config_info() project_key = jira_project[config.env] jira_tag_value = jira_tag[config.env] issue = { "fields": { "project": { "key": project_key }, "issuetype": { "name": "Task" }, "description": description, "summary": f"[{jira_tag_value}]{title}", "priority": { "name": priority }, "components": [ { "name": "cloud-finops-system" } ] } } api = config.jira_url headers = { 'Content-Type': 'application/json' } # Use basic auth instead of bearer token auth = ('charles.m@pingcap.com', config.jira_token) response = requests.post(api, headers=headers, auth=auth, data=json.dumps(issue)) return response if __name__ == '__main__': try: result = client.query(SQL).to_dataframe() if len(result) > 0: print(result) raise ValueError('Unlabeld bill amount founded Please check finops action') else: print("No unlabeld bill amount") except Exception as e: content = f""" URL: {ACTION_RUN_URL} Reason: {e} """ print(f"exception found: {content}") response = post_to_jira(content, "Unlabeld bill check failed", "Major") print(response.status_code, response.json())