diff --git a/.changelog/26908.txt b/.changelog/26908.txt new file mode 100644 index 00000000000..8cbba3e0f96 --- /dev/null +++ b/.changelog/26908.txt @@ -0,0 +1,3 @@ +``release-note:new-data-datasource +aws_appmesh_virtual_router +``` \ No newline at end of file diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 36579b551d5..5812a7f0f70 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -441,6 +441,7 @@ func New(ctx context.Context) (*schema.Provider, error) { "aws_appmesh_mesh": appmesh.DataSourceMesh(), "aws_appmesh_virtual_service": appmesh.DataSourceVirtualService(), + "aws_appmesh_virtual_router": appmesh.DataSourceVirtualRouter(), "aws_autoscaling_group": autoscaling.DataSourceGroup(), "aws_autoscaling_groups": autoscaling.DataSourceGroups(), diff --git a/internal/service/appmesh/virtual_router_data_source.go b/internal/service/appmesh/virtual_router_data_source.go new file mode 100644 index 00000000000..2bf802c285d --- /dev/null +++ b/internal/service/appmesh/virtual_router_data_source.go @@ -0,0 +1,142 @@ +package appmesh + +import ( + "context" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/appmesh" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" +) + +func DataSourceVirtualRouter() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceVirtualRouterRead, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + + "created_date": { + Type: schema.TypeString, + Computed: true, + }, + + "last_updated_date": { + Type: schema.TypeString, + Computed: true, + }, + + "mesh_name": { + Type: schema.TypeString, + Required: true, + }, + + "mesh_owner": { + Type: schema.TypeString, + Computed: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_owner": { + Type: schema.TypeString, + Computed: true, + }, + + "spec": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "listener": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "port_mapping": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "port": { + Type: schema.TypeInt, + Computed: true, + }, + + "protocol": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + + "tags": tftags.TagsSchema(), + }, + } +} + +func dataSourceVirtualRouterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).AppMeshConn() + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + req := &appmesh.DescribeVirtualRouterInput{ + MeshName: aws.String(d.Get("mesh_name").(string)), + VirtualRouterName: aws.String(d.Get("name").(string)), + } + + if v, ok := d.GetOk("mesh_owner"); ok { + req.MeshOwner = aws.String(v.(string)) + } + + resp, err := conn.DescribeVirtualRouterWithContext(ctx, req) + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading App Mesh Virtual Router: %s", err) + } + + arn := aws.StringValue(resp.VirtualRouter.Metadata.Arn) + + d.SetId(aws.StringValue(resp.VirtualRouter.VirtualRouterName)) + + d.Set("name", resp.VirtualRouter.VirtualRouterName) + d.Set("mesh_name", resp.VirtualRouter.MeshName) + d.Set("mesh_owner", resp.VirtualRouter.Metadata.MeshOwner) + d.Set("arn", arn) + d.Set("created_date", resp.VirtualRouter.Metadata.CreatedAt.Format(time.RFC3339)) + d.Set("last_updated_date", resp.VirtualRouter.Metadata.LastUpdatedAt.Format(time.RFC3339)) + d.Set("resource_owner", resp.VirtualRouter.Metadata.ResourceOwner) + + err = d.Set("spec", flattenVirtualRouterSpec(resp.VirtualRouter.Spec)) + if err != nil { + return sdkdiag.AppendErrorf(diags, "setting spec: %s", err) + } + + tags, err := ListTags(ctx, conn, arn) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "listing tags for App Mesh Virtual Router (%s): %s", arn, err) + } + + if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) + } + + return diags +} diff --git a/internal/service/appmesh/virtual_router_data_source_test.go b/internal/service/appmesh/virtual_router_data_source_test.go new file mode 100644 index 00000000000..8ed4d8fb641 --- /dev/null +++ b/internal/service/appmesh/virtual_router_data_source_test.go @@ -0,0 +1,67 @@ +package appmesh_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/service/appmesh" + 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" +) + +func TestAccAppMeshVirtualRouterDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + virtualRouterName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + meshName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_appmesh_virtual_router.test" + dataSourceName := "data.aws_appmesh_virtual_router.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(appmesh.EndpointsID, t) }, + ErrorCheck: acctest.ErrorCheck(t, appmesh.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckVirtualRouterDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccVirtualRouterDataSourceConfig(meshName, virtualRouterName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"), + resource.TestCheckResourceAttrPair(resourceName, "created_date", dataSourceName, "created_date"), + resource.TestCheckResourceAttrPair(resourceName, "last_updated_date", dataSourceName, "last_updated_date"), + resource.TestCheckResourceAttrPair(resourceName, "mesh_name", dataSourceName, "mesh_name"), + resource.TestCheckResourceAttrPair(resourceName, "mesh_owner", dataSourceName, "mesh_owner"), + resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"), + resource.TestCheckResourceAttrPair(resourceName, "resource_owner", dataSourceName, "resource_owner"), + resource.TestCheckResourceAttrPair(resourceName, "spec.0.listener.0.port_mapping.0.port", dataSourceName, "spec.0.listener.0.port_mapping.0.port"), + resource.TestCheckResourceAttrPair(resourceName, "spec.0.listener.0.port_mapping.0.protocol", dataSourceName, "spec.0.listener.0.port_mapping.0.protocol"), + resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"), + ), + }, + }, + }) +} + +func testAccVirtualRouterDataSourceConfig(meshName string, routerName string) string { + return fmt.Sprintf(` +resource "aws_appmesh_mesh" "test" { + name = %[1]q +} +resource "aws_appmesh_virtual_router" "test" { + name = %[2]q + mesh_name = aws_appmesh_mesh.test.id + spec { + listener { + port_mapping { + port = 8080 + protocol = "http" + } + } + } +} +data "aws_appmesh_virtual_router" "test" { + name = aws_appmesh_virtual_router.test.name + mesh_name = aws_appmesh_mesh.test.name +} +`, meshName, routerName) +} diff --git a/website/docs/d/appmesh_virtual_router.html.markdown b/website/docs/d/appmesh_virtual_router.html.markdown new file mode 100644 index 00000000000..e1cea9f5f37 --- /dev/null +++ b/website/docs/d/appmesh_virtual_router.html.markdown @@ -0,0 +1,51 @@ +--- +subcategory: "App Mesh" +layout: "aws" +page_title: "AWS: aws_appmesh_virtual_router" +description: |- + Provides an AWS App Mesh virtual router resource. +--- + +# Data Source: aws_appmesh_virtual_router + +The App Mesh Virtual Router data source allows details of an App Mesh Virtual Service to be retrieved by its name and mesh_name. + +## Example Usage + +```hcl +data "aws_appmesh_virtual_router" "test" { + name = "example-router-name" + mesh_name = "example-mesh-name" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Name of the virtual router. +* `mesh_name` - (Required) Name of the mesh in which the virtual router exists + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `arn` - ARN of the virtual router. +* `created_date` - Creation date of the virtual router. +* `last_updated_date` - Last update date of the virtual router. +* `resource_owner` - Resource owner's AWS account ID. +* `spec` - Virtual routers specification. +* `tags` - Map of tags. + +### Spec + +* `listener` - The listener assigned to the virtual router + +### Listener + +* `port_mapping` - The port mapping of the listener + +### Port_Mapping + +* `port` - Port number the listener is listening on. +* `protocol` - Protocol the listener is listening for.