-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make manager.Config have it's own Read and Merge methods
also moved the Config type to it's own file Signed-off-by: Madhav Puri <[email protected]>
- Loading branch information
Showing
5 changed files
with
195 additions
and
190 deletions.
There are no files selected for viewing
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package manager | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/contiv/cluster/management/src/boltdb" | ||
"github.com/contiv/cluster/management/src/collins" | ||
"github.com/contiv/cluster/management/src/configuration" | ||
"github.com/contiv/errored" | ||
"github.com/imdario/mergo" | ||
"github.com/mapuri/serf/client" | ||
) | ||
|
||
type clustermConfig struct { | ||
Addr string `json:"addr"` | ||
} | ||
|
||
type inventorySubsysConfig struct { | ||
Collins *collins.Config `json:"collins,omitempty"` | ||
BoltDB *boltdb.Config `json:"boltdb,omitempty"` | ||
} | ||
|
||
// Config is the configuration to cluster manager daemon | ||
type Config struct { | ||
Serf client.Config `json:"serf"` | ||
Inventory inventorySubsysConfig `json:"inventory"` | ||
Ansible configuration.AnsibleSubsysConfig `json:"ansible"` | ||
Manager clustermConfig `json:"manager"` | ||
} | ||
|
||
// DefaultConfig returns the default configuration values for the cluster manager | ||
// and it's sub-systems | ||
func DefaultConfig() *Config { | ||
return &Config{ | ||
Serf: client.Config{ | ||
Addr: "127.0.0.1:7373", | ||
}, | ||
Inventory: inventorySubsysConfig{ | ||
BoltDB: nil, | ||
Collins: nil, | ||
}, | ||
Ansible: configuration.AnsibleSubsysConfig{ | ||
ConfigurePlaybook: "site.yml", | ||
CleanupPlaybook: "cleanup.yml", | ||
UpgradePlaybook: "rolling-upgrade.yml", | ||
PlaybookLocation: "/vagrant/vendor/ansible", | ||
User: "vagrant", | ||
PrivKeyFile: "/vagrant/management/src/demo/files/insecure_private_key", | ||
}, | ||
Manager: clustermConfig{ | ||
Addr: "0.0.0.0:9007", | ||
}, | ||
} | ||
} | ||
|
||
// Read parses the configuration from the specified reader | ||
func (c *Config) Read(r io.Reader) error { | ||
bytes, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := json.Unmarshal(bytes, c); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Merge merges the passed configuration into the receiving configuration | ||
func (c *Config) Merge(src *Config) error { | ||
if err := mergo.MergeWithOverwrite(c, src); err != nil { | ||
return errored.Errorf("failed to merge configuration. Error: %s", err) | ||
} | ||
return 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,95 @@ | ||
// +build unittest | ||
|
||
package manager | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/contiv/cluster/management/src/boltdb" | ||
"github.com/contiv/cluster/management/src/collins" | ||
"github.com/contiv/cluster/management/src/configuration" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type configSuite struct { | ||
} | ||
|
||
var _ = Suite(&configSuite{}) | ||
|
||
func (s *configSuite) TestReadConfigSuccess(c *C) { | ||
config := &Config{} | ||
confStr := `{ | ||
"ansible" : { | ||
"playbook_location" : "foo" | ||
} | ||
}` | ||
err := config.Read(strings.NewReader(confStr)) | ||
c.Assert(err, IsNil) | ||
c.Assert(config.Ansible.PlaybookLocation, Equals, "foo") | ||
} | ||
|
||
func (s *configSuite) TestReadConfigInvalidJSON(c *C) { | ||
config := &Config{} | ||
confStr := `{ | ||
"ansible" : { | ||
"playbook_location" : "extra-comma-error", | ||
} | ||
}` | ||
err := config.Read(strings.NewReader(confStr)) | ||
c.Assert(err, NotNil) | ||
} | ||
|
||
func (s *configSuite) TestMergeConfigSuccess(c *C) { | ||
dst := DefaultConfig() | ||
src := &Config{ | ||
Ansible: configuration.AnsibleSubsysConfig{PlaybookLocation: "override-location"}, | ||
} | ||
exptdDst := DefaultConfig() | ||
exptdDst.Ansible.PlaybookLocation = "override-location" | ||
|
||
err := dst.Merge(src) | ||
c.Assert(err, IsNil) | ||
c.Assert(dst, DeepEquals, exptdDst) | ||
} | ||
|
||
func (s *configSuite) TestMergeConfigSuccessNoInventory(c *C) { | ||
dst := DefaultConfig() | ||
src := &Config{} | ||
exptdDst := DefaultConfig() | ||
|
||
err := dst.Merge(src) | ||
c.Assert(err, IsNil) | ||
c.Assert(dst, DeepEquals, exptdDst) | ||
c.Assert(dst.Inventory.Collins, Equals, (*collins.Config)(nil)) | ||
c.Assert(dst.Inventory.BoltDB, Equals, (*boltdb.Config)(nil)) | ||
} | ||
|
||
func (s *configSuite) TestMergeConfigSuccessCollinsInventory(c *C) { | ||
dst := DefaultConfig() | ||
src := &Config{ | ||
Inventory: inventorySubsysConfig{Collins: &collins.Config{}}, | ||
} | ||
exptdDst := DefaultConfig() | ||
exptdDst.Inventory.Collins = &collins.Config{} | ||
|
||
err := dst.Merge(src) | ||
c.Assert(err, IsNil) | ||
c.Assert(dst, DeepEquals, exptdDst) | ||
c.Assert(dst.Inventory.BoltDB, Equals, (*boltdb.Config)(nil)) | ||
c.Assert(dst.Inventory.Collins, DeepEquals, exptdDst.Inventory.Collins) | ||
} | ||
|
||
func (s *configSuite) TestMergeConfigSuccessBoltdbInventory(c *C) { | ||
dst := DefaultConfig() | ||
src := &Config{ | ||
Inventory: inventorySubsysConfig{BoltDB: &boltdb.Config{}}, | ||
} | ||
exptdDst := DefaultConfig() | ||
exptdDst.Inventory.BoltDB = &boltdb.Config{} | ||
|
||
err := dst.Merge(src) | ||
c.Assert(err, IsNil) | ||
c.Assert(dst, DeepEquals, exptdDst) | ||
c.Assert(dst.Inventory.BoltDB, DeepEquals, exptdDst.Inventory.BoltDB) | ||
c.Assert(dst.Inventory.Collins, Equals, (*collins.Config)(nil)) | ||
} |
Oops, something went wrong.