From 7bd53d421cc39fbbd6bcbb1025c08c8f0b302a67 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste WATENBERG Date: Thu, 24 Dec 2020 16:31:42 +0100 Subject: [PATCH] ui: start solutions reducer typing Refs: https://github.com/scality/metalk8s/issues/2999 --- ui/src/ducks/app/solutions.js | 24 ++++++++++++++++-------- ui/src/services/k8s/solutions.js | 14 +++++++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ui/src/ducks/app/solutions.js b/ui/src/ducks/app/solutions.js index bae91222fd..feb1e742fc 100644 --- a/ui/src/ducks/app/solutions.js +++ b/ui/src/ducks/app/solutions.js @@ -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'; @@ -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'; @@ -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 }; @@ -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 }; }; @@ -98,12 +106,12 @@ export const solutionsRefreshingSelector = (state) => export const solutionServicesSelector = (state) => state.app.solutions.services; // Sagas -export function* fetchEnvironments() { +export function* fetchEnvironments(): Generator { 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); @@ -112,7 +120,7 @@ export function* fetchEnvironments() { return updatedEnvironments; } -export function* createEnvironment(action) { +export function* createEnvironment(action: {payload: {name: string}}): Generator> { const { name } = action.payload; const resultCreateEnvironment = yield call( SolutionsApi.createEnvironment, @@ -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 { 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); @@ -187,7 +195,7 @@ export function* prepareEnvironment(action) { } } -export function* updateEnvironments(environments) { +export function* updateEnvironments(environments: SolutionsApi.Environment[]): Generator { for (const env of environments) { const envConfig = yield call(SolutionsApi.getEnvironmentConfigMap, env); if (envConfig) { diff --git a/ui/src/services/k8s/solutions.js b/ui/src/services/k8s/solutions.js index a5ec386a5a..1eaf69aebb 100644 --- a/ui/src/services/k8s/solutions.js +++ b/ui/src/services/k8s/solutions.js @@ -1,3 +1,4 @@ +import { V1Namespace } from '@kubernetes/client-node/dist/gen/model/models'; import { coreV1 } from './api'; import * as core from './core'; @@ -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 { + 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 = @@ -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); @@ -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) {