import * as plugins from './plugins.js'; import * as domtools from '@design.estate/dees-domtools'; const appstate = new plugins.deesDomtools.plugins.smartstate.Smartstate(); export interface ILoginState { jwt: string; } export const loginStatePart = await appstate.getStatePart( 'login', { jwt: null }, 'persistent' ); export const loginAction = loginStatePart.createAction<{ username: string; password: string }>( async (statePartArg, payloadArg) => { const currentState = statePartArg.getState(); const trLogin = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'adminLoginWithUsernameAndPassword' ); const response = await trLogin.fire({ username: payloadArg.username, password: payloadArg.password, }); return { ...currentState, ...(response.jwt ? { jwt: response.jwt } : {}), }; } ); export const logoutAction = loginStatePart.createAction(async (statePartArg) => { const currentState = statePartArg.getState(); return { ...currentState, jwt: null, }; }); export interface IDataState { secretGroups?: plugins.interfaces.data.ISecretGroup[]; secretBundles?: plugins.interfaces.data.ISecretBundle[]; clusters?: plugins.interfaces.data.ICluster[]; images?: any[]; services?: any[]; deployments?: any[]; dns?: any[]; mails?: any[]; logs?: any[]; s3?: any[]; dbs?: any[]; backups?: any[]; } export const dataState = await appstate.getStatePart( 'data', { secretGroups: [], secretBundles: [], clusters: [], images: [], services: [], deployments: [], dns: [], mails: [], logs: [], s3: [], dbs: [], backups: [], }, 'soft' ); // Getting data export const getDataAction = dataState.createAction(async (statePartArg) => { let currentState = statePartArg.getState(); // Secrets const trGetSecrets = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'adminGetConfigBundlesAndSecretGroups' ); const response = await trGetSecrets.fire({ jwt: loginStatePart.getState().jwt, }); currentState = { ...currentState, ...response, }; // Clusters const trGetClusters = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'getAllClusters' ); const responseClusters = await trGetClusters.fire({ jwt: loginStatePart.getState().jwt, }); currentState = { ...currentState, ...responseClusters, } return currentState; }); // SecretGroup Actions export const createSecretGroupAction = dataState.createAction( async (statePartArg, payloadArg: plugins.interfaces.data.ISecretGroup) => { let currentState = statePartArg.getState(); const trCreateSecretGroup = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'adminCreateConfigBundlesAndSecretGroups' ); const response = await trCreateSecretGroup.fire({ jwt: loginStatePart.getState().jwt, secretBundles: [], secretGroups: [payloadArg], }); currentState = await dataState.dispatchAction(getDataAction, null); return currentState; return currentState; } ); export const deleteSecretGroupAction = dataState.createAction( async (statePartArg, payloadArg: { secretGroupId: string }) => { let currentState = statePartArg.getState(); const trDeleteSecretGroup = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'adminDeleteConfigBundlesAndSecretGroups' ); const response = await trDeleteSecretGroup.fire({ jwt: loginStatePart.getState().jwt, secretBundleIds: [], secretGroupIds: [payloadArg.secretGroupId], }); currentState = await dataState.dispatchAction(getDataAction, null); return currentState; } ); // SecretBundle Actions export const deleteSecretBundleAction = dataState.createAction( async (statePartArg, payloadArg: { configBundleId: string }) => { let currentState = statePartArg.getState(); const trDeleteConfigBundle = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'adminDeleteConfigBundlesAndSecretGroups' ); const response = await trDeleteConfigBundle.fire({ jwt: loginStatePart.getState().jwt, secretBundleIds: [payloadArg.configBundleId], secretGroupIds: [], }); currentState = await dataState.dispatchAction(getDataAction, null); return currentState; } ); // cluster export const addClusterAction = dataState.createAction( async ( statePartArg, payloadArg: { clusterName: string; } ) => { let currentState = statePartArg.getState(); const trAddCluster = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'createCluster' ); const response = await trAddCluster.fire({ jwt: loginStatePart.getState().jwt, ...payloadArg, }); currentState = { ...currentState, ...{ clusters: [...currentState.clusters, response.clusterConfig], }, } return currentState; } );