diff --git a/.changelog/4052.txt b/.changelog/4052.txt new file mode 100644 index 0000000000..07850e6aef --- /dev/null +++ b/.changelog/4052.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +compute: added `google_compute_global_forwarding_rule` datasource +``` diff --git a/google-beta/data_source_google_global_compute_forwarding_rule.go b/google-beta/data_source_google_global_compute_forwarding_rule.go new file mode 100644 index 0000000000..61671ee356 --- /dev/null +++ b/google-beta/data_source_google_global_compute_forwarding_rule.go @@ -0,0 +1,37 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleComputeGlobalForwardingRule() *schema.Resource { + dsSchema := datasourceSchemaFromResourceSchema(resourceComputeGlobalForwardingRule().Schema) + + // Set 'Required' schema elements + addRequiredFieldsToSchema(dsSchema, "name") + + // Set 'Optional' schema elements + addOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceGoogleComputeGlobalForwardingRuleRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + name := d.Get("name").(string) + + project, err := getProject(d, config) + if err != nil { + return err + } + + d.SetId(fmt.Sprintf("projects/%s/global/forwardingRules/%s", project, name)) + + return resourceComputeGlobalForwardingRuleRead(d, meta) +} diff --git a/google-beta/data_source_google_global_compute_forwarding_rule_test.go b/google-beta/data_source_google_global_compute_forwarding_rule_test.go new file mode 100644 index 0000000000..ec276b52c5 --- /dev/null +++ b/google-beta/data_source_google_global_compute_forwarding_rule_test.go @@ -0,0 +1,55 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceGoogleGlobalForwardingRule(t *testing.T) { + t.Parallel() + + poolName := fmt.Sprintf("tf-%s", randString(t, 10)) + ruleName := fmt.Sprintf("tf-%s", randString(t, 10)) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleGlobalForwardingRuleConfig(poolName, ruleName), + Check: checkDataSourceStateMatchesResourceState("data.google_compute_global_forwarding_rule.my_forwarding_rule", "google_compute_global_forwarding_rule.foobar-fr"), + }, + }, + }) +} + +func testAccDataSourceGoogleGlobalForwardingRuleConfig(poolName, ruleName string) string { + return fmt.Sprintf(` +resource "google_compute_global_forwarding_rule" "foobar-fr" { + name = "%s" + target = google_compute_target_http_proxy.default.id + port_range = "80" +} + +resource "google_compute_target_http_proxy" "default" { + name = "%s" + description = "a description" + url_map = google_compute_url_map.default.id +} + +resource "google_compute_url_map" "default" { + name = "%s" + default_url_redirect { + https_redirect = true + redirect_response_code = "MOVED_PERMANENTLY_DEFAULT" + strip_query = false + } +} + +data "google_compute_global_forwarding_rule" "my_forwarding_rule" { + name = google_compute_global_forwarding_rule.foobar-fr.name +} +`, ruleName, poolName, poolName) +} diff --git a/google-beta/provider.go b/google-beta/provider.go index d3bfa50944..552c1c7adc 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -662,6 +662,7 @@ func Provider() *schema.Provider { "google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(), "google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(), "google_compute_global_address": dataSourceGoogleComputeGlobalAddress(), + "google_compute_global_forwarding_rule": dataSourceGoogleComputeGlobalForwardingRule(), "google_compute_image": dataSourceGoogleComputeImage(), "google_compute_instance": dataSourceGoogleComputeInstance(), "google_compute_instance_serial_port": dataSourceGoogleComputeInstanceSerialPort(), diff --git a/website/docs/d/compute_global_forwarding_rule.html.markdown b/website/docs/d/compute_global_forwarding_rule.html.markdown new file mode 100644 index 0000000000..fdcbacd0f8 --- /dev/null +++ b/website/docs/d/compute_global_forwarding_rule.html.markdown @@ -0,0 +1,34 @@ +--- +subcategory: "Compute Engine" +layout: "google" +page_title: "Google: google_compute_global_forwarding_rule" +sidebar_current: "docs-google-datasource-compute-global_forwarding-rule" +description: |- + Get a global forwarding rule within GCE. +--- + +# google\_compute\_global_\forwarding\_rule + +Get a global forwarding rule within GCE from its name. + +## Example Usage + +```tf +data "google_compute_global_forwarding_rule" "my-forwarding-rule" { + name = "forwarding-rule-global" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the global forwarding rule. + +- - - + +* `project` - (Optional) The project in which the resource belongs. If it + is not provided, the provider project is used. + +## Attributes Reference +See [google_compute_global_forwarding_rule](https://www.terraform.io/docs/providers/google/r/compute_global_forwarding_rule.html) resource for details of the available attributes. diff --git a/website/google.erb b/website/google.erb index 8f82f11e35..dd896c06d9 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1330,6 +1330,10 @@ google_compute_global_address +