2017-09-08 13:50:07 +02:00
|
|
|
// Package gcloud implements remote storage of state on Google Cloud Storage (GCS).
|
2017-07-19 11:07:24 +02:00
|
|
|
package gcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-09-08 09:25:20 +02:00
|
|
|
"strings"
|
2017-09-07 13:10:56 +02:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2017-07-19 11:07:24 +02:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
googleContext "golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
2017-09-08 13:50:07 +02:00
|
|
|
// gcsBackend implements "backend".Backend for GCS.
|
|
|
|
// Input(), Validate() and Configure() are implemented by embedding *schema.Backend.
|
|
|
|
// State(), DeleteState() and States() are implemented explicitly.
|
|
|
|
type gcsBackend struct {
|
2017-09-07 10:08:39 +02:00
|
|
|
*schema.Backend
|
|
|
|
|
|
|
|
storageClient *storage.Client
|
|
|
|
storageContext googleContext.Context
|
|
|
|
|
|
|
|
bucketName string
|
|
|
|
stateDir string
|
|
|
|
}
|
|
|
|
|
2017-07-19 11:07:24 +02:00
|
|
|
func New() backend.Backend {
|
2017-09-08 13:50:07 +02:00
|
|
|
be := &gcsBackend{}
|
2017-09-07 13:54:12 +02:00
|
|
|
be.Backend = &schema.Backend{
|
|
|
|
ConfigureFunc: be.configure,
|
2017-07-19 11:07:24 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"bucket": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "The name of the Google Cloud Storage bucket",
|
|
|
|
},
|
|
|
|
|
|
|
|
"state_dir": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The directory where state files will be saved inside the bucket",
|
|
|
|
},
|
|
|
|
|
|
|
|
"credentials": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Google Cloud JSON Account Key",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-07 13:54:12 +02:00
|
|
|
return be
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:50:07 +02:00
|
|
|
func (b *gcsBackend) configure(ctx context.Context) error {
|
2017-07-19 11:07:24 +02:00
|
|
|
if b.storageClient != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 14:09:14 +02:00
|
|
|
// ctx is a background context with the backend config added.
|
2017-09-08 13:36:40 +02:00
|
|
|
// Since no context is passed to remoteClient.Get(), .Lock(), etc. but
|
2017-09-07 14:09:14 +02:00
|
|
|
// one is required for calling the GCP API, we're holding on to this
|
|
|
|
// context here and re-use it later.
|
|
|
|
b.storageContext = ctx
|
|
|
|
|
|
|
|
data := schema.FromContextBackendConfig(b.storageContext)
|
2017-07-19 11:07:24 +02:00
|
|
|
|
|
|
|
b.bucketName = data.Get("bucket").(string)
|
2017-09-08 09:25:20 +02:00
|
|
|
b.stateDir = strings.TrimLeft(data.Get("state_dir").(string), "/")
|
2017-07-19 11:07:24 +02:00
|
|
|
|
|
|
|
var tokenSource oauth2.TokenSource
|
|
|
|
|
2017-09-07 13:54:12 +02:00
|
|
|
if credentials := data.Get("credentials").(string); credentials != "" {
|
2017-07-19 11:07:24 +02:00
|
|
|
credentialsJson, _, err := pathorcontents.Read(data.Get("credentials").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading credentials: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-07 13:55:49 +02:00
|
|
|
jwtConfig, err := google.JWTConfigFromJSON([]byte(credentialsJson), storage.ScopeReadWrite)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get Google OAuth2 token: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenSource = jwtConfig.TokenSource(b.storageContext)
|
|
|
|
} else {
|
2017-09-07 13:54:12 +02:00
|
|
|
var err error
|
|
|
|
tokenSource, err = google.DefaultTokenSource(b.storageContext, storage.ScopeReadWrite)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get Google Application Default Credentials: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := storage.NewClient(b.storageContext, option.WithTokenSource(tokenSource))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create Google Storage client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.storageClient = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|