-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Consul K/V storage backend
Fixes #672
- Loading branch information
1 parent
2826b33
commit 99dbe56
Showing
51 changed files
with
7,332 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package consul | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/blang/semver" | ||
api "github.com/hashicorp/consul/api" | ||
) | ||
|
||
type Store struct { | ||
api *api.Client | ||
} | ||
|
||
func New(host, datacenter, token string) (*Store, error) { | ||
client, err := api.NewClient(&api.Config{ | ||
Address: host, | ||
Datacenter: datacenter, | ||
Token: token, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Store{ | ||
api: client, | ||
}, nil | ||
} | ||
|
||
func (s *Store) Get(ctx context.Context, name string) ([]byte, error) { | ||
p, _, err := s.api.KV().Get(name, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if p == nil || p.Value == nil { | ||
return nil, nil | ||
} | ||
return p.Value, nil | ||
} | ||
|
||
func (s *Store) Set(ctx context.Context, name string, value []byte) error { | ||
p := &api.KVPair{ | ||
Key: name, | ||
Value: value, | ||
} | ||
_, err := s.api.KV().Put(p, nil) | ||
return err | ||
} | ||
|
||
func (s *Store) Delete(ctx context.Context, name string) error { | ||
_, err := s.api.KV().Delete(name, nil) | ||
return err | ||
} | ||
|
||
func (s *Store) Exists(ctx context.Context, name string) bool { | ||
v, err := s.Get(ctx, name) | ||
if err == nil && v != nil { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (s *Store) List(ctx context.Context, prefix string) ([]string, error) { | ||
pairs, _, err := s.api.KV().List(prefix, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := make([]string, len(pairs)) | ||
for _, kvp := range pairs { | ||
res = append(res, kvp.Key) | ||
} | ||
return res, nil | ||
} | ||
|
||
func (s *Store) IsDir(ctx context.Context, name string) bool { | ||
ls, err := s.List(ctx, name) | ||
if err == nil && len(ls) > 1 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (s *Store) Prune(ctx context.Context, prefix string) error { | ||
return s.Delete(ctx, prefix) | ||
} | ||
|
||
func (s *Store) Name() string { | ||
return "consul" | ||
} | ||
|
||
func (s *Store) Version() semver.Version { | ||
return semver.Version{Major: 1} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.