Skip to content

Commit 601ca8f

Browse files
jphalipishashchuk
authored andcommitted
Add data source for forwarding rule (hashicorp#1078)
1 parent 05041ca commit 601ca8f

5 files changed

+289
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
)
8+
9+
func dataSourceGoogleComputeForwardingRule() *schema.Resource {
10+
return &schema.Resource{
11+
Read: dataSourceGoogleComputeForwardingRuleRead,
12+
13+
Schema: map[string]*schema.Schema{
14+
"name": &schema.Schema{
15+
Type: schema.TypeString,
16+
Required: true,
17+
},
18+
19+
"region": &schema.Schema{
20+
Type: schema.TypeString,
21+
Optional: true,
22+
Computed: true,
23+
},
24+
25+
"project": &schema.Schema{
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Computed: true,
29+
},
30+
31+
"target": &schema.Schema{
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
36+
"backend_service": &schema.Schema{
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
41+
"description": &schema.Schema{
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
46+
"ip_address": &schema.Schema{
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
51+
"ip_protocol": &schema.Schema{
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
56+
"load_balancing_scheme": &schema.Schema{
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
61+
"network": &schema.Schema{
62+
Type: schema.TypeString,
63+
Computed: true,
64+
},
65+
66+
"port_range": &schema.Schema{
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
71+
"ports": &schema.Schema{
72+
Type: schema.TypeSet,
73+
Elem: &schema.Schema{Type: schema.TypeString},
74+
Computed: true,
75+
},
76+
77+
"self_link": &schema.Schema{
78+
Type: schema.TypeString,
79+
Computed: true,
80+
},
81+
82+
"subnetwork": &schema.Schema{
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
},
87+
}
88+
}
89+
90+
func dataSourceGoogleComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
91+
config := meta.(*Config)
92+
93+
region, err := getRegion(d, config)
94+
if err != nil {
95+
return err
96+
}
97+
98+
project, err := getProject(d, config)
99+
if err != nil {
100+
return err
101+
}
102+
103+
name := d.Get("name").(string)
104+
105+
frule, err := config.clientCompute.ForwardingRules.Get(
106+
project, region, name).Do()
107+
if err != nil {
108+
return handleNotFoundError(err, d, fmt.Sprintf("Forwarding Rule Not Found : %s", name))
109+
}
110+
d.SetId(frule.Name)
111+
112+
d.Set("self_link", frule.SelfLink)
113+
d.Set("description", frule.Description)
114+
d.Set("backend_service", frule.BackendService)
115+
d.Set("ip_address", frule.IPAddress)
116+
d.Set("ip_protocol", frule.IPProtocol)
117+
d.Set("load_balancing_scheme", frule.LoadBalancingScheme)
118+
d.Set("name", frule.Name)
119+
d.Set("port_range", frule.PortRange)
120+
d.Set("ports", frule.Ports)
121+
d.Set("subnetwork", frule.Subnetwork)
122+
d.Set("network", frule.Network)
123+
d.Set("target", frule.Target)
124+
d.Set("project", project)
125+
d.Set("region", region)
126+
127+
return nil
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccDataSourceGoogleForwardingRule(t *testing.T) {
13+
t.Parallel()
14+
15+
poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
16+
ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName),
24+
Check: resource.ComposeTestCheckFunc(
25+
testAccDataSourceGoogleForwardingRuleCheck("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr"),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
func testAccDataSourceGoogleForwardingRuleCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
33+
return func(s *terraform.State) error {
34+
ds, ok := s.RootModule().Resources[data_source_name]
35+
if !ok {
36+
return fmt.Errorf("root module has no resource called %s", data_source_name)
37+
}
38+
39+
rs, ok := s.RootModule().Resources[resource_name]
40+
if !ok {
41+
return fmt.Errorf("can't find %s in state", resource_name)
42+
}
43+
44+
ds_attr := ds.Primary.Attributes
45+
rs_attr := rs.Primary.Attributes
46+
forwarding_rule_attrs_to_test := []string{
47+
"id",
48+
"self_link",
49+
"name",
50+
"description",
51+
"region",
52+
"port_range",
53+
"ports",
54+
"target",
55+
"ip_address",
56+
"ip_protocol",
57+
"load_balancing_scheme",
58+
"backend_service",
59+
"network",
60+
"subnetwork",
61+
}
62+
63+
for _, attr_to_check := range forwarding_rule_attrs_to_test {
64+
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
65+
return fmt.Errorf(
66+
"%s is %s; want %s",
67+
attr_to_check,
68+
ds_attr[attr_to_check],
69+
rs_attr[attr_to_check],
70+
)
71+
}
72+
}
73+
return nil
74+
}
75+
}
76+
77+
func testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName string) string {
78+
return fmt.Sprintf(`
79+
resource "google_compute_target_pool" "foobar-tp" {
80+
description = "Resource created for Terraform acceptance testing"
81+
instances = ["us-central1-a/foo", "us-central1-b/bar"]
82+
name = "%s"
83+
}
84+
resource "google_compute_forwarding_rule" "foobar-fr" {
85+
description = "Resource created for Terraform acceptance testing"
86+
ip_protocol = "UDP"
87+
name = "%s"
88+
port_range = "80-81"
89+
target = "${google_compute_target_pool.foobar-tp.self_link}"
90+
}
91+
data "google_compute_forwarding_rule" "my_forwarding_rule" {
92+
name = "${google_compute_forwarding_rule.foobar-fr.name}"
93+
}
94+
`, poolName, ruleName)
95+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func Provider() terraform.ResourceProvider {
7676
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
7777
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
7878
"google_compute_vpn_gateway": dataSourceGoogleComputeVpnGateway(),
79+
"google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(),
7980
"google_container_cluster": dataSourceGoogleContainerCluster(),
8081
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
8182
"google_container_registry_repository": dataSourceGoogleContainerRepo(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_compute_forwarding_rule"
4+
sidebar_current: "docs-google-datasource-compute-forwarding-rule"
5+
description: |-
6+
Get a forwarding rule within GCE.
7+
---
8+
9+
# google\_compute\_forwarding\_rule
10+
11+
Get a forwarding rule within GCE from its name.
12+
13+
## Example Usage
14+
15+
```tf
16+
data "google_compute_forwarding_rule" "my-forwarding-rule" {
17+
name = "forwarding-rule-us-east1"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `name` - (Required) The name of the forwarding rule.
26+
27+
28+
- - -
29+
30+
* `project` - (Optional) The project in which the resource belongs. If it
31+
is not provided, the provider project is used.
32+
33+
* `region` - (Optional) The region in which the resource belongs. If it
34+
is not provided, the project region is used.
35+
36+
## Attributes Reference
37+
38+
In addition to the arguments listed above, the following attributes are exported:
39+
40+
* `description` - Description of this forwarding rule.
41+
42+
* `network` - Network of this forwarding rule.
43+
44+
* `subnetwork` - Subnetwork of this forwarding rule.
45+
46+
* `ip_address` - IP address of this forwarding rule.
47+
48+
* `ip_protocol` - IP protocol of this forwarding rule.
49+
50+
* `ports` - List of ports to use for internal load balancing, if this forwarding rule has any.
51+
52+
* `port_range` - Port range, if this forwarding rule has one.
53+
54+
* `target` - URL of the target pool, if this forwarding rule has one.
55+
56+
* `backend_service` - Backend service, if this forwarding rule has one.
57+
58+
* `load_balancing_scheme` - Type of load balancing of this forwarding rule.
59+
60+
* `region` - Region of this forwarding rule.
61+
62+
* `self_link` - The URI of the resource.

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<li<%= sidebar_current("docs-google-datasource-compute-image") %>>
2929
<a href="/docs/providers/google/d/datasource_compute_image.html">google_compute_image</a>
3030
</li>
31+
<li<%= sidebar_current("docs-google-datasource-compute-forwarding-rule") %>>
32+
<a href="/docs/providers/google/d/datasource_compute_forwarding_rule.html">google_compute_forwarding_rule</a>
33+
</li>
3134
<li<%= sidebar_current("docs-google-datasource-compute-global-address") %>>
3235
<a href="/docs/providers/google/d/datasource_compute_global_address.html">google_compute_global_address</a>
3336
</li>

0 commit comments

Comments
 (0)