From 9583d0945c627b008eca51d10ce3c02a08ed8205 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 13 Sep 2017 15:48:36 +0200 Subject: [PATCH] backend/remote-state/gcs: Add support for the GOOGLE_PROJECT environment variable. This copies behavior from the Google Cloud provider. --- backend/remote-state/gcs/backend.go | 9 +++++++-- website/docs/backends/types/gcs.html.md | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/remote-state/gcs/backend.go b/backend/remote-state/gcs/backend.go index 2a792ea49d..d32a166e86 100644 --- a/backend/remote-state/gcs/backend.go +++ b/backend/remote-state/gcs/backend.go @@ -23,10 +23,11 @@ type gcsBackend struct { storageClient *storage.Client storageContext context.Context - projectID string bucketName string prefix string defaultStateFile string + + projectID string } func New() backend.Backend { @@ -85,12 +86,16 @@ func (b *gcsBackend) configure(ctx context.Context) error { data := schema.FromContextBackendConfig(b.storageContext) - b.projectID = data.Get("project").(string) b.bucketName = data.Get("bucket").(string) b.prefix = strings.TrimLeft(data.Get("prefix").(string), "/") b.defaultStateFile = strings.TrimLeft(data.Get("path").(string), "/") + b.projectID = data.Get("project").(string) + if id := os.Getenv("GOOGLE_PROJECT"); b.projectID == "" && id != "" { + b.projectID = id + } + opts := []option.ClientOption{ option.WithScopes(storage.ScopeReadWrite), option.WithUserAgent(terraform.UserAgentString()), diff --git a/website/docs/backends/types/gcs.html.md b/website/docs/backends/types/gcs.html.md index f1afa3bc4d..5f55a1f8e9 100644 --- a/website/docs/backends/types/gcs.html.md +++ b/website/docs/backends/types/gcs.html.md @@ -55,5 +55,5 @@ The following configuration options are supported: The provided credentials need to have the `devstorage.read_write` scope and `WRITER` permissions on the bucket. * `prefix` - (Optional) GCS prefix inside the bucket. Named states are stored in an object called `/.tfstate`. * `path` - (Deprecated) GCS path to the state file of the default state. For backwards compatibility only, use `prefix` instead. - * `project` - (Optional) The project ID to which the bucket belongs. This is only used when creating a new bucket during initialization. + * `project` / `GOOGLE_PROJECT` - (Optional) The project ID to which the bucket belongs. This is only used when creating a new bucket during initialization. Since buckets have globally unique names, the project ID is not required to access the bucket during normal operation.