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

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