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 VPCLattice Service data source #30490

Merged
merged 2 commits into from
Apr 6, 2023
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/30490.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_vpclattice_service
```
107 changes: 107 additions & 0 deletions internal/service/vpclattice/service_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package vpclattice

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"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/create"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_vpclattice_service")
func DataSourceService() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceServiceRead,

Schema: map[string]*schema.Schema{
names.AttrARN: {
Type: schema.TypeString,
Computed: true,
},
"auth_type": {
Type: schema.TypeString,
Computed: true,
},
"certificate_arn": {
Type: schema.TypeString,
Computed: true,
},
"custom_domain_name": {
Type: schema.TypeString,
Computed: true,
},
"dns_entry": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain_name": {
Type: schema.TypeString,
Computed: true,
},
"hosted_zone_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"service_identifier": {
Type: schema.TypeString,
Required: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

const (
DSNameService = "Service Data Source"
)

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

service_id := d.Get("service_identifier").(string)
out, err := findServiceByID(ctx, conn, service_id)
if err != nil {
return create.DiagError(names.VPCLattice, create.ErrActionReading, DSNameService, service_id, err)
}

//
// If you don't set the ID, the data source will not be stored in state. In
// fact, that's how a resource can be removed from state - clearing its ID.
//
// If this data source is a companion to a resource, often both will use the
// same ID. Otherwise, the ID will be a unique identifier such as an AWS
// identifier, ARN, or name.
d.SetId(aws.StringValue(out.Id))

d.Set("arn", out.Arn)
d.Set("auth_type", out.AuthType)
d.Set("certificate_arn", out.CertificateArn)
d.Set("custom_domain_name", out.CustomDomainName)
if out.DnsEntry != nil {
if err := d.Set("dns_entry", []interface{}{flattenDNSEntry(out.DnsEntry)}); err != nil {
return diag.Errorf("setting dns_entry: %s", err)
}
} else {
d.Set("dns_entry", nil)
}
d.Set("name", out.Name)
d.Set("status", out.Status)

return nil
}
55 changes: 55 additions & 0 deletions internal/service/vpclattice/service_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package vpclattice_test

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go-v2/service/vpclattice"
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 TestAccVPCLatticeServiceDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)

var service vpclattice.GetServiceOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_vpclattice_service.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: testAccCheckServiceDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccServiceDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists(ctx, dataSourceName, &service),
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
resource.TestCheckResourceAttr(dataSourceName, "auth_type", "NONE"),
acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "vpc-lattice", regexp.MustCompile(`service/.+$`)),
),
},
},
})
}

func testAccServiceDataSourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_vpclattice_service" "test" {
name = %[1]q
}

data "aws_vpclattice_service" "test" {
service_identifier = aws_vpclattice_service.test.id
}
`, rName)
}
7 changes: 6 additions & 1 deletion internal/service/vpclattice/service_package_gen.go

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

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

# Data Source: aws_vpclattice_service

Terraform data source for managing an AWS VPC Lattice Service.

## Example Usage

### Basic Usage

```terraform
data "aws_vpclattice_service" "example" {
}
```

## Argument Reference

The following arguments are required:

* `service_identifier` - (Required) ID or Amazon Resource Name (ARN) of the service network

## Attributes Reference

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

* `arn` - ARN of the service.
* `auth_type` - Type of IAM policy. Either `NONE` or `AWS_IAM`.
* `certificate_arn` - Amazon Resource Name (ARN) of the certificate.
* `custom_domain_name` - Custom domain name of the service.
* `dns_entry` - DNS name of the service.
* `id` - Unique identifier for the service.
* `status` - Status of the service.
* `tags` - List of tags associated with the service.