Unified UX for Vultr & Scaleway

This commit is contained in:
Ivan Gromov 2020-11-03 02:12:43 +05:00
parent a8ccad9ed4
commit 2aeb292be5
4 changed files with 82 additions and 36 deletions

View file

@ -17,6 +17,13 @@ try:
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
try: try:
from google.auth.transport.requests import AuthorizedSession from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account from google.oauth2 import service_account
@ -174,6 +181,10 @@ async def ec2_regions(request):
@routes.get('/gce_config') @routes.get('/gce_config')
async def check_gce_config(_): async def check_gce_config(_):
if not HAS_REQUESTS:
return web.json_response({'error': 'missing_requests'}, status=400)
if not HAS_GOOGLE_LIBRARIES:
return web.json_response({'error': 'missing_google'}, status=400)
gce_file = join(PROJECT_ROOT, 'configs', 'gce.json') gce_file = join(PROJECT_ROOT, 'configs', 'gce.json')
response = {} response = {}
try: try:
@ -213,17 +224,17 @@ 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')) default_path = expanduser(join('~', '.vultr.ini'))
response = {'path': None} response = {'has_secret': False}
try: try:
open(default_path, 'r').read() open(default_path, 'r').read()
response['path'] = default_path response['has_secret'] = True
except IOError: except IOError:
pass 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['path'] = os.environ['VULTR_API_CONFIG'] response['has_secret'] = True
except IOError: except IOError:
pass pass
return web.json_response(response) return web.json_response(response)
@ -239,7 +250,7 @@ async def vultr_regions(_):
@routes.get('/scaleway_config') @routes.get('/scaleway_config')
async def check_scaleway_config(_): async def check_scaleway_config(_):
return web.json_response({"ok": 'SCW_TOKEN' in os.environ}) return web.json_response({"has_secret": 'SCW_TOKEN' in os.environ})
app = web.Application() app = web.Application()

View file

