Skip to content

Commit

Permalink
OCM-13401 | test: Update DestroyBastionProxy func.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameszwang committed Jan 14, 2025
1 parent 8928059 commit eaee2ee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/aws/aws_client/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (client *AWSClient) WaitForInstanceReady(instanceID string, timeout time.Du
instanceID,
}
log.LogInfo("Waiting for below instances ready: %s ", strings.Join(instanceIDs, "|"))
time.Sleep(2 * time.Second)
_, err := client.WaitForInstancesRunning(instanceIDs, 10)
return err
}
Expand Down
61 changes: 55 additions & 6 deletions pkg/test/vpc_client/bastion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/openshift-online/ocm-common/pkg/file"
"golang.org/x/crypto/bcrypt"
"net/url"
"strings"
"time"

CON "github.com/openshift-online/ocm-common/pkg/aws/consts"
Expand Down Expand Up @@ -130,15 +131,63 @@ func (vpc *VPC) PrepareBastionProxy(zone string, keypairName string, privateKeyP
return proxyUrl, nil
}

func (vpc *VPC) DestroyBastionProxy(instance types.Instance) error {
var instanceIDs []string
instanceIDs = append(instanceIDs, *instance.InstanceId)
err := vpc.AWSClient.TerminateInstances(instanceIDs, true, 10)
func (vpc *VPC) DestroyBastionProxy() error {
filters := []map[string][]string{
{
"vpc-id": []string{
vpc.VpcID,
},
},
}
filters = append(filters, map[string][]string{
"tag:Name": {
CON.BastionName,
},
})

insts, err := vpc.AWSClient.ListInstances([]string{}, filters...)

if err != nil {
log.LogError("Error happened when list instances for vpc %s: %s", vpc.VpcID, err)
return err
}
needTermination := []string{}
keyPairNames := []string{}
for _, inst := range insts {
needTermination = append(needTermination, *inst.InstanceId)
if inst.KeyName != nil {
keyPairNames = append(keyPairNames, *inst.KeyName)
}
}
err = vpc.AWSClient.TerminateInstances(needTermination, true, 20)
if err != nil {
log.LogError("Terminating instances %s meet error: %s", strings.Join(needTermination, ","), err)
} else {
log.LogInfo("Terminating instances %s successfully", strings.Join(needTermination, ","))
}
err = vpc.DeleteKeyPair(keyPairNames)
if err != nil {
log.LogError("Delete key pair %s meet error: %s", strings.Join(keyPairNames, ","), err)
}
needCleanGroups := []types.SecurityGroup{}
securityGroups, err := vpc.AWSClient.ListSecurityGroups(vpc.VpcID)
if err != nil {
log.LogError("Terminate instance failed")
return err
}
return nil
for _, sg := range securityGroups {
for _, tag := range sg.Tags {
if *tag.Key == "Name" && *tag.Value == CON.AdditionalSecurityGroupName {
needCleanGroups = append(needCleanGroups, sg)
}
}
}
for _, sg := range needCleanGroups {
_, err = vpc.AWSClient.DeleteSecurityGroup(*sg.GroupId)
if err != nil {
return err
}
}
return err
}

func generateBcryptPassword(plainPassword string) (string, error) {
Expand Down

0 comments on commit eaee2ee

Please sign in to comment.