Skip to content

Commit bb5eb4f

Browse files
committed
Merge pull request hashicorp#3702 from lwander/f-gcp-global-forwarding-rule
provider/google: global forwarding rule tests & documentation
2 parents 7f40abf + d344d3e commit bb5eb4f

5 files changed

Lines changed: 474 additions & 0 deletions

File tree

builtin/providers/google/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func Provider() terraform.ResourceProvider {
4141
"google_compute_firewall": resourceComputeFirewall(),
4242
"google_compute_forwarding_rule": resourceComputeForwardingRule(),
4343
"google_compute_global_address": resourceComputeGlobalAddress(),
44+
"google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(),
4445
"google_compute_http_health_check": resourceComputeHttpHealthCheck(),
4546
"google_compute_instance": resourceComputeInstance(),
4647
"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"google.golang.org/api/compute/v1"
9+
"google.golang.org/api/googleapi"
10+
)
11+
12+
func resourceComputeGlobalForwardingRule() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceComputeGlobalForwardingRuleCreate,
15+
Read: resourceComputeGlobalForwardingRuleRead,
16+
Update: resourceComputeGlobalForwardingRuleUpdate,
17+
Delete: resourceComputeGlobalForwardingRuleDelete,
18+
19+
Schema: map[string]*schema.Schema{
20+
"ip_address": &schema.Schema{
21+
Type: schema.TypeString,
22+
Optional: true,
23+
ForceNew: true,
24+
Computed: true,
25+
},
26+
27+
"ip_protocol": &schema.Schema{
28+
Type: schema.TypeString,
29+
Optional: true,
30+
ForceNew: true,
31+
Computed: true,
32+
},
33+
34+
"description": &schema.Schema{
35+
Type: schema.TypeString,
36+
Optional: true,
37+
ForceNew: true,
38+
},
39+
40+
"name": &schema.Schema{
41+
Type: schema.TypeString,
42+
Required: true,
43+
ForceNew: true,
44+
},
45+
46+
"port_range": &schema.Schema{
47+
Type: schema.TypeString,
48+
Optional: true,
49+
ForceNew: true,
50+
},
51+
52+
"region": &schema.Schema{
53+
Type: schema.TypeString,
54+
Optional: true,
55+
ForceNew: true,
56+
},
57+
58+
"self_link": &schema.Schema{
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
63+
"target": &schema.Schema{
64+
Type: schema.TypeString,
65+
Required: true,
66+
},
67+
},
68+
}
69+
}
70+
71+
func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
72+
config := meta.(*Config)
73+
74+
frule := &compute.ForwardingRule{
75+
IPAddress: d.Get("ip_address").(string),
76+
IPProtocol: d.Get("ip_protocol").(string),
77+
Description: d.Get("description").(string),
78+
Name: d.Get("name").(string),
79+
PortRange: d.Get("port_range").(string),
80+
Target: d.Get("target").(string),
81+
}
82+
83+
op, err := config.clientCompute.GlobalForwardingRules.Insert(
84+
config.Project, frule).Do()
85+
if err != nil {
86+
return fmt.Errorf("Error creating Global Forwarding Rule: %s", err)
87+
}
88+
89+
// It probably maybe worked, so store the ID now
90+
d.SetId(frule.Name)
91+
92+
err = computeOperationWaitGlobal(config, op, "Creating Global Fowarding Rule")
93+
if err != nil {
94+
return err
95+
}
96+
97+
return resourceComputeGlobalForwardingRuleRead(d, meta)
98+
}
99+
100+
func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
101+
config := meta.(*Config)
102+
103+
d.Partial(true)
104+
105+
if d.HasChange("target") {
106+
target_name := d.Get("target").(string)
107+
target_ref := &compute.TargetReference{Target: target_name}
108+
op, err := config.clientCompute.GlobalForwardingRules.SetTarget(
109+
config.Project, d.Id(), target_ref).Do()
110+
if err != nil {
111+
return fmt.Errorf("Error updating target: %s", err)
112+
}
113+
114+
err = computeOperationWaitGlobal(config, op, "Updating Global Forwarding Rule")
115+
if err != nil {
116+
return err
117+
}
118+
119+
d.SetPartial("target")
120+
}
121+
122+
d.Partial(false)
123+
124+
return resourceComputeGlobalForwardingRuleRead(d, meta)
125+
}
126+
127+
func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
128+
config := meta.(*Config)
129+
130+
frule, err := config.clientCompute.GlobalForwardingRules.Get(
131+
config.Project, d.Id()).Do()
132+
if err != nil {
133+
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
134+
// The resource doesn't exist anymore
135+
d.SetId("")
136+
137+
return nil
138+
}
139+
140+
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
141+
}
142+
143+
d.Set("ip_address", frule.IPAddress)
144+
d.Set("ip_protocol", frule.IPProtocol)
145+
d.Set("self_link", frule.SelfLink)
146+
147+
return nil
148+
}
149+
150+
func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
151+
config := meta.(*Config)
152+
153+
// Delete the GlobalForwardingRule
154+
log.Printf("[DEBUG] GlobalForwardingRule delete request")
155+
op, err := config.clientCompute.GlobalForwardingRules.Delete(
156+
config.Project, d.Id()).Do()
157+
if err != nil {
158+
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
159+
}
160+
161+
err = computeOperationWaitGlobal(config, op, "Deleting GlobalForwarding Rule")
162+
if err != nil {
163+
return err
164+
}
165+
166+
d.SetId("")
167+
return nil
168+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccComputeGlobalForwardingRule_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
16+
Steps: []resource.TestStep{
17+
resource.TestStep{
18+
Config: testAccComputeGlobalForwardingRule_basic1,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckComputeGlobalForwardingRuleExists(
21+
"google_compute_global_forwarding_rule.foobar"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func TestAccComputeGlobalForwardingRule_update(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
33+
Steps: []resource.TestStep{
34+
resource.TestStep{
35+
Config: testAccComputeGlobalForwardingRule_basic1,
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccCheckComputeGlobalForwardingRuleExists(
38+
"google_compute_global_forwarding_rule.foobar"),
39+
),
40+
},
41+
42+
resource.TestStep{
43+
Config: testAccComputeGlobalForwardingRule_basic2,
44+
Check: resource.ComposeTestCheckFunc(
45+
testAccCheckComputeGlobalForwardingRuleExists(
46+
"google_compute_global_forwarding_rule.foobar"),
47+
),
48+
},
49+
},
50+
})
51+
}
52+
53+
func testAccCheckComputeGlobalForwardingRuleDestroy(s *terraform.State) error {
54+
config := testAccProvider.Meta().(*Config)
55+
56+
for _, rs := range s.RootModule().Resources {
57+
if rs.Type != "google_compute_global_forwarding_rule" {
58+
continue
59+
}
60+
61+
_, err := config.clientCompute.GlobalForwardingRules.Get(
62+
config.Project, rs.Primary.ID).Do()
63+
if err == nil {
64+
return fmt.Errorf("Global Forwarding Rule still exists")
65+
}
66+
}
67+
68+
return nil
69+
}
70+
71+
func testAccCheckComputeGlobalForwardingRuleExists(n string) resource.TestCheckFunc {
72+
return func(s *terraform.State) error {
73+
rs, ok := s.RootModule().Resources[n]
74+
if !ok {
75+
return fmt.Errorf("Not found: %s", n)
76+
}
77+
78+
if rs.Primary.ID == "" {
79+
return fmt.Errorf("No ID is set")
80+
}
81+
82+
config := testAccProvider.Meta().(*Config)
83+
84+
found, err := config.clientCompute.GlobalForwardingRules.Get(
85+
config.Project, rs.Primary.ID).Do()
86+
if err != nil {
87+
return err
88+
}
89+
90+
if found.Name != rs.Primary.ID {
91+
return fmt.Errorf("Global Forwarding Rule not found")
92+
}
93+
94+
return nil
95+
}
96+
}
97+
98+
const testAccComputeGlobalForwardingRule_basic1 = `
99+
resource "google_compute_global_forwarding_rule" "foobar" {
100+
description = "Resource created for Terraform acceptance testing"
101+
ip_protocol = "TCP"
102+
name = "terraform-test"
103+
port_range = "80"
104+
target = "${google_compute_target_http_proxy.foobar1.self_link}"
105+
}
106+
107+
resource "google_compute_target_http_proxy" "foobar1" {
108+
description = "Resource created for Terraform acceptance testing"
109+
name = "terraform-test1"
110+
url_map = "${google_compute_url_map.foobar.self_link}"
111+
}
112+
113+
resource "google_compute_target_http_proxy" "foobar2" {
114+
description = "Resource created for Terraform acceptance testing"
115+
name = "terraform-test2"
116+
url_map = "${google_compute_url_map.foobar.self_link}"
117+
}
118+
119+
resource "google_compute_backend_service" "foobar" {
120+
name = "service"
121+
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
122+
}
123+
124+
resource "google_compute_http_health_check" "zero" {
125+
name = "tf-test-zero"
126+
request_path = "/"
127+
check_interval_sec = 1
128+
timeout_sec = 1
129+
}
130+
131+
resource "google_compute_url_map" "foobar" {
132+
name = "myurlmap"
133+
default_service = "${google_compute_backend_service.foobar.self_link}"
134+
host_rule {
135+
hosts = ["mysite.com", "myothersite.com"]
136+
path_matcher = "boop"
137+
}
138+
path_matcher {
139+
default_service = "${google_compute_backend_service.foobar.self_link}"
140+
name = "boop"
141+
path_rule {
142+
paths = ["/*"]
143+
service = "${google_compute_backend_service.foobar.self_link}"
144+
}
145+
}
146+
test {
147+
host = "mysite.com"
148+
path = "/*"
149+
service = "${google_compute_backend_service.foobar.self_link}"
150+
}
151+
}
152+
`
153+
154+
const testAccComputeGlobalForwardingRule_basic2 = `
155+
resource "google_compute_global_forwarding_rule" "foobar" {
156+
description = "Resource created for Terraform acceptance testing"
157+
ip_protocol = "TCP"
158+
name = "terraform-test"
159+
port_range = "80"
160+
target = "${google_compute_target_http_proxy.foobar2.self_link}"
161+
}
162+
163+
resource "google_compute_target_http_proxy" "foobar1" {
164+
description = "Resource created for Terraform acceptance testing"
165+
name = "terraform-test1"
166+
url_map = "${google_compute_url_map.foobar.self_link}"
167+
}
168+
169+
resource "google_compute_target_http_proxy" "foobar2" {
170+
description = "Resource created for Terraform acceptance testing"
171+
name = "terraform-test2"
172+
url_map = "${google_compute_url_map.foobar.self_link}"
173+
}
174+
175+
resource "google_compute_backend_service" "foobar" {
176+
name = "service"
177+
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
178+
}
179+
180+
resource "google_compute_http_health_check" "zero" {
181+
name = "tf-test-zero"
182+
request_path = "/"
183+
check_interval_sec = 1
184+
timeout_sec = 1
185+
}
186+
187+
resource "google_compute_url_map" "foobar" {
188+
name = "myurlmap"
189+
default_service = "${google_compute_backend_service.foobar.self_link}"
190+
host_rule {
191+
hosts = ["mysite.com", "myothersite.com"]
192+
path_matcher = "boop"
193+
}
194+
path_matcher {
195+
default_service = "${google_compute_backend_service.foobar.self_link}"
196+
name = "boop"
197+
path_rule {
198+
paths = ["/*"]
199+
service = "${google_compute_backend_service.foobar.self_link}"
200+
}
201+
}
202+
test {
203+
host = "mysite.com"
204+
path = "/*"
205+
service = "${google_compute_backend_service.foobar.self_link}"
206+
}
207+
}
208+
`

0 commit comments

Comments
 (0)