@@ -2,33 +2,59 @@ const _ = require('lodash');
2
2
const CFError = require ( 'cf-errors' ) ;
3
3
const { validatePipelineSpec, checkOrProjectExists } = require ( './validation' ) ;
4
4
const { sdk } = require ( '../../../logic' ) ;
5
+ const { createAnnotations } = require ( '../commands/annotation/annotation.logic' ) ;
5
6
6
- class EntitiesManifests {
7
- constructor ( { name, data } = { } ) {
8
- this . name = name ;
9
- this . data = data ;
10
- }
11
-
12
- createEntity ( entity ) {
13
- if ( ! EntitiesManifests . _entities [ entity ] ) {
14
- throw new CFError ( `Entity: ${ entity } not supported` ) ;
15
- }
16
- return EntitiesManifests . _entities [ entity ] ( {
17
- name : this . name ,
18
- data : this . data ,
19
- } ) ;
7
+ function _basicValidation ( { name } ) {
8
+ if ( ! name ) {
9
+ throw new CFError ( 'Name is missing' ) ;
20
10
}
21
11
}
22
12
23
- EntitiesManifests . _entities = {
24
- context : ( { data } ) => sdk . contexts . create ( data ) ,
25
- 'step-type' : ( { data } ) => sdk . steps . create ( data ) ,
26
- project : ( { name, data } ) => checkOrProjectExists ( name )
27
- . then ( ( ) => sdk . projects . create ( { projectName : name , ..._ . pick ( data . metadata , [ 'tags' , 'variables' ] ) } ) ) ,
28
- pipeline : ( { data } ) => validatePipelineSpec ( data )
29
- . then ( ( result = { } ) => ( result . valid ? sdk . pipelines . create ( data ) : console . warn ( result . message ) ) ) ,
13
+ const _entities = {
14
+
15
+ context : {
16
+ validate : _basicValidation ,
17
+ create : ( { data } ) => sdk . contexts . create ( data ) ,
18
+ } ,
19
+ 'step-type' : {
20
+ validate : _basicValidation ,
21
+ create : ( { data } ) => sdk . steps . create ( data ) ,
22
+ } ,
23
+ project : {
24
+ validate : _basicValidation ,
25
+ create : ( { name, data } ) => checkOrProjectExists ( name )
26
+ . then ( ( ) => sdk . projects . create ( { projectName : name , ..._ . pick ( data . metadata , [ 'tags' , 'variables' ] ) } ) ) ,
27
+ } ,
28
+ pipeline : {
29
+ validate : _basicValidation ,
30
+ create : ( { data } ) => validatePipelineSpec ( data )
31
+ . then ( ( result = { } ) => ( result . valid ? sdk . pipelines . create ( data ) : console . warn ( result . message ) ) ) ,
32
+ } ,
33
+ annotation : {
34
+ create : ( { data } ) => {
35
+ const labels = _ . get ( data . metadata , 'labels' , [ ] )
36
+ . map ( ( { key, value } ) => `${ key } =${ value } ` ) ;
37
+ return createAnnotations ( { labels, ..._ . pick ( data . metadata , [ 'entityId' , 'entityType' ] ) } ) ;
38
+ } ,
39
+ } ,
30
40
} ;
31
41
32
- EntitiesManifests . list = Object . keys ( EntitiesManifests . _entities ) ;
42
+ const entityList = Object . keys ( _entities ) ;
43
+
44
+ function createEntity ( options ) {
45
+ const { entity } = options ;
46
+ if ( ! _entities [ entity ] ) {
47
+ throw new CFError ( `Entity: ${ entity } not supported` ) ;
48
+ }
49
+
50
+ if ( _entities [ entity ] . validate instanceof Function ) {
51
+ _entities [ entity ] . validate ( options ) ;
52
+ }
33
53
34
- module . exports = EntitiesManifests ;
54
+ return _entities [ entity ] . create ( options ) ;
55
+ }
56
+
57
+ module . exports = {
58
+ createEntity,
59
+ entityList,
60
+ } ;
0 commit comments