env-manager.vue 9.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!--
  - Copyright 2014-2018 the original author or authors.
  -
  - Licensed under the Apache License, Version 2.0 (the "License");
  - you may not use this file except in compliance with the License.
  - You may obtain a copy of the License at
  -
  -     http://www.apache.org/licenses/LICENSE-2.0
  -
  - Unless required by applicable law or agreed to in writing, software
  - distributed under the License is distributed on an "AS IS" BASIS,
  - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - See the License for the specific language governing permissions and
  - limitations under the License.
  -->

<template>
18 19 20 21 22 23 24 25 26 27 28 29 30
  <div class="box">
    <h1 class="is-size-5">Environment Manager</h1>
    <datalist id="allPropertyNames">
      <option v-for="name in allPropertyNames" :key="name" v-text="name"/>
    </datalist>
    <div class="field is-horizontal" v-for="(prop, index) in managedProperties" :key="`managed-${index}`">
      <div class="field-body">
        <div class="field">
          <div class="control">
            <input class="input" type="text" placeholder="Property name" list="allPropertyNames"
                   v-model="prop.name" @input="handlePropertyNameChange(prop, index)">
          </div>
          <p class="help is-danger" v-text="prop.validation"/>
31
        </div>
32 33 34 35 36 37 38 39 40 41 42 43 44 45
        <div class="field">
          <div class="control has-icons-right" :class="{'is-loading' : prop.status === 'executing'}">
            <input class="input" type="text" placeholder="Value" v-model="prop.input"
                   @input="prop.status = null">
            <span class="icon is-right has-text-success" v-if="prop.status === 'completed'">
              <font-awesome-icon icon="check"/>
            </span>
            <span class="icon is-right has-text-warning" v-else-if="prop.status === 'failed'">
              <font-awesome-icon icon="exclamation-triangle"/>
            </span>
            <span class="icon is-right" v-else-if="prop.input !== prop.value">
              <font-awesome-icon icon="pencil-alt"/>
            </span>
          </div>
46
        </div>
47
      </div>
48
    </div>
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    <div class="field is-horizontal">
      <div class="field-body" v-if="instance.hasEndpoint('refresh')">
        <div class="field">
          <div class="control">
            <sba-confirm-button class="button is-light"
                                :class="{'is-loading' : refreshStatus === 'executing', 'is-danger' : refreshStatus === 'failed', 'is-info' : refreshStatus === 'completed'}"
                                :disabled="refreshStatus === 'executing'"
                                @click="refreshContext">
              <span v-if="refreshStatus === 'completed'">Context refreshed</span>
              <span v-else-if="refreshStatus === 'failed'">Failed</span>
              <span v-else>Refresh Context</span>
            </sba-confirm-button>
          </div>
        </div>
      </div>
      <div class="field-body">
        <div class="field is-grouped is-grouped-right">
          <div class="control">
            <button class="button is-light"
                    :class="{'is-loading' : resetStatus === 'executing', 'is-danger' : resetStatus === 'failed', 'is-success' : resetStatus === 'completed'}"
                    :disabled="!hasManagedProperty || resetStatus === 'executing'"
                    @click="resetEnvironment">
              <span v-if="resetStatus === 'completed'">Resetted</span>
              <span v-else-if="resetStatus === 'failed'">Failed</span>
              <span v-else>Reset</span>
            </button>
          </div>
          <div class="control">
            <button class="button is-primary"
                    :class="{'is-loading' : updateStatus === 'executing', 'is-danger' : updateStatus === 'failed', 'is-success' : updateStatus === 'completed'}"
                    :disabled="hasErrorProperty || !hasChangedProperty || updateStatus === 'executing'"
                    @click="updateEnvironment">
              <span v-if="updateStatus === 'completed'">Updated</span>
              <span v-else-if="updateStatus === 'failed'">Failed</span>
              <span v-else>Update</span>
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
90 91 92
</template>

<script>
93
  import Instance from '@/services/instance';
