All files / app/assets/javascripts/clusters clusters_bundle.js

79.31% Statements 92/116
58.82% Branches 30/51
55.17% Functions 16/29
80% Lines 92/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304                            1x   1x                               17x   17x 17x 17x   17x 17x           17x 17x 17x 17x 17x         17x 17x 17x 17x 17x     17x 17x   17x 17x 17x   17x   17x                   17x   17x 17x 17x             17x       17x 17x   17x 17x                                                 17x 17x                                     2x 2x       17x 17x       34x               17x 17x       17x             17x 17x     17x                                                   2x   2x   2x       27x 27x 27x 27x 27x       30x 30x           8x 8x     8x       3x 3x           18x   18x 18x     18x   18x       30x     30x   30x 27x   27x   20x 2x 2x 18x 1x 1x   20x   2x 2x 2x   1x 1x   1x 1x     3x 3x             15x   15x 15x     15x          
import { GlToast } from '@gitlab/ui';
import Visibility from 'visibilityjs';
import Vue from 'vue';
import { createAlert } from '~/alert';
import AccessorUtilities from '~/lib/utils/accessor';
import Poll from '~/lib/utils/poll';
import { s__ } from '~/locale';
import PersistentUserCallout from '~/persistent_user_callout';
import initSettingsPanels from '~/settings_panels';
import { initProjectSelects } from '~/vue_shared/components/entity_select/init_project_selects';
import RemoveClusterConfirmation from './components/remove_cluster_confirmation.vue';
import ClustersService from './services/clusters_service';
import ClustersStore from './stores/clusters_store';
 
const Environments = () => import('ee_component/clusters/components/environments.vue');
 
Vue.use(GlToast);
 
export default class Clusters {
  constructor() {
    const {
      statusPath,
      clusterEnvironmentsPath,
      hasRbac,
      providerType,
      clusterStatus,
      clusterStatusReason,
      helpPath,
      environmentsHelpPath,
      clustersHelpPath,
      deployBoardsHelpPath,
      clusterId,
    } = document.querySelector('.js-edit-cluster-form').dataset;
 
    this.clusterId = clusterId;
    this.clusterNewlyCreatedKey = `cluster_${this.clusterId}_newly_created`;
    this.clusterBannerDismissedKey = `cluster_${this.clusterId}_banner_dismissed`;
 
    this.store = new ClustersStore();
    this.store.setHelpPaths({
      helpPath,
      environmentsHelpPath,
      clustersHelpPath,
      deployBoardsHelpPath,
    });
    this.store.updateStatus(clusterStatus);
    this.store.updateStatusReason(clusterStatusReason);
    this.store.updateProviderType(providerType);
    this.store.updateRbac(hasRbac);
    this.service = new ClustersService({
      endpoint: statusPath,
      clusterEnvironmentsEndpoint: clusterEnvironmentsPath,
    });
 
    this.errorContainer = document.querySelector('.js-cluster-error');
    this.successContainer = document.querySelector('.js-cluster-success');
    this.creatingContainer = document.querySelector('.js-cluster-creating');
    this.unreachableContainer = document.querySelector('.js-cluster-api-unreachable');
    this.authenticationFailureContainer = document.querySelector(
      '.js-cluster-authentication-failure',
    );
    this.errorReasonContainer = this.errorContainer.querySelector('.js-error-reason');
    this.tokenField = document.querySelector('.js-cluster-token');
 
    initProjectSelects();
    Clusters.initDismissableCallout();
    initSettingsPanels();
 
    this.initEnvironments();
 
    Iif (clusterEnvironmentsPath && this.environments) {
      this.store.toggleFetchEnvironments(true);
 
      this.initPolling(
        'fetchClusterEnvironments',
        (data) => this.handleClusterEnvironmentsSuccess(data),
        () => this.handleEnvironmentsPollError(),
      );
    }
 
    this.updateContainer(null, this.store.state.status, this.store.state.statusReason);
 
    this.addListeners();
    Eif (statusPath && !this.environments) {
      this.initPolling(
        'fetchClusterStatus',
        (data) => this.handleClusterStatusSuccess(data),
        () => this.handlePollError(),
      );
    }
 
    this.initRemoveClusterActions();
  }
 
  initEnvironments() {
    const { store } = this;
    const el = document.querySelector('#js-cluster-environments');
 
    Eif (!el) {
      return;
    }
 
    this.environments = new Vue({
      el,
      data() {
        return {
          state: store.state,
        };
      },
      render(createElement) {
        return createElement(Environments, {
          props: {
            isFetching: this.state.fetchingEnvironments,
            environments: this.state.environments,
            environmentsHelpPath: this.state.environmentsHelpPath,
            clustersHelpPath: this.state.clustersHelpPath,
            deployBoardsHelpPath: this.state.deployBoardsHelpPath,
          },
        });
      },
    });
  }
 
