@@ -8,6 +8,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
88// input/aws-iam/policy.ts
99const base = require ( "aws-iam/base" ) ;
1010const AWSIAMEntity = base . AWSIAMEntity ;
11+ const common = require ( "aws-iam/common" ) ;
12+ const IAM_ACTIONS = common . IAM_ACTIONS ;
1113const cli = require ( "cli" ) ;
1214var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
1315 getPolicyName ( ) {
@@ -84,7 +86,7 @@ var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
8486 }
8587 }
8688 try {
87- const response = this . makeAWSRequest ( "POST" , "CreatePolicy" , params ) ;
89+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . CREATE_POLICY , params ) ;
8890 if ( response . Policy ) {
8991 this . state = {
9092 policy_arn : response . Policy . Arn ,
@@ -112,7 +114,7 @@ var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
112114 SetAsDefault : true
113115 } ;
114116 try {
115- const response = this . makeAWSRequest ( "POST" , "CreatePolicyVersion" , params ) ;
117+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . CREATE_POLICY_VERSION , params ) ;
116118 if ( response . PolicyVersion ) {
117119 this . state . default_version_id = response . PolicyVersion . VersionId ;
118120 this . state . update_date = response . PolicyVersion . CreateDate ;
@@ -127,26 +129,87 @@ var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
127129 if ( ! this . state . policy_arn ) {
128130 return ;
129131 }
132+ if ( this . state . existing ) {
133+ return ;
134+ }
130135 try {
131- if ( this . state . attachment_count && this . state . attachment_count > 0 ) {
132- cli . output ( `Warning: Policy has ${ this . state . attachment_count } attachments. You may need to detach it first.` ) ;
133- }
136+ this . detachPolicyFromAllEntities ( ) ;
134137 this . deleteNonDefaultVersions ( ) ;
135138 this . deletePolicy ( this . state . policy_arn , this . definition . policy_name ) ;
136139 } catch ( error ) {
137140 throw new Error ( `Failed to delete IAM Policy ${ this . definition . policy_name } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
138141 }
139142 }
143+ detachPolicyFromAllEntities ( ) {
144+ if ( ! this . state . policy_arn ) {
145+ return ;
146+ }
147+ try {
148+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . LIST_ENTITIES_FOR_POLICY , {
149+ PolicyArn : this . state . policy_arn
150+ } ) ;
151+ if ( response . PolicyUsers ) {
152+ const users = Array . isArray ( response . PolicyUsers ) ? response . PolicyUsers : [ response . PolicyUsers ] ;
153+ for ( const user of users ) {
154+ const userName = typeof user === "string" ? user : user . UserName ;
155+ if ( userName ) {
156+ try {
157+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DETACH_USER_POLICY , {
158+ UserName : userName ,
159+ PolicyArn : this . state . policy_arn
160+ } ) ;
161+ } catch ( error ) {
162+ cli . output ( `Warning: Failed to detach policy from user ${ userName } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
163+ }
164+ }
165+ }
166+ }
167+ if ( response . PolicyRoles ) {
168+ const roles = Array . isArray ( response . PolicyRoles ) ? response . PolicyRoles : [ response . PolicyRoles ] ;
169+ for ( const role of roles ) {
170+ const roleName = typeof role === "string" ? role : role . RoleName ;
171+ if ( roleName ) {
172+ try {
173+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DETACH_ROLE_POLICY , {
174+ RoleName : roleName ,
175+ PolicyArn : this . state . policy_arn
176+ } ) ;
177+ } catch ( error ) {
178+ cli . output ( `Warning: Failed to detach policy from role ${ roleName } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
179+ }
180+ }
181+ }
182+ }
183+ if ( response . PolicyGroups ) {
184+ const groups = Array . isArray ( response . PolicyGroups ) ? response . PolicyGroups : [ response . PolicyGroups ] ;
185+ for ( const group of groups ) {
186+ const groupName = typeof group === "string" ? group : group . GroupName ;
187+ if ( groupName ) {
188+ try {
189+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DETACH_GROUP_POLICY , {
190+ GroupName : groupName ,
191+ PolicyArn : this . state . policy_arn
192+ } ) ;
193+ } catch ( error ) {
194+ cli . output ( `Warning: Failed to detach policy from group ${ groupName } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
195+ }
196+ }
197+ }
198+ }
199+ } catch ( error ) {
200+ cli . output ( `Warning: Failed to list policy attachments: ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
201+ }
202+ }
140203 deleteNonDefaultVersions ( ) {
141204 try {
142- const response = this . makeAWSRequest ( "POST" , "ListPolicyVersions" , {
205+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . LIST_POLICY_VERSIONS , {
143206 PolicyArn : this . state . policy_arn
144207 } ) ;
145208 if ( response . Versions ) {
146209 const nonDefaultVersions = response . Versions . filter ( ( v ) => ! v . IsDefaultVersion ) ;
147210 for ( const version of nonDefaultVersions ) {
148211 try {
149- this . makeAWSRequest ( "POST" , "DeletePolicyVersion" , {
212+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DELETE_POLICY_VERSION , {
150213 PolicyArn : this . state . policy_arn ,
151214 VersionId : version . VersionId
152215 } ) ;
0 commit comments