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 #7472

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4049.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_app_engine_default_service_account`
```
80 changes: 80 additions & 0 deletions google/data_source_google_app_engine_default_service_account.go
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 google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,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
42 changes: 42 additions & 0 deletions website/docs/d/app_engine_default_service_account.html.markdown
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.
10 changes: 10 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
<li>
<a href="#">App Engine</a>
<ul class="nav">
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">

<li>
<a href="/docs/providers/google/d/app_engine_default_service_account.html">google_app_engine_default_service_account</a>
</li>

</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="nav nav-auto-expand">
Expand Down