-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19774 from philnichol/f-data-source-aws-appmesh-v…
…irtualservice F data source aws appmesh virtualservice
- Loading branch information
Showing
6 changed files
with
371 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_appmesh_virtual_service | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.