-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use consul go library to register service
- Loading branch information
Showing
25 changed files
with
1,314 additions
and
200 deletions.
There are no files selected for viewing
41 changes: 20 additions & 21 deletions
41
...ister/internal/mock/register/discovery.go → ...l/cmd/internal/mock/register/discovery.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2015 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
|
||
package register | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/hashicorp/consul/api" | ||
) | ||
|
||
// Config represents necessary configurations which can passed | ||
// for registering master and slave info for service discovery | ||
type Config struct { | ||
Backend string | ||
Endpoints string | ||
} | ||
|
||
// Validate returns nil if the config is valid, else returns error with | ||
// appropriate reason | ||
func (config *Config) Validate() error { | ||
switch config.Backend { | ||
case "consul": | ||
addresses := strings.Split(config.Endpoints, ",") | ||
if len(addresses) != 1 { | ||
return fmt.Errorf("consul does not support multiple endpoints: %s", config.Endpoints) | ||
} | ||
_, err := url.Parse(config.Endpoints) | ||
return err | ||
default: | ||
return fmt.Errorf("unknown register backend: %q", config.Backend) | ||
} | ||
} | ||
|
||
// ConsulConfig returns consul.api.ConsulConfig if register endpoint is valid consul url | ||
// else will return error with appropriate reason | ||
func (config *Config) ConsulConfig() (*api.Config, error) { | ||
url, err := url.Parse(config.Endpoints) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &api.Config{Address: url.Host, Scheme: url.Scheme}, nil | ||
} |
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,66 @@ | ||
// Copyright 2015 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
|
||
package register | ||
|
||
import "testing" | ||
|
||
func TestRegisterConfig(t *testing.T) { | ||
t.Run("validate", func(t *testing.T) { | ||
t.Run("should check for consul register backend", func(t *testing.T) { | ||
config := Config{Backend: "something other than consul"} | ||
err := config.Validate() | ||
|
||
if err == nil || err.Error() != "unknown register backend: \"something other than consul\"" { | ||
t.Errorf("expected unknown register backend but got %s", err.Error()) | ||
} | ||
}) | ||
|
||
t.Run("should not return any error if all valid configurations are specified", func(t *testing.T) { | ||
config := Config{Backend: "consul"} | ||
err := config.Validate() | ||
|
||
if err != nil { | ||
t.Errorf("expected no error but got '%v'", err.Error()) | ||
} | ||
}) | ||
|
||
t.Run("should not support multiple addresses", func(t *testing.T) { | ||
config := Config{Backend: "consul", Endpoints: "http://127.0.0.1:8500,http://127.0.0.2:8500"} | ||
err := config.Validate() | ||
|
||
if err == nil || err.Error() != "consul does not support multiple endpoints: http://127.0.0.1:8500,http://127.0.0.2:8500" { | ||
t.Errorf("expected unknown register backend but got %s", err.Error()) | ||
} | ||
}) | ||
}) | ||
|
||
t.Run("config", func(t *testing.T) { | ||
t.Run("should return config", func(t *testing.T) { | ||
c := Config{Backend: "consul", Endpoints: "http://127.0.0.1:8500"} | ||
config, err := c.ConsulConfig() | ||
|
||
if err != nil { | ||
t.Errorf("expected error to be nil but got %s", err.Error()) | ||
} | ||
|
||
if config.Address != "127.0.0.1:8500" { | ||
t.Errorf("expected address to be %s but got %s", c.Endpoints, config.Address) | ||
} | ||
|
||
if config.Scheme != "http" { | ||
t.Errorf("expected address to be http but got %s", config.Scheme) | ||
} | ||
}) | ||
}) | ||
} |
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.