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: plugins.smartstate.StatePart = 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 getAllDataAction = dataState.createAction(async (statePartArg, partialArg?: 'secrets' | 'images') => { 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, }; // images const trGetImages = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'getAllImages' ); const responseImages = await trGetImages.fire({ jwt: loginStatePart.getState().jwt, }); currentState = { ...currentState, ...responseImages, }; // 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(getAllDataAction, 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(getAllDataAction, 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(getAllDataAction, null); return currentState; } ); // image actions export const createImageAction = dataState.createAction( async (statePartArg, payloadArg: { imageName: string, description: string }) => { let currentState = statePartArg.getState(); const trCreateImage = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'createImage' ); const response = await trCreateImage.fire({ jwt: loginStatePart.getState().jwt, name: payloadArg.imageName, description: payloadArg.description, }); currentState = { ...currentState, ...{ images: [...currentState.images, response.image], }, }; return currentState; } ); export const deleteImageAction = dataState.createAction( async (statePartArg, payloadArg: { imageId: string }) => { let currentState = statePartArg.getState(); const trDeleteImage = new domtools.plugins.typedrequest.TypedRequest( '/typedrequest', 'deleteImage' ); const response = await trDeleteImage.fire({ jwt: loginStatePart.getState().jwt, imageId: payloadArg.imageId, }); currentState = { ...currentState, ...{ images: currentState.images.filter((image) => image.id !== payloadArg.imageId), }, }; 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; } );