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

F data source aws appmesh virtualservice #19774

Merged
merged 8 commits into from
Jun 14, 2021
Merged
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/19774.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_appmesh_virtual_service
```
6 changes: 3 additions & 3 deletions aws/data_source_aws_appmesh_mesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSAppmeshMeshDataSource_basic(t *testing.T) {
func TestAccDataSourceAWSAppmeshMesh_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appmesh_mesh.test"
dataSourceName := "data.aws_appmesh_mesh.test"
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestAccAWSAppmeshMeshDataSource_basic(t *testing.T) {
})
}

func TestAccAWSAppmeshMeshDataSource_meshOwner(t *testing.T) {
func TestAccDataSourceAWSAppmeshMesh_meshOwner(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appmesh_mesh.test"
dataSourceName := "data.aws_appmesh_mesh.test"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestAccAWSAppmeshMeshDataSource_meshOwner(t *testing.T) {
})
}

func TestAccAWSAppmeshMeshDataSource_specAndTagsSet(t *testing.T) {
func TestAccDataSourceAWSAppmeshMesh_specAndTagsSet(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appmesh_mesh.test"
dataSourceName := "data.aws_appmesh_mesh.test"
Expand Down
147 changes: 147 additions & 0 deletions aws/data_source_aws_appmesh_virtual_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package aws

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsAppmeshVirtualService() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsAppmeshVirtualServiceRead,

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,
Optional: true,
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{
"provider": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"virtual_node": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"virtual_node_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"virtual_router": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"virtual_router_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
},
},

"tags": tagsSchema(),
},
}
}

func dataSourceAwsAppmeshVirtualServiceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

req := &appmesh.DescribeVirtualServiceInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualServiceName: aws.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("mesh_owner"); ok {
req.MeshOwner = aws.String(v.(string))
}

resp, err := conn.DescribeVirtualService(req)
if err != nil {
return fmt.Errorf("error reading App Mesh Virtual Service: %s", err)
}

arn := aws.StringValue(resp.VirtualService.Metadata.Arn)

d.SetId(aws.StringValue(resp.VirtualService.VirtualServiceName))

d.Set("name", resp.VirtualService.VirtualServiceName)
d.Set("mesh_name", resp.VirtualService.MeshName)
d.Set("mesh_owner", resp.VirtualService.Metadata.MeshOwner)
d.Set("arn", arn)
d.Set("created_date", resp.VirtualService.Metadata.CreatedAt.Format(time.RFC3339))
d.Set("last_updated_date", resp.VirtualService.Metadata.LastUpdatedAt.Format(time.RFC3339))
d.Set("resource_owner", resp.VirtualService.Metadata.ResourceOwner)

err = d.Set("spec", flattenAppmeshVirtualServiceSpec(resp.VirtualService.Spec))
if err != nil {
return fmt.Errorf("error setting spec: %s", err)
}

tags, err := keyvaluetags.AppmeshListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for App Mesh Virtual Service (%s): %s", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
151 changes: 151 additions & 0 deletions aws/data_source_aws_appmesh_virtual_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAWSAppmeshVirtualService_virtualNode(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appmesh_virtual_service.test"
dataSourceName := "data.aws_appmesh_virtual_service.test"
vsName := fmt.Sprintf("tf-acc-test-%d.mesh.local", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(appmesh.EndpointsID, t) },
ErrorCheck: testAccErrorCheck(t, appmesh.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAppmeshVirtualServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsAppmeshVirtualServiceDataSourceConfig_virtualNode(rName, vsName),
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.provider.0.virtual_node.0.virtual_node_name", dataSourceName, "spec.0.provider.0.virtual_node.0.virtual_node_name"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
),
},
},
})
}

func TestAccDataSourceAWSAppmeshVirtualService_virtualRouter(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appmesh_virtual_service.test"
dataSourceName := "data.aws_appmesh_virtual_service.test"
vsName := fmt.Sprintf("tf-acc-test-%d.mesh.local", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(appmesh.EndpointsID, t) },
ErrorCheck: testAccErrorCheck(t, appmesh.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAppmeshVirtualServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsAppmeshVirtualServiceDataSourceConfig_virtualRouter(rName, vsName),
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.provider.0.virtual_router.0.virtual_router_name", dataSourceName, "spec.0.provider.0.virtual_router.0.virtual_router_name"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
),
},
},
})
}

func testAccCheckAwsAppmeshVirtualServiceDataSourceConfig_virtualNode(rName, vsName string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "test" {
name = %[1]q
}

resource "aws_appmesh_virtual_node" "test" {
name = %[1]q
mesh_name = aws_appmesh_mesh.test.id

spec {}
}

resource "aws_appmesh_virtual_service" "test" {
name = %[2]q
mesh_name = aws_appmesh_mesh.test.id

spec {
provider {
virtual_node {
virtual_node_name = aws_appmesh_virtual_node.test.name
}
}
}

tags = {
foo = "bar"
good = "bad"
}
}

data "aws_appmesh_virtual_service" "test" {
name = aws_appmesh_virtual_service.test.name
mesh_name = aws_appmesh_mesh.test.name
}
`, rName, vsName)
}

func testAccCheckAwsAppmeshVirtualServiceDataSourceConfig_virtualRouter(rName, vsName string) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}

resource "aws_appmesh_mesh" "test" {
name = %[1]q
}

resource "aws_appmesh_virtual_router" "test" {
name = %[1]q
mesh_name = aws_appmesh_mesh.test.id

spec {
listener {
port_mapping {
port = 8080
protocol = "http"
}
}
}
}

resource "aws_appmesh_virtual_service" "test" {
name = %[2]q
mesh_name = aws_appmesh_mesh.test.id

spec {
provider {
virtual_router {
virtual_router_name = aws_appmesh_virtual_router.test.name
}
}
}
}

data "aws_appmesh_virtual_service" "test" {
name = aws_appmesh_virtual_service.test.name
mesh_name = aws_appmesh_mesh.test.name
mesh_owner = data.aws_caller_identity.current.account_id
}
`, rName, vsName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func Provider() *schema.Provider {
"aws_apigatewayv2_api": dataSourceAwsApiGatewayV2Api(),
"aws_apigatewayv2_apis": dataSourceAwsApiGatewayV2Apis(),
"aws_appmesh_mesh": dataSourceAwsAppmeshMesh(),
"aws_appmesh_virtual_service": dataSourceAwsAppmeshVirtualService(),
"aws_arn": dataSourceAwsArn(),
"aws_autoscaling_group": dataSourceAwsAutoscalingGroup(),
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
Expand Down
Loading