2017-11-23 07:04:03 -05:00
|
|
|
/**
|
|
|
|
* Common code between environmets app and folder view
|
|
|
|
*/
|
2020-03-30 05:07:58 -04:00
|
|
|
import { isEqual, isFunction, omitBy } from 'lodash';
|
2017-11-23 07:04:03 -05:00
|
|
|
import Visibility from 'visibilityjs';
|
2019-03-13 07:55:56 -04:00
|
|
|
import EnvironmentsStore from 'ee_else_ce/environments/stores/environments_store';
|
2017-11-23 07:04:03 -05:00
|
|
|
import Poll from '../../lib/utils/poll';
|
2018-10-10 02:18:49 -04:00
|
|
|
import { getParameterByName } from '../../lib/utils/common_utils';
|
2017-11-23 07:04:03 -05:00
|
|
|
import { s__ } from '../../locale';
|
|
|
|
import Flash from '../../flash';
|
|
|
|
import eventHub from '../event_hub';
|
|
|
|
|
|
|
|
import EnvironmentsService from '../services/environments_service';
|
2019-06-12 05:18:37 -04:00
|
|
|
import tablePagination from '../../vue_shared/components/pagination/table_pagination.vue';
|
2017-11-23 07:04:03 -05:00
|
|
|
import environmentTable from '../components/environments_table.vue';
|
|
|
|
import tabs from '../../vue_shared/components/navigation_tabs.vue';
|
|
|
|
import container from '../components/container.vue';
|
|
|
|
|
2017-05-12 18:02:41 -04:00
|
|
|
export default {
|
2017-11-23 07:04:03 -05:00
|
|
|
components: {
|
|
|
|
environmentTable,
|
|
|
|
container,
|
|
|
|
tabs,
|
|
|
|
tablePagination,
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
const store = new EnvironmentsStore();
|
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
const isDetailView = document.body.contains(
|
|
|
|
document.getElementById('environments-detail-view'),
|
|
|
|
);
|
|
|
|
|
2017-11-23 07:04:03 -05:00
|
|
|
return {
|
|
|
|
store,
|
|
|
|
state: store.state,
|
|
|
|
isLoading: false,
|
|
|
|
isMakingRequest: false,
|
|
|
|
scope: getParameterByName('scope') || 'available',
|
|
|
|
page: getParameterByName('page') || '1',
|
|
|
|
requestData: {},
|
2018-07-10 04:11:04 -04:00
|
|
|
environmentInStopModal: {},
|
2020-03-25 05:08:11 -04:00
|
|
|
environmentInDeleteModal: {},
|
2019-03-05 13:33:10 -05:00
|
|
|
environmentInRollbackModal: {},
|
2020-03-25 05:08:11 -04:00
|
|
|
isDetailView,
|
2017-11-23 07:04:03 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-05-12 18:02:41 -04:00
|
|
|
methods: {
|
|
|
|
saveData(resp) {
|
2018-05-14 13:29:21 -04:00
|
|
|
this.isLoading = false;
|
2017-05-12 18:02:41 -04:00
|
|
|
|
2019-02-26 17:40:13 -05:00
|
|
|
// Prevent the absence of the nested flag from causing mismatches
|
2019-02-27 01:17:50 -05:00
|
|
|
const response = this.filterNilValues(resp.config.params);
|
|
|
|
const request = this.filterNilValues(this.requestData);
|
2019-02-26 17:40:13 -05:00
|
|
|
|
2020-03-30 05:07:58 -04:00
|
|
|
if (isEqual(response, request)) {
|
2018-05-14 13:29:21 -04:00
|
|
|
this.store.storeAvailableCount(resp.data.available_count);
|
|
|
|
this.store.storeStoppedCount(resp.data.stopped_count);
|
|
|
|
this.store.storeEnvironments(resp.data.environments);
|
2020-02-04 13:08:50 -05:00
|
|
|
this.store.setReviewAppDetails(resp.data.review_app);
|
2018-05-14 13:29:21 -04:00
|
|
|
this.store.setPagination(resp.headers);
|
|
|
|
}
|
2017-05-12 18:02:41 -04:00
|
|
|
},
|
2017-11-23 07:04:03 -05:00
|
|
|
|
2019-02-27 01:17:50 -05:00
|
|
|
filterNilValues(obj) {
|
2020-03-30 05:07:58 -04:00
|
|
|
return omitBy(obj, value => value === undefined || value === null);
|
2019-02-27 01:17:50 -05:00
|
|
|
},
|
|
|
|
|
2017-11-23 07:04:03 -05:00
|
|
|
/**
|
|
|
|
* Handles URL and query parameter changes.
|
|
|
|
* When the user uses the pagination or the tabs,
|
|
|
|
* - update URL
|
|
|
|
* - Make API request to the server with new parameters
|
|
|
|
* - Update the polling function
|
|
|
|
* - Update the internal state
|
|
|
|
*/
|
|
|
|
updateContent(parameters) {
|
|
|
|
this.updateInternalState(parameters);
|
|
|
|
// fetch new data
|
2018-10-10 02:18:49 -04:00
|
|
|
return this.service
|
|
|
|
.fetchEnvironments(this.requestData)
|
2019-02-26 17:40:13 -05:00
|
|
|
.then(response => {
|
|
|
|
this.successCallback(response);
|
|
|
|
this.poll.enable({ data: this.requestData, response });
|
2017-11-23 07:04:03 -05:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.errorCallback();
|
|
|
|
|
|
|
|
// restart polling
|
|
|
|
this.poll.restart();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
errorCallback() {
|
|
|
|
this.isLoading = false;
|
|
|
|
Flash(s__('Environments|An error occurred while fetching the environments.'));
|
|
|
|
},
|
|
|
|
|
2020-01-03 04:07:33 -05:00
|
|
|
postAction({
|
|
|
|
endpoint,
|
|
|
|
errorMessage = s__('Environments|An error occurred while making the request.'),
|
|
|
|
}) {
|
2017-11-23 07:04:03 -05:00
|
|
|
if (!this.isMakingRequest) {
|
|
|
|
this.isLoading = true;
|
|
|
|
|
2018-10-10 02:18:49 -04:00
|
|
|
this.service
|
|
|
|
.postAction(endpoint)
|
2017-11-23 07:04:03 -05:00
|
|
|
.then(() => this.fetchEnvironments())
|
2020-01-03 04:07:33 -05:00
|
|
|
.catch(err => {
|
2017-11-23 07:04:03 -05:00
|
|
|
this.isLoading = false;
|
2020-03-30 05:07:58 -04:00
|
|
|
Flash(isFunction(errorMessage) ? errorMessage(err.response.data) : errorMessage);
|
2017-11-23 07:04:03 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchEnvironments() {
|
|
|
|
this.isLoading = true;
|
|
|
|
|
2018-10-10 02:18:49 -04:00
|
|
|
return this.service
|
|
|
|
.fetchEnvironments(this.requestData)
|
2017-11-23 07:04:03 -05:00
|
|
|
.then(this.successCallback)
|
|
|
|
.catch(this.errorCallback);
|
|
|
|
},
|
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
updateStopModal(environment) {
|
|
|
|
this.environmentInStopModal = environment;
|
|
|
|
},
|
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
updateDeleteModal(environment) {
|
|
|
|
this.environmentInDeleteModal = environment;
|
|
|
|
},
|
|
|
|
|
2019-03-05 13:33:10 -05:00
|
|
|
updateRollbackModal(environment) {
|
|
|
|
this.environmentInRollbackModal = environment;
|
|
|
|
},
|
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
stopEnvironment(environment) {
|
|
|
|
const endpoint = environment.stop_path;
|
2018-10-10 02:18:49 -04:00
|
|
|
const errorMessage = s__(
|
|
|
|
'Environments|An error occurred while stopping the environment, please try again',
|
|
|
|
);
|
2018-07-10 04:11:04 -04:00
|
|
|
this.postAction({ endpoint, errorMessage });
|
|
|
|
},
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
deleteEnvironment(environment) {
|
|
|
|
const endpoint = environment.delete_path;
|
|
|
|
const mountedToShow = environment.mounted_to_show;
|
|
|
|
const errorMessage = s__(
|
|
|
|
'Environments|An error occurred while deleting the environment. Check if the environment stopped; if not, stop it and try again.',
|
|
|
|
);
|
|
|
|
|
|
|
|
this.service
|
|
|
|
.deleteAction(endpoint)
|
|
|
|
.then(() => {
|
|
|
|
if (!mountedToShow) {
|
|
|
|
// Reload as a first solution to bust the ETag cache
|
|
|
|
window.location.reload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const url = window.location.href.split('/');
|
|
|
|
url.pop();
|
|
|
|
window.location.href = url.join('/');
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Flash(errorMessage);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-03-05 13:33:10 -05:00
|
|
|
rollbackEnvironment(environment) {
|
|
|
|
const { retryUrl, isLastDeployment } = environment;
|
|
|
|
const errorMessage = isLastDeployment
|
|
|
|
? s__('Environments|An error occurred while re-deploying the environment, please try again')
|
|
|
|
: s__(
|
|
|
|
'Environments|An error occurred while rolling back the environment, please try again',
|
|
|
|
);
|
|
|
|
this.postAction({ endpoint: retryUrl, errorMessage });
|
|
|
|
},
|
2020-01-03 04:07:33 -05:00
|
|
|
|
|
|
|
cancelAutoStop(autoStopPath) {
|
|
|
|
const errorMessage = ({ message }) =>
|
|
|
|
message ||
|
|
|
|
s__('Environments|An error occurred while canceling the auto stop, please try again');
|
|
|
|
this.postAction({ endpoint: autoStopPath, errorMessage });
|
|
|
|
},
|
2017-11-23 07:04:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
tabs() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
name: s__('Available'),
|
|
|
|
scope: 'available',
|
|
|
|
count: this.state.availableCounter,
|
|
|
|
isActive: this.scope === 'available',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: s__('Stopped'),
|
|
|
|
scope: 'stopped',
|
|
|
|
count: this.state.stoppedCounter,
|
|
|
|
isActive: this.scope === 'stopped',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all the environments and stores them.
|
|
|
|
* Toggles loading property.
|
|
|
|
*/
|
|
|
|
created() {
|
|
|
|
this.service = new EnvironmentsService(this.endpoint);
|
2019-01-16 15:05:19 -05:00
|
|
|
this.requestData = { page: this.page, scope: this.scope, nested: true };
|
2017-11-23 07:04:03 -05:00
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
if (!this.isDetailView) {
|
|
|
|
this.poll = new Poll({
|
|
|
|
resource: this.service,
|
|
|
|
method: 'fetchEnvironments',
|
|
|
|
data: this.requestData,
|
|
|
|
successCallback: this.successCallback,
|
|
|
|
errorCallback: this.errorCallback,
|
|
|
|
notificationCallback: isMakingRequest => {
|
|
|
|
this.isMakingRequest = isMakingRequest;
|
|
|
|
},
|
|
|
|
});
|
2017-11-23 07:04:03 -05:00
|
|
|
|
|
|
|
if (!Visibility.hidden()) {
|
2020-03-25 05:08:11 -04:00
|
|
|
this.isLoading = true;
|
|
|
|
this.poll.makeRequest();
|
2017-11-23 07:04:03 -05:00
|
|
|
} else {
|
2020-03-25 05:08:11 -04:00
|
|
|
this.fetchEnvironments();
|
2017-11-23 07:04:03 -05:00
|
|
|
}
|
2020-03-25 05:08:11 -04:00
|
|
|
|
|
|
|
Visibility.change(() => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
this.poll.restart();
|
|
|
|
} else {
|
|
|
|
this.poll.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-23 07:04:03 -05:00
|
|
|
|
|
|
|
eventHub.$on('postAction', this.postAction);
|
2020-03-25 05:08:11 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
eventHub.$on('requestStopEnvironment', this.updateStopModal);
|
|
|
|
eventHub.$on('stopEnvironment', this.stopEnvironment);
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
eventHub.$on('requestDeleteEnvironment', this.updateDeleteModal);
|
|
|
|
eventHub.$on('deleteEnvironment', this.deleteEnvironment);
|
|
|
|
|
2019-03-05 13:33:10 -05:00
|
|
|
eventHub.$on('requestRollbackEnvironment', this.updateRollbackModal);
|
|
|
|
eventHub.$on('rollbackEnvironment', this.rollbackEnvironment);
|
2020-01-03 04:07:33 -05:00
|
|
|
|
|
|
|
eventHub.$on('cancelAutoStop', this.cancelAutoStop);
|
2017-11-23 07:04:03 -05:00
|
|
|
},
|
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
beforeDestroy() {
|
|
|
|
eventHub.$off('postAction', this.postAction);
|
2020-03-25 05:08:11 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
eventHub.$off('requestStopEnvironment', this.updateStopModal);
|
|
|
|
eventHub.$off('stopEnvironment', this.stopEnvironment);
|
2019-03-05 13:33:10 -05:00
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
eventHub.$off('requestDeleteEnvironment', this.updateDeleteModal);
|
|
|
|
eventHub.$off('deleteEnvironment', this.deleteEnvironment);
|
|
|
|
|
2019-03-05 13:33:10 -05:00
|
|
|
eventHub.$off('requestRollbackEnvironment', this.updateRollbackModal);
|
|
|
|
eventHub.$off('rollbackEnvironment', this.rollbackEnvironment);
|
2020-01-03 04:07:33 -05:00
|
|
|
|
|
|
|
eventHub.$off('cancelAutoStop', this.cancelAutoStop);
|
2017-05-12 18:02:41 -04:00
|
|
|
},
|
|
|
|
};
|