11import asyncio
2+ import json
3+
24import yaml
35import boto3
46from os .path import join , dirname
57from aiohttp import web
68import concurrent .futures
79import sys
8- from playbook import PlaybookCLI
910
11+ from google .auth .transport .requests import AuthorizedSession
12+ from google .oauth2 import service_account
13+ from playbook import PlaybookCLI
1014
1115routes = web .RouteTableDef ()
1216PROJECT_ROOT = dirname (dirname (__file__ ))
@@ -87,13 +91,15 @@ async def post_config(request):
8791 f .write (config )
8892 return web .json_response ({'ok' : True })
8993
94+
9095@routes .post ('/exit' )
9196async def post_exit (_ ):
9297 if task_future and task_future .done ():
9398 sys .exit (0 )
9499 else :
95100 sys .exit (1 )
96101
102+
97103@routes .post ('/lightsail_regions' )
98104async def post_exit (request ):
99105 data = await request .json ()
@@ -107,6 +113,7 @@ async def post_exit(request):
107113 )
108114 return web .json_response (response )
109115
116+
110117@routes .post ('/ec2_regions' )
111118async def post_exit (request ):
112119 data = await request .json ()
@@ -119,6 +126,20 @@ async def post_exit(request):
119126 return web .json_response (response )
120127
121128
129+ @routes .post ('/gce_regions' )
130+ async def post_exit (request ):
131+ #data = await request.json()
132+ gce_config_file = 'configs/gce.json' # 'data.get('gce_config_file')
133+ project_id = json .loads (open (gce_config_file , 'r' ).read ())['project_id' ]
134+
135+ response = AuthorizedSession (
136+ service_account .Credentials .from_service_account_file (gce_config_file ).with_scopes (
137+ ['https://www.googleapis.com/auth/compute' ])).get (
138+ 'https://www.googleapis.com/compute/v1/projects/{project_id}/regions' .format (project_id = project_id ))
139+
140+ return web .json_response (json .loads (response .content ))
141+
142+
122143app = web .Application ()
123144app .router .add_routes (routes )
124145app .add_routes ([web .static ('/static' , join (PROJECT_ROOT , 'app' , 'static' ))])
0 commit comments