Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCM-13401 | test: Update DestroyBastionProxy func. #84

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
xueli181114 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading