本文整理汇总了Golang中github.com/hashicorp/consul/api.Client.KV方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.KV方法的具体用法?Golang Client.KV怎么用?Golang Client.KV使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/hashicorp/consul/api.Client
的用法示例。
在下文中一共展示了Client.KV方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewAgent
// NewAgent returns an initalized Agent.
func NewAgent(prefix string, c *consul.Client, d *docker.Client) *Agent {
return &Agent{
KV: c.KV(),
Docker: d,
Prefix: prefix,
}
}
示例2: writeToConsul
func writeToConsul(t *testing.T, prefix, key string, client *consulapi.Client) []byte {
token := os.Getenv("TOKEN")
dc := os.Getenv("DC")
if dc == "" {
dc = "dc1"
}
kv := client.KV()
writeOptions := &consulapi.WriteOptions{Token: token, Datacenter: dc}
// Delete all keys in the prefixed KV space
if _, err := kv.DeleteTree(prefix, writeOptions); err != nil {
t.Fatalf("err: %v", err)
}
// Put a test KV
encodedValue := make([]byte, base64.StdEncoding.EncodedLen(1024))
base64.StdEncoding.Encode(encodedValue, createRandomBytes(1024))
p := &consulapi.KVPair{Key: key, Flags: 42, Value: encodedValue}
if _, err := kv.Put(p, writeOptions); err != nil {
t.Fatalf("err: %v", err)
}
return encodedValue
}
示例3: writeFileToConsul
func writeFileToConsul(t *testing.T, prefix, key string, file string, client *consulapi.Client) []byte {
token := os.Getenv("TOKEN")
dc := os.Getenv("DC")
if dc == "" {
dc = "dc1"
}
kv := client.KV()
writeOptions := &consulapi.WriteOptions{Token: token, Datacenter: dc}
// Delete all keys in the prefixed KV space
if _, err := kv.DeleteTree(prefix, writeOptions); err != nil {
t.Fatalf("err: %v", err)
}
fileBytes, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("err: %v", err)
}
p := &consulapi.KVPair{Key: key, Flags: 42, Value: fileBytes}
if _, err := kv.Put(p, writeOptions); err != nil {
t.Fatalf("err: %v", err)
}
return fileBytes
}
示例4: NewInstall
func NewInstall(consulClient *consul.Client, marathon *marathon.Marathon, mesos *mesos.Mesos, zkHosts []string) (*Install, error) {
apiConfig = map[string]interface{}{
"mantl": map[string]interface{}{
"zookeeper": map[string]interface{}{
"hosts": strings.Join(zkHosts, ","),
},
},
}
if mesos != nil {
mesosAuthRequired, err := mesos.RequiresAuthentication()
if err != nil {
return nil, err
}
mesosConfig := map[string]interface{}{
"principal": mesos.Principal,
"secret": mesos.Secret,
"secret-path": mesos.SecretPath,
"authentication-enabled": mesosAuthRequired,
}
mantlConfig := apiConfig["mantl"].(map[string]interface{})
mantlConfig["mesos"] = mesosConfig
}
zookeeper := zookeeper.NewZookeeper(zkHosts)
return &Install{consulClient, consulClient.KV(), marathon, mesos, zookeeper}, nil
}
示例5: getAttributes
func getAttributes(keys []string, overwriteAttributes map[string]interface{}) (map[string]interface{}, error) {
var attributes map[string]interface{}
var c *api.Client = util.Consul()
attributes = make(map[string]interface{})
// Get attributes from consul KVS
for _, key := range keys {
list, _, err := c.KV().List(key, &api.QueryOptions{})
if err != nil {
return nil, err
}
for _, kv := range list {
var a map[string]interface{}
if err := json.Unmarshal(kv.Value, &a); err != nil {
return nil, err
}
if err := mergo.MergeWithOverwrite(&attributes, a); err != nil {
return nil, errors.New(fmt.Sprintf("Failed to merge attributes(%v)", err))
}
}
}
// Overwrite some attributes by specified parameter in task.yml
if err := mergo.MergeWithOverwrite(&attributes, overwriteAttributes); err != nil {
return nil, err
}
return attributes, nil
}
示例6: NewWaiter
// NewWaiter creates a new Wait entry with a sensible default isReady function if
// nil is provided in its place.
func NewWaiter(client *api.Client, prefix string, minimumNodes int, isReady func(n *WaitNode) bool) *Wait {
if isReady == nil {
isReady = func(n *WaitNode) bool {
return true
}
}
nodeUpdateCh := make(chan WaitNodeUpdate, 2)
nodeReadyCh := make(chan WaitNode, 2)
allReadyCh := make(chan []WaitNode, 2)
return &Wait{
Prefix: prefix,
MinimumNodes: minimumNodes,
IsReady: isReady,
NodeUpdate: nodeUpdateCh,
NodeReady: nodeReadyCh,
AllReady: allReadyCh,
nodeUpdate: nodeUpdateCh,
nodeReady: nodeReadyCh,
allReady: allReadyCh,
client: client,
kv: client.KV(),
}
}
示例7: GetKVTree
/*
Get full tree under a key and optionally unmarshal.
Args:
client : Consul client
key : Key to query for.
output : Unmarshal data to this interface{} if non-nil
*/
func GetKVTree(client *api.Client, key string, output interface{}) (pairs api.KVPairs, err error) {
kv := client.KV()
pairs, _, err = kv.List(key, nil)
if output != nil {
err = Unmarshal(pairs, output)
}
return
}
示例8: putKV
func putKV(client *api.Client, key, value string, index uint64) (bool, error) {
p := &api.KVPair{Key: key[1:], Value: []byte(value), ModifyIndex: index}
ok, _, err := client.KV().CAS(p, nil)
if err != nil {
return false, err
}
return ok, nil
}
示例9: consulDel
// consulDel removes a key from the Consul KV store.
func consulDel(c *consul.Client, key string) (bool, error) {
kv := c.KV()
key = strings.TrimPrefix(key, "/")
_, err := kv.Delete(key, nil)
if err != nil {
return false, err
}
Log(fmt.Sprintf("action='consulDel' key='%s'", key), "info")
return true, err
}
示例10: Set
// Set sets a key's value inside Consul.
func Set(c *consul.Client, key, value string) bool {
p := &consul.KVPair{Key: key, Value: []byte(value)}
kv := c.KV()
Log(fmt.Sprintf("key='%s' value='%s'", key, value), "info")
_, err := kv.Put(p, nil)
if err != nil {
panic(err)
}
return true
}
示例11: consulSet
// consulSet a value for a key in the Consul KV store.
func consulSet(c *consul.Client, key string, value string) (bool, error) {
key = strings.TrimPrefix(key, "/")
p := &consul.KVPair{Key: key, Value: []byte(value)}
kv := c.KV()
_, err := kv.Put(p, nil)
if err != nil {
return false, err
}
Log(fmt.Sprintf("action='consulSet' key='%s'", key), "debug")
return true, err
}
示例12: putKey
func putKey(c *consulapi.Client, k string, v string, s *Seeder) error {
writeOpt := &consulapi.WriteOptions{}
var err error
writeKey := &consulapi.KVPair{Key: k, Value: []byte(v)}
s.Data = append(s.Data, writeKey)
_, err = c.KV().Put(writeKey, writeOpt)
if err != nil {
return fmt.Errorf("Failed to write test key: %v", err)
}
return nil
}
示例13: getKV
func getKV(client *api.Client, key string, waitIndex uint64) (string, uint64, error) {
q := &api.QueryOptions{RequireConsistent: true, WaitIndex: waitIndex}
kvpair, meta, err := client.KV().Get(key, q)
if err != nil {
return "", 0, err
}
if kvpair == nil {
return "", meta.LastIndex, nil
}
return strings.TrimSpace(string(kvpair.Value)), meta.LastIndex, nil
}
示例14: watch
func watch(
client *consulapi.Client,
prefix string,
path string,
token string,
pairCh chan<- consulapi.KVPairs,
errCh chan<- error,
quitCh <-chan struct{}) {
// Create the root for KVs, if necessary
mkdirp.Mk(path, 0777)
// Get the initial list of k/v pairs. We don't do a retryableList
// here because we want a fast fail if the initial request fails.
opts := &consulapi.QueryOptions{Token: token}
pairs, meta, err := client.KV().List(prefix, opts)
if err != nil {
errCh <- err
return
}
// Send the initial list out right away
pairCh <- pairs
// Loop forever (or until quitCh is closed) and watch the keys
// for changes.
curIndex := meta.LastIndex
for {
select {
case <-quitCh:
return
default:
}
pairs, meta, err = retryableList(
func() (consulapi.KVPairs, *consulapi.QueryMeta, error) {
opts = &consulapi.QueryOptions{WaitIndex: curIndex, Token: token}
return client.KV().List(prefix, opts)
})
if err != nil {
// This happens when the connection to the consul agent dies. Build in a retry by looping after a delay.
log.Warn("Error communicating with consul agent.")
continue
}
pairCh <- pairs
log.WithFields(log.Fields{
"curIndex": curIndex,
"lastIndex": meta.LastIndex,
}).Debug("Potential index update observed")
curIndex = meta.LastIndex
}
}
示例15: checkKey
func checkKey(c *consulapi.Client, kv *consulapi.KVPair) error {
queryOpt := &consulapi.QueryOptions{}
keyCheck, _, err := c.KV().Get(kv.Key, queryOpt)
if err != nil || keyCheck == nil {
return fmt.Errorf("Failed to get key: %v", err)
}
reflecttest := reflect.DeepEqual(keyCheck.Value, kv.Value)
if reflecttest != true {
return fmt.Errorf("Key %v did not match\n\tExpected: %v\n\tGot: %v", kv.Key, string(kv.Value), string(keyCheck.Value))
}
return nil
}