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 data source for Serverless VPC Access Connector. #12580

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/6533.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_vpc_access_connector`
```
32 changes: 32 additions & 0 deletions google/data_source_vpc_access_connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceVPCAccessConnector() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourceVPCAccessConnector().Schema)
addRequiredFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "project", "region")

return &schema.Resource{
Read: dataSourceVPCAccessConnectorRead,
Schema: dsSchema,
}
}

func dataSourceVPCAccessConnectorRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

id, err := replaceVars(d, config, "projects/{{project}}/locations/{{region}}/connectors/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}

d.SetId(id)

return resourceVPCAccessConnectorRead(d, meta)
}
48 changes: 48 additions & 0 deletions google/data_source_vpc_access_connector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccVPCAccessConnectorDatasource_basic(t *testing.T) {
t.Parallel()

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVPCAccessConnectorDatasourceConfig(randString(t, 10)),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceStateWithIgnores(
"data.google_vpc_access_connector.connector",
"google_vpc_access_connector.connector",
map[string]struct{}{
// Ignore fields not returned in response
"self_link": {},
"region": {},
},
),
),
},
},
})
}

func testAccVPCAccessConnectorDatasourceConfig(suffix string) string {
return fmt.Sprintf(`
resource "google_vpc_access_connector" "connector" {
name = "vpc-con-test-%s"
ip_cidr_range = "10.8.0.0/28"
network = "default"
region = "us-central1"
}

data "google_vpc_access_connector" "connector" {
name = google_vpc_access_connector.connector.name
}
`, suffix)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ func Provider() *schema.Provider {
"google_tags_tag_key": dataSourceGoogleTagsTagKey(),
"google_tags_tag_value": dataSourceGoogleTagsTagValue(),
"google_tpu_tensorflow_versions": dataSourceTpuTensorflowVersions(),
"google_vpc_access_connector": dataSourceVPCAccessConnector(),
"google_redis_instance": dataSourceGoogleRedisInstance(),
// ####### END datasources ###########
},
Expand Down
49 changes: 49 additions & 0 deletions website/docs/d/vpc_access_connector.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
subcategory: "Serverless VPC Access"
page_title: "Google: google_vpc_access_connector"
description: |-
Get a Serverless VPC Access connector.
---

# google\_vpc\_access\_connector

Get a Serverless VPC Access connector.

To get more information about Connector, see:

* [API documentation](https://cloud.google.com/vpc/docs/reference/vpcaccess/rest/v1/projects.locations.connectors)
* How-to Guides
* [Configuring Serverless VPC Access](https://cloud.google.com/vpc/docs/configure-serverless-vpc-access)

## Example Usage

```hcl
data "google_vpc_access_connector" "sample" {
name = "vpc-con"
}

resource "google_vpc_access_connector" "connector" {
name = "vpc-con"
ip_cidr_range = "10.8.0.0/28"
network = "default"
region = "us-central1"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) Name of the resource.

- - -

* `project` - (Optional) The ID of the project in which the resource belongs. If it
is not provided, the provider project is used.

* `region` - (Optional) The region in which the resource belongs. If it
is not provided, the provider region is used.

## Attributes Reference

See [google_vpc_access_connector](https://www.terraform.io/docs/providers/google/r/vpc_access_connector.html) resource for details of the available attributes.