Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: seperate api validator to files #884

Merged
merged 2 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions core/api-server/lib/service/algorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AlgorithmStore {
}

async updateAlgorithm(options) {
validator.validateAlgorithmName(options);
validator.algorithms.validateAlgorithmName(options);
const alg = await stateManager.algorithms.store.get(options);
if (!alg) {
throw new ResourceNotFoundError('algorithm', options.name);
Expand All @@ -53,7 +53,7 @@ class AlgorithmStore {
}

async deleteAlgorithm(options) {
validator.validateAlgorithmDelete(options);
validator.algorithms.validateAlgorithmDelete(options);
const { name, force } = options;
const algorithm = await stateManager.algorithms.store.get({ name });
if (!algorithm) {
Expand Down Expand Up @@ -137,7 +137,7 @@ class AlgorithmStore {
}

async getAlgorithm(options) {
validator.validateName(options);
validator.jobs.validateName(options);
const algorithm = await stateManager.algorithms.store.get(options);
if (!algorithm) {
throw new ResourceNotFoundError('algorithm', options.name);
Expand All @@ -156,7 +156,7 @@ class AlgorithmStore {
}

async insertAlgorithm(options) {
validator.validateAlgorithmName(options);
validator.algorithms.validateAlgorithmName(options);
const alg = await stateManager.algorithms.store.get(options);
if (alg) {
throw new ResourceExistsError('algorithm', options.name);
Expand All @@ -179,16 +179,16 @@ class AlgorithmStore {

try {
const { overrideImage } = options || {};
validator.validateApplyAlgorithm(payload);
validator.algorithms.validateApplyAlgorithm(payload);

const oldAlgorithm = await stateManager.algorithms.store.get(payload);
if (oldAlgorithm && oldAlgorithm.type !== payload.type) {
throw new InvalidDataError(`algorithm type cannot be changed from "${oldAlgorithm.type}" to "${payload.type}"`);
}

newAlgorithm = { ...oldAlgorithm, ...payload };
validator.addAlgorithmDefaults(newAlgorithm);
await validator.validateAlgorithmResources(newAlgorithm);
validator.algorithms.addAlgorithmDefaults(newAlgorithm);
await validator.algorithms.validateAlgorithmResources(newAlgorithm);

if (payload.type === buildTypes.CODE && file.path) {
if (payload.algorithmImage) {
Expand Down
2 changes: 1 addition & 1 deletion core/api-server/lib/service/boards.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Boards {
}

async startTensorboard(options) {
validator.validateCreateBoardReq(options);
validator.boards.validateCreateBoardReq(options);
const { jobId, taskId } = options;
const type = (taskId && 'task') || (jobId && 'batch') || 'node';
const boardInfo = ((type === 'node') && options) || { ...options, ...(await this.getBoardInfo(options, type)) };
Expand Down
12 changes: 6 additions & 6 deletions core/api-server/lib/service/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const minimumBytes = 4100;

class Builds {
async getBuild(options) {
validator.validateBuildId(options);
validator.builds.validateBuildId(options);
const response = await stateManager.algorithms.builds.get(options);
if (!response) {
throw new ResourceNotFoundError('build', options.buildId);
Expand All @@ -28,7 +28,7 @@ class Builds {
}

async getBuilds(options) {
validator.validateResultList(options);
validator.lists.validateResultList(options);
const response = await stateManager.algorithms.builds.list(options);
return response;
}
Expand All @@ -48,7 +48,7 @@ class Builds {
}

async stopBuild(options) {
validator.validateBuildId(options);
validator.builds.validateBuildId(options);
const { buildId } = options;
const build = await this.getBuild({ buildId });
if (!this.isActiveState(build.status)) {
Expand All @@ -63,7 +63,7 @@ class Builds {
}

async rerunBuild(options) {
validator.validateBuildId(options);
validator.builds.validateBuildId(options);
const { buildId } = options;
const build = await this.getBuild({ buildId });
if (this.isActiveState(build.status)) {
Expand Down Expand Up @@ -104,7 +104,7 @@ class Builds {
const version = newAlgorithm.gitRepository.commit.id;
buildId = this._createBuildID(newAlgorithm.name);
const { env, name, gitRepository, type, baseImage } = newAlgorithm;
validator.validateAlgorithmBuildFromGit({ env });
validator.builds.validateAlgorithmBuildFromGit({ env });
await this.startBuild({ buildId, version, env, algorithmName: name, gitRepository, type, baseImage });
}
return { buildId, messages };
Expand All @@ -113,7 +113,7 @@ class Builds {
async _newAlgorithm(file, oldAlgorithm, newAlgorithm) {
const fileInfo = await this._fileInfo(file);
const env = this._resolveEnv(oldAlgorithm, newAlgorithm);
validator.validateAlgorithmBuild({ fileExt: fileInfo.fileExt, env });
validator.builds.validateAlgorithmBuild({ fileExt: fileInfo.fileExt, env });
return { ...newAlgorithm, fileInfo, env };
}

Expand Down
10 changes: 5 additions & 5 deletions core/api-server/lib/service/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { ResourceNotFoundError } = require('../errors');

class ExecutionService {
async getCronResult(options) {
validator.validateResultList(options);
validator.lists.validateResultList(options);
const jobId = this._createCronJobID(options);
const response = await stateManager.getJobResults({ ...options, jobId });
if (response.length === 0) {
Expand All @@ -19,7 +19,7 @@ class ExecutionService {
}

async getCronStatus(options) {
validator.validateResultList(options);
validator.lists.validateResultList(options);
const jobId = this._createCronJobID(options);
const response = await stateManager.jobs.status.list({ ...options, jobId });
if (response.length === 0) {
Expand All @@ -35,7 +35,7 @@ class ExecutionService {
}

async runStoredCron(options) {
validator.validateStoredInternal(options);
validator.internal.validateStoredInternal(options);
const pipeline = await this._createPipeline(options);
const jobId = this._createCronJobID(pipeline, uid({ length: 8 }));
return execution._runStored({ pipeline, jobId, types: [pipelineTypes.STORED, pipelineTypes.INTERNAL, pipelineTypes.CRON] });
Expand All @@ -50,7 +50,7 @@ class ExecutionService {
}

async _toggleCronJob(options, enabled) {
validator.validateCronRequest(options);
validator.cron.validateCronRequest(options);
const pipeline = await stateManager.pipelines.get(options);
if (!pipeline) {
throw new ResourceNotFoundError('pipeline', options.name);
Expand All @@ -77,7 +77,7 @@ class ExecutionService {
const { name } = options;
const pipeline = await stateManager.pipelines.get({ name });
const experiment = { name: (pipeline && pipeline.experimentName) || undefined };
validator.validateExperimentName(experiment);
validator.experiments.validateExperimentName(experiment);
return experiment.name;
}

Expand Down
36 changes: 18 additions & 18 deletions core/api-server/lib/service/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ const PausedState = [pipelineStatuses.PAUSED];

class ExecutionService {
async runRaw(options) {
validator.validateRunRawPipeline(options);
validator.executions.validateRunRawPipeline(options);
return this._run({ pipeline: options, types: [pipelineTypes.RAW] });
}

async runStored(options) {
validator.validateRunStoredPipeline(options);
validator.executions.validateRunStoredPipeline(options);
return this._runStored({ pipeline: options, types: [pipelineTypes.STORED] });
}

async runCaching(options) {
validator.validateCaching(options);
validator.executions.validateCaching(options);
const { error, pipeline } = await cachingService.exec({ jobId: options.jobId, nodeName: options.nodeName });
if (error) {
throw new InvalidDataError(error.message);
Expand All @@ -39,7 +39,7 @@ class ExecutionService {
}

async runAlgorithm(options) {
validator.validateExecAlgorithmRequest(options);
validator.executions.validateExecAlgorithmRequest(options);
const { name, input } = options;
const pipeline = {
name,
Expand Down Expand Up @@ -67,8 +67,8 @@ class ExecutionService {
const { rootJobId } = payload;
const { alreadyExecuted, validateNodes, parentSpan } = payload.options || {};

validator.addPipelineDefaults(pipeline);
validator.validatePipeline(pipeline, { validateNodes });
validator.executions.addPipelineDefaults(pipeline);
validator.executions.validatePipeline(pipeline, { validateNodes });

if (!jobId) {
jobId = this._createJobID({ name: pipeline.name, experimentName: pipeline.experimentName });
Expand All @@ -77,9 +77,9 @@ class ExecutionService {
const span = tracer.startSpan({ name: 'run pipeline', tags: { jobId, name: pipeline.name }, parent: parentSpan });
try {
pipeline = await this._buildPipelineOfPipelines(pipeline);
await validator.validateExperimentExists(pipeline);
const algorithms = await validator.validateAlgorithmExists(pipeline);
const maxExceeded = await validator.validateConcurrentPipelines(pipeline, jobId);
await validator.experiments.validateExperimentExists(pipeline);
const algorithms = await validator.algorithms.validateAlgorithmExists(pipeline);
const maxExceeded = await validator.executions.validateConcurrentPipelines(pipeline, jobId);
types = this._addTypesByAlgorithms(algorithms, types);

if (pipeline.flowInput && !alreadyExecuted) {
Expand Down Expand Up @@ -223,7 +223,7 @@ class ExecutionService {
}

async getJobStatus(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const status = await stateManager.jobs.status.get({ jobId: options.jobId });
if (!status) {
throw new ResourceNotFoundError('status', options.jobId);
Expand All @@ -232,7 +232,7 @@ class ExecutionService {
}

async getPipeline(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const pipeline = await stateManager.executions.stored.get({ jobId: options.jobId });
if (!pipeline) {
throw new ResourceNotFoundError('pipeline', options.jobId);
Expand All @@ -241,7 +241,7 @@ class ExecutionService {
}

async getJobResult(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const jobStatus = await stateManager.jobs.status.get({ jobId: options.jobId });
if (!jobStatus) {
throw new ResourceNotFoundError('status', options.jobId);
Expand All @@ -257,7 +257,7 @@ class ExecutionService {
}

async getPipelinesResult(options) {
validator.validateResultList(options);
validator.lists.validateResultList(options);
const response = await stateManager.getJobResults({ ...options, jobId: `${options.experimentName}:${options.name}` });
if (response.length === 0) {
throw new ResourceNotFoundError('pipeline results', options.name);
Expand All @@ -267,7 +267,7 @@ class ExecutionService {
}

async getPipelinesStatus(options) {
validator.validateResultList(options);
validator.lists.validateResultList(options);
const response = await stateManager.jobs.status.list({ ...options, jobId: `${options.experimentName}:${options.name}` });
if (response.length === 0) {
throw new ResourceNotFoundError('pipeline status', options.name);
Expand All @@ -280,7 +280,7 @@ class ExecutionService {
}

async stopJob(options) {
validator.validateStopPipeline(options);
validator.executions.validateStopPipeline(options);
const jobStatus = await stateManager.jobs.status.get({ jobId: options.jobId });
if (!jobStatus) {
throw new ResourceNotFoundError('jobId', options.jobId);
Expand All @@ -295,7 +295,7 @@ class ExecutionService {
}

async pauseJob(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const { jobId } = options;
const jobStatus = await stateManager.jobs.status.get({ jobId });
if (!jobStatus) {
Expand All @@ -308,7 +308,7 @@ class ExecutionService {
}

async resumeJob(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const { jobId } = options;
const jobStatus = await stateManager.jobs.status.get({ jobId });
if (!jobStatus) {
Expand All @@ -322,7 +322,7 @@ class ExecutionService {
}

async getTree(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const tree = await stateManager.triggers.tree.get({ jobId: options.jobId });
if (!tree) {
throw new ResourceNotFoundError('tree', options.jobId);
Expand Down
4 changes: 2 additions & 2 deletions core/api-server/lib/service/experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Experiment {
}

async insertExperiment(options) {
validator.validateExperimentName(options);
validator.experiments.validateExperimentName(options);
await stateManager.experiments.set(options);
await storageManager.hkubeStore.put({ type: 'experiment', name: options.name, data: options });
}
Expand All @@ -32,7 +32,7 @@ class Experiment {
if (defaultExperiment.DEFAULT_EXPERIMENT_NAME === experimentName) {
throw new ActionNotAllowed(defaultExperiment.DEFAULT_EXPERIMENT_ERROR, experimentName);
}
await validator.validateExperimentExists({ experimentName });
await validator.experiments.validateExperimentExists({ experimentName });
await this._stopAllCrons(experimentName);
await this._cleanAll(experimentName);
return this._deleteExperiment(options);
Expand Down
4 changes: 2 additions & 2 deletions core/api-server/lib/service/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GraphService {
}

async getGraphRaw(options) {
validator.validateJobID(options);
validator.jobs.validateJobID(options);
const { jobId } = options;
const graph = await this._persistency.getGraph({ jobId });
if (!graph) {
Expand All @@ -23,7 +23,7 @@ class GraphService {
}

async getGraphParsed(options) {
validator.validateGraphQuery(options);
validator.graphs.validateGraphQuery(options);
const { jobId, node, sort, order, from, to } = options;
const json = await this._persistency.getGraph({ jobId });
const graph = JSON.parse(json);
Expand Down
8 changes: 4 additions & 4 deletions core/api-server/lib/service/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const execution = require('./execution');

class InternalService {
async runStoredTriggerPipeline(options) {
validator.validateStoredInternal(options);
validator.internal.validateStoredInternal(options);
const { name, parentJobId } = options;
const execPipeline = await stateManager.executions.stored.get({ jobId: parentJobId });
const experimentName = await this._getExperiment(execPipeline);
Expand All @@ -23,13 +23,13 @@ class InternalService {
}

async runStoredSubPipeline(options) {
validator.validateStoredSubPipeline(options);
validator.internal.validateStoredSubPipeline(options);
const { pipeline, rootJobId, parentSpan } = await this._createPipeline(options);
return execution._runStored({ pipeline, rootJobId, mergeFlowInput: true, options: { parentSpan }, types: [pipelineTypes.INTERNAL, pipelineTypes.STORED, pipelineTypes.SUB_PIPELINE] });
}

async runRawSubPipeline(options) {
validator.validateRawSubPipeline(options);
validator.internal.validateRawSubPipeline(options);
const { pipeline, rootJobId, parentSpan } = await this._createPipeline(options);
return execution._run({ pipeline, rootJobId, options: { parentSpan }, types: [pipelineTypes.INTERNAL, pipelineTypes.RAW, pipelineTypes.SUB_PIPELINE] });
}
Expand All @@ -49,7 +49,7 @@ class InternalService {

_getExperiment(pipeline) {
const experiment = { name: (pipeline && pipeline.experimentName) || undefined };
validator.validateExperimentName(experiment);
validator.experiments.validateExperimentName(experiment);
return experiment.name;
}
}
Expand Down
Loading