-
Notifications
You must be signed in to change notification settings - Fork 20
/
readme.js
110 lines (99 loc) · 3.71 KB
/
readme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const storageManager = require('@hkube/storage-manager');
const validator = require('../validation/api-validator');
const stateManager = require('../state/state-manager');
const { ResourceNotFoundError } = require('../errors');
class Readme {
async getPipeline(options) {
const { name } = options;
validator.pipelines.validatePipelineName(options.name);
const pipeline = await stateManager.pipelines.get(options);
if (!pipeline) {
throw new ResourceNotFoundError('pipeline', options.name);
}
let result;
let error;
try {
result = await storageManager.hkubeStore.get({ type: 'readme/pipeline', name });
}
catch (e) {
error = e.message;
}
if (error) {
throw new ResourceNotFoundError('readme', options.name, error);
}
return result;
}
async insertPipeline(options) {
return this._updatePipelineReadme(options);
}
async updatePipeline(options) {
return this._updatePipelineReadme(options);
}
async _updatePipelineReadme(options) {
const { name, data } = options;
validator.pipelines.validatePipelineName(options.name);
const pipeline = await stateManager.pipelines.get(options);
if (!pipeline) {
throw new ResourceNotFoundError('pipeline', options.name);
}
const result = await storageManager.hkubeStore.put({ type: 'readme/pipeline', name, data: { name, readme: data } });
return result;
}
async deletePipeline(options) {
const { name } = options;
validator.pipelines.validatePipelineName(name);
const pipeline = await stateManager.pipelines.get(options);
if (!pipeline) {
throw new ResourceNotFoundError('pipeline', options.name);
}
const result = await storageManager.hkubeStore.delete({ type: 'readme/pipeline', name });
return result;
}
async getAlgorithm(options) {
const { name } = options;
validator.jobs.validateName(options);
const algorithm = await stateManager.algorithms.store.get(options);
if (!algorithm) {
throw new ResourceNotFoundError('algorithm', options.name);
}
let result;
let error;
try {
result = await storageManager.hkubeStore.get({ type: 'readme/algorithms', name });
}
catch (e) {
error = e.message;
}
if (error) {
throw new ResourceNotFoundError('readme', options.name, error);
}
return result;
}
async insertAlgorithm(options) {
return this._updateAlgorithmReadme(options);
}
async updateAlgorithm(options) {
return this._updateAlgorithmReadme(options);
}
async _updateAlgorithmReadme(options) {
const { name, data } = options;
validator.algorithms.validateUpdateAlgorithm(options);
const algorithm = await stateManager.algorithms.store.get(options);
if (!algorithm) {
throw new ResourceNotFoundError('algorithm', options.name);
}
const result = await storageManager.hkubeStore.put({ type: 'readme/algorithms', name, data: { name, readme: data } });
return result;
}
async deleteAlgorithm(options) {
const { name } = options;
validator.jobs.validateName(options);
const algorithm = await stateManager.algorithms.store.get(options);
if (!algorithm) {
throw new ResourceNotFoundError('algorithm', options.name);
}
const result = await storageManager.hkubeStore.delete({ type: 'readme/algorithms', name });
return result;
}
}
module.exports = new Readme();