Skip to content

Commit dca85ba

Browse files
committed
Datasource for Backend Services
Exposes existing `google_compute_backend_service` as data sources. This addresses #149 . This allows, for instance, to collect a backend service's self_link and use it from an other workspace/tfstate, sharing most of the loadbalancers definition. ``` env GOOGLE_REGION=us-central1 make testacc TEST=./google TESTARGS='-run=TestAccDataSourceComputeBackendService_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./google -v -run=TestAccDataSourceComputeBackendService_ -timeout 120m === RUN TestAccDataSourceComputeBackendService_basic === PAUSE TestAccDataSourceComputeBackendService_basic === CONT TestAccDataSourceComputeBackendService_basic --- PASS: TestAccDataSourceComputeBackendService_basic (48.11s) PASS ok github.com/terraform-providers/terraform-provider-google/google 48.123s ```
1 parent e8ffe89 commit dca85ba

5 files changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package google
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/schema"
5+
)
6+
7+
func dataSourceGoogleComputeBackendService() *schema.Resource {
8+
dsSchema := datasourceSchemaFromResourceSchema(resourceComputeBackendService().Schema)
9+
10+
// Set 'Required' schema elements
11+
addRequiredFieldsToSchema(dsSchema, "name")
12+
13+
// Set 'Optional' schema elements
14+
addOptionalFieldsToSchema(dsSchema, "project")
15+
16+
return &schema.Resource{
17+
Read: dataSourceComputeBackendServiceRead,
18+
Schema: dsSchema,
19+
}
20+
}
21+
22+
func dataSourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) error {
23+
serviceName := d.Get("name").(string)
24+
25+
d.SetId(serviceName)
26+
27+
return resourceComputeBackendServiceRead(d, meta)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 TestAccDataSourceComputeBackendService_basic(t *testing.T) {
13+
t.Parallel()
14+
15+
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
16+
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
22+
Steps: []resource.TestStep{
23+
resource.TestStep{
24+
Config: testAccDataSourceComputeBackendService_basic(serviceName, checkName),
25+
Check: testAccDataSourceComputeBackendServiceCheck("data.google_compute_backend_service.baz", "google_compute_backend_service.foobar"),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceComputeBackendServiceCheck(dsName, rsName string) resource.TestCheckFunc {
32+
return func(s *terraform.State) error {
33+
rs, ok := s.RootModule().Resources[rsName]
34+
if !ok {
35+
return fmt.Errorf("can't find resource called %s in state", rsName)
36+
}
37+
38+
ds, ok := s.RootModule().Resources[dsName]
39+
if !ok {
40+
return fmt.Errorf("can't find data source called %s in state", dsName)
41+
}
42+
43+
dsAttr := ds.Primary.Attributes
44+
rsAttr := rs.Primary.Attributes
45+
46+
attrsToTest := []string{
47+
"id",
48+
"name",
49+
"description",
50+
"self_link",
51+
"fingerprint",
52+
"port_name",
53+
"protocol",
54+
}
55+
56+
for _, attrToTest := range attrsToTest {
57+
if dsAttr[attrToTest] != rsAttr[attrToTest] {
58+
return fmt.Errorf("%s is %s; want %s", attrToTest, dsAttr[attrToTest], rsAttr[attrToTest])
59+
}
60+
}
61+
62+
return nil
63+
}
64+
}
65+
66+
func testAccDataSourceComputeBackendService_basic(serviceName, checkName string) string {
67+
return fmt.Sprintf(`
68+
resource "google_compute_backend_service" "foobar" {
69+
name = "%s"
70+
description = "foobar backend service"
71+
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
72+
}
73+
74+
resource "google_compute_http_health_check" "zero" {
75+
name = "%s"
76+
request_path = "/"
77+
check_interval_sec = 1
78+
timeout_sec = 1
79+
}
80+
81+
data "google_compute_backend_service" "baz" {
82+
name = "${google_compute_backend_service.foobar.name}"
83+
}
84+
`, serviceName, checkName)
85+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func Provider() terraform.ResourceProvider {
8686
"google_organization": dataSourceGoogleOrganization(),
8787
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
8888
"google_storage_project_service_account": dataSourceGoogleStorageProjectServiceAccount(),
89+
"google_compute_backend_service": dataSourceGoogleComputeBackendService(),
8990
},
9091

9192
ResourcesMap: map[string]*schema.Resource{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_compute_backend_service"
4+
sidebar_current: "docs-google-datasource-compute-backend-service"
5+
description: |-
6+
Get information about a Backend Service.
7+
---
8+
9+
# google\_compute\_backend\_service
10+
11+
Provide acces to a Backend Service's attribute. For more information
12+
see [the official documentation](https://cloud.google.com/compute/docs/load-balancing/http/backend-service)
13+
and the [API](https://cloud.google.com/compute/docs/reference/latest/backendServices).
14+
15+
## Example Usage
16+
17+
```tf
18+
data "google_compute_backend_service" "baz" {
19+
name = "foobar"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `name` - (Required) The name of the Backend Service.
28+
29+
- - -
30+
31+
* `project` - (Optional) The project in which the resource belongs. If it is not provided, the provider project is used.
32+
33+
## Attributes Reference
34+
35+
In addition to the arguments listed above, the following attributes are exported:
36+
37+
* `connection_draining_timeout_sec` - Time for which instance will be drained (not accept new connections, but still work to finish started ones).
38+
39+
* `description` - Textual description for the Backend Service.
40+
41+
* `enable_cdn` - Whether or not Cloud CDN is enabled on the Backend Service.
42+
43+
* `fingerprint` - The fingerprint of the Backend Service.
44+
45+
* `port_name` - The name of a service that has been added to an instance group in this backend.
46+
47+
* `protocol` - The protocol for incoming requests.
48+
49+
* `self_link` - The URI of the Backend Service.
50+
51+
* `session_affinity` - The Backend Service session stickyness configuration.
52+
53+
* `timeout_sec` - The number of seconds to wait for a backend to respond to a request before considering the request failed.
54+
55+
* `backend` - The list of backends that serve this Backend Service.
56+
57+
* `health_checks` - The list of HTTP/HTTPS health checks used by the Backend Service.

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
<li<%= sidebar_current("docs-google-datasource-container-cluster") %>>
6363
<a href="/docs/providers/google/d/google_container_cluster.html">google_container_cluster</a>
6464
</li>
65+
<li<%= sidebar_current("docs-google-datasource-compute-backend-service") %>>
66+
<a href="/docs/providers/google/d/datasource_google_compute_backend_service.html">google_compute_backend_service</a>
67+
</li>
6568
<li<%= sidebar_current("docs-google-datasource-container-versions") %>>
6669
<a href="/docs/providers/google/d/google_container_engine_versions.html">google_container_engine_versions</a>
6770
</li>

0 commit comments

Comments
 (0)