github.com/jnehlt/terraform@v0.11.12-beta1/backend/legacy/backend.go (about)

     1  package legacy
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/backend"
     7  	"github.com/hashicorp/terraform/state"
     8  	"github.com/hashicorp/terraform/state/remote"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/mitchellh/mapstructure"
    11  )
    12  
    13  // Backend is an implementation of backend.Backend for legacy remote state
    14  // clients.
    15  type Backend struct {
    16  	// Type is the type of remote state client to support
    17  	Type string
    18  
    19  	// client is set after Configure is called and client is initialized.
    20  	client remote.Client
    21  }
    22  
    23  func (b *Backend) Input(
    24  	ui terraform.UIInput, c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
    25  	// Return the config as-is, legacy doesn't support input
    26  	return c, nil
    27  }
    28  
    29  func (b *Backend) Validate(*terraform.ResourceConfig) ([]string, []error) {
    30  	// No validation was supported for old clients
    31  	return nil, nil
    32  }
    33  
    34  func (b *Backend) Configure(c *terraform.ResourceConfig) error {
    35  	// Legacy remote state was only map[string]string config
    36  	var conf map[string]string
    37  	if err := mapstructure.Decode(c.Raw, &conf); err != nil {
    38  		return fmt.Errorf(
    39  			"Failed to decode %q configuration: %s\n\n"+
    40  				"This backend expects all configuration keys and values to be\n"+
    41  				"strings. Please verify your configuration and try again.",
    42  			b.Type, err)
    43  	}
    44  
    45  	client, err := remote.NewClient(b.Type, conf)
    46  	if err != nil {
    47  		return fmt.Errorf(
    48  			"Failed to configure remote backend %q: %s",
    49  			b.Type, err)
    50  	}
    51  
    52  	// Set our client
    53  	b.client = client
    54  	return nil
    55  }
    56  
    57  func (b *Backend) State(name string) (state.State, error) {
    58  	if name != backend.DefaultStateName {
    59  		return nil, backend.ErrNamedStatesNotSupported
    60  	}
    61  
    62  	if b.client == nil {
    63  		panic("State called with nil remote state client")
    64  	}
    65  
    66  	return &remote.State{Client: b.client}, nil
    67  }
    68  
    69  func (b *Backend) States() ([]string, error) {
    70  	return nil, backend.ErrNamedStatesNotSupported
    71  }
    72  
    73  func (b *Backend) DeleteState(string) error {
    74  	return backend.ErrNamedStatesNotSupported
    75  }