diff --git a/go.mod b/go.mod index 758523f..be394aa 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.26.4 require ( github.com/apparentlymart/go-cidr v1.1.1 - github.com/aws/aws-sdk-go v1.55.8 + github.com/aws/aws-sdk-go-v2 v1.42.1 github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.11.1 gopkg.in/yaml.v2 v2.4.0 @@ -15,20 +15,20 @@ require ( ) require ( - github.com/aws/aws-sdk-go-v2 v1.42.0 // indirect github.com/aws/aws-sdk-go-v2/config v1.32.24 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.19.23 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.29 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.29 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.29 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30 // indirect github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.30 // indirect + github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.56.0 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.12 // indirect github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.29 // indirect github.com/aws/aws-sdk-go-v2/service/signin v1.1.5 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.31.3 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.6 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.43.3 // indirect - github.com/aws/smithy-go v1.27.2 // indirect + github.com/aws/smithy-go v1.27.3 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect diff --git a/go.sum b/go.sum index eca0262..4ea6c97 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,11 @@ github.com/apparentlymart/go-cidr v1.1.1 h1:oEEk8CE0HP0YpHxsegk/TaOtR2FLHdWv4p3eM4ceUwg= github.com/apparentlymart/go-cidr v1.1.1/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc= -github.com/aws/aws-sdk-go v1.55.8 h1:JRmEUbU52aJQZ2AjX4q4Wu7t4uZjOu71uyNmaWlUkJQ= -github.com/aws/aws-sdk-go v1.55.8/go.mod h1:ZkViS9AqA6otK+JBBNH2++sx1sgxrPKcSzPPvQkUtXk= github.com/aws/aws-sdk-go-v2 v1.42.0 h1:XvXMJTkFQtpBKIWZnmr9ZEOc2InWM2yldjXEJ/bymhA= github.com/aws/aws-sdk-go-v2 v1.42.0/go.mod h1:27+ACypSLljLAEKsCYOmrjKh83vuTRkuAe9Uv/3A4bg= +github.com/aws/aws-sdk-go-v2 v1.42.1 h1:9eOTgu1z/dVtYpNZ3/8/XbbaX0x/BqE3HUzAzs6K0ek= +github.com/aws/aws-sdk-go-v2 v1.42.1/go.mod h1:5pKeft2eJj+gElQ38Jqg4ibCqh+/AK33/0X3hip7IjM= +github.com/aws/aws-sdk-go-v2 v1.55.8 h1:JRmEUbU52aJQZ2AjX4q4Wu7t4uZjOu71uyNmaWlUkJQ= +github.com/aws/aws-sdk-go-v2 v1.55.8/go.mod h1:ZkViS9AqA6otK+JBBNH2++sx1sgxrPKcSzPPvQkUtXk= github.com/aws/aws-sdk-go-v2/config v1.32.24 h1:aEDEj533yGdVvEHfkCY0D/1FbDrjnZr4pIulxRjqpHs= github.com/aws/aws-sdk-go-v2/config v1.32.24/go.mod h1:yZtrGKJGlqfEW+/m2uTsJK+Jz7xF5R0eZfgcIG9m1ss= github.com/aws/aws-sdk-go-v2/credentials v1.19.23 h1:Zhu3GOpRCkNjtE/gJpuPDsytSnaCCTQk8neAGsgzG5Y= @@ -12,10 +14,16 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.29 h1:r6qZHbT+wxgWO/e9vYNUEt github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.29/go.mod h1:QRnaRcTVGKPGRy8w78HMQtKUGRYcnMZAANATkeVA6Mo= github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.29 h1:f3vKqSo13fhTYb+JEcXwXefZQE26I1FB5eTSniU67ko= github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.29/go.mod h1:MzoLFUArKGpGD+ukmPiTPG1X5x4o6M2kq4v2dr1FiEc= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30 h1:xM/Is9cKMHa8Jj8zkvWhvrFkZsXJV9E+BB4g0HW0duQ= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30/go.mod h1:WueJeNDZvK1fMYEWJIkcivBfEzUkTpBhzlrUKKY8EuA= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.29 h1:RdwIf/CuUsvJX3RgJagbOyotl/cxoLY4xviKuE7p2GY= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.29/go.mod h1:71wt8W2EgswdZy9Mf9KNnzxZ3TiZlv4caKghPktDOkA= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30 h1:jn46zC9LdsVR/ZpMIJqMqb8hHv31BlLx3ulVqNspUOk= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30/go.mod h1:1hTMsAgbdS/AtUi4bw8+gUuh1pceo+eXRLfpSuSQj3M= github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.30 h1:VTGy885W5DKBxWRUJbym9hytNaYzsyaPkCHGRRMAOhU= github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.30/go.mod h1:AS0HycUvJRFvTt613AYDOgO2jzw+00cVSMny8XB3yMY= +github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.56.0 h1:OJRqQ6G7RjmwJ9fkhFgcJBSinjrLJxfd5AacBUrhKXc= +github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.56.0/go.mod h1:qNnJkZTDHDL2sO8hyVH2yILcfSEkjP/pIns2JsF1g1o= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.12 h1:ZD2+BSw9vFsNlKYIasSNt3uDbjqqXIBcM13UJv/Lx2k= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.12/go.mod h1:Ms4zlcVBbXbiP7EVLhl+lgjvA/a7YphqQ3Ih3174EmI= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.29 h1:DRebniUGZ2MqiiIVmQJ04vIXr918hubdHMnarSLEWyU= @@ -30,6 +38,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.43.3 h1:VrIhKRCSK1umelSgB9RghvA9RTUY github.com/aws/aws-sdk-go-v2/service/sts v1.43.3/go.mod h1:r8wkDOuLaaMFqFiYAb8dGY2A3gJCOujMc6CFOVC4Zhc= github.com/aws/smithy-go v1.27.2 h1:y9NPmSE6am6LjEFPfqHqG/jJk7AauQvhCJONKh7kpzk= github.com/aws/smithy-go v1.27.2/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= +github.com/aws/smithy-go v1.27.3 h1:F3Zb497UhhskkfpJmfkXswyo+t0sh9OTBnIHjogWbVY= +github.com/aws/smithy-go v1.27.3/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= diff --git a/hatchery/alb.go b/hatchery/alb.go index b87c899..7473f1b 100644 --- a/hatchery/alb.go +++ b/hatchery/alb.go @@ -1,51 +1,50 @@ package hatchery import ( + "context" + "errors" "fmt" "os" "strings" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/elbv2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2" + "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types" + "github.com/aws/smithy-go" ) -func (creds *CREDS) createTargetGroup(userName string, vpcId string, svc *elbv2.ELBV2) (*elbv2.CreateTargetGroupOutput, error) { +func (creds *CREDS) createTargetGroup(userName string, vpcId string, svc *elasticloadbalancingv2.Client) (*elasticloadbalancingv2.CreateTargetGroupOutput, error) { tgName := truncateString(strings.ReplaceAll(os.Getenv("GEN3_ENDPOINT"), ".", "-")+userToResourceName(userName, "service")+"tg", 32) - input := &elbv2.CreateTargetGroupInput{ + input := &elasticloadbalancingv2.CreateTargetGroupInput{ Name: aws.String(tgName), - Port: aws.Int64(80), - Protocol: aws.String("HTTP"), + Port: aws.Int32(80), + Protocol: types.ProtocolEnumHttp, VpcId: aws.String(vpcId), - TargetType: aws.String("ip"), + TargetType: types.TargetTypeEnumIp, HealthCheckPath: aws.String("/lw-workspace/proxy/"), - Matcher: &elbv2.Matcher{ + Matcher: &types.Matcher{ HttpCode: aws.String("200-499"), }, } - result, err := svc.CreateTargetGroup(input) + //TODO: Consider using parent request context for propagation. + //In AWS GO SDK V2 context is now required. + //Currently we do not propagate context canceling to downstream requests + //To preserve current behavior, local context is created. + ctx = context.TODO() + + result, err := svc.CreateTargetGroup(ctx, input) + + //TODO: Consider typecasting error to specific type for more specific error handling + //i.e. types.DuplicateTargetGroupNameException + //Previous code only print out error type string, so there is no need for type casting if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeDuplicateTargetGroupNameException: - fmt.Println(elbv2.ErrCodeDuplicateTargetGroupNameException, aerr.Error()) - case elbv2.ErrCodeTooManyTargetGroupsException: - fmt.Println(elbv2.ErrCodeTooManyTargetGroupsException, aerr.Error()) - case elbv2.ErrCodeInvalidConfigurationRequestException: - fmt.Println(elbv2.ErrCodeInvalidConfigurationRequestException, aerr.Error()) - case elbv2.ErrCodeTooManyTagsException: - fmt.Println(elbv2.ErrCodeTooManyTagsException, aerr.Error()) - default: - fmt.Println(aerr.Error()) - return nil, err - } + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + fmt.Println(apiErr.ErrorCode(), apiErr.ErrorMessage()) } else { - // Print the error, cast err to awserr.Error to get the Code and - // Message from an error. fmt.Println(err.Error()) - return nil, err } return nil, err } @@ -54,163 +53,107 @@ func (creds *CREDS) createTargetGroup(userName string, vpcId string, svc *elbv2. } -func (creds *CREDS) setTargetGroupAttributes(svc *elbv2.ELBV2, targetGroupArn string) (*elbv2.ModifyTargetGroupAttributesOutput, error) { - modifyTargetGroupAttributesInput := &elbv2.ModifyTargetGroupAttributesInput{ +func (creds *CREDS) setTargetGroupAttributes(svc *elasticloadbalancingv2.Client, targetGroupArn string) (*elasticloadbalancingv2.ModifyTargetGroupAttributesOutput, error) { + modifyTargetGroupAttributesInput := &elasticloadbalancingv2.ModifyTargetGroupAttributesInput{ TargetGroupArn: aws.String(targetGroupArn), - Attributes: []*elbv2.TargetGroupAttribute{ + Attributes: []types.TargetGroupAttribute{ { Key: aws.String("deregistration_delay.timeout_seconds"), Value: aws.String("0"), }, }, } - modifyTargetGroup, err := svc.ModifyTargetGroupAttributes(modifyTargetGroupAttributesInput) + + ctx = context.TODO() + + modifyTargetGroup, err := svc.ModifyTargetGroupAttributes(ctx, modifyTargetGroupAttributesInput) + if err != nil { return nil, err } return modifyTargetGroup, nil } -func (creds *CREDS) createListener(svc *elbv2.ELBV2, loadBalancer string, targetGroup string) (*elbv2.CreateListenerOutput, error) { - input := &elbv2.CreateListenerInput{ - DefaultActions: []*elbv2.Action{ +func (creds *CREDS) createListener(svc *elasticloadbalancingv2.Client, loadBalancer string, targetGroup string) (*elasticloadbalancingv2.CreateListenerOutput, error) { + input := &elasticloadbalancingv2.CreateListenerInput{ + DefaultActions: []types.Action{ { TargetGroupArn: aws.String(targetGroup), - Type: aws.String("forward"), + Type: types.ActionTypeEnumForward, }, }, LoadBalancerArn: aws.String(loadBalancer), - Port: aws.Int64(80), - Protocol: aws.String("HTTP"), + Port: aws.Int32(80), + Protocol: types.ProtocolEnumHttp, } - result, err := svc.CreateListener(input) + ctx = context.TODO() + + result, err := svc.CreateListener(ctx, input) + if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeDuplicateListenerException: - fmt.Println(elbv2.ErrCodeDuplicateListenerException, aerr.Error()) - case elbv2.ErrCodeTooManyListenersException: - fmt.Println(elbv2.ErrCodeTooManyListenersException, aerr.Error()) - case elbv2.ErrCodeTooManyCertificatesException: - fmt.Println(elbv2.ErrCodeTooManyCertificatesException, aerr.Error()) - case elbv2.ErrCodeLoadBalancerNotFoundException: - fmt.Println(elbv2.ErrCodeLoadBalancerNotFoundException, aerr.Error()) - case elbv2.ErrCodeTargetGroupNotFoundException: - fmt.Println(elbv2.ErrCodeTargetGroupNotFoundException, aerr.Error()) - case elbv2.ErrCodeTargetGroupAssociationLimitException: - fmt.Println(elbv2.ErrCodeTargetGroupAssociationLimitException, aerr.Error()) - case elbv2.ErrCodeInvalidConfigurationRequestException: - fmt.Println(elbv2.ErrCodeInvalidConfigurationRequestException, aerr.Error()) - case elbv2.ErrCodeIncompatibleProtocolsException: - fmt.Println(elbv2.ErrCodeIncompatibleProtocolsException, aerr.Error()) - case elbv2.ErrCodeSSLPolicyNotFoundException: - fmt.Println(elbv2.ErrCodeSSLPolicyNotFoundException, aerr.Error()) - case elbv2.ErrCodeCertificateNotFoundException: - fmt.Println(elbv2.ErrCodeCertificateNotFoundException, aerr.Error()) - case elbv2.ErrCodeUnsupportedProtocolException: - fmt.Println(elbv2.ErrCodeUnsupportedProtocolException, aerr.Error()) - case elbv2.ErrCodeTooManyRegistrationsForTargetIdException: - fmt.Println(elbv2.ErrCodeTooManyRegistrationsForTargetIdException, aerr.Error()) - case elbv2.ErrCodeTooManyTargetsException: - fmt.Println(elbv2.ErrCodeTooManyTargetsException, aerr.Error()) - case elbv2.ErrCodeTooManyActionsException: - fmt.Println(elbv2.ErrCodeTooManyActionsException, aerr.Error()) - case elbv2.ErrCodeInvalidLoadBalancerActionException: - fmt.Println(elbv2.ErrCodeInvalidLoadBalancerActionException, aerr.Error()) - case elbv2.ErrCodeTooManyUniqueTargetGroupsPerLoadBalancerException: - fmt.Println(elbv2.ErrCodeTooManyUniqueTargetGroupsPerLoadBalancerException, aerr.Error()) - case elbv2.ErrCodeALPNPolicyNotSupportedException: - fmt.Println(elbv2.ErrCodeALPNPolicyNotSupportedException, aerr.Error()) - case elbv2.ErrCodeTooManyTagsException: - fmt.Println(elbv2.ErrCodeTooManyTagsException, aerr.Error()) - default: - fmt.Println(aerr.Error()) - } + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + fmt.Println(apiErr.ErrorCode(), apiErr.ErrorMessage()) } else { - // Print the error, cast err to awserr.Error to get the Code and - // Message from an error. fmt.Println(err.Error()) } return result, nil } + return result, nil } -func (creds *CREDS) CreateLoadBalancer(userName string) (*elbv2.CreateLoadBalancerOutput, *string, *elbv2.CreateListenerOutput, error) { - svc := elbv2.New(session.Must(session.NewSession(&aws.Config{ - Credentials: creds.creds, - Region: aws.String("us-east-1"), - }))) +func (creds *CREDS) CreateLoadBalancer(userName string) (*elasticloadbalancingv2.CreateLoadBalancerOutput, *string, *elasticloadbalancingv2.CreateListenerOutput, error) { + + //Client with Config replaces session in aws go sdk v1 + cfg, err := config.LoadDefaultConfig(ctx, + config.WithRegion("us-east-1"), + config.WithCredentialsProvider(aws.NewCredentialsCache(creds.creds)), + ) + svc := elasticloadbalancingv2.NewFromConfig(cfg) networkInfo, err := creds.describeWorkspaceNetwork(userName) if err != nil { return nil, nil, nil, err } + albName := truncateString(strings.ReplaceAll(userToResourceName(userName, "service")+os.Getenv("GEN3_ENDPOINT"), ".", "-")+"alb", 32) - input := &elbv2.CreateLoadBalancerInput{ + + input := &elasticloadbalancingv2.CreateLoadBalancerInput{ Name: aws.String(albName), - Scheme: aws.String("internal"), - SecurityGroups: []*string{ + Scheme: types.LoadBalancerSchemeEnumInternal, // Corrected package destination + SecurityGroups: []string{ networkInfo.securityGroups.SecurityGroups[0].GroupId, }, - Subnets: []*string{ + Subnets: []string{ networkInfo.subnets.Subnets[0].SubnetId, networkInfo.subnets.Subnets[1].SubnetId, }, } - loadBalancer, err := svc.CreateLoadBalancer(input) + ctx = context.TODO() + + loadBalancer, err := svc.CreateLoadBalancer(ctx, input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeDuplicateLoadBalancerNameException: - fmt.Println(elbv2.ErrCodeDuplicateLoadBalancerNameException, aerr.Error()) - case elbv2.ErrCodeTooManyLoadBalancersException: - fmt.Println(elbv2.ErrCodeTooManyLoadBalancersException, aerr.Error()) - case elbv2.ErrCodeInvalidConfigurationRequestException: - fmt.Println(elbv2.ErrCodeInvalidConfigurationRequestException, aerr.Error()) - case elbv2.ErrCodeSubnetNotFoundException: - fmt.Println(elbv2.ErrCodeSubnetNotFoundException, aerr.Error()) - case elbv2.ErrCodeInvalidSubnetException: - fmt.Println(elbv2.ErrCodeInvalidSubnetException, aerr.Error()) - case elbv2.ErrCodeInvalidSecurityGroupException: - fmt.Println(elbv2.ErrCodeInvalidSecurityGroupException, aerr.Error()) - case elbv2.ErrCodeInvalidSchemeException: - fmt.Println(elbv2.ErrCodeInvalidSchemeException, aerr.Error()) - case elbv2.ErrCodeTooManyTagsException: - fmt.Println(elbv2.ErrCodeTooManyTagsException, aerr.Error()) - case elbv2.ErrCodeDuplicateTagKeysException: - fmt.Println(elbv2.ErrCodeDuplicateTagKeysException, aerr.Error()) - case elbv2.ErrCodeResourceInUseException: - fmt.Println(elbv2.ErrCodeResourceInUseException, aerr.Error()) - case elbv2.ErrCodeAllocationIdNotFoundException: - fmt.Println(elbv2.ErrCodeAllocationIdNotFoundException, aerr.Error()) - case elbv2.ErrCodeAvailabilityZoneNotSupportedException: - fmt.Println(elbv2.ErrCodeAvailabilityZoneNotSupportedException, aerr.Error()) - case elbv2.ErrCodeOperationNotPermittedException: - fmt.Println(elbv2.ErrCodeOperationNotPermittedException, aerr.Error()) - default: - fmt.Println(aerr.Error()) - } + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + fmt.Println(apiErr.ErrorCode(), apiErr.ErrorMessage()) } else { - // Print the error, cast err to awserr.Error to get the Code and - // Message from an error. fmt.Println(err.Error()) - return nil, nil, nil, err } return nil, nil, nil, err } - targetGroup, err := creds.createTargetGroup(userName, *networkInfo.vpc.Vpcs[0].VpcId, svc) + targetGroup, err := creds.createTargetGroup(ctx, userName, *networkInfo.vpc.Vpcs[0].VpcId, svc) if err != nil { return nil, nil, nil, err } - _, err = creds.setTargetGroupAttributes(svc, *targetGroup.TargetGroups[0].TargetGroupArn) + _, err = creds.setTargetGroupAttributes(ctx, svc, *targetGroup.TargetGroups[0].TargetGroupArn) if err != nil { return nil, nil, nil, err } - listener, err := creds.createListener(svc, *loadBalancer.LoadBalancers[0].LoadBalancerArn, *targetGroup.TargetGroups[0].TargetGroupArn) + listener, err := creds.createListener(ctx, svc, *loadBalancer.LoadBalancers[0].LoadBalancerArn, *targetGroup.TargetGroups[0].TargetGroupArn) if err != nil { return nil, nil, nil, err } @@ -218,82 +161,88 @@ func (creds *CREDS) CreateLoadBalancer(userName string) (*elbv2.CreateLoadBalanc } func (creds *CREDS) terminateLoadBalancerTargetGroup(userName string) error { - svc := elbv2.New(session.Must(session.NewSession(&aws.Config{ - Credentials: creds.creds, - Region: aws.String("us-east-1"), - }))) + //Client with Config replaces session in aws go sdk v1 + cfg, err := config.LoadDefaultConfig(ctx, + config.WithRegion("us-east-1"), + config.WithCredentialsProvider(aws.NewCredentialsCache(creds.creds)), + ) + svc := elasticloadbalancingv2.NewFromConfig(cfg) tgName := truncateString(strings.ReplaceAll(os.Getenv("GEN3_ENDPOINT"), ".", "-")+userToResourceName(userName, "service")+"tg", 32) Config.Logger.Printf("Deleting target group: %s", tgName) - tgArn, err := svc.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ - Names: []*string{aws.String(tgName)}, + tgArn, err := svc.DescribeTargetGroups(ctx, &elasticloadbalancingv2.DescribeTargetGroupsInput{ + Names: []string{tgName}, }) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeTargetGroupNotFoundException: - // Target group not found, nothing to do - return nil - } + var targetGroupNotFoundException *types.TargetGroupNotFoundException + if errors.As(err, &targetGroupNotFoundException) { + / Target group not found, nothing to do + return nil } else { Config.Logger.Printf("Error describing target group: %s", err.Error()) - return err } + return err } - input := &elbv2.DeleteTargetGroupInput{ + + input := &elasticloadbalancingv2.DeleteTargetGroupInput{ TargetGroupArn: tgArn.TargetGroups[0].TargetGroupArn, } - _, err = svc.DeleteTargetGroup(input) + ctx = context.TODO() + + _, err = svc.DeleteTargetGroup(ctx, input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeResourceInUseException: - // Target group in use, nothing to do - return nil - } + var resourceInUseException *types.ResourceInUseException + if errors.As(err, &resourceInUseException) { + // Target group in use, nothing to do + return nil } else { Config.Logger.Printf("Error deleting target group: %s", err.Error()) } + return err } + return nil } -func (creds *CREDS) terminateLoadBalancer(userName string) error { - svc := elbv2.New(session.Must(session.NewSession(&aws.Config{ - Credentials: creds.creds, - Region: aws.String("us-east-1"), - }))) +func (creds *CREDS) terminateLoadBalancer(ctx context.Context, userName string) error { + //Client with Config replaces session in aws go sdk v1 + cfg, err := config.LoadDefaultConfig(ctx, + config.WithRegion("us-east-1"), + config.WithCredentialsProvider(aws.NewCredentialsCache(creds.creds)), + ) + svc := elasticloadbalancingv2.NewFromConfig(cfg) + albName := truncateString(strings.ReplaceAll(userToResourceName(userName, "service")+os.Getenv("GEN3_ENDPOINT"), ".", "-")+"alb", 32) - getInput := &elbv2.DescribeLoadBalancersInput{ - Names: []*string{aws.String(albName)}, + getInput := &elasticloadbalancingv2.DescribeLoadBalancersInput{ + Names: []string{albName}, } - result, err := svc.DescribeLoadBalancers(getInput) + + ctx = context.TODO() + + result, err := svc.DescribeLoadBalancers(ctx, getInput) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeLoadBalancerNotFoundException: - // Load balancer doesn't exist, we are happy! :) - return nil - } - } else { + var loadBalancerNotFoundException *types.LoadBalancerNotFoundException + if errors.As(err, &loadBalancerNotFoundException) { + // Load balancer doesn't exist, we are happy! :) + return nil + } + else { return err } } + if len(result.LoadBalancers) == 1 { - delInput := &elbv2.DeleteLoadBalancerInput{ + delInput := &elasticloadbalancingv2.DeleteLoadBalancerInput{ LoadBalancerArn: result.LoadBalancers[0].LoadBalancerArn, } - _, err := svc.DeleteLoadBalancer(delInput) + _, err := svc.DeleteLoadBalancer(ctx, delInput) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case elbv2.ErrCodeLoadBalancerNotFoundException: - fmt.Println(elbv2.ErrCodeLoadBalancerNotFoundException, aerr.Error()) - } - } else { - return err + var loadBalancerNotFoundException *types.LoadBalancerNotFoundException + if errors.As(err, &loadBalancerNotFoundException) { + fmt.Printf("%T: %s\n", loadBalancerNotFoundException, loadBalancerNotFoundException.Error()) } + return err } }