Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add App Engine Default Service Account datasource #4049

Merged
merged 8 commits into from
Oct 8, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleAppEngineDefaultServiceAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleAppEngineDefaultServiceAccountRead,
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"unique_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGoogleAppEngineDefaultServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
return err
}

serviceAccountEmail := fmt.Sprintf("%[email protected]", project)

serviceAccountName, err := serviceAccountFQN(serviceAccountEmail, d, config)
if err != nil {
return err
}

sa, err := config.NewIamClient(userAgent).Projects.ServiceAccounts.Get(serviceAccountName).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", serviceAccountName))
}

d.SetId(sa.Name)
if err := d.Set("email", sa.Email); err != nil {
return fmt.Errorf("Error setting email: %s", err)
}
if err := d.Set("unique_id", sa.UniqueId); err != nil {
return fmt.Errorf("Error setting unique_id: %s", err)
}
if err := d.Set("project", sa.ProjectId); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("name", sa.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
if err := d.Set("display_name", sa.DisplayName); err != nil {
return fmt.Errorf("Error setting display_name: %s", err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package google

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGoogleAppEngineDefaultServiceAccount_basic(t *testing.T) {
t.Parallel()

resourceName := "data.google_app_engine_default_service_account.default"

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleAppEngineDefaultServiceAccount_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "email"),
resource.TestCheckResourceAttrSet(resourceName, "unique_id"),
resource.TestCheckResourceAttrSet(resourceName, "name"),
resource.TestCheckResourceAttrSet(resourceName, "display_name"),
),
},
},
})
}

const testAccCheckGoogleAppEngineDefaultServiceAccount_basic = `
data "google_app_engine_default_service_account" "default" {}
`
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func TestAccDataSourceGoogleComputeDefaultServiceAccount_basic(t *testing.T) {
}

const testAccCheckGoogleComputeDefaultServiceAccount_basic = `
data "google_compute_default_service_account" "default" { }
data "google_compute_default_service_account" "default" {}
`
1 change: 1 addition & 0 deletions third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func Provider() *schema.Provider {

DataSourcesMap: map[string]*schema.Resource{
"google_active_folder": dataSourceGoogleActiveFolder(),
"google_app_engine_default_service_account": dataSourceGoogleAppEngineDefaultServiceAccount(),
"google_billing_account": dataSourceGoogleBillingAccount(),
"google_bigquery_default_service_account": dataSourceGoogleBigqueryDefaultServiceAccount(),
"google_client_config": dataSourceGoogleClientConfig(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "App Engine"
layout: "google"
page_title: "Google: google_app_engine_default_service_account"
sidebar_current: "docs-google-datasource-app_engine-default-service-account"
description: |-
Retrieve the default App Engine service account used in this project
---

# google\_app_engine\_default\_service\_account

Use this data source to retrieve the default App Engine service account for the specified project.

## Example Usage

```hcl
data "google_app_engine_default_service_account" "default" {
}

output "default_account" {
value = data.google_app_engine_default_service_account.default.email
}
```

## Argument Reference

The following arguments are supported:

* `project` - (Optional) The project ID. If it is not provided, the provider project is used.


## Attributes Reference

The following attributes are exported:

* `email` - Email address of the default service account used by App Engine in this project.

* `unique_id` - The unique id of the service account.

* `name` - The fully-qualified name of the service account.

* `display_name` - The display name for the service account.