mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
etcd rewrote its import path from coreos/etcd to go.etcd.io/etcd. Changed the imports path in this commit, which also updates the code version. This lets us remove the github.com/ugorji/go/codec dependency, which was pinned to a fairly old version. The net change is a loss of 30,000 lines of code in the vendor directory. (I first noticed this problem because the outdated go/codec dependency was causing a dependency failure when I tried to put Terraform and another project in the same vendor directory.) Note the version shows up funkily in go.mod, but I verified visually it's the same commit as the "release-3.4" tag in github.com/coreos/etcd. The etcd team plans to fix the release version tagging in v3.5, which should be released soon.
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package etcdv2
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/remote"
|
|
etcdapi "go.etcd.io/etcd/client"
|
|
)
|
|
|
|
// EtcdClient is a remote client that stores data in etcd.
|
|
type EtcdClient struct {
|
|
Client etcdapi.Client
|
|
Path string
|
|
}
|
|
|
|
func (c *EtcdClient) Get() (*remote.Payload, error) {
|
|
resp, err := etcdapi.NewKeysAPI(c.Client).Get(context.Background(), c.Path, &etcdapi.GetOptions{Quorum: true})
|
|
if err != nil {
|
|
if err, ok := err.(etcdapi.Error); ok && err.Code == etcdapi.ErrorCodeKeyNotFound {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
if resp.Node.Dir {
|
|
return nil, fmt.Errorf("path is a directory")
|
|
}
|
|
|
|
data := []byte(resp.Node.Value)
|
|
md5 := md5.Sum(data)
|
|
return &remote.Payload{
|
|
Data: data,
|
|
MD5: md5[:],
|
|
}, nil
|
|
}
|
|
|
|
func (c *EtcdClient) Put(data []byte) error {
|
|
_, err := etcdapi.NewKeysAPI(c.Client).Set(context.Background(), c.Path, string(data), nil)
|
|
return err
|
|
}
|
|
|
|
func (c *EtcdClient) Delete() error {
|
|
_, err := etcdapi.NewKeysAPI(c.Client).Delete(context.Background(), c.Path, nil)
|
|
return err
|
|
}
|