Skip to content

Commit 60dc8bc

Browse files
cbleckerdanawillow
authored andcommitted
Add google_project_services data source (#1822)
1 parent 3db96c9 commit 60dc8bc

5 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package google
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/schema"
5+
)
6+
7+
func dataSourceGoogleProjectServices() *schema.Resource {
8+
// Generate datasource schema from resource
9+
dsSchema := datasourceSchemaFromResourceSchema(resourceGoogleProjectServices().Schema)
10+
11+
// Set 'Optional' schema elements
12+
addOptionalFieldsToSchema(dsSchema, "project")
13+
14+
return &schema.Resource{
15+
Read: dataSourceGoogleProjectServicesRead,
16+
Schema: dsSchema,
17+
}
18+
}
19+
20+
func dataSourceGoogleProjectServicesRead(d *schema.ResourceData, meta interface{}) error {
21+
config := meta.(*Config)
22+
23+
project, err := getProject(d, config)
24+
if err != nil {
25+
return err
26+
}
27+
d.SetId(project)
28+
29+
return resourceGoogleProjectServicesRead(d, meta)
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 TestAccDataSourceGoogleProjectServices_basic(t *testing.T) {
13+
t.Parallel()
14+
org := getTestOrgFromEnv(t)
15+
project := "terraform-" + acctest.RandString(10)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccCheckGoogleProjectServicesConfig(project, org),
23+
Check: resource.ComposeTestCheckFunc(
24+
testAccDataSourceGoogleProjectServicesCheck("data.google_project_services.project_services", "google_project_services.project_services"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceGoogleProjectServicesCheck(dataSourceName string, resourceName string) resource.TestCheckFunc {
32+
return func(s *terraform.State) error {
33+
ds, ok := s.RootModule().Resources[dataSourceName]
34+
if !ok {
35+
return fmt.Errorf("root module has no resource called %s", dataSourceName)
36+
}
37+
38+
rs, ok := s.RootModule().Resources[resourceName]
39+
if !ok {
40+
return fmt.Errorf("can't find %s in state", resourceName)
41+
}
42+
43+
dsAttr := ds.Primary.Attributes
44+
rsAttr := rs.Primary.Attributes
45+
46+
projectAttrToCheck := []string{
47+
"project",
48+
"services",
49+
}
50+
51+
for _, attr := range projectAttrToCheck {
52+
if dsAttr[attr] != rsAttr[attr] {
53+
return fmt.Errorf(
54+
"%s is %s; want %s",
55+
attr,
56+
dsAttr[attr],
57+
rsAttr[attr],
58+
)
59+
}
60+
}
61+
62+
return nil
63+
}
64+
}
65+
66+
func testAccCheckGoogleProjectServicesConfig(project, org string) string {
67+
return fmt.Sprintf(`
68+
resource "google_project" "project" {
69+
project_id = "%s"
70+
name = "%s"
71+
org_id = "%s"
72+
}
73+
74+
resource "google_project_services" "project_services" {
75+
project = "${google_project.project.id}"
76+
services = ["admin.googleapis.com"]
77+
}
78+
79+
data "google_project_services" "project_services" {
80+
project = "${google_project.project.id}"
81+
}`, project, project, org)
82+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func Provider() terraform.ResourceProvider {
7373
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
7474
"google_compute_network": dataSourceGoogleComputeNetwork(),
7575
"google_project": dataSourceGoogleProject(),
76+
"google_project_services": dataSourceGoogleProjectServices(),
7677
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
7778
"google_compute_zones": dataSourceGoogleComputeZones(),
7879
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_project_services"
4+
sidebar_current: "docs-google-datasource-project-services"
5+
description: |-
6+
Retrieve enabled of API services for a Google Cloud Platform project
7+
---
8+
9+
# google\_project\_services
10+
11+
Use this data source to get details on the enabled project services.
12+
13+
For a list of services available, visit the
14+
[API library page](https://console.cloud.google.com/apis/library) or run `gcloud services list`.
15+
16+
## Example Usage
17+
18+
```hcl
19+
data "google_project_services" "project" {
20+
project = "your-project-id"
21+
}
22+
23+
output "project_services" {
24+
value = "${join(",", data.google_project_services.project.services)}"
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `project` - (Required) The project ID.
33+
34+
35+
## Attributes Reference
36+
37+
The following attributes are exported:
38+
39+
See [google_project_services](https://www.terraform.io/docs/providers/google/r/google_project_services.html) resource for details of the available attributes.

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
<li<%= sidebar_current("docs-google-datasource-compute-region-instance-group") %>>
5656
<a href="/docs/providers/google/d/datasource_compute_region_instance_group.html">google_compute_region_instance_group</a>
5757
</li>
58+
<li<%= sidebar_current("docs-google-datasource-project-services") %>>
59+
<a href="/docs/providers/google/d/google_project_services.html">google_project_services</a>
60+
</li>
5861
<li<%= sidebar_current("docs-google-datasource-compute-regions") %>>
5962
<a href="/docs/providers/google/d/google_compute_regions.html">google_compute_regions</a>
6063
</li>

0 commit comments

Comments
 (0)