github.com/almamedia/fargate@v0.2.4-0.20220704071213-7b5b3d27c5eb/elbv2/target_group.go (about)

     1  package elbv2
     2  
     3  import (
     4  	"github.com/almamedia/fargate/console"
     5  	"github.com/aws/aws-sdk-go/aws"
     6  	awselbv2 "github.com/aws/aws-sdk-go/service/elbv2"
     7  )
     8  
     9  type TargetGroup struct {
    10  	Name            string
    11  	Arn             string
    12  	LoadBalancerARN string
    13  }
    14  
    15  type CreateTargetGroupParameters struct {
    16  	Name       string
    17  	Port       int64
    18  	Protocol   string
    19  	VPCID      string
    20  	HealthPath string
    21  }
    22  
    23  func (elbv2 SDKClient) CreateTargetGroup(i CreateTargetGroupParameters) (string, error) {
    24  	resp, err := elbv2.client.CreateTargetGroup(
    25  		&awselbv2.CreateTargetGroupInput{
    26  			Name:            aws.String(i.Name),
    27  			Port:            aws.Int64(i.Port),
    28  			Protocol:        aws.String(i.Protocol),
    29  			TargetType:      aws.String(awselbv2.TargetTypeEnumIp),
    30  			VpcId:           aws.String(i.VPCID),
    31  			HealthCheckPath: aws.String(i.HealthPath),
    32  		},
    33  	)
    34  
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  
    39  	return aws.StringValue(resp.TargetGroups[0].TargetGroupArn), nil
    40  }
    41  
    42  func (elbv2 SDKClient) DeleteTargetGroup(targetGroupName string) {
    43  	console.Debug("Deleting ELB target group")
    44  
    45  	targetGroup := elbv2.describeTargetGroupByName(targetGroupName)
    46  
    47  	elbv2.client.DeleteTargetGroup(
    48  		&awselbv2.DeleteTargetGroupInput{
    49  			TargetGroupArn: targetGroup.TargetGroupArn,
    50  		},
    51  	)
    52  }
    53  
    54  func (elbv2 SDKClient) DeleteTargetGroupByArn(targetGroupARN string) {
    55  	_, err := elbv2.client.DeleteTargetGroup(
    56  		&awselbv2.DeleteTargetGroupInput{
    57  			TargetGroupArn: aws.String(targetGroupARN),
    58  		},
    59  	)
    60  
    61  	if err != nil {
    62  		console.ErrorExit(err, "Could not delete ELB target group")
    63  	}
    64  }
    65  
    66  func (elbv2 SDKClient) GetTargetGroupArn(targetGroupName string) string {
    67  	resp, _ := elbv2.client.DescribeTargetGroups(
    68  		&awselbv2.DescribeTargetGroupsInput{
    69  			Names: aws.StringSlice([]string{targetGroupName}),
    70  		},
    71  	)
    72  
    73  	if len(resp.TargetGroups) == 1 {
    74  		return aws.StringValue(resp.TargetGroups[0].TargetGroupArn)
    75  	}
    76  
    77  	return ""
    78  }
    79  
    80  func (elbv2 SDKClient) GetTargetGroupLoadBalancerArn(targetGroupARN string) string {
    81  	targetGroup := elbv2.describeTargetGroupByArn(targetGroupARN)
    82  
    83  	if len(targetGroup.LoadBalancerArns) > 0 {
    84  		return aws.StringValue(targetGroup.LoadBalancerArns[0])
    85  	} else {
    86  		return ""
    87  	}
    88  }
    89  
    90  func (elbv2 SDKClient) DescribeTargetGroups(targetGroupARNs []string) []TargetGroup {
    91  	var targetGroups []TargetGroup
    92  
    93  	resp, err := elbv2.client.DescribeTargetGroups(
    94  		&awselbv2.DescribeTargetGroupsInput{
    95  			TargetGroupArns: aws.StringSlice(targetGroupARNs),
    96  		},
    97  	)
    98  
    99  	if err != nil {
   100  		console.ErrorExit(err, "Could not describe ELB target groups")
   101  	}
   102  
   103  	for _, targetGroup := range resp.TargetGroups {
   104  		tg := TargetGroup{
   105  			Name: aws.StringValue(targetGroup.TargetGroupName),
   106  			Arn:  aws.StringValue(targetGroup.TargetGroupArn),
   107  		}
   108  
   109  		if len(targetGroup.LoadBalancerArns) > 0 {
   110  			tg.LoadBalancerARN = aws.StringValue(targetGroup.LoadBalancerArns[0])
   111  		}
   112  
   113  		targetGroups = append(targetGroups, tg)
   114  	}
   115  
   116  	return targetGroups
   117  }
   118  
   119  func (elbv2 SDKClient) describeTargetGroupByName(targetGroupName string) *awselbv2.TargetGroup {
   120  	resp, err := elbv2.client.DescribeTargetGroups(
   121  		&awselbv2.DescribeTargetGroupsInput{
   122  			Names: aws.StringSlice([]string{targetGroupName}),
   123  		},
   124  	)
   125  
   126  	if err != nil {
   127  		console.ErrorExit(err, "Could not describe ELB target groups")
   128  	}
   129  
   130  	if len(resp.TargetGroups) != 1 {
   131  		console.IssueExit("Could not describe ELB target groups")
   132  	}
   133  
   134  	return resp.TargetGroups[0]
   135  }
   136  
   137  func (elbv2 SDKClient) describeTargetGroupByArn(targetGroupARN string) *awselbv2.TargetGroup {
   138  	resp, err := elbv2.client.DescribeTargetGroups(
   139  		&awselbv2.DescribeTargetGroupsInput{
   140  			TargetGroupArns: aws.StringSlice([]string{targetGroupARN}),
   141  		},
   142  	)
   143  
   144  	if err != nil {
   145  		console.ErrorExit(err, "Could not describe ELB target groups")
   146  	}
   147  
   148  	if len(resp.TargetGroups) != 1 {
   149  		console.IssueExit("Could not describe ELB target groups")
   150  	}
   151  
   152  	return resp.TargetGroups[0]
   153  }