Skip to content

Commit 7d2ddff

Browse files
committed
Updated Vultr provider UX
1 parent 27d21db commit 7d2ddff

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

app/server.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,33 @@ async def gce_regions(request):
243243

244244
@routes.get('/vultr_config')
245245
async def check_vultr_config(request):
246-
default_path = expanduser(join('~', '.vultr.ini'))
247246
response = {'has_secret': False}
247+
if 'VULTR_API_CONFIG' in os.environ:
248+
try:
249+
open(os.environ['VULTR_API_CONFIG'], 'r').read()
250+
response['has_secret'] = True
251+
except IOError:
252+
pass
248253
try:
254+
default_path = expanduser(join('~', '.vultr.ini'))
249255
open(default_path, 'r').read()
250256
response['has_secret'] = True
251257
except IOError:
252258
pass
259+
return web.json_response(response)
253260

254-
if 'VULTR_API_CONFIG' in os.environ:
261+
262+
@routes.post('/vultr_config')
263+
async def save_vultr_config(request):
264+
data = await request.json()
265+
token = data.get('token')
266+
path = os.environ.get('VULTR_API_CONFIG') or expanduser(join('~', '.vultr.ini'))
267+
with open(path, 'w') as f:
255268
try:
256-
open(os.environ['VULTR_API_CONFIG'], 'r').read()
257-
response['has_secret'] = True
269+
f.write('[default]\nkey = {0}'.format(token))
258270
except IOError:
259-
pass
260-
return web.json_response(response)
271+
return web.json_response({'error': 'can not save config file'}, status=400)
272+
return web.json_response({'saved_to': path})
261273

262274

263275
@routes.get('/vultr_regions')
@@ -327,7 +339,7 @@ async def linode_regions(_):
327339

328340

329341
@routes.get('/cloudstack_config')
330-
async def get_cloudstack_config(_):
342+
async def check_cloudstack_config(_):
331343
if not HAS_REQUESTS:
332344
return web.json_response({'error': 'missing_requests'}, status=400)
333345
if not HAS_CS_LIBRARIES:
@@ -367,7 +379,7 @@ async def cloudstack_regions(request):
367379
}, status=400)
368380
# if config was passed from client, save it after successful zone retrieval
369381
if _read_cloudstack_config() is None:
370-
path = os.environ['CLOUDSTACK_CONFIG'] or expanduser(join('~', '.cloudstack.ini'))
382+
path = os.environ.get('CLOUDSTACK_CONFIG') or expanduser(join('~', '.cloudstack.ini'))
371383
with open(path, 'w') as f:
372384
try:
373385
f.write(client_config)

app/static/provider-vultr.vue

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<template>
22
<div>
3-
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
3+
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert">
44
Vultr config file was found in your system
55
</div>
66
<div v-else class="form-group">
77
<label
8-
>Enter the local path to your configuration INI file
8+
>Enter Vultr API Token, it will be saved in your system
99
<a
1010
href="https://trailofbits.github.io/algo/cloud-vultr.html"
1111
title="https://trailofbits.github.io/algo/cloud-vultr.html"
@@ -18,12 +18,13 @@
1818
<input
1919
type="text"
2020
class="form-control"
21-
name="vultr_config"
21+
name="vultr_token"
2222
v-bind:disabled="ui_loading_check"
23-
v-model="vultr_config"
23+
v-model="ui_token"
24+
v-on:blur="save_config"
2425
/>
25-
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
26-
Configuration file was found in your system. You still can change the path to it
26+
<div v-if="vultr_config" class="form-text alert alert-success" role="alert">
27+
The config file was saved on your system
2728
</div>
2829
</div>
2930

@@ -52,7 +53,8 @@ module.exports = {
5253
vultr_config: null,
5354
region: null,
5455
// helper variables
55-
ui_env_secrets: false,
56+
ui_token: null,
57+
ui_token_from_env: false,
5658
ui_loading_check: false,
5759
ui_loading_regions: false,
5860
ui_region_options: []
@@ -64,7 +66,7 @@ module.exports = {
6466
},
6567
computed: {
6668
has_secrets() {
67-
return this.ui_env_secrets || this.vultr_config;
69+
return this.ui_token_from_env || this.vultr_config;
6870
},
6971
is_valid() {
7072
return this.has_secrets && this.region;
@@ -82,7 +84,7 @@ module.exports = {
8284
})
8385
.then(response => {
8486
if (response.has_secret) {
85-
this.ui_env_secrets = true;
87+
this.ui_token_from_env = true;
8688
this.load_regions();
8789
} else if (response.error) {
8890
this.ui_config_error = response.error;
@@ -92,14 +94,42 @@ module.exports = {
9294
this.ui_loading_check = false;
9395
});
9496
},
97+
save_config() {
98+
this.ui_loading_check = true;
99+
fetch("/vultr_config", {
100+
method: 'post',
101+
headers: {
102+
'Content-Type': 'application/json'
103+
},
104+
body: JSON.stringify({
105+
token: this.ui_token
106+
})
107+
})
108+
.then(r => {
109+
if (r.status === 200 || r.status === 400) {
110+
return r.json();
111+
}
112+
throw new Error(r.status);
113+
})
114+
.then(response => {
115+
if ('saved_to' in response) {
116+
this.vultr_config = response.saved_to;
117+
} else if (response.error) {
118+
this.ui_config_error = response.error;
119+
}
120+
})
121+
.finally(() => {
122+
this.ui_loading_check = false;
123+
});
124+
},
95125
load_regions() {
96126
this.ui_loading_regions = true;
97127
fetch("/vultr_regions")
98128
.then((r) => r.json())
99129
.then((data) => {
100130
this.ui_region_options = Object.keys(data).map(k => ({
101131
value: data[k].name,
102-
key: data[k].name
132+
key: data[k].DCID
103133
}));
104134
})
105135
.finally(() => {
@@ -110,7 +140,7 @@ module.exports = {
110140
let submit_value = {
111141
region: this.region
112142
}
113-
if (!this.ui_env_secrets) {
143+
if (!this.ui_token_from_env) {
114144
submit_value['vultr_config'] = this.vultr_config;
115145
}
116146
this.$emit("submit", submit_value);

0 commit comments

Comments
 (0)