Debian Codegraph @

providers/consul/consul.go

raw
1 package consul
2
3 import (
4 "errors"
5 "fmt"
6
7 "github.com/hashicorp/consul/api"
8 "github.com/hashicorp/consul/api/watch"
9 )
10
11 // Config represents the Consul client configuration.
12 type Config struct {
13 // Path of the key to read. If Recurse is true, this is treated
14 // as a prefix.
15 Key string
16
17 // https://www.consul.io/api-docs/kv#read-key
18 // If recurse is true, Consul returns an array of keys.
19 // It specifies if the lookup should be recursive and treat
20 // Key as a prefix instead of a literal match.
21 // This is analogous to: consul kv get -recurse key
22 Recurse bool
23
24 // Gets additional metadata about the key in addition to the value such
25 // as the ModifyIndex and any flags that may have been set on the key.
26 // This is analogous to: consul kv get -detailed key
27 Detailed bool
28
29 // Consul client config
30 Cfg *api.Config
31 }
32
33 // Consul implements the Consul provider.
34 type Consul struct {
35 client *api.Client
36 cfg Config
37 }
38
39 // Provider returns an instance of the Consul provider.
40 func Provider(cfg Config) (*Consul, error) {
41 c, err := api.NewClient(cfg.Cfg)
42 if err != nil {
43 return nil, err
44 }
45
46 return &Consul{client: c, cfg: cfg}, nil
47 }
48
49 // ReadBytes is not supported by the Consul provider.
50 func (c *Consul) ReadBytes() ([]byte, error) {
51 return nil, errors.New("consul provider does not support this method")
52 }
53
54 // Read reads configuration from the Consul provider.
55 func (c *Consul) Read() (map[string]any, error) {
56 var (
57 mp = make(map[string]any)
58 kv = c.client.KV()
59 )
60
61 if c.cfg.Recurse {
62 pairs, _, err := kv.List(c.cfg.Key, nil)
63 if err != nil {
64 return nil, err
65 }
66
67 // Detailed information can be obtained using standard koanf flattened delimited keys:
68 // For example:
69 // "parent1.CreateIndex"
70 // "parent1.Flags"
71 // "parent1.LockIndex"
72 // "parent1.ModifyIndex"
73 // "parent1.Session"
74 // "parent1.Value"
75 if c.cfg.Detailed {
76 for _, pair := range pairs {
77 m := make(map[string]any)
78 m["CreateIndex"] = fmt.Sprintf("%d", pair.CreateIndex)
79 m["Flags"] = fmt.Sprintf("%d", pair.Flags)
80 m["LockIndex"] = fmt.Sprintf("%d", pair.LockIndex)
81 m["ModifyIndex"] = fmt.Sprintf("%d", pair.ModifyIndex)
82
83 if pair.Session == "" {
84 m["Session"] = "-"
85 } else {
86 m["Session"] = fmt.Sprintf("%s", pair.Session)
87 }
88
89 m["Value"] = string(pair.Value)
90
91 mp[pair.Key] = m
92 }
93 } else {
94 for _, pair := range pairs {
95 mp[pair.Key] = string(pair.Value)
96 }
97 }
98
99 return mp, nil
100 }
101
102 pair, _, err := kv.Get(c.cfg.Key, nil)
103 if err != nil {
104 return nil, err
105 }
106
107 if c.cfg.Detailed {
108 m := make(map[string]any)
109 m["CreateIndex"] = fmt.Sprintf("%d", pair.CreateIndex)
110 m["Flags"] = fmt.Sprintf("%d", pair.Flags)
111 m["LockIndex"] = fmt.Sprintf("%d", pair.LockIndex)
112 m["ModifyIndex"] = fmt.Sprintf("%d", pair.ModifyIndex)
113
114 if pair.Session == "" {
115 m["Session"] = "-"
116 } else {
117 m["Session"] = fmt.Sprintf("%s", pair.Session)
118 }
119
120 m["Value"] = string(pair.Value)
121
122 mp[pair.Key] = m
123 } else {
124 mp[pair.Key] = string(pair.Value)
125 }
126
127 return mp, nil
128 }
129
130 // Watch watches for changes in the Consul API and triggers a callback.
131 func (c *Consul) Watch(cb func(event any, err error)) error {
132 p := make(map[string]any)
133
134 if c.cfg.Recurse {
135 p["type"] = "keyprefix"
136 p["prefix"] = c.cfg.Key
137 } else {
138 p["type"] = "key"
139 p["key"] = c.cfg.Key
140 }
141
142 plan, err := watch.Parse(p)
143 if err != nil {
144 return err
145 }
146
147 plan.Handler = func(_ uint64, val any) {
148 cb(val, nil)
149 }
150
151 go func() {
152 plan.Run(c.cfg.Cfg.Address)
153 }()
154
155 return nil
156 }