@ -1,5 +1,11 @@
<template> <template>
<div> <div>
<div v-if="ui_config_error && ui_config_error === 'missing_google'" class="form-text alert alert-danger" role="alert">
Python module "google-auth" is missing, please install it to proceed
</div>
<div v-if="ui_config_error && ui_config_error === 'missing_requests'" class="form-text alert alert-danger" role="alert">
Python module "requests" is missing, please install it to proceed
</div>
<div <div
class="form-group dropzone" class="form-group dropzone"
v-if="ui_needs_upload" v-if="ui_needs_upload"
@ -31,14 +37,14 @@
<strong>{{ ui_drop_filename }} loaded successfully</strong> <strong>{{ ui_drop_filename }} loaded successfully</strong>
</div> </div>
</div> </div>
<input type="file" accept=".json,applciation/json" v-on:change="filechange_handler" /> <div v-else class="form-text alert alert-success" role="alert">
Google Compute Engine credentials were found in the project
<div class="form-group">
<region-select v-model="region" v-bind:options="ui_region_options" v-bind:loading="ui_loading_check || ui_loading_regions">
<label>Please specify <code>gce.json</code> credentials file to select region</label>
</region-select>
</div> </div>
<input type="file" accept=".json,applciation/json" v-on:change="filechange_handler" />
<region-select v-model="region" v-bind:options="ui_region_options" v-bind:loading="ui_loading_check || ui_loading_regions">
<label v-if="ui_needs_upload">Please select region</label>
<label v-else>Please specify <code>gce.json</code> credentials file to select region</label>
</region-select>
<button <button
class="btn btn-primary" class="btn btn-primary"
type="button" type="button"
@ -62,6 +68,7 @@ module.exports = {
ui_drop_error: null, ui_drop_error: null,
ui_drop_success: null, ui_drop_success: null,
ui_drop_filename: null, ui_drop_filename: null,
ui_config_error: null,
ui_needs_upload: null, ui_needs_upload: null,
ui_loading_regions: false, ui_loading_regions: false,
ui_loading_check: false, ui_loading_check: false,
@ -140,12 +147,19 @@ module.exports = {
check_config() { check_config() {
this.ui_loading_check = true; this.ui_loading_check = true;
fetch("/gce_config") fetch("/gce_config")
.then(r => r.json()) .then(r => {
if (r.status === 200 || r.status === 400) {
return r.json();
}
throw new Error(r.status);
})
.then(response => { .then(response => {
if (response.status === 'ok') { if (response.status === 'ok') {
this.gce_credentials_file = 'configs/gce.json'; this.gce_credentials_file = 'configs/gce.json';
this.load_regions(); this.load_regions();
this.ui_needs_upload = false; this.ui_needs_upload = false;
} else if (response.error) {
this.ui_config_error = response.error;
} else { } else {
this.ui_needs_upload = true; this.ui_needs_upload = true;
} }

View file

@ -1,7 +1,10 @@
<template> <template>
<div> <div>
<div class="form-group"> <div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
Token was read from the environment variable
</div>
<div v-else class="form-group">
<label <label
>Enter your auth token >Enter your auth token
<a <a
@ -17,14 +20,10 @@
type="text" type="text"
class="form-control" class="form-control"
name="scaleway_token" name="scaleway_token"
v-bind:disabled="ui_loading_check || ui_token_from_env" v-bind:disabled="ui_loading_check"
v-model="scaleway_token" v-model="scaleway_token"
/> />
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert">
Token was read from the environment variable
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<region-select v-model="region" v-bind:options="ui_region_options"> <region-select v-model="region" v-bind:options="ui_region_options">
</region-select> </region-select>
@ -48,7 +47,7 @@ module.exports = {
scaleway_token: null, scaleway_token: null,
region: null, region: null,
// helper variables // helper variables
ui_token_from_env: false, ui_env_secrets: false,
ui_loading_check: false, ui_loading_check: false,
ui_region_options: [ ui_region_options: [
{value: 'Paris 1', key: 'par1'}, {value: 'Paris 1', key: 'par1'},
@ -61,17 +60,24 @@ module.exports = {
}, },
computed: { computed: {
is_valid() { is_valid() {
return this.region && (this.scaleway_token || this.ui_token_from_env); return this.region && (this.scaleway_token || this.ui_env_secrets);
} }
}, },
methods: { methods: {
check_config() { check_config() {
this.ui_loading_check = true; this.ui_loading_check = true;
fetch("/scaleway_config") fetch("/scaleway_config")
.then(r => r.json()) .then(r => {
if (r.status === 200 || r.status === 400) {
return r.json();
}
throw new Error(r.status);
})
.then(response => { .then(response => {
if (response.ok) { if (response.has_secret) {
this.ui_token_from_env = true; this.ui_env_secrets = true;
} else if (response.error) {
this.ui_config_error = response.error;
} }
}) })
.finally(() => { .finally(() => {
@ -79,7 +85,7 @@ module.exports = {
}); });
}, },
submit() { submit() {
if (this.ui_token_from_env) { if (this.ui_env_secrets) {
this.$emit("submit", { this.$emit("submit", {
region: this.region region: this.region
}); });

View file

@ -1,7 +1,9 @@
<template> <template>
<div> <div>
<div v-if="ui_env_secrets" class="form-text alert alert-success" role="alert">
<div class="form-group"> Vultr config file was found in your system
</div>
<div v-else class="form-group">
<label <label
>Enter the local path to your configuration INI file >Enter the local path to your configuration INI file
<a <a
@ -20,7 +22,7 @@
v-bind:disabled="ui_loading_check" v-bind:disabled="ui_loading_check"
v-model="vultr_config" v-model="vultr_config"
/> />
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert"> <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 Configuration file was found in your system. You still can change the path to it
</div> </div>
</div> </div>
@ -50,7 +52,7 @@ module.exports = {
vultr_config: null, vultr_config: null,
region: null, region: null,
// helper variables // helper variables
ui_token_from_env: false, ui_env_secrets: false,
ui_loading_check: false, ui_loading_check: false,
ui_loading_regions: false, ui_loading_regions: false,
ui_region_options: [] ui_region_options: []
@ -61,19 +63,29 @@ module.exports = {
this.load_regions(); this.load_regions();
}, },
computed: { computed: {
has_secrets() {
return this.ui_env_secrets || this.vultr_config;
},
is_valid() { is_valid() {
return this.vultr_config && this.region; return this.has_secrets && this.region;
} }
}, },
methods: { methods: {
check_config() { check_config() {
this.ui_loading_check = true; this.ui_loading_check = true;
fetch("/vultr_config") fetch("/vultr_config")
.then(r => r.json()) .then(r => {
if (r.status === 200 || r.status === 400) {
return r.json();
}
throw new Error(r.status);
})
.then(response => { .then(response => {
if (response.path) { if (response.has_secret) {
this.vultr_config = response.path; this.ui_env_secrets = true;
this.ui_token_from_env = true; this.load_regions();
} else if (response.error) {
this.ui_config_error = response.error;
} }
}) })
.finally(() => { .finally(() => {
@ -95,10 +107,13 @@ module.exports = {
}); });
}, },
submit() { submit() {
this.$emit("submit", { let submit_value = {
vultr_config: this.vultr_config,
region: this.region region: this.region
}); }
if (!this.ui_env_secrets) {
submit_value['vultr_config'] = this.vultr_config;
}
this.$emit("submit", submit_value);
}, },
}, },
components: { components: {