Skip to content

Commit

Permalink
Adding data source for App Connection resource (GoogleCloudPlatform#7019
Browse files Browse the repository at this point in the history
)

* Adding data source for App Connection resource

* Minor formatting fix

* Minor formatting fix

* Addressing code review comments
  • Loading branch information
palramanathan authored and kimihrr committed Jan 3, 2023
1 parent aaeb14d commit cd1e305
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit cd1e305

Please sign in to comment.