@@ -59,6 +59,7 @@ export const handler = async (input: PolicyChangeEvent) => {
5959
6060 const organizationAdminRole = config [ 'global-options' ] [ 'organization-admin-role' ] ! ;
6161 const configScps = config [ 'global-options' ] . scps ;
62+ const ignoredOus : string [ ] = config [ 'global-options' ] [ 'ignored-ous' ] || [ ] ;
6263 const scpNames = configScps . map ( scp =>
6364 ServiceControlPolicy . policyNameToAcceleratorPolicyName ( {
6465 acceleratorPrefix,
@@ -71,24 +72,40 @@ export const handler = async (input: PolicyChangeEvent) => {
7172 console . warn ( `Missing policyId, Ignoring` ) ;
7273 return 'INVALID_REQUEST' ;
7374 }
74- if ( ! ( await isAcceleratorScp ( policyId , scpNames ) ) ) {
75+ const eventName = requestDetail . eventName ;
76+ if ( eventName !== 'DeletePolicy' && ! ( await isAcceleratorScp ( policyId , scpNames ) ) ) {
7577 console . log ( `SCP ${ policyId } is not managed by Accelerator` ) ;
7678 return 'SUCCESS' ;
7779 }
78- const eventName = requestDetail . eventName ;
80+ const scps = new ServiceControlPolicy ( acceleratorPrefix , organizationAdminRole , organizations ) ;
81+ const { organizationalUnits, accounts } = await loadAccountsAndOrganizationsFromConfig ( config ) ;
7982 if ( eventName === 'DetachPolicy' ) {
8083 const { targetId } = requestDetail . requestParameters ;
8184 if ( ! targetId ) {
8285 console . warn ( `Missing required parameters, Ignoring` ) ;
8386 return 'INVALID_REQUEST' ;
8487 }
88+ if ( ignoredOus . length > 0 ) {
89+ if ( targetId . startsWith ( 'ou-' ) ) {
90+ const destinationOrg = await organizations . getOrganizationalUnitWithPath ( targetId ) ;
91+ const destinationRootOrg = destinationOrg . Name ! ;
92+ if ( ignoredOus . includes ( destinationRootOrg ) ) {
93+ console . log ( `DetachPolicy is on ignored-ou from ROOT, no need to reattach` ) ;
94+ return 'IGNORE' ;
95+ }
96+ } else {
97+ const accountObject = accounts . find ( acc => acc . accountId === targetId ) ;
98+ if ( ignoredOus . includes ( accountObject ?. organizationalUnit ! ) ) {
99+ console . log ( `DetachPolicy is on account in ignored-ous from ROOT, no need to reattach` ) ;
100+ return 'IGNORE' ;
101+ }
102+ }
103+ }
85104 // ReAttach target to policy
86- console . log ( `ReAttaching target "${ targetId } " to policy "${ policyId } "` ) ;
105+ console . log ( `Reattaching target "${ targetId } " to policy "${ policyId } "` ) ;
87106 await organizations . attachPolicy ( policyId , targetId ) ;
88107 } else if ( eventName === 'UpdatePolicy' || eventName === 'DeletePolicy' ) {
89- console . log ( `${ eventName } , Changing back to original config from config` ) ;
90- const scps = new ServiceControlPolicy ( acceleratorPrefix , organizationAdminRole , organizations ) ;
91- const { organizationalUnits, accounts } = await loadAccountsAndOrganizationsFromConfig ( config ) ;
108+ console . log ( `${ eventName } , changing back to original config from config` ) ;
92109
93110 // Find policy config
94111 const globalOptionsConfig = config [ 'global-options' ] ;
0 commit comments