Skip to content

Commit 77a131e

Browse files
committed
Unified UX for Vultr & Scaleway
1 parent 56fbaea commit 77a131e

File tree

4 files changed

+82
-36
lines changed

4 files changed

+82
-36
lines changed

app/server.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
except ImportError:
1818
HAS_BOTO3 = False
1919

20+
try:
21+
import requests
22+
23+
HAS_REQUESTS = True
24+
except ImportError:
25+
HAS_REQUESTS = False
26+
2027
try:
2128
from google.auth.transport.requests import AuthorizedSession
2229
from google.oauth2 import service_account
@@ -174,6 +181,10 @@ async def ec2_regions(request):
174181

175182
@routes.get('/gce_config')
176183
async def check_gce_config(_):
184+
if not HAS_REQUESTS:
185+
return web.json_response({'error': 'missing_requests'}, status=400)
186+
if not HAS_GOOGLE_LIBRARIES:
187+
return web.json_response({'error': 'missing_google'}, status=400)
177188
gce_file = join(PROJECT_ROOT, 'configs', 'gce.json')
178189
response = {}
179190
try:
@@ -213,17 +224,17 @@ async def gce_regions(request):
213224
@routes.get('/vultr_config')
214225
async def check_vultr_config(request):
215226
default_path = expanduser(join('~', '.vultr.ini'))
216-
response = {'path': None}
227+
response = {'has_secret': False}
217228
try:
218229
open(default_path, 'r').read()
219-
response['path'] = default_path
230+
response['has_secret'] = True
220231
except IOError:
221232
pass
222233

223234
if 'VULTR_API_CONFIG' in os.environ:
224235
try:
225236
open(os.environ['VULTR_API_CONFIG'], 'r').read()
226-
response['path'] = os.environ['VULTR_API_CONFIG']
237+
response['has_secret'] = True
227238
except IOError:
228239
pass
229240
return web.json_response(response)
@@ -239,7 +250,7 @@ async def vultr_regions(_):
239250

240251
@routes.get('/scaleway_config')
241252
async def check_scaleway_config(_):
242-
return web.json_response({"ok": 'SCW_TOKEN' in os.environ})
253+
return web.json_response({"has_secret": 'SCW_TOKEN' in os.environ})
243254

244255

245256
app = web.Application()

