Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Datasource: Appmesh Virtual Router #29283

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/26908.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``release-note:new-data-datasource
aws_appmesh_virtual_router
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
142 changes: 142 additions & 0 deletions internal/service/appmesh/virtual_router_data_source.go
Original file line number Diff line number Diff line change
@@ -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
}
67 changes: 67 additions & 0 deletions internal/service/appmesh/virtual_router_data_source_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
51 changes: 51 additions & 0 deletions website/docs/d/appmesh_virtual_router.html.markdown
Original file line number Diff line number Diff line change
@@ -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.