Add data source for forwarding rule (#1078)

This commit is contained in:
Julien Phalip 2018-02-13 17:43:08 -08:00 committed by Nathan McKinley
parent ca6c39031f
commit 1966a791a6
5 changed files with 289 additions and 0 deletions

View File

@ -0,0 +1,128 @@
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceGoogleComputeForwardingRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeForwardingRuleRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"target": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"backend_service": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ip_protocol": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"load_balancing_scheme": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"port_range": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ports": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"subnetwork": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceGoogleComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region, err := getRegion(d, config)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
frule, err := config.clientCompute.ForwardingRules.Get(
project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Forwarding Rule Not Found : %s", name))
}
d.SetId(frule.Name)
d.Set("self_link", frule.SelfLink)
d.Set("description", frule.Description)
d.Set("backend_service", frule.BackendService)
d.Set("ip_address", frule.IPAddress)
d.Set("ip_protocol", frule.IPProtocol)
d.Set("load_balancing_scheme", frule.LoadBalancingScheme)
d.Set("name", frule.Name)
d.Set("port_range", frule.PortRange)
d.Set("ports", frule.Ports)
d.Set("subnetwork", frule.Subnetwork)
d.Set("network", frule.Network)
d.Set("target", frule.Target)
d.Set("project", project)
d.Set("region", region)
return nil
}

View File

@ -0,0 +1,95 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDataSourceGoogleForwardingRule(t *testing.T) {
t.Parallel()
poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleForwardingRuleCheck("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr"),
),
},
},
})
}
func testAccDataSourceGoogleForwardingRuleCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[data_source_name]
if !ok {
return fmt.Errorf("root module has no resource called %s", data_source_name)
}
rs, ok := s.RootModule().Resources[resource_name]
if !ok {
return fmt.Errorf("can't find %s in state", resource_name)
}
ds_attr := ds.Primary.Attributes
rs_attr := rs.Primary.Attributes
forwarding_rule_attrs_to_test := []string{
"id",
"self_link",
"name",
"description",
"region",
"port_range",
"ports",
"target",
"ip_address",
"ip_protocol",
"load_balancing_scheme",
"backend_service",
"network",
"subnetwork",
}
for _, attr_to_check := range forwarding_rule_attrs_to_test {
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
return fmt.Errorf(
"%s is %s; want %s",
attr_to_check,
ds_attr[attr_to_check],
rs_attr[attr_to_check],
)
}
}
return nil
}
}
func testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName string) string {
return fmt.Sprintf(`
resource "google_compute_target_pool" "foobar-tp" {
description = "Resource created for Terraform acceptance testing"
instances = ["us-central1-a/foo", "us-central1-b/bar"]
name = "%s"
}
resource "google_compute_forwarding_rule" "foobar-fr" {
description = "Resource created for Terraform acceptance testing"
ip_protocol = "UDP"
name = "%s"
port_range = "80-81"
target = "${google_compute_target_pool.foobar-tp.self_link}"
}
data "google_compute_forwarding_rule" "my_forwarding_rule" {
name = "${google_compute_forwarding_rule.foobar-fr.name}"
}
`, poolName, ruleName)
}

View File

@ -75,6 +75,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
"google_compute_vpn_gateway": dataSourceGoogleComputeVpnGateway(),
"google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(),
"google_container_cluster": dataSourceGoogleContainerCluster(),
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
"google_container_registry_repository": dataSourceGoogleContainerRepo(),

View File

@ -0,0 +1,62 @@
---
layout: "google"
page_title: "Google: google_compute_forwarding_rule"
sidebar_current: "docs-google-datasource-compute-forwarding-rule"
description: |-
Get a forwarding rule within GCE.
---
# google\_compute\_forwarding\_rule
Get a forwarding rule within GCE from its name.
## Example Usage
```tf
data "google_compute_forwarding_rule" "my-forwarding-rule" {
name = "forwarding-rule-us-east1"
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the forwarding rule.
- - -
* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.
* `region` - (Optional) The region in which the resource belongs. If it
is not provided, the project region is used.
## Attributes Reference
In addition to the arguments listed above, the following attributes are exported:
* `description` - Description of this forwarding rule.
* `network` - Network of this forwarding rule.
* `subnetwork` - Subnetwork of this forwarding rule.
* `ip_address` - IP address of this forwarding rule.
* `ip_protocol` - IP protocol of this forwarding rule.
* `ports` - List of ports to use for internal load balancing, if this forwarding rule has any.
* `port_range` - Port range, if this forwarding rule has one.
* `target` - URL of the target pool, if this forwarding rule has one.
* `backend_service` - Backend service, if this forwarding rule has one.
* `load_balancing_scheme` - Type of load balancing of this forwarding rule.
* `region` - Region of this forwarding rule.
* `self_link` - The URI of the resource.

View File

@ -28,6 +28,9 @@
<li<%= sidebar_current("docs-google-datasource-compute-image") %>>
<a href="/docs/providers/google/d/datasource_compute_image.html">google_compute_image</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-forwarding-rule") %>>
<a href="/docs/providers/google/d/datasource_compute_forwarding_rule.html">google_compute_forwarding_rule</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-global-address") %>>
<a href="/docs/providers/google/d/datasource_compute_global_address.html">google_compute_global_address</a>
</li>