Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Global forwarding rule datasource #2548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-beta/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)
}
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-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
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 @@ -1330,6 +1330,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