From 4eace72d4089bcffe45bc439ebc9ae56e81955c8 Mon Sep 17 00:00:00 2001 From: palramanathan <117597159+palramanathan@users.noreply.github.com> Date: Thu, 22 Dec 2022 15:07:43 -0800 Subject: [PATCH] Adding data source for App Connection resource (#7019) * Adding data source for App Connection resource * Minor formatting fix * Minor formatting fix * Addressing code review comments --- ...source_google_beyondcorp_app_connection.go | 42 +++++++ ...e_google_beyondcorp_app_connection_test.go | 117 ++++++++++++++++++ .../terraform/utils/provider.go.erb | 1 + 3 files changed, 160 insertions(+) create mode 100644 mmv1/third_party/terraform/data_sources/data_source_google_beyondcorp_app_connection.go create mode 100644 mmv1/third_party/terraform/tests/data_source_google_beyondcorp_app_connection_test.go diff --git a/mmv1/third_party/terraform/data_sources/data_source_google_beyondcorp_app_connection.go b/mmv1/third_party/terraform/data_sources/data_source_google_beyondcorp_app_connection.go new file mode 100644 index 000000000000..29a1fd6c4ee0 --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_google_beyondcorp_app_connection.go @@ -0,0 +1,42 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleBeyondcorpAppConnection() *schema.Resource { + + dsSchema := datasourceSchemaFromResourceSchema(resourceBeyondcorpAppConnection().Schema) + + addRequiredFieldsToSchema(dsSchema, "name") + + addOptionalFieldsToSchema(dsSchema, "project") + addOptionalFieldsToSchema(dsSchema, "region") + + return &schema.Resource{ + Read: dataSourceGoogleBeyondcorpAppConnectionRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleBeyondcorpAppConnectionRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + name := d.Get("name").(string) + + project, err := getProject(d, config) + if err != nil { + return err + } + + region, err := getRegion(d, config) + if err != nil { + return err + } + + d.SetId(fmt.Sprintf("projects/%s/locations/%s/appConnections/%s", project, region, name)) + + return resourceBeyondcorpAppConnectionRead(d, meta) +} diff --git a/mmv1/third_party/terraform/tests/data_source_google_beyondcorp_app_connection_test.go b/mmv1/third_party/terraform/tests/data_source_google_beyondcorp_app_connection_test.go new file mode 100644 index 000000000000..30b119f745c1 --- /dev/null +++ b/mmv1/third_party/terraform/tests/data_source_google_beyondcorp_app_connection_test.go @@ -0,0 +1,117 @@ +package google + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "testing" +) + +func TestAccDataSourceGoogleBeyondcorpAppConnection_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBeyondcorpAppConnectionDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleBeyondcorpAppConnection_basic(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_beyondcorp_app_connection.foo", "google_beyondcorp_app_connection.foo"), + ), + }, + }, + }) +} + +func TestAccDataSourceGoogleBeyondcorpAppConnection_full(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBeyondcorpAppConnectionDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleBeyondcorpAppConnection_full(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_beyondcorp_app_connection.foo", "google_beyondcorp_app_connection.foo"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleBeyondcorpAppConnection_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_service_account" "service_account" { + account_id = "tf-test-my-account%{random_suffix}" + display_name = "Test Service Account" +} + +resource "google_beyondcorp_app_connector" "app_connector" { + name = "tf-test-appconnector-%{random_suffix}" + principal_info { + service_account { + email = google_service_account.service_account.email + } + } +} + +resource "google_beyondcorp_app_connection" "foo" { + name = "tf-test-my-app-connection-%{random_suffix}" + type = "TCP_PROXY" + application_endpoint { + host = "foo-host" + port = 8080 + } + connectors = [google_beyondcorp_app_connector.app_connector.id] +} + +data "google_beyondcorp_app_connection" "foo" { + name = google_beyondcorp_app_connection.foo.name +} +`, context) +} + +func testAccDataSourceGoogleBeyondcorpAppConnection_full(context map[string]interface{}) string { + return Nprintf(` +resource "google_service_account" "service_account" { + account_id = "tf-test-my-account%{random_suffix}" + display_name = "Test Service Account" +} + +resource "google_beyondcorp_app_connector" "app_connector" { + name = "tf-test-appconnector-%{random_suffix}" + principal_info { + service_account { + email = google_service_account.service_account.email + } + } +} + +resource "google_beyondcorp_app_connection" "foo" { + name = "tf-test-my-app-connection-%{random_suffix}" + type = "TCP_PROXY" + application_endpoint { + host = "foo-host" + port = 8080 + } + connectors = [google_beyondcorp_app_connector.app_connector.id] +} + +data "google_beyondcorp_app_connection" "foo" { + name = google_beyondcorp_app_connection.foo.name + project = google_beyondcorp_app_connection.foo.project + region = google_beyondcorp_app_connection.foo.region +} +`, context) +} diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index d26b92f6c870..8634c0207faf 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -196,6 +196,7 @@ func Provider() *schema.Provider { "google_active_folder": dataSourceGoogleActiveFolder(), "google_artifact_registry_repository": dataSourceArtifactRegistryRepository(), "google_app_engine_default_service_account": dataSourceGoogleAppEngineDefaultServiceAccount(), + "google_beyondcorp_app_connection": dataSourceGoogleBeyondcorpAppConnection(), "google_beyondcorp_app_connector": dataSourceGoogleBeyondcorpAppConnector(), "google_beyondcorp_app_gateway": dataSourceGoogleBeyondcorpAppGateway(), "google_billing_account": dataSourceGoogleBillingAccount(),