app/static/provider-gce.vue

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<template>
22
<div>
3+
<div v-if="ui_config_error && ui_config_error === 'missing_google'" class="form-text alert alert-danger" role="alert">
4+
Python module "google-auth" is missing, please install it to proceed
5+
</div>
6+
<div v-if="ui_config_error && ui_config_error === 'missing_requests'" class="form-text alert alert-danger" role="alert">
7+
Python module "requests" is missing, please install it to proceed
8+
</div>
39
<div
410
class="form-group dropzone"
511
v-if="ui_needs_upload"
@@ -31,14 +37,14 @@
3137
<strong>{{ ui_drop_filename }} loaded successfully</strong>
3238
</div>
3339
</div>
34-
<input type="file" accept=".json,applciation/json" v-on:change="filechange_handler" />
35-
36-
<div class="form-group">
37-
<region-select v-model="region" v-bind:options="ui_region_options" v-bind:loading="ui_loading_check || ui_loading_regions">
38-
<label>Please specify <code>gce.json</code> credentials file to select region</label>
39-
</region-select>
40+
<div v-else class="form-text alert alert-success" role="alert">
41+
Google Compute Engine credentials were found in the project
4042
</div>
41-
43+
<input type="file" accept=".json,applciation/json" v-on:change="filechange_handler" />
44+
<region-select v-model="region" v-bind:options="ui_region_options" v-bind:loading="ui_loading_check || ui_loading_regions">
45+
<label v-if="ui_needs_upload">Please select region</label>
46+
<label v-else>Please specify <code>gce.json</code> credentials file to select region</label>
47+
</region-select>
4248
<button
4349
class="btn btn-primary"
4450
type="button"
@@ -62,6 +68,7 @@ module.exports = {
6268
ui_drop_error: null,
6369
ui_drop_success: null,
6470
ui_drop_filename: null,
71+
ui_config_error: null,
6572
ui_needs_upload: null,
6673
ui_loading_regions: false,
6774
ui_loading_check: false,
@@ -140,12 +147,19 @@ module.exports = {
140147
check_config() {
141148
this.ui_loading_check = true;
142149
fetch("/gce_config")
143-
.then(r => r.json())
150+
.then(r => {
151+
if (r.status === 200 || r.status === 400) {
152+
return r.json();
153+
}
154+
throw new Error(r.status);
155+
})
144156
.then(response => {
145157
if (response.status === 'ok') {
146158
this.gce_credentials_file = 'configs/gce.json';
147159
this.load_regions();
148160
this.ui_needs_upload = false;
161+
} else if (response.error) {
162+
this.ui_config_error = response.error;
149163
} else {
150164
this.ui_needs_upload = true;
151165
}

app/static/provider-scaleway.vue

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<template>
22
<div>
33

4-
<div class="form-group">
4+
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
5+
Token was read from the environment variable
6+
</div>
7+
<div v-else class="form-group">
58
<label
69
>Enter your auth token
710
<a
@@ -17,14 +20,10 @@
1720
type="text"
1821
class="form-control"
1922
name="scaleway_token"
20-
v-bind:disabled="ui_loading_check || ui_token_from_env"
23+
v-bind:disabled="ui_loading_check"
2124
v-model="scaleway_token"
2225
/>
23-
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert">
24-
Token was read from the environment variable
25-
</div>
2626
</div>
27-
2827
<div class="form-group">
2928
<region-select v-model="region" v-bind:options="ui_region_options">
3029
</region-select>
@@ -48,7 +47,7 @@ module.exports = {
4847
scaleway_token: null,
4948
region: null,
5049
// helper variables
51-
ui_token_from_env: false,
50+
ui_env_secrets: false,
5251
ui_loading_check: false,
5352
ui_region_options: [
5453
{value: 'Paris 1', key: 'par1'},
@@ -61,25 +60,32 @@ module.exports = {
6160
},
6261
computed: {
6362
is_valid() {
64-
return this.region && (this.scaleway_token || this.ui_token_from_env);
63+
return this.region && (this.scaleway_token || this.ui_env_secrets);
6564
}
6665
},
6766
methods: {
6867
check_config() {
6968
this.ui_loading_check = true;
7069
fetch("/scaleway_config")
71-
.then(r => r.json())
70+
.then(r => {
71+
if (r.status === 200 || r.status === 400) {
72+
return r.json();
73+
}
74+
throw new Error(r.status);
75+
})
7276
.then(response => {
73-
if (response.ok) {
74-
this.ui_token_from_env = true;
77+
if (response.has_secret) {
78+
this.ui_env_secrets = true;
79+
} else if (response.error) {
80+
this.ui_config_error = response.error;
7581
}
7682
})
7783
.finally(() => {
7884
this.ui_loading_check = false;
7985
});
8086
},
8187
submit() {
82-
if (this.ui_token_from_env) {
88+
if (this.ui_env_secrets) {
8389
this.$emit("submit", {
8490
region: this.region
8591
});

app/static/provider-vultr.vue

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<template>
22
<div>
3-
4-
<div class="form-group">
3+
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
4+
Vultr config file was found in your system
5+
</div>
6+
<div v-else class="form-group">
57
<label
68
>Enter the local path to your configuration INI file
79
<a
@@ -20,7 +22,7 @@
2022
v-bind:disabled="ui_loading_check"
2123
v-model="vultr_config"
2224
/>
23-
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert">
25+
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
2426
Configuration file was found in your system. You still can change the path to it
2527
</div>
2628
</div>
@@ -50,7 +52,7 @@ module.exports = {
5052
vultr_config: null,
5153
region: null,
5254
// helper variables
53-
ui_token_from_env: false,
55+
ui_env_secrets: false,
5456
ui_loading_check: false,
5557
ui_loading_regions: false,
5658
ui_region_options: []
@@ -61,19 +63,29 @@ module.exports = {
6163
this.load_regions();
6264
},
6365
computed: {
66+
has_secrets() {
67+
return this.ui_env_secrets || this.vultr_config;
68+
},
6469
is_valid() {
65-
return this.vultr_config && this.region;
70+
return this.has_secrets && this.region;
6671
}
6772
},
6873
methods: {
6974
check_config() {
7075
this.ui_loading_check = true;
7176
fetch("/vultr_config")
72-
.then(r => r.json())
77+
.then(r => {
78+
if (r.status === 200 || r.status === 400) {
79+
return r.json();
80+
}
81+
throw new Error(r.status);
82+
})
7383
.then(response => {
74-
if (response.path) {
75-
this.vultr_config = response.path;
76-
this.ui_token_from_env = true;
84+
if (response.has_secret) {
85+
this.ui_env_secrets = true;
86+
this.load_regions();
87+
} else if (response.error) {
88+
this.ui_config_error = response.error;
7789
}
7890
})
7991
.finally(() => {
@@ -95,10 +107,13 @@ module.exports = {
95107
});
96108
},
97109
submit() {
98-
this.$emit("submit", {
99-
vultr_config: this.vultr_config,
110+
let submit_value = {
100111
region: this.region
101-
});
112+
}
113+
if (!this.ui_env_secrets) {
114+
submit_value['vultr_config'] = this.vultr_config;
115+
}
116+
this.$emit("submit", submit_value);
102117
},
103118
},
104119
components: {

0 commit comments

Comments
 (0)