|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div v-if="ui_token_from_env"> |
| 4 | + <div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert"> |
| 5 | + The token was read from the environment variable |
| 6 | + </div> |
| 7 | + </div> |
| 8 | + <div class="form-group" v-else> |
| 9 | + <label for="id_hcloud_token"> |
| 10 | + Enter your API token. The token must have read and write permissions |
| 11 | + <a href="https://github.com/trailofbits/algo/blob/master/docs/cloud-hetzner.md" title="https://github.com/trailofbits/algo/blob/master/docs/cloud-hetzner.md" class="badge bagde-pill badge-primary" target="_blank" rel="noopener noreferrer">?</a> |
| 12 | + </label> |
| 13 | + <input |
| 14 | + type="text" |
| 15 | + class="form-control" |
| 16 | + id="id_hcloud_token" |
| 17 | + name="hcloud_token" |
| 18 | + v-bind:disabled="ui_loading_check" |
| 19 | + v-model="hcloud_token" |
| 20 | + @blur="load_regions" |
| 21 | + /> |
| 22 | + </div> |
| 23 | + <region-select v-model="region" |
| 24 | + v-bind:options="ui_region_options" |
| 25 | + v-bind:loading="ui_loading_check || ui_loading_regions" |
| 26 | + v-bind:error="ui_region_error"> |
| 27 | + </region-select> |
| 28 | + <button v-on:click="submit" |
| 29 | + v-bind:disabled="!is_valid" class="btn btn-primary" type="button">Next</button> |
| 30 | + </div> |
| 31 | +</template> |
| 32 | + |
| 33 | +<script> |
| 34 | +module.exports = { |
| 35 | + data: function() { |
| 36 | + return { |
| 37 | + hcloud_token: null, |
| 38 | + region: null, |
| 39 | + // helper variables |
| 40 | + ui_loading_check: false, |
| 41 | + ui_loading_regions: false, |
| 42 | + ui_region_error: null, |
| 43 | + ui_token_from_env: false, |
| 44 | + ui_region_options: [] |
| 45 | + } |
| 46 | + }, |
| 47 | + computed: { |
| 48 | + is_valid() { |
| 49 | + return (this.hcloud_config || this.ui_token_from_env) && this.region; |
| 50 | + } |
| 51 | + }, |
| 52 | + created: function() { |
| 53 | + this.check_config(); |
| 54 | + }, |
| 55 | + methods: { |
| 56 | + check_config() { |
| 57 | + this.ui_loading_check = true; |
| 58 | + return fetch("/hetzner_config") |
| 59 | + .then(r => r.json()) |
| 60 | + .then(response => { |
| 61 | + if (response.has_secret) { |
| 62 | + this.ui_token_from_env = true; |
| 63 | + this.load_regions(); |
| 64 | + } |
| 65 | + }) |
| 66 | + .finally(() => { |
| 67 | + this.ui_loading_check = false; |
| 68 | + }); |
| 69 | + }, |
| 70 | + load_regions() { |
| 71 | + if (this.ui_token_from_env || this.hcloud_token) { |
| 72 | + this.ui_loading_regions = true; |
| 73 | + this.ui_region_error = null; |
| 74 | + const payload = this.ui_token_from_env ? {} : { |
| 75 | + token: this.hcloud_token |
| 76 | + }; |
| 77 | + fetch("/hetzner_regions", { |
| 78 | + method: 'post', |
| 79 | + headers: { |
| 80 | + 'Content-Type': 'application/json' |
| 81 | + }, |
| 82 | + body: JSON.stringify(payload) |
| 83 | + }) |
| 84 | + .then((r) => { |
| 85 | + if (r.status === 200) { |
| 86 | + return r.json(); |
| 87 | + } |
| 88 | + throw new Error(r.status); |
| 89 | + }) |
| 90 | + .then((data) => { |
| 91 | + this.ui_region_options = data.datacenters.map(i => ({key: i.location.name, value: i.location.city})); |
| 92 | + }) |
| 93 | + .catch((err) => { |
| 94 | + this.ui_region_error = err; |
| 95 | + }) |
| 96 | + .finally(() => { |
| 97 | + this.ui_loading_regions = false; |
| 98 | + }); |
| 99 | + } |
| 100 | + }, |
| 101 | + submit() { |
| 102 | + if (this.ui_token_from_env) { |
| 103 | + this.$emit("submit", { |
| 104 | + region: this.region |
| 105 | + }); |
| 106 | + } else { |
| 107 | + this.$emit("submit", { |
| 108 | + hcloud_token: this.hcloud_token, |
| 109 | + region: this.region |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + }, |
| 114 | + components: { |
| 115 | + "region-select": window.httpVueLoader("/static/region-select.vue"), |
| 116 | + } |
| 117 | +}; |
| 118 | +</script> |
0 commit comments