github.com/profects/terraform@v0.9.0-beta1.0.20170227135739-92d4809db30d/backend/remote-state/backend.go (about)

     1  // Package remotestate implements a Backend for remote state implementations
     2  // from the state/remote package that also implement a backend schema for
     3  // configuration.
     4  package remotestate
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/hashicorp/terraform/state"
    11  	"github.com/hashicorp/terraform/state/remote"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  // Backend implements backend.Backend for remote state backends.
    16  //
    17  // All exported fields should be set. This struct should only be used
    18  // by implementers of backends, not by consumers. If you're consuming, please
    19  // use a higher level package such as Consul backends.
    20  type Backend struct {
    21  	// Backend should be set to the configuration schema. ConfigureFunc
    22  	// should not be set on the schema.
    23  	*schema.Backend
    24  
    25  	// ConfigureFunc takes the ctx from a schema.Backend and returns a
    26  	// fully configured remote client to use for state operations.
    27  	ConfigureFunc func(ctx context.Context) (remote.Client, error)
    28  
    29  	client remote.Client
    30  }
    31  
    32  func (b *Backend) Configure(rc *terraform.ResourceConfig) error {
    33  	// Set our configureFunc manually
    34  	b.Backend.ConfigureFunc = func(ctx context.Context) error {
    35  		c, err := b.ConfigureFunc(ctx)
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		// Set the client for later
    41  		b.client = c
    42  		return nil
    43  	}
    44  
    45  	// Run the normal configuration
    46  	return b.Backend.Configure(rc)
    47  }
    48  
    49  func (b *Backend) State() (state.State, error) {
    50  	// This shouldn't happen
    51  	if b.client == nil {
    52  		panic("nil remote client")
    53  	}
    54  
    55  	s := &remote.State{Client: b.client}
    56  	return s, nil
    57  }