Skip to content

Commit

Permalink
Merge pull request #30904 from sQu4rks/f-vpclattice-service_network-d…
Browse files Browse the repository at this point in the history
…ata_source

F vpclattice service network data source
  • Loading branch information
ewbankkit authored May 1, 2023
2 parents 6825fca + a9cd836 commit df32d6c
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/30904.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_vpclattice_service_network
```
130 changes: 130 additions & 0 deletions internal/service/vpclattice/service_network_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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"
"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"
)

// @SDKDataSource("aws_vpclattice_service_network")
func DataSourceServiceNetwork() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceServiceNetworkRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"auth_type": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"last_updated_at": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"number_of_associated_services": {
Type: schema.TypeInt,
Computed: true,
},
"number_of_associated_vpcs": {
Type: schema.TypeInt,
Computed: true,
},
"service_network_identifier": {
Type: schema.TypeString,
Required: true,
},
"tags": tftags.TagsSchemaComputed(), // TIP: Many, but not all, data sources have `tags` attributes.
},
}
}

const (
DSNameServiceNetwork = "Service Network Data Source"
)

func dataSourceServiceNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).VPCLatticeClient()

serviceNetworkID := d.Get("service_network_identifier").(string)

out, err := findServiceNetworkById(ctx, conn, serviceNetworkID)
if err != nil {
return create.DiagError(names.VPCLattice, create.ErrActionReading, DSNameServiceNetwork, serviceNetworkID, err)
}

d.SetId(aws.ToString(out.Id))
d.Set("arn", out.Arn)
d.Set("auth_type", out.AuthType)
d.Set("created_at", aws.ToTime(out.CreatedAt).String())
d.Set("id", out.Id)
d.Set("last_updated_at", aws.ToTime(out.LastUpdatedAt).String())
d.Set("name", out.Name)
d.Set("number_of_associated_services", out.NumberOfAssociatedServices)
d.Set("number_of_associated_vpcs", out.NumberOfAssociatedVPCs)

ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
tags, err := ListTags(ctx, conn, aws.ToString(out.Arn))

if err != nil {
return create.DiagError(names.VPCLattice, create.ErrActionReading, DSNameServiceNetwork, serviceNetworkID, err)
}

//lintignore:AWSR002
if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return create.DiagError(names.VPCLattice, create.ErrActionSetting, DSNameServiceNetwork, d.Id(), err)
}

return nil
}

func findServiceNetworkById(ctx context.Context, conn *vpclattice.Client, service_network_identifier string) (*vpclattice.GetServiceNetworkOutput, error) {
in := &vpclattice.GetServiceNetworkInput{
ServiceNetworkIdentifier: aws.String(service_network_identifier),
}

out, err := conn.GetServiceNetwork(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
}
107 changes: 107 additions & 0 deletions internal/service/vpclattice/service_network_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package vpclattice_test

import (
"fmt"
"regexp"
"testing"

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 TestAccVPCLatticeServiceNetworkDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
// TIP: This is a long-running test guard for tests that run longer than
// 300s (5 min) generally.
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_vpclattice_service_network.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: testAccCheckServiceNetworkDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccServiceNetworkDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`servicenetwork/sn-.+`)),
),
},
},
})
}

func TestAccVPCLatticeServiceNetworkDataSource_tags(t *testing.T) {
ctx := acctest.Context(t)
// TIP: This is a long-running test guard for tests that run longer than
// 300s (5 min) generally.
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
tagKey := "tag1"
tagValue := "value1"
dataSourceName := "data.aws_vpclattice_service_network.test_tags"

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: testAccCheckServiceNetworkDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccServiceNetworkDataSourceConfig_tags(rName, tagKey, tagValue),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
resource.TestCheckResourceAttr(dataSourceName, "tags.tag1", "value1"),
acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`servicenetwork/sn-.+`)),
),
},
},
})
}

func testAccServiceNetworkDataSourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_vpclattice_service_network" "test" {
name = %[1]q
}
data "aws_vpclattice_service_network" "test" {
service_network_identifier = aws_vpclattice_service_network.test.id
}
`, rName)
}

func testAccServiceNetworkDataSourceConfig_tags(rName string, tagKey string, tagValue string) string {
return fmt.Sprintf(`
resource "aws_vpclattice_service_network" "test_tags" {
name = %[1]q
tags = {
%[2]q = %[3]q
}
}
data "aws_vpclattice_service_network" "test_tags" {
service_network_identifier = aws_vpclattice_service_network.test_tags.id
}
`, rName, tagKey, tagValue)
}
4 changes: 4 additions & 0 deletions internal/service/vpclattice/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions website/docs/d/vpclattice_service_network.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
subcategory: "VPC Lattice"
layout: "aws"
page_title: "AWS: aws_vpclattice_service_network"
description: |-
Terraform data source for managing an AWS VPC Lattice Service Network.
---

# Data Source: aws_vpclattice_service_network

Terraform data source for managing an AWS VPC Lattice Service Network.

## Example Usage

### Basic Usage

```terraform
data "aws_vpclattice_service_network" "example" {
service_network_identifier = ""
}
```

## Argument Reference

The following arguments are required:

* `service_network_identifier` - (Required) Identifier of the network service.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - ARN of the Service Network.
* `auth_type` - Authentication type for the service network. Either `NONE` or `AWS_IAM`.
* `created_at` - Date and time the service network was created.
* `id` - ID of the service network.
* `last_updated_at` - Date and time the service network was last updated.
* `name` - Name of the service network.
* `number_of_associated_services` - Number of services associated with this service network.
* `number_of_associated_vpcs` - Number of VPCs associated with this service network.

0 comments on commit df32d6c

Please sign in to comment.