Skip to content

Commit

Permalink
Add Global forwarding rule datasource (#4052) (#7434)
Browse files Browse the repository at this point in the history
* add global forwarding rule datasource

* fix broken test

* fix typos

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 5, 2020
1 parent 795e744 commit 5d93cfb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4052.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
compute: added `google_compute_global_forwarding_rule` datasource
```
37 changes: 37 additions & 0 deletions google/data_source_google_global_compute_forwarding_rule.go
Original file line number Diff line number Diff line change
@@ -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)
}
55 changes: 55 additions & 0 deletions google/data_source_google_global_compute_forwarding_rule_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,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(),
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/compute_global_forwarding_rule.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,10 @@
<a href="/docs/providers/google/d/compute_global_address.html">google_compute_global_address</a>
</li>

<li>
<a href="/docs/providers/google/d/compute_global_forwarding_rule.html">google_compute_global_forwarding_rule</a>
</li>

<li>
<a href="/docs/providers/google/d/compute_image.html">google_compute_image</a>
</li>
Expand Down

0 comments on commit 5d93cfb

Please sign in to comment.