Skip to content

Commit 2efa710

Browse files
ishashchukrosbo
authored andcommitted
Datasource for GCE service account (#1119)
* Data source for retrieving GCE service account * Added project id for service account lookups (#15) * Renaming attribute project_id -> project
1 parent c33b33e commit 2efa710

5 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package google
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/schema"
5+
)
6+
7+
func dataSourceGoogleComputeDefaultServiceAccount() *schema.Resource {
8+
return &schema.Resource{
9+
Read: dataSourceGoogleComputeDefaultServiceAccountRead,
10+
Schema: map[string]*schema.Schema{
11+
"email": {
12+
Type: schema.TypeString,
13+
Optional: true,
14+
},
15+
"project": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
},
19+
},
20+
}
21+
}
22+
23+
func dataSourceGoogleComputeDefaultServiceAccountRead(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+
projectCompResource, err := config.clientCompute.Projects.Get(project).Do()
32+
if err != nil {
33+
return handleNotFoundError(err, d, "GCE service account not found")
34+
}
35+
36+
d.SetId(projectCompResource.DefaultServiceAccount)
37+
d.Set("email", projectCompResource.DefaultServiceAccount)
38+
d.Set("project", project)
39+
return nil
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGoogleComputeDefaultServiceAccount_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
resourceName := "data.google_compute_default_service_account.default"
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccCheckGoogleComputeDefaultServiceAccount_basic,
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttrSet(resourceName, "id"),
22+
resource.TestCheckResourceAttrSet(resourceName, "email"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccCheckGoogleComputeDefaultServiceAccount_basic = `
30+
data "google_compute_default_service_account" "default" { }
31+
`

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func Provider() terraform.ResourceProvider {
6666
"google_client_config": dataSourceGoogleClientConfig(),
6767
"google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(),
6868
"google_compute_address": dataSourceGoogleComputeAddress(),
69+
"google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(),
6970
"google_compute_image": dataSourceGoogleComputeImage(),
7071
"google_compute_global_address": dataSourceGoogleComputeGlobalAddress(),
7172
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_compute_default_service_account"
4+
sidebar_current: "docs-google-datasource-compute-default-service-account"
5+
description: |-
6+
Retrieve default service account used by VMs running in this project
7+
---
8+
9+
# google\_compute\_default\_service\_account
10+
11+
Use this data source to retrieve default service account for this project
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "google_compute_default_service_account" "default" { }
17+
18+
output "default_account" {
19+
value = "${google_compute_default_service_account.default.email}"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `project` - (Optional) The project ID. If it is not provided, the provider project is used.
28+
29+
30+
## Attributes Reference
31+
32+
The following attributes are exported:
33+
34+
* `email` - Email address of the default service account used by VMs running in this project

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<li<%= sidebar_current("docs-google-datasource-compute-address") %>>
2626
<a href="/docs/providers/google/d/datasource_compute_address.html">google_compute_address</a>
2727
</li>
28+
<li<%= sidebar_current("docs-google-datasource-compute-default-service-account") %>>
29+
<a href="/docs/providers/google/d/google_compute_default_service_account.html">google_compute_default_service_account</a>
30+
</li>
2831
<li<%= sidebar_current("docs-google-datasource-compute-image") %>>
2932
<a href="/docs/providers/google/d/datasource_compute_image.html">google_compute_image</a>
3033
</li>

0 commit comments

Comments
 (0)