  initRemoveClusterActions() {
    const el = document.querySelector('#js-cluster-remove-actions');
    Iif (el && el.dataset) {
      const { clusterName, clusterPath, hasManagementProject } = el.dataset;
 
      this.removeClusterAction = new Vue({
        el,
        render(createElement) {
          return createElement(RemoveClusterConfirmation, {
            props: {
              clusterName,
              clusterPath,
              hasManagementProject,
            },
          });
        },
      });
    }
  }
 
  handleClusterEnvironmentsSuccess(data) {
    this.store.toggleFetchEnvironments(false);
    this.store.updateEnvironments(data.data);
  }
 
  static initDismissableCallout() {
    const callout = document.querySelector('.js-cluster-security-warning');
    PersistentUserCallout.factory(callout);
  }
 
  addBannerCloseHandler(el, status) {
    el.querySelector('.js-close').addEventListener('click', () => {
      el.classList.add('hidden');
      this.setBannerDismissedState(status, true);
    });
  }
 
  addListeners() {
    // Add event listener to all the banner close buttons
    this.addBannerCloseHandler(this.unreachableContainer, 'unreachable');
    this.addBannerCloseHandler(this.authenticationFailureContainer, 'authentication_failure');
  }
 
  initPolling(method, successCallback, errorCallback) {
    this.poll = new Poll({
      resource: this.service,
      method,
      successCallback,
      errorCallback,
    });
 
    Eif (!Visibility.hidden()) {
      this.poll.makeRequest();
    }
 
    Visibility.change(() => {
      if (!Visibility.hidden() && !this.destroyed) {
        this.poll.restart();
      } else {
        this.poll.stop();
      }
    });
  }
 
  handlePollError() {
    this.constructor.handleError();
  }
 
  handleEnvironmentsPollError() {
    this.store.toggleFetchEnvironments(false);
 
    this.handlePollError();
  }
 
  static handleError() {
    createAlert({
      message: s__('ClusterIntegration|Something went wrong on our end.'),
    });
  }
 
  handleClusterStatusSuccess(data) {
    const prevStatus = this.store.state.status;
 
    this.store.updateStateFromServer(data.data);
 
    this.updateContainer(prevStatus, this.store.state.status, this.store.state.statusReason);
  }
 
  hideAll() {
    this.errorContainer.classList.add('hidden');
    this.successContainer.classList.add('hidden');
    this.creatingContainer.classList.add('hidden');
    this.unreachableContainer.classList.add('hidden');
    this.authenticationFailureContainer.classList.add('hidden');
  }
 
  setBannerDismissedState(status, isDismissed) {
    Eif (AccessorUtilities.canUseLocalStorage()) {
      window.localStorage.setItem(this.clusterBannerDismissedKey, `${status}_${isDismissed}`);
    }
  }
 
  isBannerDismissed(status) {
    let bannerState;
    Eif (AccessorUtilities.canUseLocalStorage()) {
      bannerState = window.localStorage.getItem(this.clusterBannerDismissedKey);
    }
 
    return bannerState === `${status}_true`;
  }
 
  setClusterNewlyCreated(state) {
    Eif (AccessorUtilities.canUseLocalStorage()) {
      window.localStorage.setItem(this.clusterNewlyCreatedKey, Boolean(state));
    }
  }
 
  isClusterNewlyCreated() {
    // once this is true, it will always be true for a given page load
    Eif (!this.isNewlyCreated) {
      let newlyCreated;
      Eif (AccessorUtilities.canUseLocalStorage()) {
        newlyCreated = window.localStorage.getItem(this.clusterNewlyCreatedKey);
      }
 
      this.isNewlyCreated = newlyCreated === 'true';
    }
    return this.isNewlyCreated;
  }
 
  updateContainer(prevStatus, status, error) {
    Iif (status !== 'created' && this.isBannerDismissed(status)) {
      return;
    }
    this.setBannerDismissedState(status, false);
 
    if (prevStatus !== status) {
      this.hideAll();
 
      switch (status) {
        case 'created':
          if (this.isClusterNewlyCreated()) {
            this.setClusterNewlyCreated(false);
            this.successContainer.classList.remove('hidden');
          } else if (prevStatus) {
            this.setClusterNewlyCreated(true);
            window.location.reload();
          }
          break;
        case 'errored':
          this.errorContainer.classList.remove('hidden');
          this.errorReasonContainer.textContent = error;
          break;
        case 'unreachable':
          this.unreachableContainer.classList.remove('hidden');
          break;
        case 'authentication_failure':
          this.authenticationFailureContainer.classList.remove('hidden');
          break;
        case 'scheduled':
        case 'creating':
          this.creatingContainer.classList.remove('hidden');
          break;
        default:
      }
    }
  }
 
  destroy() {
    this.destroyed = true;
 
    Eif (this.poll) {
      this.poll.stop();
    }
 
    Iif (this.environments) {
      this.environments.$destroy();
    }
  }
}