Updated Vultr provider UX

This commit is contained in:
Ivan Gromov 2021-05-30 01:09:00 +05:00
parent 27d21db3c9
commit 7d2ddffcd7
2 changed files with 62 additions and 20 deletions

View file

@ -243,23 +243,35 @@ async def gce_regions(request):
@routes.get('/vultr_config')
async def check_vultr_config(request):
default_path = expanduser(join('~', '.vultr.ini'))
response = {'has_secret': False}
try:
open(default_path, 'r').read()
response['has_secret'] = True
except IOError:
pass
if 'VULTR_API_CONFIG' in os.environ:
try:
open(os.environ['VULTR_API_CONFIG'], 'r').read()
response['has_secret'] = True
except IOError:
pass
try:
default_path = expanduser(join('~', '.vultr.ini'))
open(default_path, 'r').read()
response['has_secret'] = True
except IOError:
pass
return web.json_response(response)
@routes.post('/vultr_config')
async def save_vultr_config(request):
data = await request.json()
token = data.get('token')
path = os.environ.get('VULTR_API_CONFIG') or expanduser(join('~', '.vultr.ini'))
with open(path, 'w') as f:
try:
f.write('[default]\nkey = {0}'.format(token))
except IOError:
return web.json_response({'error': 'can not save config file'}, status=400)
return web.json_response({'saved_to': path})
@routes.get('/vultr_regions')
async def vultr_regions(_):
async with ClientSession() as session:
@ -327,7 +339,7 @@ async def linode_regions(_):
@routes.get('/cloudstack_config')
async def get_cloudstack_config(_):
async def check_cloudstack_config(_):
if not HAS_REQUESTS:
return web.json_response({'error': 'missing_requests'}, status=400)
if not HAS_CS_LIBRARIES:
@ -367,7 +379,7 @@ async def cloudstack_regions(request):
}, status=400)
# if config was passed from client, save it after successful zone retrieval
if _read_cloudstack_config() is None:
path = os.environ['CLOUDSTACK_CONFIG'] or expanduser(join('~', '.cloudstack.ini'))
path = os.environ.get('CLOUDSTACK_CONFIG') or expanduser(join('~', '.cloudstack.ini'))
with open(path, 'w') as f:
try:
f.write(client_config)

View file

@ -1,11 +1,11 @@
<template>
<div>
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert">
Vultr config file was found in your system
</div>
<div v-else class="form-group">
<label
>Enter the local path to your configuration INI file
>Enter Vultr API Token, it will be saved in your system
<a
href="https://trailofbits.github.io/algo/cloud-vultr.html"
title="https://trailofbits.github.io/algo/cloud-vultr.html"
@ -18,12 +18,13 @@
<input
type="text"
class="form-control"
name="vultr_config"
name="vultr_token"
v-bind:disabled="ui_loading_check"
v-model="vultr_config"
v-model="ui_token"
v-on:blur="save_config"
/>
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
Configuration file was found in your system. You still can change the path to it
<div v-if="vultr_config" class="form-text alert alert-success" role="alert">
The config file was saved on your system
</div>
</div>
@ -52,7 +53,8 @@ module.exports = {
vultr_config: null,
region: null,
// helper variables
ui_env_secrets: false,
ui_token: null,
ui_token_from_env: false,
ui_loading_check: false,
ui_loading_regions: false,
ui_region_options: []
@ -64,7 +66,7 @@ module.exports = {
},
computed: {
has_secrets() {
return this.ui_env_secrets || this.vultr_config;
return this.ui_token_from_env || this.vultr_config;
},
is_valid() {
return this.has_secrets && this.region;
@ -82,7 +84,7 @@ module.exports = {
})
.then(response => {
if (response.has_secret) {
this.ui_env_secrets = true;
this.ui_token_from_env = true;
this.load_regions();
} else if (response.error) {
this.ui_config_error = response.error;
@ -92,6 +94,34 @@ module.exports = {
this.ui_loading_check = false;
});
},
save_config() {
this.ui_loading_check = true;
fetch("/vultr_config", {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: this.ui_token
})
})
.then(r => {
if (r.status === 200 || r.status === 400) {
return r.json();
}
throw new Error(r.status);
})
.then(response => {
if ('saved_to' in response) {
this.vultr_config = response.saved_to;
} else if (response.error) {
this.ui_config_error = response.error;
}
})
.finally(() => {
this.ui_loading_check = false;
});
},
load_regions() {
this.ui_loading_regions = true;
fetch("/vultr_regions")
@ -99,7 +129,7 @@ module.exports = {
.then((data) => {
this.ui_region_options = Object.keys(data).map(k => ({
value: data[k].name,
key: data[k].name
key: data[k].DCID
}));
})
.finally(() => {
@ -110,7 +140,7 @@ module.exports = {
let submit_value = {
region: this.region
}
if (!this.ui_env_secrets) {
if (!this.ui_token_from_env) {
submit_value['vultr_config'] = this.vultr_config;
}
this.$emit("submit", submit_value);