@@ -4,6 +4,7 @@ const _ = require('lodash');
44const applyRoot = require ( '../root/apply.cmd' ) ;
55const { crudFilenameOption } = require ( '../../helpers/general' ) ;
66const sysRe = require ( '../../helpers/sys-re' ) ;
7+ const { sdk } = require ( '../../../../logic' ) ;
78
89
910const command = new Command ( {
@@ -38,6 +39,18 @@ const command = new Command({
3839 alias : 'd' ,
3940 describe : 'Description of the new runtime environment' ,
4041 } )
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+ } )
4154 . example ( 'codefresh patch system-runtime-environments -f ./re.yml' , 'Apply changes to a system runtime environment' ) ;
4255 } ,
4356 handler : async ( argv ) => {
@@ -60,6 +73,37 @@ const command = new Command({
6073 } else {
6174 await sysRe . update ( options , body ) ;
6275 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+ }
63107 }
64108 } ,
65109} ) ;
0 commit comments