Skip to content

Commit 03f10c7

Browse files
Merge pull request #6 from lenny-intel/FileRefactor
Refactor to move files out of /pkg and fix import cycle
2 parents 171439d + 72dc35a commit 03f10c7

File tree

8 files changed

+169
-130
lines changed

8 files changed

+169
-130
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func initializeConfiguration(useRegistry bool, useProfile string) (*Configuratio
3636
}
3737
3838
if useRegistry {
39-
registryConfig := registry.Config{
39+
registryConfig := types.Config{
4040
Host: conf.Registry.Host,
4141
Port: conf.Registry.Port,
4242
Type: conf.Registry.Type,
@@ -49,7 +49,7 @@ func initializeConfiguration(useRegistry bool, useProfile string) (*Configuratio
4949
Stem: internal.ConfigRegistryStem,
5050
}
5151
52-
registryClient, err = factory.NewRegistryClient(registryConfig)
52+
registryClient, err = registry.NewRegistryClient(registryConfig)
5353
if err != nil {
5454
return fmt.Errorf("connection to Registry could not be made: %v", err.Error())
5555
}
@@ -123,11 +123,12 @@ func listenForConfigChanges() {
123123
This code snippet shows how to get dependent service endpoint information and check status of the dependent service.
124124
```
125125
...
126-
if registry.Client != nil {
127-
endpoint, err = registry.Client.GetServiceEndpoint(params.ServiceKey)
126+
if e.RegistryClient != nil {
127+
endpoint, err = (*e.RegistryClient).GetServiceEndpoint(params.ServiceKey)
128+
...
128129
url := fmt.Sprintf("http://%s:%v%s", endpoint.Address, endpoint.Port, params.Path)
129130
...
130-
if registry.Client.IsServiceAvailable(params.ServiceKey) {
131+
if (*e.RegistryClient).IsServiceAvailable(params.ServiceKey) {
131132
...
132133
}
133134
}

internal/pkg/consul/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package consul
1818

1919
import (
2020
"fmt"
21-
"github.com/edgexfoundry/go-mod-registry"
21+
"github.com/edgexfoundry/go-mod-registry/pkg/types"
2222
consulapi "github.com/hashicorp/consul/api"
2323
"github.com/mitchellh/consulstructure"
2424
"github.com/pelletier/go-toml"
@@ -46,7 +46,7 @@ type consulClient struct {
4646
}
4747

4848
// Create new Consul Client. Service details are optional, not needed just for configuration, but required if registering
49-
func NewConsulClient(registryConfig registry.Config,) (*consulClient, error) {
49+
func NewConsulClient(registryConfig types.Config,) (*consulClient, error) {
5050

5151
client := consulClient{
5252
serviceKey: registryConfig.ServiceKey,
@@ -280,13 +280,13 @@ func (client *consulClient) PutConfigurationValue(name string, value []byte) err
280280
}
281281

282282
// Gets the service endpoint information for the target ID from Consul
283-
func (client *consulClient) GetServiceEndpoint(serviceID string) (registry.ServiceEndpoint, error) {
283+
func (client *consulClient) GetServiceEndpoint(serviceID string) (types.ServiceEndpoint, error) {
284284
services, err := client.consulClient.Agent().Services()
285285
if err != nil {
286-
return registry.ServiceEndpoint{}, err
286+
return types.ServiceEndpoint{}, err
287287
}
288288

289-
endpoint := registry.ServiceEndpoint{}
289+
endpoint := types.ServiceEndpoint{}
290290
if service, ok := services[serviceID]; ok {
291291
endpoint.Port = service.Port
292292
endpoint.ServiceId = serviceID

internal/pkg/consul/client_test.go

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package consul
1818

1919
import (
20-
"github.com/edgexfoundry/go-mod-registry"
2120
"log"
2221
"net/http"
2322
"net/http/httptest"
@@ -31,6 +30,8 @@ import (
3130
"github.com/hashicorp/consul/api"
3231
"github.com/pelletier/go-toml"
3332
"github.com/stretchr/testify/assert"
33+
34+
"github.com/edgexfoundry/go-mod-registry/pkg/types"
3435
)
3536

3637
const (
@@ -55,7 +56,7 @@ type LoggingInfo struct {
5556

5657
type MyConfig struct {
5758
Logging LoggingInfo
58-
Service registry.ServiceEndpoint
59+
Service types.ServiceEndpoint
5960
Port int
6061
Host string
6162
LogLevel string
@@ -116,7 +117,7 @@ func TestHasConfigurationTrue(t *testing.T) {
116117
}
117118

118119
// Now push a value so the configuration will exist
119-
client.PutConfigurationValue("Dummy", []byte("Value"))
120+
_ = client.PutConfigurationValue("Dummy", []byte("Value"))
120121

121122
actual, err := client.HasConfiguration()
122123
if !assert.NoError(t, err) {
@@ -164,7 +165,7 @@ func TestRegisterWithPingCallback(t *testing.T) {
164165
receivedPing = true
165166

166167
writer.Header().Set("Content-Type", "text/plain")
167-
writer.Write([]byte("pong"))
168+
_,_ = writer.Write([]byte("pong"))
168169

169170
doneChan <- true
170171
}
@@ -178,13 +179,13 @@ func TestRegisterWithPingCallback(t *testing.T) {
178179

179180
client := makeConsulClient(t, serverPort, true)
180181
// Make sure service is not already registered.
181-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
182-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
182+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
183+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
183184

184185
// Try to clean-up after test
185186
defer func(client *consulClient) {
186-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
187-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
187+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
188+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
188189
}(client)
189190

190191
// Register the service endpoint and health check callback
@@ -203,22 +204,22 @@ func TestRegisterWithPingCallback(t *testing.T) {
203204
}
204205

205206
func TestGetServiceEndpoint(t *testing.T) {
206-
expectedNotFoundEndpoint := registry.ServiceEndpoint{}
207-
expectedFoundEndpoint := registry.ServiceEndpoint{
207+
expectedNotFoundEndpoint := types.ServiceEndpoint{}
208+
expectedFoundEndpoint := types.ServiceEndpoint{
208209
ServiceId: serviceName,
209210
Host: serviceHost,
210211
Port: defaultServicePort,
211212
}
212213

213214
client := makeConsulClient(t, defaultServicePort, true)
214215
// Make sure service is not already registered.
215-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
216-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
216+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
217+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
217218

218219
// Try to clean-up after test
219220
defer func(client *consulClient) {
220-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
221-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
221+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
222+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
222223
}(client)
223224

224225
// Test for endpoint not found
@@ -251,8 +252,8 @@ func TestIsServiceAvailableNotRegistered(t *testing.T) {
251252
client := makeConsulClient(t, defaultServicePort, true)
252253

253254
// Make sure service is not already registered.
254-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
255-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
255+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
256+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
256257

257258
actual := client.IsServiceAvailable(client.serviceKey)
258259
if !assert.Error(t, actual, "expected error") {
@@ -267,13 +268,13 @@ func TestIsServiceAvailableNotHealthy(t *testing.T) {
267268
client := makeConsulClient(t, defaultServicePort, true)
268269

269270
// Make sure service is not already registered.
270-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
271-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
271+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
272+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
272273

273274
// Try to clean-up after test
274275
defer func(client *consulClient) {
275-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
276-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
276+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
277+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
277278
}(client)
278279

279280
// Register the service endpoint, without test service to respond to health check
@@ -303,7 +304,7 @@ func TestIsServiceAvailableHealthy(t *testing.T) {
303304
switch request.Method {
304305
case "GET":
305306
writer.Header().Set("Content-Type", "text/plain")
306-
writer.Write([]byte("pong"))
307+
_,_ = writer.Write([]byte("pong"))
307308

308309
doneChan <- true
309310
}
@@ -317,13 +318,13 @@ func TestIsServiceAvailableHealthy(t *testing.T) {
317318

318319
client := makeConsulClient(t, serverPort, true)
319320
// Make sure service is not already registered.
320-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
321-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
321+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
322+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
322323

323324
// Try to clean-up after test
324325
defer func(client *consulClient) {
325-
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
326-
client.consulClient.Agent().CheckDeregister(client.serviceKey)
326+
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
327+
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
327328
}(client)
328329

329330
// Register the service endpoint
@@ -366,7 +367,7 @@ func TestConfigurationValueExists(t *testing.T) {
366367
if !assert.NoError(t, err) {
367368
t.Fatal()
368369
}
369-
if !assert.Equal(t, expected, actual) {
370+
if !assert.False(t, actual) {
370371
t.Fatal()
371372
}
372373

@@ -423,7 +424,7 @@ func TestPutConfigurationValue(t *testing.T) {
423424
client := makeConsulClient(t, defaultServicePort, true)
424425

425426
//clean up the the key, if it exists
426-
client.consulClient.KV().Delete(expectedFullKey, nil)
427+
_,_ = client.consulClient.KV().Delete(expectedFullKey, nil)
427428

428429
err := client.PutConfigurationValue(key, expected)
429430
assert.NoError(t, err)
@@ -448,7 +449,7 @@ func TestGetConfiguration(t *testing.T) {
448449
EnableRemote: true,
449450
File: "NONE",
450451
},
451-
Service: registry.ServiceEndpoint{
452+
Service: types.ServiceEndpoint{
452453
ServiceId: "Dummy",
453454
Host: "10.6.7.8",
454455
Port: 8080,
@@ -460,14 +461,14 @@ func TestGetConfiguration(t *testing.T) {
460461

461462
client := makeConsulClient(t, defaultServicePort, true)
462463

463-
client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expected.Logging.EnableRemote)))
464-
client.PutConfigurationValue("Logging/File", []byte(expected.Logging.File))
465-
client.PutConfigurationValue("Service/ServiceId", []byte(expected.Service.ServiceId))
466-
client.PutConfigurationValue("Service/Host", []byte(expected.Service.Host))
467-
client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expected.Service.Port)))
468-
client.PutConfigurationValue("Port", []byte(strconv.Itoa(expected.Port)))
469-
client.PutConfigurationValue("Host", []byte(expected.Host))
470-
client.PutConfigurationValue("LogLevel", []byte(expected.LogLevel))
464+
_ = client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expected.Logging.EnableRemote)))
465+
_ = client.PutConfigurationValue("Logging/File", []byte(expected.Logging.File))
466+
_ = client.PutConfigurationValue("Service/ServiceId", []byte(expected.Service.ServiceId))
467+
_ = client.PutConfigurationValue("Service/Host", []byte(expected.Service.Host))
468+
_ = client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expected.Service.Port)))
469+
_ = client.PutConfigurationValue("Port", []byte(strconv.Itoa(expected.Port)))
470+
_ = client.PutConfigurationValue("Host", []byte(expected.Host))
471+
_ = client.PutConfigurationValue("LogLevel", []byte(expected.LogLevel))
471472

472473
result, err := client.GetConfiguration(&MyConfig{})
473474

@@ -497,7 +498,7 @@ func TestPutConfiguration(t *testing.T) {
497498
EnableRemote: true,
498499
File: "NONE",
499500
},
500-
Service: registry.ServiceEndpoint{
501+
Service: types.ServiceEndpoint{
501502
ServiceId: "Dummy",
502503
Host: "10.6.7.8",
503504
Port: 8080,
@@ -511,11 +512,11 @@ func TestPutConfiguration(t *testing.T) {
511512
client := makeConsulClient(t, defaultServicePort, true)
512513

513514
// Make sure the tree of values doesn't exist.
514-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
515+
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
515516

516517
defer func() {
517518
// Clean up
518-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
519+
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
519520
}()
520521

521522
err := client.PutConfiguration(expected, true)
@@ -538,7 +539,7 @@ func TestPutConfiguration(t *testing.T) {
538539
assert.True(t,configValueSet("LogLevel", client))
539540
}
540541

541-
func configValueSet(key string, client registry.Client) bool {
542+
func configValueSet(key string, client *consulClient) bool {
542543
exists, _ := client.ConfigurationValueExists(key)
543544
return exists
544545
}
@@ -547,11 +548,11 @@ func TestPutConfigurationTomlNoPreviousValues(t *testing.T) {
547548
client := makeConsulClient(t, defaultServicePort, true)
548549

549550
// Make sure the tree of values doesn't exist.
550-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
551+
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
551552

552553
defer func() {
553554
// Clean up
554-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
555+
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
555556
}()
556557

557558
configMap := createKeyValueMap()
@@ -582,11 +583,11 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) {
582583
client := makeConsulClient(t, defaultServicePort, true)
583584

584585
// Make sure the tree of values doesn't exist.
585-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
586+
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
586587

587588
defer func() {
588589
// Clean up
589-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
590+
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
590591
}()
591592

592593
configMap := createKeyValueMap()
@@ -629,10 +630,10 @@ func TestPutConfigurationTomlOverWrite(t *testing.T) {
629630
client := makeConsulClient(t, defaultServicePort, true)
630631

631632
// Make sure the tree of values doesn't exist.
632-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
633+
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
633634
// Clean up after unit test
634635
defer func() {
635-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
636+
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
636637
}()
637638

638639
configMap := createKeyValueMap()
@@ -676,7 +677,7 @@ func TestWatchForChanges(t *testing.T) {
676677
EnableRemote: true,
677678
File: "NONE",
678679
},
679-
Service: registry.ServiceEndpoint{
680+
Service: types.ServiceEndpoint{
680681
ServiceId: "Dummy",
681682
Host: "10.6.7.8",
682683
Port: 8080,
@@ -691,20 +692,20 @@ func TestWatchForChanges(t *testing.T) {
691692
client := makeConsulClient(t, defaultServicePort, false)
692693

693694
// Make sure the tree of values doesn't exist.
694-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
695+
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
695696
// Clean up after unit test
696697
defer func() {
697-
client.consulClient.KV().DeleteTree(consulBasePath, nil)
698+
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
698699
}()
699700

700-
client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expectedConfig.Logging.EnableRemote)))
701-
client.PutConfigurationValue("Logging/File", []byte(expectedConfig.Logging.File))
702-
client.PutConfigurationValue("Service/ServiceId", []byte(expectedConfig.Service.ServiceId))
703-
client.PutConfigurationValue("Service/Host", []byte(expectedConfig.Service.Host))
704-
client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expectedConfig.Service.Port)))
705-
client.PutConfigurationValue("Port", []byte(strconv.Itoa(expectedConfig.Port)))
706-
client.PutConfigurationValue("Host", []byte(expectedConfig.Host))
707-
client.PutConfigurationValue("LogLevel", []byte(expectedConfig.LogLevel))
701+
_ = client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expectedConfig.Logging.EnableRemote)))
702+
_ = client.PutConfigurationValue("Logging/File", []byte(expectedConfig.Logging.File))
703+
_ = client.PutConfigurationValue("Service/ServiceId", []byte(expectedConfig.Service.ServiceId))
704+
_ = client.PutConfigurationValue("Service/Host", []byte(expectedConfig.Service.Host))
705+
_ = client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expectedConfig.Service.Port)))
706+
_ = client.PutConfigurationValue("Port", []byte(strconv.Itoa(expectedConfig.Port)))
707+
_ = client.PutConfigurationValue("Host", []byte(expectedConfig.Host))
708+
_ = client.PutConfigurationValue("LogLevel", []byte(expectedConfig.LogLevel))
708709

709710
updateChannel := make(chan interface{})
710711
errorChannel := make(chan error)
@@ -728,7 +729,7 @@ func TestWatchForChanges(t *testing.T) {
728729
}
729730

730731
// Make a change to logging
731-
client.PutConfigurationValue("Logging/File", []byte(expectedChange))
732+
_ = client.PutConfigurationValue("Logging/File", []byte(expectedChange))
732733

733734
pass--
734735
continue
@@ -745,7 +746,7 @@ func TestWatchForChanges(t *testing.T) {
745746
}
746747

747748
func makeConsulClient(t *testing.T, servicePort int, setServiceInfo bool) *consulClient {
748-
registryConfig := registry.Config{
749+
registryConfig := types.Config{
749750
Host: testHost,
750751
Port: port,
751752
Stem: "edgex/core/1.0/",

0 commit comments

Comments
 (0)