From 93057182aeae4c679cf7d316950eae5ec1798806 Mon Sep 17 00:00:00 2001 From: Marcel Neidinger Date: Thu, 20 Apr 2023 13:32:19 +0200 Subject: [PATCH 01/14] Added listener data source and tests --- .../vpclattice/listener_data_source.go | 259 ++++++++++++++++++ .../vpclattice/listener_data_source_test.go | 145 ++++++++++ .../service/vpclattice/service_package_gen.go | 4 + 3 files changed, 408 insertions(+) create mode 100644 internal/service/vpclattice/listener_data_source.go create mode 100644 internal/service/vpclattice/listener_data_source_test.go diff --git a/internal/service/vpclattice/listener_data_source.go b/internal/service/vpclattice/listener_data_source.go new file mode 100644 index 00000000000..ee41180f6d2 --- /dev/null +++ b/internal/service/vpclattice/listener_data_source.go @@ -0,0 +1,259 @@ +package vpclattice + +import ( + "context" + "errors" + "fmt" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/vpclattice" + "github.com/aws/aws-sdk-go-v2/service/vpclattice/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/create" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" + "strings" +) + +// Function annotations are used for datasource registration to the Provider. DO NOT EDIT. +// @SDKDataSource("aws_vpclattice_listener", name="Listener") +// @Tags(identifierAttribute="arn") +func DataSourceListener() *schema.Resource { + return &schema.Resource{ + // Data sources only have a read function. + ReadWithoutTimeout: dataSourceListenerRead, + // Id returned by GetListener does not contain required service name, use a custom import function + Importer: &schema.ResourceImporter{ + StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + idParts := strings.Split(d.Id(), "/") + if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + return nil, fmt.Errorf("unexpected format of ID (%q), expected SERVICE-ID/LISTENER-ID", d.Id()) + } + d.Set("service_identifier", idParts[0]) + d.Set("listener_id", idParts[1]) + + return []*schema.ResourceData{d}, nil + }, + }, + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "created_at": { + Type: schema.TypeString, + Computed: true, + }, + "default_action": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "fixed_response": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "status_code": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + "forward": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "target_groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "target_group_identifier": { + Type: schema.TypeString, + Computed: true, + }, + "weight": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + //"id": { + // Type: schema.TypeString, + // Computed: true, + //}, + "last_updated_at": { + Type: schema.TypeString, + Computed: true, + }, + "listener_id": { + Type: schema.TypeString, + Computed: true, + }, + "listener_identifier": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "port": { + Type: schema.TypeInt, + Computed: true, + }, + "protocol": { + Type: schema.TypeString, + Computed: true, + }, + "service_arn": { + Type: schema.TypeString, + Computed: true, + }, + "service_id": { + Type: schema.TypeString, + Computed: true, + }, + "service_identifier": { + Type: schema.TypeString, + Required: true, + }, + names.AttrTags: tftags.TagsSchema(), + names.AttrTagsAll: tftags.TagsSchemaComputed(), + }, + } +} + +const ( + DSNameListener = "Listener Data Source" +) + +func dataSourceListenerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).VPCLatticeClient() + + serviceId := d.Get("service_identifier").(string) + listenerId := d.Get("listener_identifier").(string) + + out, err := findListenerByListenerIdAndServiceId(ctx, conn, listenerId, serviceId) + if err != nil { + return create.DiagError(names.VPCLattice, create.ErrActionReading, DSNameListener, d.Id(), err) + } + + // Set simple arguments + d.SetId(aws.ToString(out.Id)) + d.Set("arn", out.Arn) + d.Set("created_at", aws.ToTime(out.CreatedAt).String()) + d.Set("last_updated_at", aws.ToTime(out.LastUpdatedAt).String()) + d.Set("listener_id", out.Id) + d.Set("name", out.Name) + d.Set("port", out.Port) + d.Set("protocol", out.Protocol) + d.Set("service_arn", out.ServiceArn) + d.Set("service_id", out.ServiceId) + + // Flatten complex default_action attribute - uses flatteners from listener.go + if err := d.Set("default_action", flattenListenerRuleActionsDataSource(out.DefaultAction)); err != nil { + return create.DiagError(names.VPCLattice, create.ErrActionSetting, DSNameListener, d.Id(), err) + } + + return nil +} + +func findListenerByListenerIdAndServiceId(ctx context.Context, conn *vpclattice.Client, listener_id string, service_id string) (*vpclattice.GetListenerOutput, error) { + in := &vpclattice.GetListenerInput{ + ListenerIdentifier: aws.String(listener_id), + ServiceIdentifier: aws.String(service_id), + } + + out, err := conn.GetListener(ctx, in) + if err != nil { + var nfe *types.ResourceNotFoundException + if errors.As(err, &nfe) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: in, + } + } + + return nil, err + } + + if out == nil || out.Id == nil { + return nil, tfresource.NewEmptyResultError(in) + } + + return out, nil +} + +func flattenListenerRuleActionsDataSource(config types.RuleAction) []interface{} { + m := map[string]interface{}{} + + if config == nil { + return []interface{}{} + } + + switch v := config.(type) { + case *types.RuleActionMemberFixedResponse: + m["fixed_response"] = flattenRuleActionMemberFixedResponseDataSource(&v.Value) + case *types.RuleActionMemberForward: + m["forward"] = flattenComplexDefaultActionForwardDataSource(&v.Value) + } + + return []interface{}{m} +} + +// Flatten function for fixed_response action +func flattenRuleActionMemberFixedResponseDataSource(response *types.FixedResponseAction) []interface{} { + tfMap := map[string]interface{}{} + + if v := response.StatusCode; v != nil { + tfMap["status_code"] = aws.ToInt32(v) + } + + return []interface{}{tfMap} +} + +// Flatten function for forward action +func flattenComplexDefaultActionForwardDataSource(forwardAction *types.ForwardAction) []interface{} { + if forwardAction == nil { + return []interface{}{} + } + + m := map[string]interface{}{ + "target_groups": flattenDefaultActionForwardTargetGroupsDataSource(forwardAction.TargetGroups), + } + + return []interface{}{m} +} + +// Flatten function for target_groups +func flattenDefaultActionForwardTargetGroupsDataSource(groups []types.WeightedTargetGroup) []interface{} { + if len(groups) == 0 { + return []interface{}{} + } + + var targetGroups []interface{} + + for _, targetGroup := range groups { + m := map[string]interface{}{ + "target_group_identifier": aws.ToString(targetGroup.TargetGroupIdentifier), + "weight": aws.ToInt32(targetGroup.Weight), + } + targetGroups = append(targetGroups, m) + } + + return targetGroups +} diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go new file mode 100644 index 00000000000..6a6105d53c6 --- /dev/null +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -0,0 +1,145 @@ +package vpclattice_test + +import ( + "fmt" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go-v2/service/vpclattice" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func TestAccVPCLatticeListenerDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var listener vpclattice.GetListenerOutput + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + dataSourceName := "data.aws_vpclattice_listener.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.VPCLatticeEndpointID) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckListenerDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccListenerDataSourceConfig_fixedResponseHTTP(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckListenerExists(ctx, dataSourceName, &listener), + resource.TestCheckResourceAttr(dataSourceName, "name", rName), + resource.TestCheckResourceAttr(dataSourceName, "protocol", "HTTP"), + resource.TestCheckResourceAttr(dataSourceName, "default_action.0.fixed_response.0.status_code", "404"), + acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/svc-.*/listener/listener-.+`)), + ), + }, + }, + }) +} + +func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var listener vpclattice.GetListenerOutput + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + dataSourceName := "data.aws_vpclattice_listener.test_tags" + tag_name := sdkacctest.RandomWithPrefix("tag_name") + tag_value := sdkacctest.RandomWithPrefix("tag_value") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.VPCLatticeEndpointID) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckListenerDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccListenerDataSourceConfig_one_tag(rName, tag_name, tag_value), + Check: resource.ComposeTestCheckFunc( + testAccCheckListenerExists(ctx, dataSourceName, &listener), + acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/svc-.*/listener/listener-.+`)), + ), + }, + }, + }) + +} + +func testAccListenerDataSourceConfig_one_tag(rName, tag_key, tag_value string) string { + return acctest.ConfigCompose(testAccListenerDataSourceConfig_basic(rName), fmt.Sprintf(` +resource "aws_vpclattice_listener" "test_tags" { + name = %[1]q + protocol = "HTTP" + service_identifier = aws_vpclattice_service.test.id + default_action { + forward { + target_groups { + target_group_identifier = aws_vpclattice_target_group.test.id + weight = 100 + } + } + } + tags = { + %[2]q = %[3]q + } + } +data "aws_vpclattice_listener" "test_tags" { + service_identifier = aws_vpclattice_service.test.id + listener_identifier = aws_vpclattice_listener.test_tags.arn +} +`, rName, tag_key, tag_value)) +} +func testAccListenerDataSourceConfig_basic(rName string) string { + return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 0), fmt.Sprintf(` +resource "aws_vpclattice_service" "test" { + name = %[1]q + } + +resource "aws_vpclattice_target_group" "test" { +name = %[1]q +type = "INSTANCE" + + config { + port = 80 + protocol = "HTTP" + vpc_identifier = aws_vpc.test.id + } +} + +`, rName)) +} + +func testAccListenerDataSourceConfig_fixedResponseHTTP(rName string) string { + return acctest.ConfigCompose(testAccListenerDataSourceConfig_basic(rName), fmt.Sprintf(` +resource "aws_vpclattice_listener" "test" { + name = %[1]q + protocol = "HTTP" + service_identifier = aws_vpclattice_service.test.id + default_action { + fixed_response { + status_code = 404 + } + } +} + +data "aws_vpclattice_listener" "test" { + service_identifier = aws_vpclattice_service.test.arn + listener_identifier = aws_vpclattice_listener.test.arn +} +`, rName)) +} diff --git a/internal/service/vpclattice/service_package_gen.go b/internal/service/vpclattice/service_package_gen.go index 4966221eff0..68e3aa9febb 100644 --- a/internal/service/vpclattice/service_package_gen.go +++ b/internal/service/vpclattice/service_package_gen.go @@ -21,6 +21,10 @@ func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.Servic func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePackageSDKDataSource { return []*types.ServicePackageSDKDataSource{ + { + Factory: DataSourceListener, + TypeName: "aws_vpclattice_listener", + }, { Factory: DataSourceService, TypeName: "aws_vpclattice_service", From 28a5ec8fedbc0af9309f57e4a2434237801f87e1 Mon Sep 17 00:00:00 2001 From: Marcel Neidinger Date: Thu, 20 Apr 2023 13:57:39 +0200 Subject: [PATCH 02/14] Added tags schema --- internal/service/vpclattice/listener_data_source.go | 7 +------ internal/service/vpclattice/listener_data_source_test.go | 1 + 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/service/vpclattice/listener_data_source.go b/internal/service/vpclattice/listener_data_source.go index ee41180f6d2..3721cc4aaca 100644 --- a/internal/service/vpclattice/listener_data_source.go +++ b/internal/service/vpclattice/listener_data_source.go @@ -91,10 +91,6 @@ func DataSourceListener() *schema.Resource { }, }, }, - //"id": { - // Type: schema.TypeString, - // Computed: true, - //}, "last_updated_at": { Type: schema.TypeString, Computed: true, @@ -131,8 +127,7 @@ func DataSourceListener() *schema.Resource { Type: schema.TypeString, Required: true, }, - names.AttrTags: tftags.TagsSchema(), - names.AttrTagsAll: tftags.TagsSchemaComputed(), + "tags": tftags.TagsSchemaComputed(), }, } } diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go index 6a6105d53c6..ab6ce9f442c 100644 --- a/internal/service/vpclattice/listener_data_source_test.go +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -72,6 +72,7 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { Config: testAccListenerDataSourceConfig_one_tag(rName, tag_name, tag_value), Check: resource.ComposeTestCheckFunc( testAccCheckListenerExists(ctx, dataSourceName, &listener), + resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("tags.%s", tag_name), tag_value), acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/svc-.*/listener/listener-.+`)), ), }, From edf4272c2a6b29d72af5fd47b8dab6b315f54b4d Mon Sep 17 00:00:00 2001 From: Marcel Neidinger Date: Thu, 20 Apr 2023 15:06:51 +0200 Subject: [PATCH 03/14] Added test for forwardMultiTargetGroupHTTP --- .../vpclattice/listener_data_source_test.go | 83 ++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go index ab6ce9f442c..e93a2efd188 100644 --- a/internal/service/vpclattice/listener_data_source_test.go +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -55,8 +55,8 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { var listener vpclattice.GetListenerOutput rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) dataSourceName := "data.aws_vpclattice_listener.test_tags" - tag_name := sdkacctest.RandomWithPrefix("tag_name") - tag_value := sdkacctest.RandomWithPrefix("tag_value") + tag_name := "tag0" + tag_value := "value0" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { @@ -81,6 +81,46 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { } +func TestAccVPCLatticeListenerDataSource_forwardMultiTargetGroupHTTP(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var listener vpclattice.GetListenerOutput + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + targetGroupName1 := fmt.Sprintf("testtargetgroup-%s", sdkacctest.RandString(10)) + + targetGroupResourceName := "aws_vpclattice_target_group.test" + targetGroup1ResourceName := "aws_vpclattice_target_group.test1" + dataSourceName := "data.aws_vpclattice_listener.test_multi_target" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.VPCLatticeEndpointID) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckListenerDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccListenerDataSourceConfig_forwardMultiTargetGroupHTTP(rName, targetGroupName1), + Check: resource.ComposeTestCheckFunc( + testAccCheckListenerExists(ctx, dataSourceName, &listener), + resource.TestCheckResourceAttrPair(dataSourceName, "default_action.0.forward.0.target_groups.0.target_group_identifier", targetGroupResourceName, "id"), + resource.TestCheckResourceAttr(dataSourceName, "default_action.0.forward.0.target_groups.0.weight", "80"), + resource.TestCheckResourceAttrPair(dataSourceName, "default_action.0.forward.0.target_groups.1.target_group_identifier", targetGroup1ResourceName, "id"), + resource.TestCheckResourceAttr(dataSourceName, "default_action.0.forward.0.target_groups.1.weight", "20"), + acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/svc-.*/listener/listener-.+`)), + ), + }, + }, + }) + +} + func testAccListenerDataSourceConfig_one_tag(rName, tag_key, tag_value string) string { return acctest.ConfigCompose(testAccListenerDataSourceConfig_basic(rName), fmt.Sprintf(` resource "aws_vpclattice_listener" "test_tags" { @@ -105,6 +145,7 @@ data "aws_vpclattice_listener" "test_tags" { } `, rName, tag_key, tag_value)) } + func testAccListenerDataSourceConfig_basic(rName string) string { return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 0), fmt.Sprintf(` resource "aws_vpclattice_service" "test" { @@ -144,3 +185,41 @@ data "aws_vpclattice_listener" "test" { } `, rName)) } + +func testAccListenerDataSourceConfig_forwardMultiTargetGroupHTTP(rName string, targetGroupName1 string) string { + return acctest.ConfigCompose(testAccListenerConfig_basic(rName), fmt.Sprintf(` +resource "aws_vpclattice_target_group" "test1" { + name = %[2]q + type = "INSTANCE" + + config { + port = 8080 + protocol = "HTTP" + vpc_identifier = aws_vpc.test.id + } +} + +resource "aws_vpclattice_listener" "test" { + name = %[1]q + protocol = "HTTP" + service_identifier = aws_vpclattice_service.test.id + default_action { + forward { + target_groups { + target_group_identifier = aws_vpclattice_target_group.test.id + weight = 80 + } + target_groups { + target_group_identifier = aws_vpclattice_target_group.test1.id + weight = 20 + } + } + } +} + +data "aws_vpclattice_listener" "test_multi_target" { + service_identifier = aws_vpclattice_service.test.id + listener_identifier = aws_vpclattice_listener.test.arn +} +`, rName, targetGroupName1)) +} From 72f0c15bb95f9bfe2d15d7ff4a2066782da6bd00 Mon Sep 17 00:00:00 2001 From: Marcel Neidinger Date: Thu, 20 Apr 2023 16:32:32 +0200 Subject: [PATCH 04/14] Fixed tags issue --- internal/service/vpclattice/listener_data_source.go | 9 +++++++++ internal/service/vpclattice/listener_data_source_test.go | 2 +- internal/service/vpclattice/service_package_gen.go | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/service/vpclattice/listener_data_source.go b/internal/service/vpclattice/listener_data_source.go index 3721cc4aaca..80d02119626 100644 --- a/internal/service/vpclattice/listener_data_source.go +++ b/internal/service/vpclattice/listener_data_source.go @@ -164,6 +164,15 @@ func dataSourceListenerRead(ctx context.Context, d *schema.ResourceData, meta in return create.DiagError(names.VPCLattice, create.ErrActionSetting, DSNameListener, d.Id(), err) } + // Set tags + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + tags, err := ListTags(ctx, conn, aws.ToString(out.Arn)) + + //lintignore:AWSR002 + if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + return create.DiagError(names.VPCLattice, create.ErrActionSetting, DSNameListener, d.Id(), err) + } + return nil } diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go index e93a2efd188..e3eb3bba4a4 100644 --- a/internal/service/vpclattice/listener_data_source_test.go +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -72,7 +72,7 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { Config: testAccListenerDataSourceConfig_one_tag(rName, tag_name, tag_value), Check: resource.ComposeTestCheckFunc( testAccCheckListenerExists(ctx, dataSourceName, &listener), - resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("tags.%s", tag_name), tag_value), + resource.TestCheckResourceAttr(dataSourceName, "tags.tag0", "value0"), acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/svc-.*/listener/listener-.+`)), ), }, diff --git a/internal/service/vpclattice/service_package_gen.go b/internal/service/vpclattice/service_package_gen.go index 68e3aa9febb..366e92dd29f 100644 --- a/internal/service/vpclattice/service_package_gen.go +++ b/internal/service/vpclattice/service_package_gen.go @@ -24,6 +24,10 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: DataSourceListener, TypeName: "aws_vpclattice_listener", + Name: "Listener", + Tags: &types.ServicePackageResourceTags{ + IdentifierAttribute: "arn", + }, }, { Factory: DataSourceService, From 09e10ac2898ba2d990517cc2192920675ef48caf Mon Sep 17 00:00:00 2001 From: Marcel Neidinger Date: Thu, 20 Apr 2023 16:44:40 +0200 Subject: [PATCH 05/14] Added docs --- .../docs/d/vpclattice_listener.html.markdown | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 website/docs/d/vpclattice_listener.html.markdown diff --git a/website/docs/d/vpclattice_listener.html.markdown b/website/docs/d/vpclattice_listener.html.markdown new file mode 100644 index 00000000000..df99a01213a --- /dev/null +++ b/website/docs/d/vpclattice_listener.html.markdown @@ -0,0 +1,43 @@ +--- +subcategory: "VPC Lattice" +layout: "aws" +page_title: "AWS: aws_vpclattice_listener" +description: |- + Terraform data source for managing an AWS VPC Lattice Listener. +--- + +# Data Source: aws_vpclattice_listener + +Terraform data source for managing an AWS VPC Lattice Listener. + +## Example Usage + +### Basic Usage + +```terraform +data "aws_vpclattice_listener" "example" { +} +``` + +## Argument Reference + +The following arguments are required: + +* `service_identifier` - (Required) ID or Amazon Resource Name (ARN) of the service network +* `listener_identifier` - (Required) ID or Amazon Resource Name (ARN) of the listener + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `arn` - ARN of the listener. +* `created_at` - The date and time that the listener was created. +* `default_action` - The actions for the default listener rule. +* `last_updated_at` - The date and time the listener was last updated. +* `listener_id` - The ID of the listener. +* `name` - The name of the listener. +* `port` - The listener port. +* `protocol` - The listener protocol. Either `HTTPS` or `HTTP`. +* `service_arn` - The ARN of the service. +* `service_id` - The ID of the service. +* `tags` - List of tags associated with the listener. \ No newline at end of file From 041b0d810fc7d7d384c21dce442914d837add86c Mon Sep 17 00:00:00 2001 From: Marcel Neidinger Date: Thu, 20 Apr 2023 16:48:29 +0200 Subject: [PATCH 06/14] Added changelog file --- .changelog/30843.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/30843.txt diff --git a/.changelog/30843.txt b/.changelog/30843.txt new file mode 100644 index 00000000000..9b587984587 --- /dev/null +++ b/.changelog/30843.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_vpclattice_listener +``` \ No newline at end of file From 183bc9dd5e4969da88c9bd03e3b617c5ad84858e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 11:06:30 -0400 Subject: [PATCH 07/14] d/aws_vpclattice_listener: No transparent tagging or import for data sources. --- .../service/vpclattice/listener_data_source.go | 16 ---------------- .../service/vpclattice/service_package_gen.go | 3 --- 2 files changed, 19 deletions(-) diff --git a/internal/service/vpclattice/listener_data_source.go b/internal/service/vpclattice/listener_data_source.go index 80d02119626..9886b4abd52 100644 --- a/internal/service/vpclattice/listener_data_source.go +++ b/internal/service/vpclattice/listener_data_source.go @@ -3,7 +3,6 @@ package vpclattice import ( "context" "errors" - "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/vpclattice" "github.com/aws/aws-sdk-go-v2/service/vpclattice/types" @@ -15,29 +14,14 @@ import ( tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" - "strings" ) // Function annotations are used for datasource registration to the Provider. DO NOT EDIT. // @SDKDataSource("aws_vpclattice_listener", name="Listener") -// @Tags(identifierAttribute="arn") func DataSourceListener() *schema.Resource { return &schema.Resource{ - // Data sources only have a read function. ReadWithoutTimeout: dataSourceListenerRead, - // Id returned by GetListener does not contain required service name, use a custom import function - Importer: &schema.ResourceImporter{ - StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - idParts := strings.Split(d.Id(), "/") - if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { - return nil, fmt.Errorf("unexpected format of ID (%q), expected SERVICE-ID/LISTENER-ID", d.Id()) - } - d.Set("service_identifier", idParts[0]) - d.Set("listener_id", idParts[1]) - return []*schema.ResourceData{d}, nil - }, - }, Schema: map[string]*schema.Schema{ "arn": { Type: schema.TypeString, diff --git a/internal/service/vpclattice/service_package_gen.go b/internal/service/vpclattice/service_package_gen.go index 84db2b26396..c4d9dc57fe8 100644 --- a/internal/service/vpclattice/service_package_gen.go +++ b/internal/service/vpclattice/service_package_gen.go @@ -25,9 +25,6 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Factory: DataSourceListener, TypeName: "aws_vpclattice_listener", Name: "Listener", - Tags: &types.ServicePackageResourceTags{ - IdentifierAttribute: "arn", - }, }, { Factory: DataSourceService, From f9f3e1c4d368f48d9fd33292b7095c97d595bd19 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 11:11:11 -0400 Subject: [PATCH 08/14] Fix terrafmt errors. --- .../vpclattice/listener_data_source_test.go | 74 +++++++++---------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go index e3eb3bba4a4..a977476ab3d 100644 --- a/internal/service/vpclattice/listener_data_source_test.go +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -5,7 +5,6 @@ import ( "regexp" "testing" - "github.com/aws/aws-sdk-go-v2/service/vpclattice" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" @@ -18,7 +17,6 @@ func TestAccVPCLatticeListenerDataSource_basic(t *testing.T) { t.Skip("skipping long-running test in short mode") } - var listener vpclattice.GetListenerOutput rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) dataSourceName := "data.aws_vpclattice_listener.test" @@ -30,12 +28,10 @@ func TestAccVPCLatticeListenerDataSource_basic(t *testing.T) { }, ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: testAccCheckListenerDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccListenerDataSourceConfig_fixedResponseHTTP(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckListenerExists(ctx, dataSourceName, &listener), resource.TestCheckResourceAttr(dataSourceName, "name", rName), resource.TestCheckResourceAttr(dataSourceName, "protocol", "HTTP"), resource.TestCheckResourceAttr(dataSourceName, "default_action.0.fixed_response.0.status_code", "404"), @@ -52,7 +48,6 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { t.Skip("skipping long-running test in short mode") } - var listener vpclattice.GetListenerOutput rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) dataSourceName := "data.aws_vpclattice_listener.test_tags" tag_name := "tag0" @@ -66,12 +61,10 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { }, ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: testAccCheckListenerDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccListenerDataSourceConfig_one_tag(rName, tag_name, tag_value), Check: resource.ComposeTestCheckFunc( - testAccCheckListenerExists(ctx, dataSourceName, &listener), resource.TestCheckResourceAttr(dataSourceName, "tags.tag0", "value0"), acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/svc-.*/listener/listener-.+`)), ), @@ -87,7 +80,6 @@ func TestAccVPCLatticeListenerDataSource_forwardMultiTargetGroupHTTP(t *testing. t.Skip("skipping long-running test in short mode") } - var listener vpclattice.GetListenerOutput rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) targetGroupName1 := fmt.Sprintf("testtargetgroup-%s", sdkacctest.RandString(10)) @@ -103,12 +95,10 @@ func TestAccVPCLatticeListenerDataSource_forwardMultiTargetGroupHTTP(t *testing. }, ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: testAccCheckListenerDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccListenerDataSourceConfig_forwardMultiTargetGroupHTTP(rName, targetGroupName1), Check: resource.ComposeTestCheckFunc( - testAccCheckListenerExists(ctx, dataSourceName, &listener), resource.TestCheckResourceAttrPair(dataSourceName, "default_action.0.forward.0.target_groups.0.target_group_identifier", targetGroupResourceName, "id"), resource.TestCheckResourceAttr(dataSourceName, "default_action.0.forward.0.target_groups.0.weight", "80"), resource.TestCheckResourceAttrPair(dataSourceName, "default_action.0.forward.0.target_groups.1.target_group_identifier", targetGroup1ResourceName, "id"), @@ -124,24 +114,27 @@ func TestAccVPCLatticeListenerDataSource_forwardMultiTargetGroupHTTP(t *testing. func testAccListenerDataSourceConfig_one_tag(rName, tag_key, tag_value string) string { return acctest.ConfigCompose(testAccListenerDataSourceConfig_basic(rName), fmt.Sprintf(` resource "aws_vpclattice_listener" "test_tags" { - name = %[1]q - protocol = "HTTP" - service_identifier = aws_vpclattice_service.test.id - default_action { - forward { - target_groups { - target_group_identifier = aws_vpclattice_target_group.test.id - weight = 100 - } - } - } - tags = { - %[2]q = %[3]q - } - } + name = %[1]q + protocol = "HTTP" + service_identifier = aws_vpclattice_service.test.id + + default_action { + forward { + target_groups { + target_group_identifier = aws_vpclattice_target_group.test.id + weight = 100 + } + } + } + + tags = { + %[2]q = %[3]q + } +} + data "aws_vpclattice_listener" "test_tags" { - service_identifier = aws_vpclattice_service.test.id - listener_identifier = aws_vpclattice_listener.test_tags.arn + service_identifier = aws_vpclattice_service.test.id + listener_identifier = aws_vpclattice_listener.test_tags.arn } `, rName, tag_key, tag_value)) } @@ -149,20 +142,19 @@ data "aws_vpclattice_listener" "test_tags" { func testAccListenerDataSourceConfig_basic(rName string) string { return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 0), fmt.Sprintf(` resource "aws_vpclattice_service" "test" { - name = %[1]q - } + name = %[1]q +} resource "aws_vpclattice_target_group" "test" { -name = %[1]q -type = "INSTANCE" + name = %[1]q + type = "INSTANCE" - config { - port = 80 - protocol = "HTTP" - vpc_identifier = aws_vpc.test.id - } + config { + port = 80 + protocol = "HTTP" + vpc_identifier = aws_vpc.test.id + } } - `, rName)) } @@ -180,8 +172,8 @@ resource "aws_vpclattice_listener" "test" { } data "aws_vpclattice_listener" "test" { - service_identifier = aws_vpclattice_service.test.arn - listener_identifier = aws_vpclattice_listener.test.arn + service_identifier = aws_vpclattice_service.test.arn + listener_identifier = aws_vpclattice_listener.test.arn } `, rName)) } @@ -218,8 +210,8 @@ resource "aws_vpclattice_listener" "test" { } data "aws_vpclattice_listener" "test_multi_target" { - service_identifier = aws_vpclattice_service.test.id - listener_identifier = aws_vpclattice_listener.test.arn + service_identifier = aws_vpclattice_service.test.id + listener_identifier = aws_vpclattice_listener.test.arn } `, rName, targetGroupName1)) } From ac939450467cf2ad748315d8b465e553fb4bc00e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 11:13:05 -0400 Subject: [PATCH 09/14] Fix markdownlint 'MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]'. --- website/docs/d/vpclattice_listener.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/vpclattice_listener.html.markdown b/website/docs/d/vpclattice_listener.html.markdown index df99a01213a..d5799573b59 100644 --- a/website/docs/d/vpclattice_listener.html.markdown +++ b/website/docs/d/vpclattice_listener.html.markdown @@ -39,5 +39,5 @@ In addition to all arguments above, the following attributes are exported: * `port` - The listener port. * `protocol` - The listener protocol. Either `HTTPS` or `HTTP`. * `service_arn` - The ARN of the service. -* `service_id` - The ID of the service. +* `service_id` - The ID of the service. * `tags` - List of tags associated with the listener. \ No newline at end of file From 6682607f2b771fd39590d68f405cbb81bd0452ab Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 11:13:44 -0400 Subject: [PATCH 10/14] Fix markdownlint 'MD047/single-trailing-newline Files should end with a single newline character'. --- website/docs/d/vpclattice_listener.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/vpclattice_listener.html.markdown b/website/docs/d/vpclattice_listener.html.markdown index d5799573b59..d3b2791009d 100644 --- a/website/docs/d/vpclattice_listener.html.markdown +++ b/website/docs/d/vpclattice_listener.html.markdown @@ -40,4 +40,4 @@ In addition to all arguments above, the following attributes are exported: * `protocol` - The listener protocol. Either `HTTPS` or `HTTP`. * `service_arn` - The ARN of the service. * `service_id` - The ID of the service. -* `tags` - List of tags associated with the listener. \ No newline at end of file +* `tags` - List of tags associated with the listener. From a08588eb363fe92af536a0a2aad1cc8365807ebf Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 11:22:39 -0400 Subject: [PATCH 11/14] Fix terrafmt errors. --- .../service/vpclattice/listener_data_source_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go index a977476ab3d..af95b1f847a 100644 --- a/internal/service/vpclattice/listener_data_source_test.go +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -123,7 +123,7 @@ resource "aws_vpclattice_listener" "test_tags" { target_groups { target_group_identifier = aws_vpclattice_target_group.test.id weight = 100 - } + } } } @@ -133,7 +133,7 @@ resource "aws_vpclattice_listener" "test_tags" { } data "aws_vpclattice_listener" "test_tags" { - service_identifier = aws_vpclattice_service.test.id + service_identifier = aws_vpclattice_service.test.id listener_identifier = aws_vpclattice_listener.test_tags.arn } `, rName, tag_key, tag_value)) @@ -144,7 +144,7 @@ func testAccListenerDataSourceConfig_basic(rName string) string { resource "aws_vpclattice_service" "test" { name = %[1]q } - + resource "aws_vpclattice_target_group" "test" { name = %[1]q type = "INSTANCE" @@ -172,7 +172,7 @@ resource "aws_vpclattice_listener" "test" { } data "aws_vpclattice_listener" "test" { - service_identifier = aws_vpclattice_service.test.arn + service_identifier = aws_vpclattice_service.test.arn listener_identifier = aws_vpclattice_listener.test.arn } `, rName)) @@ -210,7 +210,7 @@ resource "aws_vpclattice_listener" "test" { } data "aws_vpclattice_listener" "test_multi_target" { - service_identifier = aws_vpclattice_service.test.id + service_identifier = aws_vpclattice_service.test.id listener_identifier = aws_vpclattice_listener.test.arn } `, rName, targetGroupName1)) From b0ecbb37188865ffa7f1234091bc8d48f074a4dc Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 11:38:14 -0400 Subject: [PATCH 12/14] Fix importlint 'Imports of different types are not allowed in the same group'. --- internal/service/vpclattice/listener_data_source.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/service/vpclattice/listener_data_source.go b/internal/service/vpclattice/listener_data_source.go index 9886b4abd52..dc6c70f6612 100644 --- a/internal/service/vpclattice/listener_data_source.go +++ b/internal/service/vpclattice/listener_data_source.go @@ -3,6 +3,7 @@ package vpclattice import ( "context" "errors" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/vpclattice" "github.com/aws/aws-sdk-go-v2/service/vpclattice/types" From bae4d40e1b86624e99ef5c105bc205766687a007 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 12:48:35 -0400 Subject: [PATCH 13/14] Fix golangci-lint 'whitespace'. --- internal/service/vpclattice/listener_data_source_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/service/vpclattice/listener_data_source_test.go b/internal/service/vpclattice/listener_data_source_test.go index af95b1f847a..7ce8048e18d 100644 --- a/internal/service/vpclattice/listener_data_source_test.go +++ b/internal/service/vpclattice/listener_data_source_test.go @@ -71,7 +71,6 @@ func TestAccVPCLatticeListenerDataSource_tags(t *testing.T) { }, }, }) - } func TestAccVPCLatticeListenerDataSource_forwardMultiTargetGroupHTTP(t *testing.T) { @@ -108,7 +107,6 @@ func TestAccVPCLatticeListenerDataSource_forwardMultiTargetGroupHTTP(t *testing. }, }, }) - } func testAccListenerDataSourceConfig_one_tag(rName, tag_key, tag_value string) string { From 4dcdc0b4f2a4fd8676151270e4f8feab4149de7e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 Apr 2023 12:50:02 -0400 Subject: [PATCH 14/14] Fix golangci-lint 'ineffassign'. --- internal/service/vpclattice/listener_data_source.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/service/vpclattice/listener_data_source.go b/internal/service/vpclattice/listener_data_source.go index dc6c70f6612..6a0a3ab784b 100644 --- a/internal/service/vpclattice/listener_data_source.go +++ b/internal/service/vpclattice/listener_data_source.go @@ -153,6 +153,10 @@ func dataSourceListenerRead(ctx context.Context, d *schema.ResourceData, meta in ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig tags, err := ListTags(ctx, conn, aws.ToString(out.Arn)) + if err != nil { + return create.DiagError(names.VPCLattice, create.ErrActionReading, DSNameListener, d.Id(), err) + } + //lintignore:AWSR002 if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return create.DiagError(names.VPCLattice, create.ErrActionSetting, DSNameListener, d.Id(), err)