Skip to content

Commit 149e7dd

Browse files
committed
Draft for GCE
1 parent 2da62f4 commit 149e7dd

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

app/server.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import asyncio
2+
import json
3+
24
import yaml
35
import boto3
46
from os.path import join, dirname
57
from aiohttp import web
68
import concurrent.futures
79
import 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

1115
routes = web.RouteTableDef()
1216
PROJECT_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')
9196
async 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')
98104
async 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')
111118
async 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+
122143
app = web.Application()
123144
app.router.add_routes(routes)
124145
app.add_routes([web.static('/static', join(PROJECT_ROOT, 'app', 'static'))])

app/static/provider-gce.vue

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<template>
2+
<div>
3+
<button
4+
class="btn btn-primary"
5+
type="button"
6+
v-on:click="submit"
7+
v-bind:disabled="!is_valid"
8+
>
9+
Next
10+
</button>
11+
</div>
12+
</template>
13+
14+
<script>
15+
module.exports = {
16+
data: function() {
17+
return {
18+
gce_credentials_file: null,
19+
region: null,
20+
// helper variables
21+
region_options: [],
22+
is_loading: false
23+
};
24+
},
25+
computed: {
26+
is_valid() {
27+
return this.gce_credentials_file && this.region;
28+
},
29+
is_region_disabled() {
30+
return !(this.gce_credentials_file) || this.is_loading;
31+
}
32+
},
33+
methods: {
34+
load_regions() {
35+
if (this.gce_credentials_file && this.region_options.length === 0) {
36+
this.is_loading = true;
37+
fetch('/gce_regions', {
38+
method: 'post',
39+
headers: {
40+
'Content-Type': 'application/json'
41+
},
42+
body: JSON.stringify({
43+
gce_credentials_file: this.gce_credentials_file
44+
})
45+
})
46+
.then(r => r.json())
47+
.then(data => {
48+
this.region_options = data;
49+
})
50+
.finally(() => {
51+
this.is_loading = false;
52+
});
53+
}
54+
},
55+
submit() {
56+
this.$emit('submit', {
57+
gce_credentials_file: this.gce_credentials_file,
58+
region: this.region
59+
});
60+
}
61+
}
62+
};
63+
</script>

0 commit comments

Comments
 (0)