From 3d7187f405c355b24c6052eca19fe2db70cd682b Mon Sep 17 00:00:00 2001 From: chriss Date: Thu, 17 Nov 2016 21:03:54 +0000 Subject: [PATCH 1/2] Add the ability to pass consul client config via registry options. This can be used when you need to configure the consul client to use a specific configuration (E.G to pass a Token if consul is using ACL policies) --- registry/consul/options.go | 16 ++++++++++++++++ registry/consul_registry.go | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 registry/consul/options.go diff --git a/registry/consul/options.go b/registry/consul/options.go new file mode 100644 index 00000000..3c28d45d --- /dev/null +++ b/registry/consul/options.go @@ -0,0 +1,16 @@ +package consul + +import ( + consul "github.com/hashicorp/consul/api" + "github.com/micro/go-micro/registry" + "golang.org/x/net/context" +) + +func Config(c *consul.Config) registry.Option { + return func(o *registry.Options) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, "consul_config", c) + } +} diff --git a/registry/consul_registry.go b/registry/consul_registry.go index 4eecde04..3d34c09d 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -53,6 +53,13 @@ func newConsulRegistry(opts ...Option) Registry { // use default config config := consul.DefaultConfig() + if options.Context != nil { + // Use the consul config passed in the options, if available + c := options.Context.Value("consul_config") + if c != nil { + config = c.(*consul.Config) + } + } // set timeout if options.Timeout > 0 { @@ -253,7 +260,7 @@ func (c *consulRegistry) ListServices() ([]*Service, error) { var services []*Service - for service, _ := range rsp { + for service := range rsp { services = append(services, &Service{Name: service}) } From 8a25723fe07c02d47308a32e629b2fe11184c91a Mon Sep 17 00:00:00 2001 From: chriss Date: Fri, 18 Nov 2016 00:07:12 +0000 Subject: [PATCH 2/2] Set consul token via registry options. Optionally configure a token to use with the consul client. --- registry/consul/options.go | 9 +++++++++ registry/consul_registry.go | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/registry/consul/options.go b/registry/consul/options.go index 3c28d45d..f85bbf79 100644 --- a/registry/consul/options.go +++ b/registry/consul/options.go @@ -14,3 +14,12 @@ func Config(c *consul.Config) registry.Option { o.Context = context.WithValue(o.Context, "consul_config", c) } } + +func Token(t string) registry.Option { + return func(o *registry.Options) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, "consul_token", t) + } +} diff --git a/registry/consul_registry.go b/registry/consul_registry.go index 3d34c09d..32a12490 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -59,6 +59,10 @@ func newConsulRegistry(opts ...Option) Registry { if c != nil { config = c.(*consul.Config) } + t := options.Context.Value("consul_token") + if t != nil { + config.Token = t.(string) + } } // set timeout