github.com/Kevinklinger/open_terraform@v0.11.12-beta1/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/backend"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  	"github.com/hashicorp/terraform/state"
    12  	"github.com/hashicorp/terraform/state/remote"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  // Backend implements backend.Backend for remote state backends.
    17  //
    18  // All exported fields should be set. This struct should only be used
    19  // by implementers of backends, not by consumers. If you're consuming, please
    20  // use a higher level package such as Consul backends.
    21  type Backend struct {
    22  	// Backend should be set to the configuration schema. ConfigureFunc
    23  	// should not be set on the schema.
    24  	*schema.Backend
    25  
    26  	// ConfigureFunc takes the ctx from a schema.Backend and returns a
    27  	// fully configured remote client to use for state operations.
    28  	ConfigureFunc func(ctx context.Context) (remote.Client, error)
    29  
    30  	client remote.Client
    31  }
    32  
    33  func (b *Backend) Configure(rc *terraform.ResourceConfig) error {
    34  	// Set our configureFunc manually
    35  	b.Backend.ConfigureFunc = func(ctx context.Context) error {
    36  		c, err := b.ConfigureFunc(ctx)
    37  		if err != nil {
    38  			return err
    39  		}
    40  
    41  		// Set the client for later
    42  		b.client = c
    43  		return nil
    44  	}
    45  
    46  	// Run the normal configuration
    47  	return b.Backend.Configure(rc)
    48  }
    49  
    50  func (b *Backend) States() ([]string, error) {
    51  	return nil, backend.ErrNamedStatesNotSupported
    52  }
    53  
    54  func (b *Backend) DeleteState(name string) error {
    55  	return backend.ErrNamedStatesNotSupported
    56  }
    57  
    58  func (b *Backend) State(name string) (state.State, error) {
    59  	// This shouldn't happen
    60  	if b.client == nil {
    61  		panic("nil remote client")
    62  	}
    63  
    64  	if name != backend.DefaultStateName {
    65  		return nil, backend.ErrNamedStatesNotSupported
    66  	}
    67  
    68  	s := &remote.State{Client: b.client}
    69  	return s, nil
    70  }