Skip to content

Retrieve current OAuth access token from google_client_config data source #1277

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

Merged
merged 2 commits into from
Apr 3, 2018
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
4 changes: 4 additions & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Config struct {
client *http.Client
userAgent string

tokenSource oauth2.TokenSource

clientBilling *cloudbilling.Service
clientCompute *compute.Service
clientComputeBeta *computeBeta.Service
Expand Down Expand Up @@ -135,6 +137,8 @@ func (c *Config) loadAndValidate() error {
}
}

c.tokenSource = tokenSource

client.Transport = logging.NewTransport("Google", client.Transport)

versionString := terraform.VersionString()
Expand Down
12 changes: 12 additions & 0 deletions google/data_source_google_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func dataSourceGoogleClientConfig() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"access_token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
Expand All @@ -30,5 +36,11 @@ func dataSourceClientConfigRead(d *schema.ResourceData, meta interface{}) error
d.Set("project", config.Project)
d.Set("region", config.Region)

token, err := config.tokenSource.Token()
if err != nil {
return err
}
d.Set("access_token", token.AccessToken)

return nil
}
1 change: 1 addition & 0 deletions google/data_source_google_client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestAccDataSourceGoogleClientConfig_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "project"),
resource.TestCheckResourceAttrSet(resourceName, "region"),
resource.TestCheckResourceAttrSet(resourceName, "access_token"),
),
},
},
Expand Down
21 changes: 21 additions & 0 deletions website/docs/d/datasource_client_config.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ output "project" {
}
```

## Example Usage: Configure Kubernetes provider with OAuth2 access token

```tf
data "google_client_config" "default" {}

data "google_container_cluster" "my_cluster" {
name = "my-cluster"
zone = "us-east1-a"
}

provider "kubernetes" {
load_config_file = false

host = "https://${google_container_cluster.my_cluster.endpoint}"
token = "${data.google_client_config.default.access_token}"
cluster_ca_certificate = "${base64decode(google_container_cluster.my_cluster.master_auth.0.cluster_ca_certificate)}"
}
```

## Argument Reference

There are no arguments available for this data source.
Expand All @@ -31,3 +50,5 @@ In addition to the arguments listed above, the following attributes are exported
* `project` - The ID of the project to apply any resources to.

* `region` - The region to operate under.

* `access_token` - The OAuth2 access token used by the client to authenticate against the Google Cloud API.