backend/remote-state/gcs: Implement the "region" config option.

This allows to select the region in which a bucket is created.
This copies behavior from the Google Cloud provider.
This commit is contained in:
Florian Forster 2017-09-13 15:52:39 +02:00 committed by James Bardin
parent 9583d0945c
commit 927085289d
2 changed files with 15 additions and 0 deletions

View File

@ -28,6 +28,7 @@ type gcsBackend struct {
defaultStateFile string defaultStateFile string
projectID string projectID string
region string
} }
func New() backend.Backend { func New() backend.Backend {
@ -67,6 +68,13 @@ func New() backend.Backend {
Description: "Google Cloud Project ID", Description: "Google Cloud Project ID",
Default: "", Default: "",
}, },
"region": {
Type: schema.TypeString,
Optional: true,
Description: "Region / location in which to create the bucket",
Default: "",
},
}, },
} }
@ -95,6 +103,10 @@ func (b *gcsBackend) configure(ctx context.Context) error {
if id := os.Getenv("GOOGLE_PROJECT"); b.projectID == "" && id != "" { if id := os.Getenv("GOOGLE_PROJECT"); b.projectID == "" && id != "" {
b.projectID = id b.projectID = id
} }
b.region = data.Get("region").(string)
if r := os.Getenv("GOOGLE_REGION"); b.projectID == "" && r != "" {
b.region = r
}
opts := []option.ClientOption{ opts := []option.ClientOption{
option.WithScopes(storage.ScopeReadWrite), option.WithScopes(storage.ScopeReadWrite),
@ -128,6 +140,7 @@ func (b *gcsBackend) ensureBucketExists() error {
attrs := &storage.BucketAttrs{ attrs := &storage.BucketAttrs{
VersioningEnabled: true, VersioningEnabled: true,
Location: b.region,
} }
return b.storageClient.Bucket(b.bucketName).Create(b.storageContext, b.projectID, attrs) return b.storageClient.Bucket(b.bucketName).Create(b.storageContext, b.projectID, attrs)

View File

@ -57,3 +57,5 @@ The following configuration options are supported:
* `path` - (Deprecated) GCS path to the state file of the default state. For backwards compatibility only, use `prefix` instead. * `path` - (Deprecated) GCS path to the state file of the default state. For backwards compatibility only, use `prefix` instead.
* `project` / `GOOGLE_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. Since buckets have globally unique names, the project ID is not required to access the bucket during normal operation.
* `region` / `GOOGLE_REGION` - (Optional) The region in which a new bucket is created.
For more information, see [Bucket Locations](https://cloud.google.com/storage/docs/bucket-locations).