From 61a5d5ca3b7f6b6a48e6b0584ca4336ca27f5bdb Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Thu, 21 Apr 2022 19:27:14 +0000 Subject: [PATCH] ELBv2 - enable Tags for create_target_group (#5045) --- moto/elbv2/models.py | 6 ++++++ moto/elbv2/responses.py | 17 ++++++++++------- setup.cfg | 2 +- .../terraformtests/terraform-tests.failures.txt | 5 +---- .../terraformtests/terraform-tests.success.txt | 5 +++++ tests/test_elbv2/test_elbv2_target_groups.py | 8 +++++--- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/moto/elbv2/models.py b/moto/elbv2/models.py index 960bbad33..21c5fb480 100644 --- a/moto/elbv2/models.py +++ b/moto/elbv2/models.py @@ -115,6 +115,9 @@ class FakeTargetGroup(CloudFormationModel): self.attributes = { "deregistration_delay.timeout_seconds": 300, "stickiness.enabled": "false", + "load_balancing.algorithm.type": "round_robin", + "slow_start.duration_seconds": 0, + "waf.fail_open.enabled": "false", } self.targets = OrderedDict() @@ -1009,8 +1012,11 @@ Member must satisfy regular expression pattern: {}".format( arn = make_arn_for_target_group( account_id=ACCOUNT_ID, name=name, region_name=self.region_name ) + tags = kwargs.pop("tags", None) target_group = FakeTargetGroup(name, arn, **kwargs) self.target_groups[target_group.arn] = target_group + if tags: + self.add_tags(resource_arns=[target_group.arn], tags=tags) return target_group def modify_target_group_attributes(self, target_group_arn, attributes): diff --git a/moto/elbv2/responses.py b/moto/elbv2/responses.py index e275ef217..88312087a 100644 --- a/moto/elbv2/responses.py +++ b/moto/elbv2/responses.py @@ -177,11 +177,12 @@ class ELBV2Response(BaseResponse): @amzn_request_id def create_target_group(self): - name = self._get_param("Name") - vpc_id = self._get_param("VpcId") - protocol = self._get_param("Protocol") - protocol_version = self._get_param("ProtocolVersion", "HTTP1") - port = self._get_param("Port") + params = self._get_params() + name = params.get("Name") + vpc_id = params.get("VpcId") + protocol = params.get("Protocol") + protocol_version = params.get("ProtocolVersion", "HTTP1") + port = params.get("Port") healthcheck_protocol = self._get_param("HealthCheckProtocol") healthcheck_port = self._get_param("HealthCheckPort") healthcheck_path = self._get_param("HealthCheckPath") @@ -190,8 +191,9 @@ class ELBV2Response(BaseResponse): healthcheck_enabled = self._get_param("HealthCheckEnabled") healthy_threshold_count = self._get_param("HealthyThresholdCount") unhealthy_threshold_count = self._get_param("UnhealthyThresholdCount") - matcher = self._get_params().get("Matcher") - target_type = self._get_param("TargetType") + matcher = params.get("Matcher") + target_type = params.get("TargetType") + tags = params.get("Tags") target_group = self.elbv2_backend.create_target_group( name, @@ -209,6 +211,7 @@ class ELBV2Response(BaseResponse): unhealthy_threshold_count=unhealthy_threshold_count, matcher=matcher, target_type=target_type, + tags=tags, ) template = self.response_template(CREATE_TARGET_GROUP_TEMPLATE) diff --git a/setup.cfg b/setup.cfg index 425fbab78..a17a6e279 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,7 +10,7 @@ relative_files = True [flake8] ignore = W503,W605,E128,E501,E203,E266,E501,E231 -exclude = moto/packages,dist +exclude = moto/packages,dist,tests/terraformtests [pylint.MASTER] ignore-paths=moto/packages diff --git a/tests/terraformtests/terraform-tests.failures.txt b/tests/terraformtests/terraform-tests.failures.txt index 2bc13ef9c..f1c10a66f 100644 --- a/tests/terraformtests/terraform-tests.failures.txt +++ b/tests/terraformtests/terraform-tests.failures.txt @@ -6,7 +6,6 @@ TestAccAPIGatewayV2Authorizer TestAccAPIGatewayV2Route TestAccAppsyncApiKey TestAccAppsyncGraphqlApi -TestAccAutoscalingAttachment TestAccAutoscalingPolicy TestAccBatchJobQueue TestAccCloudTrail @@ -48,8 +47,6 @@ TestAccIAMRole TestAccIotThing TestAccIPRanges TestAccKinesisStream -TestAccLbListenerCertificate -TestAccLBTargetGroupAttachment TestAccELBPolicy TestAccLambdaAlias TestAccLambdaLayerVersion @@ -68,4 +65,4 @@ TestAccTimestreamWriteTable TestAccDataSourceLambdaLayerVersion TestAccDataSourceLambdaInvocation TestAccDataSourceNetworkInterface_ -TestAccSQSQueueDataSource \ No newline at end of file +TestAccSQSQueueDataSource diff --git a/tests/terraformtests/terraform-tests.success.txt b/tests/terraformtests/terraform-tests.success.txt index 9a5279034..97baa542f 100644 --- a/tests/terraformtests/terraform-tests.success.txt +++ b/tests/terraformtests/terraform-tests.success.txt @@ -9,6 +9,7 @@ apigatewayv2: - TestAccAPIGatewayV2VPCLink autoscaling: - TestAccAutoScalingGroupDataSource + - TestAccAutoScalingAttachment batch: - TestAccBatchJobDefinition cloudtrail: @@ -50,6 +51,10 @@ elb: - TestAccELBListenerPolicy - TestAccELBServiceAccountDataSource - TestAccELBSSLNegotiationPolicy +elbv2: + - TestAccELBV2ListenerCertificate + - TestAccELBV2TargetGroupAttachment + - TestAccELBV2TargetGroupDataSource events: - TestAccEventsAPIDestination - TestAccEventsArchive diff --git a/tests/test_elbv2/test_elbv2_target_groups.py b/tests/test_elbv2/test_elbv2_target_groups.py index b6a0c8f68..6765679ba 100644 --- a/tests/test_elbv2/test_elbv2_target_groups.py +++ b/tests/test_elbv2/test_elbv2_target_groups.py @@ -54,6 +54,7 @@ def test_create_target_group_with_tags(): HealthyThresholdCount=5, UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, + Tags=[{"Key": "key1", "Value": "val1"}], ) target_group = response.get("TargetGroups")[0] target_group_arn = target_group["TargetGroupArn"] @@ -61,7 +62,7 @@ def test_create_target_group_with_tags(): # Add tags to the target group conn.add_tags( ResourceArns=[target_group_arn], - Tags=[{"Key": "key1", "Value": "val1"}, {"Key": "key2", "Value": "val2"}], + Tags=[{"Key": "key2", "Value": "val2"}], ) conn.describe_tags(ResourceArns=[target_group_arn])["TagDescriptions"][0][ "Tags" @@ -384,10 +385,11 @@ def test_target_group_attributes(): # The attributes should start with the two defaults response = conn.describe_target_group_attributes(TargetGroupArn=target_group_arn) - response["Attributes"].should.have.length_of(2) + response["Attributes"].should.have.length_of(5) attributes = {attr["Key"]: attr["Value"] for attr in response["Attributes"]} attributes["deregistration_delay.timeout_seconds"].should.equal("300") attributes["stickiness.enabled"].should.equal("false") + attributes["waf.fail_open.enabled"].should.equal("false") # Add cookie stickiness response = conn.modify_target_group_attributes( @@ -406,7 +408,7 @@ def test_target_group_attributes(): # These new values should be in the full attribute list response = conn.describe_target_group_attributes(TargetGroupArn=target_group_arn) - response["Attributes"].should.have.length_of(3) + response["Attributes"].should.have.length_of(6) attributes = {attr["Key"]: attr["Value"] for attr in response["Attributes"]} attributes["stickiness.type"].should.equal("lb_cookie") attributes["stickiness.enabled"].should.equal("true")