github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/remote-state/etcdv2/backend.go (about)

     1  // legacy etcd2.x backend
     2  
     3  package etcdv2
     4  
     5  import (
     6  	"context"
     7  	"strings"
     8  
     9  	etcdapi "github.com/coreos/etcd/client"
    10  	"github.com/hashicorp/terraform/backend"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/hashicorp/terraform/state"
    13  	"github.com/hashicorp/terraform/state/remote"
    14  )
    15  
    16  func New() backend.Backend {
    17  	s := &schema.Backend{
    18  		Schema: map[string]*schema.Schema{
    19  			"path": &schema.Schema{
    20  				Type:        schema.TypeString,
    21  				Required:    true,
    22  				Description: "The path where to store the state",
    23  			},
    24  			"endpoints": &schema.Schema{
    25  				Type:        schema.TypeString,
    26  				Required:    true,
    27  				Description: "A space-separated list of the etcd endpoints",
    28  			},
    29  			"username": &schema.Schema{
    30  				Type:        schema.TypeString,
    31  				Optional:    true,
    32  				Description: "Username",
    33  			},
    34  			"password": &schema.Schema{
    35  				Type:        schema.TypeString,
    36  				Optional:    true,
    37  				Description: "Password",
    38  			},
    39  		},
    40  	}
    41  
    42  	result := &Backend{Backend: s}
    43  	result.Backend.ConfigureFunc = result.configure
    44  	return result
    45  }
    46  
    47  type Backend struct {
    48  	*schema.Backend
    49  
    50  	client etcdapi.Client
    51  	path   string
    52  }
    53  
    54  func (b *Backend) configure(ctx context.Context) error {
    55  	data := schema.FromContextBackendConfig(ctx)
    56  
    57  	b.path = data.Get("path").(string)
    58  
    59  	endpoints := data.Get("endpoints").(string)
    60  	username := data.Get("username").(string)
    61  	password := data.Get("password").(string)
    62  
    63  	config := etcdapi.Config{
    64  		Endpoints: strings.Split(endpoints, " "),
    65  		Username:  username,
    66  		Password:  password,
    67  	}
    68  
    69  	client, err := etcdapi.New(config)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	b.client = client
    75  	return nil
    76  }
    77  
    78  func (b *Backend) Workspaces() ([]string, error) {
    79  	return nil, backend.ErrWorkspacesNotSupported
    80  }
    81  
    82  func (b *Backend) DeleteWorkspace(string) error {
    83  	return backend.ErrWorkspacesNotSupported
    84  }
    85  
    86  func (b *Backend) StateMgr(name string) (state.State, error) {
    87  	if name != backend.DefaultStateName {
    88  		return nil, backend.ErrWorkspacesNotSupported
    89  	}
    90  	return &remote.State{
    91  		Client: &EtcdClient{
    92  			Client: b.client,
    93  			Path:   b.path,
    94  		},
    95  	}, nil
    96  }
    97  
    98  func (b *Backend) StateMgrWithoutCheckVersion(name string) (state.State, error) {
    99  	return b.StateMgr(name)
   100  }