Commit 5a2b43fc authored by Filipa Lacerda's avatar Filipa Lacerda

Adjustments to polling & forms

parent 8cff79bc
...@@ -4,74 +4,58 @@ import axios from 'axios'; ...@@ -4,74 +4,58 @@ import axios from 'axios';
import Poll from './lib/utils/poll'; import Poll from './lib/utils/poll';
import { s__ } from './locale'; import { s__ } from './locale';
import './flash'; import './flash';
import { convertPermissionToBoolean } from './lib/utils/common_utils';
class ClusterService {
constructor(options = {}) {
this.options = options;
}
fetchData() {
return axios.get(this.options.endpoints.checkStatus);
}
updateData(value) {
return axios.put(this.options.endpoints.editPath,
{
cluster: {
enabled: value,
},
},
);
}
}
/** /**
* Cluster page has 2 separate parts: * Cluster page has 2 separate parts:
* - Update form with toggle button * Toggle button
* -- Check initial state and set the toggle button
* -- Listen to changes
* -- Check permissions in order to disable
* -- Update cluster based on toggle status
* *
* - Polling status while creating or scheduled * - Polling status while creating or scheduled
* -- Update status area with the response result * -- Update status area with the response result
* *
*/ */
class ClusterService {
constructor(options = {}) {
this.options = options;
}
fetchData() {
return axios.get(this.options.endpoint);
}
}
export default class ClusterEdit { export default class ClusterEdit {
constructor() { constructor() {
const dataset = document.querySelector('.js-edit-cluster-form').dataset; const dataset = document.querySelector('.js-edit-cluster-form').dataset;
this.state = { this.state = {
endpoints: { statusPath: dataset.statusPath,
checkStatus: dataset.checkStatus,
editPath: dataset.editPath,
},
canUpdate: convertPermissionToBoolean(dataset.canUpdate),
clusterStatus: dataset.clusterStatus, clusterStatus: dataset.clusterStatus,
clusterStatusReason: dataset.clusterStatusReason,
toggleStatus: dataset.toggleStatus, toggleStatus: dataset.toggleStatus,
}; };
this.service = new ClusterService({ endpoints: this.state.endpoints }); this.service = new ClusterService({ endpoint: this.state.statusPath });
this.toggleButton = document.querySelector('.js-toggle-cluster'); this.toggleButton = document.querySelector('.js-toggle-cluster');
this.toggleInput = document.querySelector('.js-toggle-input');
this.errorContainer = document.querySelector('.js-cluster-error'); this.errorContainer = document.querySelector('.js-cluster-error');
this.successContainer = document.querySelector('.js-cluster-success'); this.successContainer = document.querySelector('.js-cluster-success');
this.creatingContainer = document.querySelector('.js-cluster-creating'); this.creatingContainer = document.querySelector('.js-cluster-creating');
this.initEditForm(); this.toggleButton.addEventListener('click', this.toggle.bind(this));
if (this.clusterStatus === 'scheduled' || this.clusterStatus === 'creating') { if (this.state.clusterStatus !== 'created') {
this.initPoling(); this.updateContainer(this.state.clusterStatus, this.state.clusterStatusReason);
}
} }
initEditForm() { if (this.state.statusPath) {
this.toggleButton.addEventListener('click', this.toggle.bind(this)); this.initPoling();
document.querySelector('.js-edit-cluster-button').addEventListener('click', this.updateData.bind(this)); }
} }
toggle() { toggle() {
this.toggleButton.classList.toggle('checked'); this.toggleButton.classList.toggle('checked');
this.state.toggleStatus = this.toggleButton.classList.contains('checked').toString(); this.state.toggleStatus = this.toggleButton.classList.contains('checked').toString();
this.toggleInput.setAttribute('value', this.state.toggleStatus);
} }
updateData() { updateData() {
...@@ -83,11 +67,10 @@ export default class ClusterEdit { ...@@ -83,11 +67,10 @@ export default class ClusterEdit {
resource: this.service, resource: this.service,
method: 'fetchData', method: 'fetchData',
successCallback: (data) => { successCallback: (data) => {
const { status } = data.data; const { status, status_reason } = data.data;
this.updateContainer(status); this.updateContainer(status, status_reason);
}, },
errorCallback: (error) => { errorCallback: () => {
this.updateContainer('error', error);
Flash(s__('ClusterIntegration|Something went wrong on our end.')); Flash(s__('ClusterIntegration|Something went wrong on our end.'));
}, },
}); });
...@@ -119,12 +102,13 @@ export default class ClusterEdit { ...@@ -119,12 +102,13 @@ export default class ClusterEdit {
case 'created': case 'created':
this.successContainer.classList.remove('hidden'); this.successContainer.classList.remove('hidden');
break; break;
case 'error': case 'errored':
this.errorContainer.classList.remove('hidden'); this.errorContainer.classList.remove('hidden');
this.errorContainer.querySelector('.js-error-reason').textContent = error.status_reason; this.errorContainer.querySelector('.js-error-reason').textContent = error.status_reason;
break; break;
case 'scheduled':
case 'creating': case 'creating':
this.creatingContainer.classList.add('hidden'); this.creatingContainer.classList.remove('hidden');
break; break;
default: default:
this.hideAll(); this.hideAll();
......
...@@ -7,34 +7,31 @@ ...@@ -7,34 +7,31 @@
= form_for [@project.namespace.becomes(Namespace), @project, @cluster] do |field| = form_for [@project.namespace.becomes(Namespace), @project, @cluster] do |field|
= form_errors(@cluster) = form_errors(@cluster)
.form-group .form-group
= field.label :gcp_cluster_name = field.label :gcp_cluster_name, s_('ClusterIntegration|Cluster name')
= field.text_field :gcp_cluster_name, class: 'form-control' = field.text_field :gcp_cluster_name, class: 'form-control'
.form-group .form-group
= field.label :gcp_project_id = field.label :gcp_project_id, s_('ClusterIntegration|Google Cloud Platform project ID')
= link_to(s_('ClusterIntegration|See your projects'), 'https://console.cloud.google.com/home/dashboard', target: '_blank', rel: 'noopener noreferrer') = link_to(s_('ClusterIntegration|See your projects'), 'https://console.cloud.google.com/home/dashboard', target: '_blank', rel: 'noopener noreferrer')
= field.text_field :gcp_project_id, class: 'form-control' = field.text_field :gcp_project_id, class: 'form-control'
.form-group .form-group
= field.label :gcp_cluster_zone = field.label :gcp_cluster_zone, s_('ClusterIntegration|Zone')
= link_to(s_('ClusterIntegration|See zones'), 'https://cloud.google.com/compute/docs/regions-zones/regions-zones', target: '_blank', rel: 'noopener noreferrer') = link_to(s_('ClusterIntegration|See zones'), 'https://cloud.google.com/compute/docs/regions-zones/regions-zones', target: '_blank', rel: 'noopener noreferrer')
= field.text_field :gcp_cluster_zone, class: 'form-control' = field.text_field :gcp_cluster_zone, class: 'form-control'
.form-group .form-group
= field.label :gcp_cluster_size = field.label :gcp_cluster_size, s_('ClusterIntegration|Number of nodes')
= field.text_field :gcp_cluster_size, class: 'form-control' = field.text_field :gcp_cluster_size, class: 'form-control'
.form-group .form-group
= field.label :project_namespace = field.label :gcp_machine_type, s_('ClusterIntegration|Machine type')
= field.text_field :project_namespace, class: 'form-control', placeholder: @cluster.project_namespace_placeholder
.form-group
= field.label :gcp_machine_type
= link_to(s_('ClusterIntegration|Machine type'), 'https://cloud.google.com/compute/docs/machine-types', target: '_blank', rel: 'noopener noreferrer') = link_to(s_('ClusterIntegration|Machine type'), 'https://cloud.google.com/compute/docs/machine-types', target: '_blank', rel: 'noopener noreferrer')
= field.text_field :gcp_machine_type, class: 'form-control' = field.text_field :gcp_machine_type, class: 'form-control'
.form-group .form-group
= field.submit s_('ClusterIntegration|Create cluster'), class: 'btn btn-save' = field.label :project_namespace, s_('ClusterIntegration|Project namespace (optional, unique)')
= field.text_field :project_namespace, class: 'form-control', placeholder: @cluster.project_namespace_placeholder
-# TODO: Remove before merge .form-group
= link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, cluster: {gcp_cluster_name: "gke-test-creation#{Random.rand(100)}", gcp_project_id: 'gitlab-internal-153318', gcp_cluster_zone: 'us-central1-a', gcp_cluster_size: '1', project_namespace: 'aaa', gcp_machine_type: 'n1-standard-1'}), method: :post = field.submit s_('ClusterIntegration|Create cluster'), class: 'btn btn-save'
.row.prepend-top-default.edit-cluster-form.js-edit-cluster-form{ data: { check_status: status_namespace_project_cluster_path(@cluster.project.namespace, @cluster.project, @cluster.id, format: :json), - status_path = status_namespace_project_cluster_path(@cluster.project.namespace, @cluster.project, @cluster.id, format: :json) if can?(current_user, :admin_cluster, @cluster) && @cluster.on_creation?
edit_path: namespace_project_cluster_path(@project.namespace, @project, @cluster.id, format: :json), .row.prepend-top-default.edit-cluster-form.js-edit-cluster-form{ data: { status_path: status_path,
can_update: can?(current_user, :update_cluster, @cluster),
toggle_status: @cluster.enabled? ? 'true': 'false', toggle_status: @cluster.enabled? ? 'true': 'false',
cluster_status: @cluster.status }} cluster_status: @cluster.status,
cluster_status_reason: @cluster.status_reason }}
= render 'sidebar' = render 'sidebar'
.col-lg-8 .col-lg-8
%h4.prepend-top-0 %h4.prepend-top-0
...@@ -16,7 +16,12 @@ ...@@ -16,7 +16,12 @@
- else - else
= s_('ClusterIntegration|Cluster integration is disabled for this project.') = s_('ClusterIntegration|Cluster integration is disabled for this project.')
= form_for [@project.namespace.becomes(Namespace), @project, @cluster] do |field|
= form_errors(@cluster)
.form-group
%label.toggle-wrapper %label.toggle-wrapper
= field.hidden_field :enabled, { class: 'js-toggle-input'}
%button{ type: 'button', %button{ type: 'button',
class: "js-toggle-cluster project-feature-toggle #{'checked' unless !@cluster.enabled?} #{'disabled' unless can?(current_user, :update_cluster, @cluster)}", class: "js-toggle-cluster project-feature-toggle #{'checked' unless !@cluster.enabled?} #{'disabled' unless can?(current_user, :update_cluster, @cluster)}",
'aria-label': 'Toggle', 'aria-label': 'Toggle',
...@@ -25,29 +30,23 @@ ...@@ -25,29 +30,23 @@
- if can?(current_user, :update_cluster, @cluster) - if can?(current_user, :update_cluster, @cluster)
.form-group .form-group
%button{ type: 'button', class: 'js-edit-cluster-button btn btn-success'} = field.submit s_('ClusterIntegration|Save changes'), class: 'btn btn-success'
= s_('ClusterIntegration|Save changes')
- if can?(current_user, :admin_cluster, @cluster) - if can?(current_user, :admin_cluster, @cluster)
.form_group
%label %label
= s_('ClusterIntegration|Google container engine') = s_('ClusterIntegration|Google container engine')
%p %p
- link_gke = link_to(s_('ClusterIntegration|Google Container Engine'), '', target: '_blank', rel: 'noopener noreferrer') - link_gke = link_to(s_('ClusterIntegration|Google Container Engine'), '', target: '_blank', rel: 'noopener noreferrer')
= s_('ClusterIntegration|Manage your cluster by visiting %{link_gke}').html_safe % { link_gke: link_gke } = s_('ClusterIntegration|Manage your cluster by visiting %{link_gke}').html_safe % { link_gke: link_gke }
- if @cluster.errored? .hidden.js-cluster-error.alert.alert-danger{ role: 'alert' }
.js-cluster-error.alert.alert-danger{ role: 'alert' }
= s_('ClusterIntegration|Something went wrong while creating your cluster on Google Container Engine.') = s_('ClusterIntegration|Something went wrong while creating your cluster on Google Container Engine.')
%code.js-error-reason %code.js-error-reason
- if @cluster.on_creation? .hidden.js-cluster-creating.alert.alert-info{ role: 'alert' }
.js-cluster-creating.alert.alert-info{ role: 'alert' }
= s_('ClusterIntegration|Cluster is being created on Google Container Engine...') = s_('ClusterIntegration|Cluster is being created on Google Container Engine...')
- if @cluster.created? .hidden.js-cluster-success.alert.alert-info{ role: 'alert' }
.js-cluster-success.alert.alert-info{ role: 'alert' }
= s_('ClusterIntegration|Cluster was successfully created on Google Container Engine.') = s_('ClusterIntegration|Cluster was successfully created on Google Container Engine.')
.form_group .form_group
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment