|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START functions_billing_limit] |
| 16 | +# [START functions_billing_stop] |
| 17 | +import base64 |
| 18 | +import json |
| 19 | +# [END functions_billing_stop] |
| 20 | +import os |
| 21 | +# [END functions_billing_limit] |
| 22 | + |
| 23 | +# [START functions_billing_limit] |
| 24 | +# [START functions_billing_stop] |
| 25 | +from googleapiclient import discovery |
| 26 | +from oauth2client.client import GoogleCredentials |
| 27 | + |
| 28 | +# [END functions_billing_stop] |
| 29 | +# [END functions_billing_limit] |
| 30 | + |
| 31 | +# [START functions_billing_slack] |
| 32 | +from slackclient import SlackClient |
| 33 | +# [END functions_billing_slack] |
| 34 | + |
| 35 | +# [START functions_billing_limit] |
| 36 | +# [START functions_billing_stop] |
| 37 | +PROJECT_ID = os.getenv('GCP_PROJECT') |
| 38 | +PROJECT_NAME = f'projects/{PROJECT_ID}' |
| 39 | +# [END functions_billing_stop] |
| 40 | +# [END functions_billing_limit] |
| 41 | + |
| 42 | +# [START functions_billing_slack] |
| 43 | + |
| 44 | +# See https://api.slack.com/docs/token-types#bot for more info |
| 45 | +BOT_ACCESS_TOKEN = 'xxxx-111111111111-abcdefghidklmnopq' |
| 46 | + |
| 47 | +CHANNEL = 'general' |
| 48 | + |
| 49 | +slack_client = SlackClient(BOT_ACCESS_TOKEN) |
| 50 | + |
| 51 | + |
| 52 | +def notify_slack(data, context): |
| 53 | + pubsub_message = data |
| 54 | + |
| 55 | + notification_attrs = json.dumps(pubsub_message['attributes']) |
| 56 | + notification_data = base64.b64decode(data['data']).decode('utf-8') |
| 57 | + budget_notification_text = f'{notification_attrs}, {notification_data}' |
| 58 | + |
| 59 | + res = slack_client.api_call( |
| 60 | + 'chat.postMessage', |
| 61 | + channel=CHANNEL, |
| 62 | + text=budget_notification_text) |
| 63 | + print(res) |
| 64 | +# [END functions_billing_slack] |
| 65 | + |
| 66 | + |
| 67 | +# [START functions_billing_limit] |
| 68 | +def stop_billing(data, context): |
| 69 | + pubsub_data = base64.b64decode(data['data']).decode('utf-8') |
| 70 | + pubsub_json = json.loads(pubsub_data) |
| 71 | + cost_amount = pubsub_json['costAmount'] |
| 72 | + budget_amount = pubsub_json['budgetAmount'] |
| 73 | + if cost_amount <= budget_amount: |
| 74 | + print(f'No action necessary. (Current cost: {cost_amount})') |
| 75 | + return |
| 76 | + |
| 77 | + billing = discovery.build( |
| 78 | + 'cloudbilling', |
| 79 | + 'v1', |
| 80 | + cache_discovery=False, |
| 81 | + credentials=GoogleCredentials.get_application_default() |
| 82 | + ) |
| 83 | + |
| 84 | + projects = billing.projects() |
| 85 | + |
| 86 | + if __is_billing_enabled(PROJECT_NAME, projects): |
| 87 | + print(__disable_billing_for_project(PROJECT_NAME, projects)) |
| 88 | + else: |
| 89 | + print('Billing already disabled') |
| 90 | + |
| 91 | + |
| 92 | +def __is_billing_enabled(project_name, projects): |
| 93 | + """ |
| 94 | + Determine whether billing is enabled for a project |
| 95 | + @param {string} project_name Name of project to check if billing is enabled |
| 96 | + @return {bool} Whether project has billing enabled or not |
| 97 | + """ |
| 98 | + res = projects.getBillingInfo(name=project_name).execute() |
| 99 | + return res['billingEnabled'] |
| 100 | + |
| 101 | + |
| 102 | +def __disable_billing_for_project(project_name, projects): |
| 103 | + """ |
| 104 | + Disable billing for a project by removing its billing account |
| 105 | + @param {string} project_name Name of project disable billing on |
| 106 | + @return {string} Text containing response from disabling billing |
| 107 | + """ |
| 108 | + body = {'billingAccountName': ''} # Disable billing |
| 109 | + res = projects.updateBillingInfo(name=project_name, body=body).execute() |
| 110 | + print(f'Billing disabled: {json.dumps(res)}') |
| 111 | +# [END functions_billing_stop] |
| 112 | + |
| 113 | + |
| 114 | +# [START functions_billing_limit] |
| 115 | +ZONE = 'us-west1-b' |
| 116 | + |
| 117 | + |
| 118 | +def limit_use(data, context): |
| 119 | + pubsub_data = base64.b64decode(data['data']).decode('utf-8') |
| 120 | + pubsub_json = json.loads(pubsub_data) |
| 121 | + cost_amount = pubsub_json['costAmount'] |
| 122 | + budget_amount = pubsub_json['budgetAmount'] |
| 123 | + if cost_amount <= budget_amount: |
| 124 | + print(f'No action necessary. (Current cost: {cost_amount})') |
| 125 | + return |
| 126 | + |
| 127 | + compute = discovery.build( |
| 128 | + 'compute', |
| 129 | + 'v1', |
| 130 | + cache_discovery=False, |
| 131 | + credentials=GoogleCredentials.get_application_default() |
| 132 | + ) |
| 133 | + instances = compute.instances() |
| 134 | + |
| 135 | + instance_names = __list_running_instances(PROJECT_ID, ZONE, instances) |
| 136 | + __stop_instances(PROJECT_ID, ZONE, instance_names, instances) |
| 137 | + |
| 138 | + |
| 139 | +def __list_running_instances(project_id, zone, instances): |
| 140 | + """ |
| 141 | + @param {string} project_id ID of project that contains instances to stop |
| 142 | + @param {string} zone Zone that contains instances to stop |
| 143 | + @return {Promise} Array of names of running instances |
| 144 | + """ |
| 145 | + res = instances.list(project=project_id, zone=zone).execute() |
| 146 | + |
| 147 | + items = res['items'] |
| 148 | + running_names = [i['name'] for i in items if i['status'] == 'RUNNING'] |
| 149 | + return running_names |
| 150 | + |
| 151 | + |
| 152 | +def __stop_instances(project_id, zone, instance_names, instances): |
| 153 | + """ |
| 154 | + @param {string} project_id ID of project that contains instances to stop |
| 155 | + @param {string} zone Zone that contains instances to stop |
| 156 | + @param {Array} instance_names Names of instance to stop |
| 157 | + @return {Promise} Response from stopping instances |
| 158 | + """ |
| 159 | + if not len(instance_names): |
| 160 | + print('No running instances were found.') |
| 161 | + return |
| 162 | + |
| 163 | + for name in instance_names: |
| 164 | + instances.stop( |
| 165 | + project=project_id, |
| 166 | + zone=zone, |
| 167 | + instance=name).execute() |
| 168 | + print(f'Instance stopped successfully: {name}') |
| 169 | +# [END functions_billing_limit] |
0 commit comments