Skip to content

Commit

Permalink
Add data source for Serverless VPC Access Connector. (#6533) (#12580)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 16, 2022
1 parent 6868751 commit 68ec524
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
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.

0 comments on commit 68ec524

Please sign in to comment.