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"
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