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') @routes.get('/vultr_config')
async def check_vultr_config(request): async def check_vultr_config(request):
default_path = expanduser(join('~', '.vultr.ini'))
response = {'has_secret': False} response = {'has_secret': False}
try:
open(default_path, 'r').read()
response['has_secret'] = True
except IOError:
pass
if 'VULTR_API_CONFIG' in os.environ: if 'VULTR_API_CONFIG' in os.environ:
try: try:
open(os.environ['VULTR_API_CONFIG'], 'r').read() open(os.environ['VULTR_API_CONFIG'], 'r').read()
response['has_secret'] = True response['has_secret'] = True
except IOError: except IOError:
pass 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) 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') @routes.get('/vultr_regions')
async def vultr_regions(_): async def vultr_regions(_):
async with ClientSession() as session: async with ClientSession() as session:
@ -327,7 +339,7 @@ async def linode_regions(_):
@routes.get('/cloudstack_config') @routes.get('/cloudstack_config')
async def get_cloudstack_config(_): async def check_cloudstack_config(_):
if not HAS_REQUESTS: if not HAS_REQUESTS:
return web.json_response({'error': 'missing_requests'}, status=400) return web.json_response({'error': 'missing_requests'}, status=400)
if not HAS_CS_LIBRARIES: if not HAS_CS_LIBRARIES:
@ -367,7 +379,7 @@ async def cloudstack_regions(request):
}, status=400) }, status=400)
# if config was passed from client, save it after successful zone retrieval # if config was passed from client, save it after successful zone retrieval
if _read_cloudstack_config() is None: 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: with open(path, 'w') as f:
try: try:
f.write(client_config) f.write(client_config)

View file

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