github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/backend/remote-state/consul/backend.go (about)

     1  package consul
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	consulapi "github.com/hashicorp/consul/api"
     8  	"github.com/hashicorp/terraform/backend"
     9  	"github.com/hashicorp/terraform/backend/remote-state"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  	"github.com/hashicorp/terraform/state/remote"
    12  )
    13  
    14  // New creates a new backend for Consul remote state.
    15  func New() backend.Backend {
    16  	return &remotestate.Backend{
    17  		ConfigureFunc: configure,
    18  
    19  		// Set the schema
    20  		Backend: &schema.Backend{
    21  			Schema: map[string]*schema.Schema{
    22  				"path": &schema.Schema{
    23  					Type:        schema.TypeString,
    24  					Required:    true,
    25  					Description: "Path to store state in Consul",
    26  				},
    27  
    28  				"access_token": &schema.Schema{
    29  					Type:        schema.TypeString,
    30  					Optional:    true,
    31  					Description: "Access token for a Consul ACL",
    32  					Default:     "", // To prevent input
    33  				},
    34  
    35  				"address": &schema.Schema{
    36  					Type:        schema.TypeString,
    37  					Optional:    true,
    38  					Description: "Address to the Consul Cluster",
    39  				},
    40  
    41  				"scheme": &schema.Schema{
    42  					Type:        schema.TypeString,
    43  					Optional:    true,
    44  					Description: "Scheme to communicate to Consul with",
    45  					Default:     "", // To prevent input
    46  				},
    47  
    48  				"datacenter": &schema.Schema{
    49  					Type:        schema.TypeString,
    50  					Optional:    true,
    51  					Description: "Datacenter to communicate with",
    52  					Default:     "", // To prevent input
    53  				},
    54  
    55  				"http_auth": &schema.Schema{
    56  					Type:        schema.TypeString,
    57  					Optional:    true,
    58  					Description: "HTTP Auth in the format of 'username:password'",
    59  					Default:     "", // To prevent input
    60  				},
    61  			},
    62  		},
    63  	}
    64  }
    65  
    66  func configure(ctx context.Context) (remote.Client, error) {
    67  	// Grab the resource data
    68  	data := schema.FromContextBackendConfig(ctx)
    69  
    70  	// Configure the client
    71  	config := consulapi.DefaultConfig()
    72  	if v, ok := data.GetOk("access_token"); ok && v.(string) != "" {
    73  		config.Token = v.(string)
    74  	}
    75  	if v, ok := data.GetOk("address"); ok && v.(string) != "" {
    76  		config.Address = v.(string)
    77  	}
    78  	if v, ok := data.GetOk("scheme"); ok && v.(string) != "" {
    79  		config.Scheme = v.(string)
    80  	}
    81  	if v, ok := data.GetOk("datacenter"); ok && v.(string) != "" {
    82  		config.Datacenter = v.(string)
    83  	}
    84  	if v, ok := data.GetOk("http_auth"); ok && v.(string) != "" {
    85  		auth := v.(string)
    86  
    87  		var username, password string
    88  		if strings.Contains(auth, ":") {
    89  			split := strings.SplitN(auth, ":", 2)
    90  			username = split[0]
    91  			password = split[1]
    92  		} else {
    93  			username = auth
    94  		}
    95  
    96  		config.HttpAuth = &consulapi.HttpBasicAuth{
    97  			Username: username,
    98  			Password: password,
    99  		}
   100  	}
   101  
   102  	client, err := consulapi.NewClient(config)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	return &RemoteClient{
   108  		Client: client,
   109  		Path:   data.Get("path").(string),
   110  	}, nil
   111  }