backend/remote-state/gcs: Add support for the GOOGLE_PROJECT environment variable.

This copies behavior from the Google Cloud provider.
This commit is contained in:
Florian Forster 2017-09-13 15:48:36 +02:00 committed by James Bardin
parent 37dc95158d
commit 9583d0945c
2 changed files with 8 additions and 3 deletions

View File

@ -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()),

View File

@ -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 `<prefix>/<name>.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.