From f45f1ae91bc702cba1991117d46e7cefc94b16b0 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 16 Sep 2022 17:46:01 +0000 Subject: [PATCH] Add data source for Serverless VPC Access Connector. (#6533) Signed-off-by: Modular Magician --- .changelog/6533.txt | 3 ++ google/data_source_vpc_access_connector.go | 32 ++++++++++++ .../data_source_vpc_access_connector_test.go | 48 ++++++++++++++++++ google/provider.go | 1 + .../docs/d/vpc_access_connector.html.markdown | 49 +++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 .changelog/6533.txt create mode 100644 google/data_source_vpc_access_connector.go create mode 100644 google/data_source_vpc_access_connector_test.go create mode 100644 website/docs/d/vpc_access_connector.html.markdown diff --git a/.changelog/6533.txt b/.changelog/6533.txt new file mode 100644 index 00000000000..d6883c9e0e7 --- /dev/null +++ b/.changelog/6533.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_vpc_access_connector` +``` diff --git a/google/data_source_vpc_access_connector.go b/google/data_source_vpc_access_connector.go new file mode 100644 index 00000000000..b11cf686955 --- /dev/null +++ b/google/data_source_vpc_access_connector.go @@ -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) +} diff --git a/google/data_source_vpc_access_connector_test.go b/google/data_source_vpc_access_connector_test.go new file mode 100644 index 00000000000..fac1c35a875 --- /dev/null +++ b/google/data_source_vpc_access_connector_test.go @@ -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) +} diff --git a/google/provider.go b/google/provider.go index 5a84b634112..e33083f835d 100644 --- a/google/provider.go +++ b/google/provider.go @@ -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 ########### }, diff --git a/website/docs/d/vpc_access_connector.html.markdown b/website/docs/d/vpc_access_connector.html.markdown new file mode 100644 index 00000000000..e5c8b298cc1 --- /dev/null +++ b/website/docs/d/vpc_access_connector.html.markdown @@ -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.