@@ -4,6 +4,7 @@ const _ = require('lodash');
4
4
const applyRoot = require ( '../root/apply.cmd' ) ;
5
5
const { crudFilenameOption } = require ( '../../helpers/general' ) ;
6
6
const sysRe = require ( '../../helpers/sys-re' ) ;
7
+ const { sdk } = require ( '../../../../logic' ) ;
7
8
8
9
9
10
const command = new Command ( {
@@ -38,6 +39,18 @@ const command = new Command({
38
39
alias : 'd' ,
39
40
describe : 'Description of the new runtime environment' ,
40
41
} )
42
+ . option ( 'assign-accounts' , {
43
+ alias : 'a' ,
44
+ type : Boolean ,
45
+ // default: false,
46
+ describe : 'Assign runtime environment to accounts specified in the yaml' ,
47
+ } )
48
+ . option ( 'unassign-accounts' , {
49
+ alias : 'u' ,
50
+ type : Boolean ,
51
+ // default: false,
52
+ describe : 'Unassign runtime environment from accounts based on diff with current state' ,
53
+ } )
41
54
. example ( 'codefresh patch system-runtime-environments -f ./re.yml' , 'Apply changes to a system runtime environment' ) ;
42
55
} ,
43
56
handler : async ( argv ) => {
@@ -60,6 +73,37 @@ const command = new Command({
60
73
} else {
61
74
await sysRe . update ( options , body ) ;
62
75
console . log ( `Runtime-Environment: '${ options . name } ' patched.` ) ;
76
+ const yamlAccounts = body . accounts ;
77
+ if ( yamlAccounts ) {
78
+ if ( argv . unassignAccounts ) {
79
+ const re = await sysRe . get ( { ...options , extend : false } ) ;
80
+ const accounts = _ . difference ( re . accounts , yamlAccounts ) ;
81
+ if ( ! _ . isEmpty ( accounts ) ) {
82
+ await sdk . onPrem . runtimeEnvs . account . delete ( { name : options . name } , { accounts } ) ;
83
+ console . log ( `Runtime-Environment unassigned from accounts: ${ accounts } ` ) ;
84
+ } else {
85
+ console . log ( 'No accounts to unassign' ) ;
86
+ }
87
+ }
88
+
89
+ if ( argv . assignAccounts ) {
90
+ const re = await sysRe . get ( { ...options , extend : false } ) ;
91
+ const accounts = _ . difference ( yamlAccounts , re . accounts ) ;
92
+ const existing = await sdk . accounts . listAccounts ( { _id : accounts } ) ;
93
+ const nonExisting = _ . difference ( accounts , existing . map ( ( { id} ) => id ) ) ;
94
+ if ( ! _ . isEmpty ( nonExisting ) ) {
95
+ throw new CFError ( {
96
+ message : `Accounts do not exist: ${ nonExisting } ` ,
97
+ } ) ;
98
+ }
99
+ if ( ! _ . isEmpty ( accounts ) ) {
100
+ await sdk . onPrem . runtimeEnvs . account . modify ( { name : options . name } , { accounts } ) ;
101
+ console . log ( `Runtime-Environment assigned to accounts: ${ accounts } ` ) ;
102
+ } else {
103
+ console . log ( 'No accounts to assign' ) ;
104
+ }
105
+ }
106
+ }
63
107
}
64
108
} ,
65
109
} ) ;
0 commit comments