mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-06 22:23:43 -06:00
backend/remote-state/gcs: Automatically create the bucket if needed.
This resurrects the previously documented but unused "project" option. This option is required to create buckets (so they are associated with the right cloud project) but not to access the buckets later on (because their names are globally unique).
This commit is contained in:
parent
14263223e7
commit
df386d3133
@ -23,6 +23,7 @@ type gcsBackend struct {
|
||||
storageClient *storage.Client
|
||||
storageContext context.Context
|
||||
|
||||
projectID string
|
||||
bucketName string
|
||||
prefix string
|
||||
defaultStateFile string
|
||||
@ -58,6 +59,13 @@ func New() backend.Backend {
|
||||
Description: "Google Cloud JSON Account Key",
|
||||
Default: "",
|
||||
},
|
||||
|
||||
"project": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "Google Cloud Project ID",
|
||||
Default: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -77,6 +85,7 @@ 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), "/")
|
||||
|
||||
@ -99,5 +108,18 @@ func (b *gcsBackend) configure(ctx context.Context) error {
|
||||
|
||||
b.storageClient = client
|
||||
|
||||
return nil
|
||||
return b.ensureBucketExists()
|
||||
}
|
||||
|
||||
func (b *gcsBackend) ensureBucketExists() error {
|
||||
_, err := b.storageClient.Bucket(b.bucketName).Attrs(b.storageContext)
|
||||
if err != storage.ErrBucketNotExist {
|
||||
return err
|
||||
}
|
||||
|
||||
if b.projectID == "" {
|
||||
return fmt.Errorf("bucket %q does not exist; specify the \"project\" option or create the bucket manually using `gsutil mb gs://%s`", b.bucketName, b.bucketName)
|
||||
}
|
||||
|
||||
return b.storageClient.Bucket(b.bucketName).Create(b.storageContext, b.projectID, nil)
|
||||
}
|
||||
|
@ -52,3 +52,5 @@ The following configuration options are supported:
|
||||
If unset, [Google Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) are used.
|
||||
* `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.
|
||||
Since buckets have globally unique names, the project ID is not required to access the bucket during normal operation.
|
||||
|
Loading…
Reference in New Issue
Block a user