Skip to content

Commit 1262045

Browse files
committed
Datasource for retrieving GCS service account
1 parent ca6c390 commit 1262045

5 files changed

+139
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"google.golang.org/api/googleapi"
9+
)
10+
11+
func dataSourceGoogleStorageProjectServiceAccount() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGoogleStorageProjectServiceAccountRead,
14+
Schema: map[string]*schema.Schema{
15+
"email_address": {
16+
Type: schema.TypeString,
17+
Computed: true,
18+
},
19+
},
20+
}
21+
}
22+
23+
func dataSourceGoogleStorageProjectServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
24+
config := meta.(*Config)
25+
26+
project, err := getProject(d, config)
27+
if err != nil {
28+
return err
29+
}
30+
31+
serviceAccount, err := config.clientStorage.Projects.ServiceAccount.Get(project).Do()
32+
if err != nil {
33+
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound {
34+
return fmt.Errorf("GCS service account not found")
35+
}
36+
37+
return fmt.Errorf("Error reading GCS service account: %s", err)
38+
}
39+
40+
d.SetId(serviceAccount.EmailAddress)
41+
d.Set("email_address", serviceAccount.EmailAddress)
42+
43+
return nil
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGoogleStorageProjectServiceAccount_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
resourceName := "data.google_storage_project_service_account.gcs_account"
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccCheckGoogleStorageProjectServiceAccount_basic,
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttrSet(resourceName, "email_address"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccCheckGoogleStorageProjectServiceAccount_basic = `
29+
data "google_storage_project_service_account" "gcs_account" { }
30+
`

google/provider.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,30 @@ func Provider() terraform.ResourceProvider {
6060
},
6161

6262
DataSourcesMap: map[string]*schema.Resource{
63-
"google_active_folder": dataSourceGoogleActiveFolder(),
64-
"google_billing_account": dataSourceGoogleBillingAccount(),
65-
"google_dns_managed_zone": dataSourceDnsManagedZone(),
66-
"google_client_config": dataSourceGoogleClientConfig(),
67-
"google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(),
68-
"google_compute_address": dataSourceGoogleComputeAddress(),
69-
"google_compute_image": dataSourceGoogleComputeImage(),
70-
"google_compute_global_address": dataSourceGoogleComputeGlobalAddress(),
71-
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
72-
"google_compute_network": dataSourceGoogleComputeNetwork(),
73-
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
74-
"google_compute_zones": dataSourceGoogleComputeZones(),
75-
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
76-
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
77-
"google_compute_vpn_gateway": dataSourceGoogleComputeVpnGateway(),
78-
"google_container_cluster": dataSourceGoogleContainerCluster(),
79-
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
80-
"google_container_registry_repository": dataSourceGoogleContainerRepo(),
81-
"google_container_registry_image": dataSourceGoogleContainerImage(),
82-
"google_iam_policy": dataSourceGoogleIamPolicy(),
83-
"google_kms_secret": dataSourceGoogleKmsSecret(),
84-
"google_organization": dataSourceGoogleOrganization(),
85-
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
63+
"google_active_folder": dataSourceGoogleActiveFolder(),
64+
"google_billing_account": dataSourceGoogleBillingAccount(),
65+
"google_dns_managed_zone": dataSourceDnsManagedZone(),
66+
"google_client_config": dataSourceGoogleClientConfig(),
67+
"google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(),
68+
"google_compute_address": dataSourceGoogleComputeAddress(),
69+
"google_compute_image": dataSourceGoogleComputeImage(),
70+
"google_compute_global_address": dataSourceGoogleComputeGlobalAddress(),
71+
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
72+
"google_compute_network": dataSourceGoogleComputeNetwork(),
73+
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
74+
"google_compute_zones": dataSourceGoogleComputeZones(),
75+
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
76+
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
77+
"google_compute_vpn_gateway": dataSourceGoogleComputeVpnGateway(),
78+
"google_container_cluster": dataSourceGoogleContainerCluster(),
79+
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
80+
"google_container_registry_repository": dataSourceGoogleContainerRepo(),
81+
"google_container_registry_image": dataSourceGoogleContainerImage(),
82+
"google_iam_policy": dataSourceGoogleIamPolicy(),
83+
"google_kms_secret": dataSourceGoogleKmsSecret(),
84+
"google_organization": dataSourceGoogleOrganization(),
85+
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
86+
"google_storage_project_service_account": dataSourceGoogleStorageProjectServiceAccount(),
8687
},
8788

8889
ResourcesMap: map[string]*schema.Resource{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_storage_project_service_account"
4+
sidebar_current: "docs-google-datasource-storage-project-service-account"
5+
description: |-
6+
Get the email address of the project's Google Cloud Storage service account
7+
---
8+
9+
# google\_client\_config
10+
11+
Use this data source to get the email address of the project's Google Cloud Storage service account.
12+
For more information see
13+
[API](https://cloud.google.com/storage/docs/json_api/v1/projects/serviceAccount).
14+
15+
## Example Usage
16+
17+
```hcl
18+
data "google_storage_project_service_account" "gcs_account" {}
19+
20+
resource "google_pubsub_topic_iam_binding" "binding" {
21+
topic = "${google_pubsub_topic.topic.name}"
22+
role = "roles/pubsub.publisher"
23+
24+
members = ["${data.google_storage_project_service_account.gcs_account.id}"]
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
There are no arguments available for this data source.
31+
32+
## Attributes Reference
33+
34+
In addition to the arguments listed above, the following attributes are exported:
35+
36+
* `id` - The ID of the service account, which is its email_address
37+
38+
* `email_address` - The email_address for this account

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
<li<%= sidebar_current("docs-google-datasource-signed_url") %>>
8383
<a href="/docs/providers/google/d/signed_url.html">google_storage_object_signed_url</a>
8484
</li>
85+
<li<%= sidebar_current("docs-google-datasource-storage-project-service-account") %>>
86+
<a href="/docs/providers/google/d/google_storage_project_service_account.html">google_storage_project_service_account</a>
87+
</li>
8588
</ul>
8689
</li>
8790

0 commit comments

Comments
 (0)