94 95 96 97
  import {Observable} from '@/utils/rxjs';
  import _ from 'lodash';

  export default {
98 99 100 101 102 103 104 105 106 107
    props: {
      instance: {
        type: Instance,
        required: true
      },
      propertySources: {
        type: Array,
        default: () => []
      }
    },
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    data: () => ({
      error: null,
      refreshStatus: null,
      resetStatus: null,
      updateStatus: null,
      managedProperties: [{
        name: null,
        input: null,
        value: null,
        status: null,
        validation: null
      }]
    }),
    computed: {
      allPropertyNames() {
        return _.uniq(this.propertySources.map(ps => Object.keys(ps.properties))
          .reduce((result, names) => result.concat(names))
          .sort());
      },
      managerPropertySource() {
        return this.propertySources.find(ps => ps.name === 'manager') || {name: 'manager', properties: []};
      },
      hasManagedProperty() {
        return this.managedProperties.findIndex(property => !!property.name) >= 0;
      },
      hasChangedProperty() {
        return this.managedProperties.findIndex(property => property.input !== property.value) >= 0;
      },
      hasErrorProperty() {
        return this.managedProperties.findIndex(property => property.validation !== null) >= 0;
      }
    },
    methods: {
      handlePropertyNameChange: _.debounce(function (prop, idx) {
        if (prop.name && idx === this.managedProperties.length - 1) {
          this.managedProperties.push({
            name: null,
            input: null,
            value: null,
            status: null,
            validation: null
          });
        }
      }, 250),
      refreshContext() {
        const vm = this;
        Observable.from(vm.instance.refreshContext())
          .listen(status => vm.refreshStatus = status)
          .subscribe({
            complete: () => {
              setTimeout(() => vm.refreshStatus = null, 2500);
              return vm.$emit('reset');
            },
            error: () => vm.$emit('reset')
          });
      },
      updateEnvironment() {
        const vm = this;
        Observable.from(vm.managedProperties)
          .filter(property => !!property.name && property.input !== property.value)
          .listen(status => vm.updateStatus = status)
          .concatMap(property => Observable.from(vm.instance.setEnv(property.name, property.input))
            .listen(status => property.status = status)
          )
          .subscribe({
            complete: () => {
              setTimeout(() => vm.updateStatus = null, 2500);
              return vm.$emit('update');
            },
            error: () => vm.$emit('update')
          });
      },
      resetEnvironment() {
        const vm = this;
        Observable.from(vm.instance.resetEnv())
          .listen(status => vm.resetStatus = status)
          .subscribe({
            complete: () => {
              vm.managedProperties = [{
                name: null,
                input: null,
                value: null,
                status: null,
                validation: null
              }];
              setTimeout(() => vm.resetStatus = null, 2500);
              return vm.$emit('refresh');
            },
            error: () => vm.$emit('refresh')
          });
      },
      updateManagedProperties(manager) {
        _.entries(manager.properties).forEach(([name, property]) => {
          const managedProperty = this.managedProperties.find(property => property.name === name);
          if (managedProperty) {
            managedProperty.value = property.value
          } else {
            const idx = this.managedProperties.length - 1;
            this.managedProperties.splice(idx, 0, {
              name,
              input: property.value,
              value: property.value,
              status: null,
              validation: null
            })
          }
        });
      }
    },
    created() {
      this.updateManagedProperties(this.managerPropertySource);
    },
    watch: {
      managerPropertySource(newVal) {
        this.updateManagedProperties(newVal);
      },
      managedProperties: {
        deep: true,
        handler() {
          const counts = this.managedProperties.reduce(
            (acc, v) => {
              if (v.name) {
                acc[v.name] = (acc[v.name] || 0) + 1;
              }
              return acc;
            }, {});
          this.managedProperties.forEach(property => {
            if (!property.name) {
              if (property.input) {
                property.validation = 'Property name is required';
              }
              return;
            }
            const count = counts[property.name] || 0;
            if (count > 1) {
              property.validation = 'Property name must be unique';
              return;
            }
            property.validation = null;
          });
        }
      }
    }
  }
252
</script>