clusters_store.js 5.84 KB
Newer Older
1
import { s__ } from '../../locale';
2
import { parseBoolean } from '../../lib/utils/common_utils';
3 4 5 6 7 8 9
import {
  INGRESS,
  JUPYTER,
  KNATIVE,
  CERT_MANAGER,
  RUNNER,
  APPLICATION_INSTALLED_STATUSES,
10 11 12
  APPLICATION_STATUS,
  INSTALL_EVENT,
  UPDATE_EVENT,
13
  UNINSTALL_EVENT,
14
} from '../constants';
15
import transitionApplicationState from '../services/application_state_machine';
16 17 18 19 20 21 22 23

const isApplicationInstalled = appStatus => APPLICATION_INSTALLED_STATUSES.includes(appStatus);

const applicationInitialState = {
  status: null,
  statusReason: null,
  requestReason: null,
  installed: false,
24
  installFailed: false,
25 26 27
  uninstallable: false,
  uninstallFailed: false,
  uninstallSuccessful: false,
28
};
29 30 31 32 33

export default class ClusterStore {
  constructor() {
    this.state = {
      helpPath: null,
34
      ingressHelpPath: null,
35
      status: null,
36
      rbac: false,
37 38 39
      statusReason: null,
      applications: {
        helm: {
40
          ...applicationInitialState,
41 42 43
          title: s__('ClusterIntegration|Helm Tiller'),
        },
        ingress: {
44
          ...applicationInitialState,
45
          title: s__('ClusterIntegration|Ingress'),
46
          externalIp: null,
47
          externalHostname: null,
48
        },
Amit Rathi's avatar
Amit Rathi committed
49
        cert_manager: {
50
          ...applicationInitialState,
Amit Rathi's avatar
Amit Rathi committed
51
          title: s__('ClusterIntegration|Cert-Manager'),
52
          email: null,
Amit Rathi's avatar
Amit Rathi committed
53
        },
54
        runner: {
55
          ...applicationInitialState,
56
          title: s__('ClusterIntegration|GitLab Runner'),
57
          version: null,
58
          chartRepo: 'https://gitlab.com/gitlab-org/charts/gitlab-runner',
59
          updateAvailable: null,
60 61
          updateSuccessful: false,
          updateFailed: false,
62
        },
63
        prometheus: {
64
          ...applicationInitialState,
65 66
          title: s__('ClusterIntegration|Prometheus'),
        },
67
        jupyter: {
68
          ...applicationInitialState,
69 70 71
          title: s__('ClusterIntegration|JupyterHub'),
          hostname: null,
        },
Chris Baumbauer's avatar
Chris Baumbauer committed
72
        knative: {
73
          ...applicationInitialState,
Chris Baumbauer's avatar
Chris Baumbauer committed
74
          title: s__('ClusterIntegration|Knative'),
75
          hostname: null,
76
          isEditingHostName: false,
77
          externalIp: null,
78
          externalHostname: null,
79 80
          updateSuccessful: false,
          updateFailed: false,
Chris Baumbauer's avatar
Chris Baumbauer committed
81
        },
82 83 84 85
      },
    };
  }

86
  setHelpPaths(helpPath, ingressHelpPath, ingressDnsHelpPath) {
87
    this.state.helpPath = helpPath;
88
    this.state.ingressHelpPath = ingressHelpPath;
89
    this.state.ingressDnsHelpPath = ingressDnsHelpPath;
90 91
  }

92 93 94 95
  setManagePrometheusPath(managePrometheusPath) {
    this.state.managePrometheusPath = managePrometheusPath;
  }

96 97 98 99
  updateStatus(status) {
    this.state.status = status;
  }

100 101 102 103
  updateRbac(rbac) {
    this.state.rbac = parseBoolean(rbac);
  }

104 105 106 107
  updateStatusReason(reason) {
    this.state.statusReason = reason;
  }

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  installApplication(appId) {
    this.handleApplicationEvent(appId, INSTALL_EVENT);
  }

  notifyInstallFailure(appId) {
    this.handleApplicationEvent(appId, APPLICATION_STATUS.ERROR);
  }

  updateApplication(appId) {
    this.handleApplicationEvent(appId, UPDATE_EVENT);
  }

  notifyUpdateFailure(appId) {
    this.handleApplicationEvent(appId, APPLICATION_STATUS.UPDATE_ERRORED);
  }

124 125 126 127 128 129 130 131
  uninstallApplication(appId) {
    this.handleApplicationEvent(appId, UNINSTALL_EVENT);
  }

  notifyUninstallFailure(appId) {
    this.handleApplicationEvent(appId, APPLICATION_STATUS.UNINSTALL_ERRORED);
  }

132 133 134 135 136 137
  handleApplicationEvent(appId, event) {
    const currentAppState = this.state.applications[appId];

    this.state.applications[appId] = transitionApplicationState(currentAppState, event);
  }

138 139 140 141 142 143 144
  updateAppProperty(appId, prop, value) {
    this.state.applications[appId][prop] = value;
  }

  updateStateFromServer(serverState = {}) {
    this.state.status = serverState.status;
    this.state.statusReason = serverState.status_reason;
145

146
    serverState.applications.forEach(serverAppEntry => {
147 148 149 150 151
      const {
        name: appId,
        status,
        status_reason: statusReason,
        version,
152
        update_available: updateAvailable,
153
        can_uninstall: uninstallable,
154
      } = serverAppEntry;
155 156
      const currentApplicationState = this.state.applications[appId] || {};
      const nextApplicationState = transitionApplicationState(currentApplicationState, status);
157 158

      this.state.applications[appId] = {
159 160
        ...currentApplicationState,
        ...nextApplicationState,
161
        statusReason,
162
        installed: isApplicationInstalled(nextApplicationState.status),
163
        uninstallable,
164
      };
165

Filipa Lacerda's avatar
Filipa Lacerda committed
166
      if (appId === INGRESS) {
167
        this.state.applications.ingress.externalIp = serverAppEntry.external_ip;
168
        this.state.applications.ingress.externalHostname = serverAppEntry.external_hostname;
169 170 171
      } else if (appId === CERT_MANAGER) {
        this.state.applications.cert_manager.email =
          this.state.applications.cert_manager.email || serverAppEntry.email;
172
      } else if (appId === JUPYTER) {
Filipa Lacerda's avatar
Filipa Lacerda committed
173
        this.state.applications.jupyter.hostname =
174
          this.state.applications.jupyter.hostname ||
Filipa Lacerda's avatar
Filipa Lacerda committed
175 176
          serverAppEntry.hostname ||
          (this.state.applications.ingress.externalIp
177
            ? `jupyter.${this.state.applications.ingress.externalIp}.nip.io`
Filipa Lacerda's avatar
Filipa Lacerda committed
178
            : '');
179
      } else if (appId === KNATIVE) {
180 181 182 183
        if (!this.state.applications.knative.isEditingHostName) {
          this.state.applications.knative.hostname =
            serverAppEntry.hostname || this.state.applications.knative.hostname;
        }
184 185
        this.state.applications.knative.externalIp =
          serverAppEntry.external_ip || this.state.applications.knative.externalIp;
186 187
        this.state.applications.knative.externalHostname =
          serverAppEntry.external_hostname || this.state.applications.knative.externalHostname;
188 189
      } else if (appId === RUNNER) {
        this.state.applications.runner.version = version;
190
        this.state.applications.runner.updateAvailable = updateAvailable;
191
      }
192 193 194
    });
  }
}