gitlab-org--gitlab-foss/app/assets/javascripts/projects/gke_cluster_dropdowns/index.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-05-06 08:42:51 -04:00
/* global gapi */
import Vue from 'vue';
import Flash from '~/flash';
import GkeProjectIdDropdown from './components/gke_project_id_dropdown.vue';
import GkeZoneDropdown from './components/gke_zone_dropdown.vue';
import GkeMachineTypeDropdown from './components/gke_machine_type_dropdown.vue';
import * as CONSTANTS from './constants';
const mountComponent = (entryPoint, component, componentName, extraProps = {}) => {
const el = document.querySelector(entryPoint);
2018-05-06 08:42:51 -04:00
if (!el) return false;
2018-05-07 14:15:53 -04:00
const hiddenInput = el.querySelector('input');
2018-05-06 08:42:51 -04:00
return new Vue({
el,
components: {
[componentName]: component,
2018-05-06 08:42:51 -04:00
},
render: createElement =>
createElement(componentName, {
2018-05-06 08:42:51 -04:00
props: {
fieldName: hiddenInput.getAttribute('name'),
fieldId: hiddenInput.getAttribute('id'),
defaultValue: hiddenInput.value,
...extraProps,
2018-05-06 08:42:51 -04:00
},
}),
});
};
const mountGkeProjectIdDropdown = () => {
const entryPoint = '.js-gcp-project-id-dropdown-entry-point';
const el = document.querySelector(entryPoint);
2018-05-06 08:42:51 -04:00
mountComponent(entryPoint, GkeProjectIdDropdown, 'gke-project-id-dropdown', {
docsUrl: el.dataset.docsurl,
2018-05-06 08:42:51 -04:00
});
};
const mountGkeZoneDropdown = () => {
mountComponent('.js-gcp-zone-dropdown-entry-point', GkeZoneDropdown, 'gke-zone-dropdown');
};
2018-05-06 08:42:51 -04:00
const mountGkeMachineTypeDropdown = () => {
mountComponent(
'.js-gcp-machine-type-dropdown-entry-point',
GkeMachineTypeDropdown,
'gke-machine-type-dropdown',
);
2018-05-06 08:42:51 -04:00
};
const gkeDropdownErrorHandler = () => {
Flash(CONSTANTS.GCP_API_ERROR);
};
const initializeGapiClient = () => {
2018-05-06 11:40:48 -04:00
const el = document.querySelector('.js-gke-cluster-creation');
if (!el) return false;
2018-05-06 08:42:51 -04:00
return gapi.client
.init({
discoveryDocs: [
CONSTANTS.GCP_API_CLOUD_BILLING_ENDPOINT,
CONSTANTS.GCP_API_CLOUD_RESOURCE_MANAGER_ENDPOINT,
CONSTANTS.GCP_API_COMPUTE_ENDPOINT,
],
2018-05-06 08:42:51 -04:00
})
.then(() => {
gapi.client.setToken({ access_token: el.dataset.token });
mountGkeProjectIdDropdown();
2018-05-06 08:42:51 -04:00
mountGkeZoneDropdown();
mountGkeMachineTypeDropdown();
})
.catch(gkeDropdownErrorHandler);
};
const initGkeDropdowns = () => {
2018-05-07 14:15:53 -04:00
if (!gapi) {
2018-05-06 08:42:51 -04:00
gkeDropdownErrorHandler();
return false;
}
return gapi.load('client', initializeGapiClient);
};
export default initGkeDropdowns;