@@ -37,10 +37,15 @@ import (
37
37
"github.com/openshift/rosa/pkg/rosa"
38
38
)
39
39
40
+ const (
41
+ deleteHcpSharedVpcPoliciesFlagName = "delete-hcp-shared-vpc-policies"
42
+ )
43
+
40
44
var args struct {
41
- prefix string
42
- hostedCP bool
43
- classic bool
45
+ prefix string
46
+ hostedCP bool
47
+ classic bool
48
+ deleteHcpSharedVpcPolicies bool
44
49
}
45
50
46
51
var Cmd = & cobra.Command {
@@ -79,6 +84,13 @@ func init() {
79
84
"Delete classic account roles" ,
80
85
)
81
86
87
+ flags .BoolVar (
88
+ & args .deleteHcpSharedVpcPolicies ,
89
+ deleteHcpSharedVpcPoliciesFlagName ,
90
+ false ,
91
+ "Deletes the Hosted Control Plane shared vpc policies" ,
92
+ )
93
+
82
94
interactive .AddModeFlag (Cmd )
83
95
confirm .AddFlag (flags )
84
96
}
@@ -153,7 +165,7 @@ func run(cmd *cobra.Command, _ []string) {
153
165
}
154
166
155
167
if deleteClassic {
156
- err = deleteAccountRoles (r , env , prefix , clusters , mode , false )
168
+ err = deleteAccountRoles (r , cmd , env , prefix , clusters , mode , false )
157
169
if err != nil {
158
170
r .Reporter .Errorf ("%s" , err )
159
171
os .Exit (1 )
@@ -165,7 +177,7 @@ func run(cmd *cobra.Command, _ []string) {
165
177
}
166
178
167
179
if deleteHostedCP {
168
- err = deleteAccountRoles (r , env , prefix , clusters , mode , true )
180
+ err = deleteAccountRoles (r , cmd , env , prefix , clusters , mode , true )
169
181
if err != nil {
170
182
r .Reporter .Errorf ("%s" , err )
171
183
os .Exit (1 )
@@ -182,8 +194,8 @@ func setDeleteRoles(isClassicFlagSet bool, isHostedCPFlagSet bool) (bool, bool)
182
194
return isClassicFlagSet , isHostedCPFlagSet
183
195
}
184
196
185
- func deleteAccountRoles (r * rosa.Runtime , env string , prefix string , clusters []* cmv1.Cluster , mode string ,
186
- hostedCP bool ) error {
197
+ func deleteAccountRoles (r * rosa.Runtime , cmd * cobra. Command , env string , prefix string , clusters []* cmv1.Cluster ,
198
+ mode string , hostedCP bool ) error {
187
199
var accountRolesMap map [string ]aws.AccountRole
188
200
var roleTypeString string
189
201
if hostedCP {
@@ -203,28 +215,32 @@ func deleteAccountRoles(r *rosa.Runtime, env string, prefix string, clusters []*
203
215
return nil
204
216
}
205
217
218
+ deleteHcpSharedVpcPolicies := args .deleteHcpSharedVpcPolicies
219
+
206
220
switch mode {
207
221
case interactive .ModeAuto :
208
222
r .Reporter .Infof (fmt .Sprintf ("Deleting %saccount roles" , roleTypeString ))
209
223
210
224
r .OCMClient .LogEvent ("ROSADeleteAccountRoleModeAuto" , nil )
211
- deleteHcpSharedVpcPolicies := false
212
- if roles .CheckIfRolesAreHcpSharedVpc (r , finalRoleList ) {
213
- deleteHcpSharedVpcPolicies = confirm .Prompt (true , "Attempt to delete Hosted CP shared VPC" +
214
- " policies?" )
225
+ if roles .CheckIfRolesAreHcpSharedVpc (r , finalRoleList ) &&
226
+ ! cmd .Flag (deleteHcpSharedVpcPoliciesFlagName ).Changed {
227
+ deleteHcpSharedVpcPolicies = confirm .Prompt (true , "Attempt to delete Hosted CP shared VPC policies?" )
215
228
}
216
- for _ , role := range finalRoleList {
217
- if ! confirm .Prompt (true , "Delete the account role '%s'?" , role ) {
218
- continue
219
- }
220
- r .Reporter .Infof ("Deleting account role '%s'" , role )
221
- err := r .AWSClient .DeleteAccountRole (role , prefix , managedPolicies , deleteHcpSharedVpcPolicies )
222
- if err != nil {
223
- r .Reporter .Warnf ("There was an error deleting the account roles or policies: %s" , err )
224
- continue
229
+
230
+ if deleteHcpSharedVpcPolicies {
231
+ for _ , role := range finalRoleList {
232
+ if ! confirm .Prompt (true , "Delete the account role '%s'?" , role ) {
233
+ continue
234
+ }
235
+ r .Reporter .Infof ("Deleting account role '%s'" , role )
236
+ err := r .AWSClient .DeleteAccountRole (role , prefix , managedPolicies , deleteHcpSharedVpcPolicies )
237
+ if err != nil {
238
+ r .Reporter .Warnf ("There was an error deleting the account roles or policies: %s" , err )
239
+ continue
240
+ }
225
241
}
242
+ r .Reporter .Infof (fmt .Sprintf ("Successfully deleted the %s account roles" , roleTypeString ))
226
243
}
227
- r .Reporter .Infof (fmt .Sprintf ("Successfully deleted the %saccount roles" , roleTypeString ))
228
244
case interactive .ModeManual :
229
245
r .OCMClient .LogEvent ("ROSADeleteAccountRoleModeManual" , nil )
230
246
policyMap , arbitraryPolicyMap , err := r .AWSClient .GetAccountRolePolicies (finalRoleList , prefix )
@@ -234,8 +250,7 @@ func deleteAccountRoles(r *rosa.Runtime, env string, prefix string, clusters []*
234
250
235
251
// Get HCP shared vpc policy details if the user is deleting roles related to HCP shared vpc
236
252
policiesOutput := make ([]* iam.GetPolicyOutput , 0 )
237
- if roles .CheckIfRolesAreHcpSharedVpc (r , finalRoleList ) &&
238
- confirm .Prompt (true , "Create commands to delete Hosted CP shared VPC policies?" ) {
253
+ if roles .CheckIfRolesAreHcpSharedVpc (r , finalRoleList ) && deleteHcpSharedVpcPolicies {
239
254
for _ , role := range finalRoleList {
240
255
policies , err := r .AWSClient .GetPolicyDetailsFromRole (awssdk .String (role ))
241
256
policiesOutput = append (policiesOutput , policies ... )
0 commit comments