Reuse 'etcd' client.

This commit is contained in:
Bruno Miguel Custodio 2017-09-09 00:21:23 +01:00
parent 6daf1d9d84
commit c8ff10f603
No known key found for this signature in database
GPG Key ID: 84CDD9E18A1A6B2C
2 changed files with 6 additions and 19 deletions

View File

@ -60,12 +60,14 @@ type Backend struct {
*schema.Backend *schema.Backend
// The fields below are set from configure. // The fields below are set from configure.
client *etcdv3.Client
data *schema.ResourceData data *schema.ResourceData
lock bool lock bool
prefix string prefix string
} }
func (b *Backend) configure(ctx context.Context) error { func (b *Backend) configure(ctx context.Context) error {
var err error
// Grab the resource data. // Grab the resource data.
b.data = schema.FromContextBackendConfig(ctx) b.data = schema.FromContextBackendConfig(ctx)
// Store the lock information. // Store the lock information.
@ -73,7 +75,7 @@ func (b *Backend) configure(ctx context.Context) error {
// Store the prefix information. // Store the prefix information.
b.prefix = b.data.Get("prefix").(string) b.prefix = b.data.Get("prefix").(string)
// Initialize a client to test config. // Initialize a client to test config.
_, err := b.rawClient() b.client, err = b.rawClient()
// Return err, if any. // Return err, if any.
return err return err
} }

View File

@ -14,12 +14,7 @@ import (
) )
func (b *Backend) States() ([]string, error) { func (b *Backend) States() ([]string, error) {
client, err := b.rawClient() res, err := b.client.Get(context.TODO(), b.prefix, etcdv3.WithPrefix(), etcdv3.WithKeysOnly())
if err != nil {
return nil, err
}
res, err := client.Get(context.TODO(), b.prefix, etcdv3.WithPrefix(), etcdv3.WithKeysOnly())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -39,26 +34,16 @@ func (b *Backend) DeleteState(name string) error {
return fmt.Errorf("Can't delete default state.") return fmt.Errorf("Can't delete default state.")
} }
client, err := b.rawClient()
if err != nil {
return err
}
key := b.determineKey(name) key := b.determineKey(name)
_, err = client.Delete(context.TODO(), key) _, err := b.client.Delete(context.TODO(), key)
return err return err
} }
func (b *Backend) State(name string) (state.State, error) { func (b *Backend) State(name string) (state.State, error) {
client, err := b.rawClient()
if err != nil {
return nil, err
}
var stateMgr state.State = &remote.State{ var stateMgr state.State = &remote.State{
Client: &RemoteClient{ Client: &RemoteClient{
Client: client, Client: b.client,
DoLock: b.lock, DoLock: b.lock,
Key: b.determineKey(name), Key: b.determineKey(name),
}, },