Skip to content

Commit e6e5b6d

Browse files
author
ace-n
committed
Add GAE billing limit sample
1 parent 07603ac commit e6e5b6d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

functions/billing/main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,36 @@ def __stop_instances(project_id, zone, instance_names, instances):
168168
instance=name).execute()
169169
print(f'Instance stopped successfully: {name}')
170170
# [END functions_billing_limit]
171+
172+
# [START functions_billing_limit_appengine]
173+
APP_NAME = os.getenv('GCP_PROJECT')
174+
175+
176+
def limit_use_appengine(data, context):
177+
pubsub_data = base64.b64decode(data['data']).decode('utf-8')
178+
pubsub_json = json.loads(pubsub_data)
179+
cost_amount = pubsub_json['costAmount']
180+
budget_amount = pubsub_json['budgetAmount']
181+
if cost_amount <= budget_amount:
182+
print(f'No action necessary. (Current cost: {cost_amount})')
183+
return
184+
185+
appengine = discovery.build(
186+
'appengine',
187+
'v1',
188+
cache_discovery=False,
189+
credentials=GoogleCredentials.get_application_default()
190+
)
191+
apps = appengine.apps()
192+
193+
# Get the target app's serving status
194+
target_app = apps.get(appsId=APP_NAME).execute()
195+
current_status = target_app['servingStatus']
196+
197+
# Disable target app, if necessary
198+
if current_status == 'SERVING':
199+
print(f'Attempting to disable app {APP_NAME}...')
200+
body = {'servingStatus': 'USER_DISABLED'}
201+
app = apps.patch(
202+
appsId=APP_NAME, updateMask='serving_status', body=body).execute()
203+
# [END functions_billing_limit_appengine]

0 commit comments

Comments
 (0)