debian-mirror-gitlab/app/assets/javascripts/clusters/clusters_bundle.js

304 lines
8.6 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlToast } from '@gitlab/ui';
2018-03-17 18:26:18 +05:30
import Visibility from 'visibilityjs';
import Vue from 'vue';
2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2020-01-01 13:55:28 +05:30
import AccessorUtilities from '~/lib/utils/accessor';
2021-03-11 19:13:27 +05:30
import initProjectSelectDropdown from '~/project_select';
2022-06-21 17:19:12 +05:30
import Poll from '~/lib/utils/poll';
import { s__ } from '~/locale';
import PersistentUserCallout from '~/persistent_user_callout';
import initSettingsPanels from '~/settings_panels';
2021-03-11 19:13:27 +05:30
import RemoveClusterConfirmation from './components/remove_cluster_confirmation.vue';
2018-03-17 18:26:18 +05:30
import ClustersService from './services/clusters_service';
import ClustersStore from './stores/clusters_store';
2019-12-04 20:38:33 +05:30
const Environments = () => import('ee_component/clusters/components/environments.vue');
2019-07-31 22:56:46 +05:30
Vue.use(GlToast);
2018-03-17 18:26:18 +05:30
export default class Clusters {
constructor() {
const {
statusPath,
2019-12-04 20:38:33 +05:30
clusterEnvironmentsPath,
2019-02-15 15:39:39 +05:30
hasRbac,
2019-12-21 20:55:43 +05:30
providerType,
2018-03-17 18:26:18 +05:30
clusterStatus,
clusterStatusReason,
helpPath,
2019-12-04 20:38:33 +05:30
environmentsHelpPath,
clustersHelpPath,
deployBoardsHelpPath,
2019-09-04 21:01:54 +05:30
clusterId,
2018-03-17 18:26:18 +05:30
} = document.querySelector('.js-edit-cluster-form').dataset;
2019-09-04 21:01:54 +05:30
this.clusterId = clusterId;
2019-10-12 21:52:04 +05:30
this.clusterNewlyCreatedKey = `cluster_${this.clusterId}_newly_created`;
this.clusterBannerDismissedKey = `cluster_${this.clusterId}_banner_dismissed`;
2018-03-17 18:26:18 +05:30
this.store = new ClustersStore();
2021-02-22 17:27:13 +05:30
this.store.setHelpPaths({
2019-12-04 20:38:33 +05:30
helpPath,
environmentsHelpPath,
clustersHelpPath,
deployBoardsHelpPath,
2021-02-22 17:27:13 +05:30
});
2018-03-17 18:26:18 +05:30
this.store.updateStatus(clusterStatus);
this.store.updateStatusReason(clusterStatusReason);
2019-12-21 20:55:43 +05:30
this.store.updateProviderType(providerType);
2019-02-15 15:39:39 +05:30
this.store.updateRbac(hasRbac);
2018-03-17 18:26:18 +05:30
this.service = new ClustersService({
endpoint: statusPath,
2019-12-04 20:38:33 +05:30
clusterEnvironmentsEndpoint: clusterEnvironmentsPath,
2018-03-17 18:26:18 +05:30
});
this.errorContainer = document.querySelector('.js-cluster-error');
this.successContainer = document.querySelector('.js-cluster-success');
this.creatingContainer = document.querySelector('.js-cluster-creating');
2019-09-04 21:01:54 +05:30
this.unreachableContainer = document.querySelector('.js-cluster-api-unreachable');
this.authenticationFailureContainer = document.querySelector(
'.js-cluster-authentication-failure',
);
2018-03-17 18:26:18 +05:30
this.errorReasonContainer = this.errorContainer.querySelector('.js-error-reason');
this.tokenField = document.querySelector('.js-cluster-token');
2019-12-26 22:10:19 +05:30
initProjectSelectDropdown();
2019-02-15 15:39:39 +05:30
Clusters.initDismissableCallout();
2018-03-17 18:26:18 +05:30
initSettingsPanels();
2019-12-26 22:10:19 +05:30
2019-12-04 20:38:33 +05:30
this.initEnvironments();
if (clusterEnvironmentsPath && this.environments) {
this.store.toggleFetchEnvironments(true);
this.initPolling(
'fetchClusterEnvironments',
2021-03-08 18:12:59 +05:30
(data) => this.handleClusterEnvironmentsSuccess(data),
2019-12-04 20:38:33 +05:30
() => this.handleEnvironmentsPollError(),
);
}
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
this.updateContainer(null, this.store.state.status, this.store.state.statusReason);
2018-03-17 18:26:18 +05:30
this.addListeners();
2019-12-04 20:38:33 +05:30
if (statusPath && !this.environments) {
this.initPolling(
'fetchClusterStatus',
2021-03-08 18:12:59 +05:30
(data) => this.handleClusterStatusSuccess(data),
2019-12-04 20:38:33 +05:30
() => this.handlePollError(),
);
2018-03-17 18:26:18 +05:30
}
2020-01-01 13:55:28 +05:30
this.initRemoveClusterActions();
2018-03-17 18:26:18 +05:30
}
2019-12-04 20:38:33 +05:30
initEnvironments() {
const { store } = this;
const el = document.querySelector('#js-cluster-environments');
if (!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,
},
});
},
});
}
2020-01-01 13:55:28 +05:30
initRemoveClusterActions() {
const el = document.querySelector('#js-cluster-remove-actions');
if (el && el.dataset) {
2020-07-28 23:09:34 +05:30
const { clusterName, clusterPath, hasManagementProject } = el.dataset;
2020-01-01 13:55:28 +05:30
this.removeClusterAction = new Vue({
el,
render(createElement) {
return createElement(RemoveClusterConfirmation, {
props: {
clusterName,
clusterPath,
2020-07-28 23:09:34 +05:30
hasManagementProject,
2020-01-01 13:55:28 +05:30
},
});
},
});
}
}
2019-12-04 20:38:33 +05:30
handleClusterEnvironmentsSuccess(data) {
this.store.toggleFetchEnvironments(false);
this.store.updateEnvironments(data.data);
}
2019-02-15 15:39:39 +05:30
static initDismissableCallout() {
const callout = document.querySelector('.js-cluster-security-warning');
2019-07-07 11:18:12 +05:30
PersistentUserCallout.factory(callout);
2019-02-15 15:39:39 +05:30
}
2019-09-04 21:01:54 +05:30
addBannerCloseHandler(el, status) {
2020-11-24 15:15:51 +05:30
el.querySelector('.js-close').addEventListener('click', () => {
2019-09-04 21:01:54 +05:30
el.classList.add('hidden');
this.setBannerDismissedState(status, true);
});
}
2018-03-17 18:26:18 +05:30
addListeners() {
2019-09-04 21:01:54 +05:30
// Add event listener to all the banner close buttons
this.addBannerCloseHandler(this.unreachableContainer, 'unreachable');
this.addBannerCloseHandler(this.authenticationFailureContainer, 'authentication_failure');
2018-03-17 18:26:18 +05:30
}
2019-12-04 20:38:33 +05:30
initPolling(method, successCallback, errorCallback) {
2018-03-17 18:26:18 +05:30
this.poll = new Poll({
resource: this.service,
2019-12-04 20:38:33 +05:30
method,
successCallback,
errorCallback,
2018-03-17 18:26:18 +05:30
});
if (!Visibility.hidden()) {
this.poll.makeRequest();
}
Visibility.change(() => {
if (!Visibility.hidden() && !this.destroyed) {
this.poll.restart();
} else {
this.poll.stop();
}
});
}
2019-12-04 20:38:33 +05:30
handlePollError() {
this.constructor.handleError();
}
handleEnvironmentsPollError() {
this.store.toggleFetchEnvironments(false);
this.handlePollError();
}
2018-03-17 18:26:18 +05:30
static handleError() {
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-30 23:02:18 +05:30
message: s__('ClusterIntegration|Something went wrong on our end.'),
});
2018-03-17 18:26:18 +05:30
}
2019-12-04 20:38:33 +05:30
handleClusterStatusSuccess(data) {
2018-03-17 18:26:18 +05:30
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');
2019-09-04 21:01:54 +05:30
this.unreachableContainer.classList.add('hidden');
this.authenticationFailureContainer.classList.add('hidden');
2018-03-17 18:26:18 +05:30
}
2019-09-04 21:01:54 +05:30
setBannerDismissedState(status, isDismissed) {
2021-11-11 11:23:49 +05:30
if (AccessorUtilities.canUseLocalStorage()) {
2019-10-12 21:52:04 +05:30
window.localStorage.setItem(this.clusterBannerDismissedKey, `${status}_${isDismissed}`);
2019-09-04 21:01:54 +05:30
}
}
isBannerDismissed(status) {
let bannerState;
2021-11-11 11:23:49 +05:30
if (AccessorUtilities.canUseLocalStorage()) {
2019-10-12 21:52:04 +05:30
bannerState = window.localStorage.getItem(this.clusterBannerDismissedKey);
2019-09-04 21:01:54 +05:30
}
return bannerState === `${status}_true`;
}
2019-10-12 21:52:04 +05:30
setClusterNewlyCreated(state) {
2021-11-11 11:23:49 +05:30
if (AccessorUtilities.canUseLocalStorage()) {
2019-10-12 21:52:04 +05:30
window.localStorage.setItem(this.clusterNewlyCreatedKey, Boolean(state));
}
}
isClusterNewlyCreated() {
// once this is true, it will always be true for a given page load
if (!this.isNewlyCreated) {
let newlyCreated;
2021-11-11 11:23:49 +05:30
if (AccessorUtilities.canUseLocalStorage()) {
2019-10-12 21:52:04 +05:30
newlyCreated = window.localStorage.getItem(this.clusterNewlyCreatedKey);
}
this.isNewlyCreated = newlyCreated === 'true';
}
return this.isNewlyCreated;
}
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
updateContainer(prevStatus, status, error) {
if (status !== 'created' && this.isBannerDismissed(status)) {
2019-09-04 21:01:54 +05:30
return;
}
this.setBannerDismissedState(status, false);
2019-10-12 21:52:04 +05:30
if (prevStatus !== status) {
this.hideAll();
2018-03-17 18:26:18 +05:30
switch (status) {
case 'created':
2019-10-12 21:52:04 +05:30
if (this.isClusterNewlyCreated()) {
this.setClusterNewlyCreated(false);
this.successContainer.classList.remove('hidden');
} else if (prevStatus) {
this.setClusterNewlyCreated(true);
window.location.reload();
}
2018-03-17 18:26:18 +05:30
break;
case 'errored':
this.errorContainer.classList.remove('hidden');
this.errorReasonContainer.textContent = error;
break;
2019-09-04 21:01:54 +05:30
case 'unreachable':
this.unreachableContainer.classList.remove('hidden');
break;
case 'authentication_failure':
this.authenticationFailureContainer.classList.remove('hidden');
break;
2018-03-17 18:26:18 +05:30
case 'scheduled':
case 'creating':
this.creatingContainer.classList.remove('hidden');
break;
default:
}
}
}
destroy() {
this.destroyed = true;
if (this.poll) {
this.poll.stop();
}
2019-12-04 20:38:33 +05:30
if (this.environments) {
this.environments.$destroy();
}
2018-03-17 18:26:18 +05:30
}
}