Skip to content

Commit

Permalink
ui: start solutions reducer typing
Browse files Browse the repository at this point in the history
Refs: #2999
  • Loading branch information
JBWatenbergScality committed Jan 8, 2021
1 parent 5a03cc1 commit 7bd53d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
24 changes: 16 additions & 8 deletions ui/src/ducks/app/solutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import * as CoreApi from '../../services/k8s/core';
import * as SolutionsApi from '../../services/k8s/solutions';
import * as SaltApi from '../../services/salt/api';
import type {APIResult} from '../../types';

import history from '../../history';
import { REFRESH_TIMEOUT } from '../../constants';
Expand All @@ -21,6 +22,7 @@ import {
} from './notifications';
import { intl } from '../../translations/IntlGlobalProvider';
import { addJobAction, JOB_COMPLETED } from './salt';
import { V1ConfigMap } from '@kubernetes/client-node/dist/gen/model/models';

const OPERATOR_ = '-operator';
const _K8S = 'app.kubernetes.io';
Expand All @@ -43,7 +45,13 @@ const defaultState = {
isSolutionsRefreshing: false,
};

export default function reducer(state = defaultState, action = {}) {
export type SolutionsState = {
solutions: any[],
environments: SolutionsApi.Environment[],
isSolutionsRefreshing: boolean
}

export default function reducer(state: SolutionsState = defaultState, action: any = {}) {
switch (action.type) {
case SET_SOLUTIONS:
return { ...state, solutions: action.payload };
Expand All @@ -65,7 +73,7 @@ export function setSolutionsRefeshingAction(payload) {
return { type: SET_SOLUTIONS_REFRESHING, payload };
}

export const setEnvironmentsAction = (environments) => {
export const setEnvironmentsAction = (environments: SolutionsApi.Environment[]) => {
return { type: SET_ENVIRONMENTS, payload: environments };
};

Expand Down Expand Up @@ -98,12 +106,12 @@ export const solutionsRefreshingSelector = (state) =>
export const solutionServicesSelector = (state) => state.app.solutions.services;

// Sagas
export function* fetchEnvironments() {
export function* fetchEnvironments(): Generator<any, void, any> {
const jobs = yield select((state) => state.app.salt.jobs);
const preparingEnvs = jobs?.filter(
(job) => job.type === 'prepare-env/' && !job.completed,
);
const environments = yield call(SolutionsApi.listEnvironments);
const environments: SolutionsApi.Environment[] = yield call(SolutionsApi.listEnvironments);
const updatedEnvironments = yield call(updateEnvironments, environments);
for (const env of updatedEnvironments) {
env.isPreparing = preparingEnvs?.includes(env.name);
Expand All @@ -112,7 +120,7 @@ export function* fetchEnvironments() {
return updatedEnvironments;
}

export function* createEnvironment(action) {
export function* createEnvironment(action: {payload: {name: string}}): Generator<Effect, void, APIResult<V1ConfigMap>> {
const { name } = action.payload;
const resultCreateEnvironment = yield call(
SolutionsApi.createEnvironment,
Expand All @@ -130,10 +138,10 @@ export function* createEnvironment(action) {
yield call(fetchEnvironments);
}

export function* prepareEnvironment(action) {
export function* prepareEnvironment(action: {payload: {envName: string, solName: string, solVersion: string }}): Generator<any, void, any> {
const { envName, solName, solVersion } = action.payload;

const existingEnv = yield select((state) => state.app.solutions.environments);
const existingEnv: SolutionsApi.Environment[] = yield select((state: RootState) => state.app.solutions.environments);

const preparingEnv = existingEnv.find((env) => env.name === envName);

Expand Down Expand Up @@ -187,7 +195,7 @@ export function* prepareEnvironment(action) {
}
}

export function* updateEnvironments(environments) {
export function* updateEnvironments(environments: SolutionsApi.Environment[]): Generator<any, SolutionsApi.Environment[], any> {
for (const env of environments) {
const envConfig = yield call(SolutionsApi.getEnvironmentConfigMap, env);
if (envConfig) {
Expand Down
14 changes: 9 additions & 5 deletions ui/src/services/k8s/solutions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { V1Namespace } from '@kubernetes/client-node/dist/gen/model/models';
import { coreV1 } from './api';
import * as core from './core';

Expand Down Expand Up @@ -26,12 +27,15 @@ export async function getSolutionsConfigMap() {
}
// }}}
// Environment-scoped management {{{
export async function listEnvironments() {
const result = await core.listNamespaces({
export type Environment = {name: string, description: String, namespaces: V1Namespace[]};
export type Environments = {[name: string]: Environment};

export async function listEnvironments(): Promise<Environment[]> {
const result: { body: V1NamespaceList[] } = await core.listNamespaces({
labelSelector: LABEL_ENVIRONMENT_NAME,
});
if (!result.error) {
const namespaces = result?.body?.items;
const namespaces: V1Namespace[] = result.body.items;
const environmentMap = namespaces.reduce((environments, ns) => {
const name = ns.metadata?.labels?.[LABEL_ENVIRONMENT_NAME];
const description =
Expand Down Expand Up @@ -85,7 +89,7 @@ async function getUIServices(namespace) {
}
}

export async function getEnvironmentAdminUIs(environment) {
export async function getEnvironmentAdminUIs(environment: Environment) {
const services = [];
for (const namespace of environment.namespaces) {
const result = await getUIServices(namespace);
Expand All @@ -102,7 +106,7 @@ export async function getEnvironmentAdminUIs(environment) {
}));
}

export async function getEnvironmentConfigMap(environment) {
export async function getEnvironmentConfigMap(environment: Environment) {
// we may support multiple namespaces in one environment later
const environmentConfigMaps = [];
for (const namespace of environment.namespaces) {
Expand Down

0 comments on commit 7bd53d4

Please sign in to comment.