@@ -22,11 +22,14 @@ import (
22
22
"os"
23
23
"strings"
24
24
25
+ awssdk "github.com/aws/aws-sdk-go-v2/aws"
26
+ "github.com/aws/aws-sdk-go-v2/service/iam"
25
27
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
26
28
"github.com/spf13/cobra"
27
29
28
30
"github.com/openshift/rosa/pkg/aws"
29
31
awscb "github.com/openshift/rosa/pkg/aws/commandbuilder"
32
+ "github.com/openshift/rosa/pkg/aws/tags"
30
33
"github.com/openshift/rosa/pkg/interactive"
31
34
"github.com/openshift/rosa/pkg/interactive/confirm"
32
35
"github.com/openshift/rosa/pkg/ocm"
@@ -207,7 +210,8 @@ func deleteAccountRoles(r *rosa.Runtime, env string, prefix string, clusters []*
207
210
r .OCMClient .LogEvent ("ROSADeleteAccountRoleModeAuto" , nil )
208
211
deleteHcpSharedVpcPolicies := false
209
212
if roles .CheckIfRolesAreHcpSharedVpc (r , finalRoleList ) {
210
- deleteHcpSharedVpcPolicies = confirm .Prompt (true , "Attempt to delete Hosted CP shared VPC policies?" )
213
+ deleteHcpSharedVpcPolicies = confirm .Prompt (true , "Create commands to delete Hosted CP shared VPC" +
214
+ " policies?" )
211
215
}
212
216
for _ , role := range finalRoleList {
213
217
if ! confirm .Prompt (true , "Delete the account role '%s'?" , role ) {
@@ -227,7 +231,21 @@ func deleteAccountRoles(r *rosa.Runtime, env string, prefix string, clusters []*
227
231
if err != nil {
228
232
return fmt .Errorf ("There was an error getting the policy: %v" , err )
229
233
}
230
- commands := buildCommand (finalRoleList , policyMap , arbitraryPolicyMap , managedPolicies )
234
+
235
+ // Get HCP shared vpc policy details if the user is deleting roles related to HCP shared vpc
236
+ policiesOutput := make ([]* iam.GetPolicyOutput , 0 )
237
+ if roles .CheckIfRolesAreHcpSharedVpc (r , finalRoleList ) &&
238
+ confirm .Prompt (true , "Attempt to delete Hosted CP shared VPC policies?" ) {
239
+ for _ , role := range finalRoleList {
240
+ policiesOutput , err = r .AWSClient .GetPolicyDetailsFromRole (awssdk .String (role ))
241
+ if err != nil {
242
+ r .Reporter .Infof ("There was an error getting details of policies attached to role '%s': %v" ,
243
+ role , err )
244
+ }
245
+ }
246
+ }
247
+
248
+ commands := buildCommand (finalRoleList , policyMap , arbitraryPolicyMap , managedPolicies , policiesOutput )
231
249
232
250
if r .Reporter .IsTerminal () {
233
251
r .Reporter .Infof ("Run the following commands to delete the account roles and policies:\n " )
@@ -298,7 +316,8 @@ func checkIfRoleAssociated(clusters []*cmv1.Cluster, role aws.Role) string {
298
316
}
299
317
300
318
func buildCommand (roleNames []string , policyMap map [string ][]aws.PolicyDetail ,
301
- arbitraryPolicyMap map [string ][]aws.PolicyDetail , managedPolicies bool ) string {
319
+ arbitraryPolicyMap map [string ][]aws.PolicyDetail , managedPolicies bool ,
320
+ hcpSharedVpcPoliciesOutput []* iam.GetPolicyOutput ) string {
302
321
commands := []string {}
303
322
for _ , roleName := range roleNames {
304
323
policyDetails := policyMap [roleName ]
@@ -344,6 +363,27 @@ func buildCommand(roleNames []string, policyMap map[string][]aws.PolicyDetail,
344
363
AddParam (awscb .RoleName , roleName ).
345
364
Build ()
346
365
commands = append (commands , deleteRole )
366
+
367
+ if len (hcpSharedVpcPoliciesOutput ) > 0 { // Delete HCP shared VPC policies
368
+ for _ , hcpSharedVpcPolicy := range hcpSharedVpcPoliciesOutput {
369
+ hasRhManagedTag := false
370
+ hasHcpSharedVpcTag := false
371
+ for _ , tag := range hcpSharedVpcPolicy .Policy .Tags {
372
+ if * tag .Key == tags .RedHatManaged {
373
+ hasRhManagedTag = true
374
+ } else if * tag .Key == tags .HcpSharedVpc {
375
+ hasHcpSharedVpcTag = true
376
+ }
377
+ }
378
+ if hasHcpSharedVpcTag && hasRhManagedTag {
379
+ deletePolicy := awscb .NewIAMCommandBuilder ().
380
+ SetCommand (awscb .DeletePolicy ).
381
+ AddParam (awscb .PolicyName , * hcpSharedVpcPolicy .Policy .PolicyName ).
382
+ Build ()
383
+ commands = append (commands , deletePolicy )
384
+ }
385
+ }
386
+ }
347
387
}
348
388
return awscb .JoinCommands (commands )
349
389
}
0 commit comments