Skip to content

Commit

Permalink
Rework name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kobajagi committed Nov 23, 2023
1 parent 6bf20a3 commit 8167b3e
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions cmd/iam_role_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ func (c *iamRoleDeleteCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

if _, err := uuid.Parse(c.Role); err != nil {
roles, err := globalstate.EgoscaleClient.ListIAMRoles(ctx, zone)
if err != nil {
return err
}

found := false
for _, role := range roles {
if role.Name != nil && *role.Name == c.Role {
c.Role = *role.ID
found = true
break
}
}

if !found {
return fmt.Errorf("role with name %q not found", c.Role)
}
}

if !c.Force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete IAM Role %s?", c.Role)) {
return nil
Expand All @@ -49,19 +69,6 @@ func (c *iamRoleDeleteCmd) cmdRun(_ *cobra.Command, _ []string) error {

var err error
decorateAsyncOperation(fmt.Sprintf("Deleting IAM role %s...", c.Role), func() {
if _, err = uuid.Parse(c.Role); err != nil {
roles, err := globalstate.EgoscaleClient.ListIAMRoles(ctx, zone)
if err != nil {
return
}

for _, role := range roles {
if role.Name != nil && *role.Name == c.Role {
c.Role = *role.ID
break
}
}
}

err = globalstate.EgoscaleClient.DeleteIAMRole(ctx, zone, &egoscale.IAMRole{ID: &c.Role})
})
Expand Down

0 comments on commit 8167b3e

Please sign in to comment.