Skip to content

Commit

Permalink
Merge pull request #70 from mapuri/discovery
Browse files Browse the repository at this point in the history
switch to using contiv/errored for errors
  • Loading branch information
mapuri committed Mar 23, 2016
2 parents 7d42ff3 + d4aa4bc commit b7d4315
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 56 deletions.
21 changes: 19 additions & 2 deletions management/src/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions management/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ deps:

checks: deps clean-generate check-format check-code

godeps-save:
$(all_packages) | xargs godep save

generate: deps
@echo "auto generating files"
@mkdir -p mock
Expand Down
4 changes: 2 additions & 2 deletions management/src/clusterctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"os"

log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/contiv/cluster/management/src/clusterm/manager"
"github.com/contiv/errored"
)

var (
errNodeNameMissing = func(c string) error { return fmt.Errorf("command %q expects a node name", c) }
errNodeNameMissing = func(c string) error { return errored.Errorf("command %q expects a node name", c) }

clustermFlags = []cli.Flag{
cli.StringFlag{
Expand Down
5 changes: 3 additions & 2 deletions management/src/clusterm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/contiv/cluster/management/src/clusterm/manager"
"github.com/contiv/errored"
"github.com/imdario/mergo"
)

Expand Down Expand Up @@ -59,11 +60,11 @@ func main() {
func mergeConfig(dst *manager.Config, srcJSON []byte) (*manager.Config, error) {
src := &manager.Config{}
if err := json.Unmarshal(srcJSON, src); err != nil {
return nil, fmt.Errorf("failed to parse configuration. Error: %s", err)
return nil, errored.Errorf("failed to parse configuration. Error: %s", err)
}

if err := mergo.MergeWithOverwrite(dst, src); err != nil {
return nil, fmt.Errorf("failed to merge configuration. Error: %s", err)
return nil, errored.Errorf("failed to merge configuration. Error: %s", err)
}

return dst, nil
Expand Down
4 changes: 3 additions & 1 deletion management/src/clusterm/manager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"net/http"
"net/url"
"strings"

"github.com/contiv/errored"
)

var httpErrorResp = func(rsrc, status string, body []byte) error {
return fmt.Errorf("Request: %s Response status: %q. Response body: %s", rsrc, status, body)
return errored.Errorf("Request: %s Response status: %q. Response body: %s", rsrc, status, body)
}

// Client provides the methods for issuing post and get requests to cluster manager
Expand Down
5 changes: 3 additions & 2 deletions management/src/clusterm/manager/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/contiv/cluster/management/src/configuration"
"github.com/contiv/cluster/management/src/monitor"
"github.com/contiv/errored"
)

// errInvalidJSON is the error returned when an invalid json value is specified for
// the ansible extra variables configuration
var errInvalidJSON = func(name string, err error) error {
return fmt.Errorf("%q should be a valid json. Error: %s", name, err)
return errored.Errorf("%q should be a valid json. Error: %s", name, err)
}

// event associates an event to corresponding processing logic
Expand Down Expand Up @@ -192,7 +193,7 @@ func (e *nodeDecommissioned) process() error {
}

if isWorkerNode {
return fmt.Errorf("%q is a master node and it can only be decommissioned after all worker nodes have been decommissioned", e.nodeName)
return errored.Errorf("%q is a master node and it can only be decommissioned after all worker nodes have been decommissioned", e.nodeName)
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions management/src/clusterm/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
package manager

import (
"fmt"

"github.com/contiv/cluster/management/src/collins"
"github.com/contiv/cluster/management/src/configuration"
"github.com/contiv/cluster/management/src/inventory"
"github.com/contiv/cluster/management/src/monitor"
"github.com/contiv/errored"
"github.com/mapuri/serf/client"
)

Expand Down Expand Up @@ -83,7 +82,7 @@ type Manager struct {
// if a failure occurs as part of initialization.
func NewManager(config *Config) (*Manager, error) {
if config == nil {
return nil, fmt.Errorf("nil config passed")
return nil, errored.Errorf("nil config passed")
}

var err error
Expand All @@ -105,11 +104,11 @@ func NewManager(config *Config) (*Manager, error) {
}

if err := m.monitor.RegisterCb(monitor.Discovered, m.enqueueMonitorEvent); err != nil {
return nil, fmt.Errorf("failed to register node discovery callback. Error: %s", err)
return nil, errored.Errorf("failed to register node discovery callback. Error: %s", err)
}

if err := m.monitor.RegisterCb(monitor.Disappeared, m.enqueueMonitorEvent); err != nil {
return nil, fmt.Errorf("failed to register node disappearance callback. Error: %s", err)
return nil, errored.Errorf("failed to register node disappearance callback. Error: %s", err)
}

return m, nil
Expand Down
9 changes: 4 additions & 5 deletions management/src/clusterm/manager/utils.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package manager

import (
"fmt"

log "github.com/Sirupsen/logrus"
"github.com/contiv/cluster/management/src/inventory"
"github.com/contiv/errored"
)

func nodeNotExistsError(name string) error {
return fmt.Errorf("node with name %q doesn't exists", name)
return errored.Errorf("node with name %q doesn't exists", name)
}

func nodeConfigNotExistsError(name string) error {
return fmt.Errorf("the configuration info for node %q doesn't exist", name)
return errored.Errorf("the configuration info for node %q doesn't exist", name)
}

func nodeInventoryNotExistsError(name string) error {
return fmt.Errorf("the inventory info for node %q doesn't exist", name)
return errored.Errorf("the inventory info for node %q doesn't exist", name)
}

func (m *Manager) findNode(name string) (*node, error) {
Expand Down
22 changes: 11 additions & 11 deletions management/src/collins/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package collins

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/contiv/errored"
)

// Config denotes the configuration for collins client
Expand Down Expand Up @@ -90,7 +90,7 @@ func (c *Client) CreateAsset(tag, status string) error {
if err != nil {
body = []byte{}
}
return fmt.Errorf("status code %d unexpected. Response body: %q",
return errored.Errorf("status code %d unexpected. Response body: %q",
resp.StatusCode, body)
}

Expand All @@ -117,11 +117,11 @@ func (c *Client) GetAsset(tag string) (Asset, error) {

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Asset{}, fmt.Errorf("failed to read response body. Error: %s", err)
return Asset{}, errored.Errorf("failed to read response body. Error: %s", err)
}

if resp.StatusCode != http.StatusOK {
return Asset{}, fmt.Errorf("status code %d unexpected. Response body: %q",
return Asset{}, errored.Errorf("status code %d unexpected. Response body: %q",
resp.StatusCode, body)
}

Expand All @@ -132,7 +132,7 @@ func (c *Client) GetAsset(tag string) (Asset, error) {
} `json:"data"`
}{}
if err := json.Unmarshal(body, collinsResp); err != nil {
return Asset{}, fmt.Errorf("failed to unmarshal response. Error: %s", err)
return Asset{}, errored.Errorf("failed to unmarshal response. Error: %s", err)
}

log.Debugf("collins asset: %+v", collinsResp.Data.Asset)
Expand All @@ -155,11 +155,11 @@ func (c *Client) GetAllAssets() ([]Asset, error) {

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body. Error: %s", err)
return nil, errored.Errorf("failed to read response body. Error: %s", err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code %d unexpected. Response body: %q",
return nil, errored.Errorf("status code %d unexpected. Response body: %q",
resp.StatusCode, body)
}

Expand All @@ -172,7 +172,7 @@ func (c *Client) GetAllAssets() ([]Asset, error) {
} `json:"data"`
}{}
if err := json.Unmarshal(body, collinsResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response. Error: %s", err)
return nil, errored.Errorf("failed to unmarshal response. Error: %s", err)
}

assets := []Asset{}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *Client) CreateState(name, description, status string) error {
if err != nil {
body = []byte{}
}
return fmt.Errorf("status code %d unexpected. Response body: %q",
return errored.Errorf("status code %d unexpected. Response body: %q",
resp.StatusCode, body)
}

Expand All @@ -222,7 +222,7 @@ func (c *Client) CreateState(name, description, status string) error {

// AddAssetLog creates a log entry for an asset
func (c *Client) AddAssetLog(tag, mtype, message string) error {
return fmt.Errorf("not implemented")
return errored.Errorf("not implemented")
}

// SetAssetStatus sets the status of an asset
Expand All @@ -249,7 +249,7 @@ func (c *Client) SetAssetStatus(tag, status, state, reason string) error {
if err != nil {
body = []byte{}
}
return fmt.Errorf("status code %d unexpected. Response body: %q",
return errored.Errorf("status code %d unexpected. Response body: %q",
resp.StatusCode, body)
}

Expand Down
10 changes: 5 additions & 5 deletions management/src/configuration/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package configuration

import (
"encoding/json"
"fmt"
"io"
"strings"

"github.com/contiv/cluster/management/src/ansible"
"github.com/contiv/errored"
"github.com/imdario/mergo"
)

Expand Down Expand Up @@ -96,17 +96,17 @@ func mergeExtraVars(dst, src string) (string, error) {
)

if err := json.Unmarshal([]byte(dst), &d); err != nil {
return "", fmt.Errorf("failed to unmarshal dest extra vars %q. Error: %v", dst, err)
return "", errored.Errorf("failed to unmarshal dest extra vars %q. Error: %v", dst, err)
}
if err := json.Unmarshal([]byte(src), &s); err != nil {
return "", fmt.Errorf("failed to unmarshal src extra vars %q. Error: %v", src, err)
return "", errored.Errorf("failed to unmarshal src extra vars %q. Error: %v", src, err)
}
if err := mergo.MergeWithOverwrite(&d, &s); err != nil {
return "", fmt.Errorf("failed to merge extra vars, dst: %q src: %q. Error: %v", dst, src, err)
return "", errored.Errorf("failed to merge extra vars, dst: %q src: %q. Error: %v", dst, src, err)
}
o, err := json.Marshal(d)
if err != nil {
return "", fmt.Errorf("failed to marshal resulting extra vars %q. Error: %v", o, err)
return "", errored.Errorf("failed to marshal resulting extra vars %q. Error: %v", o, err)
}

return string(o), nil
Expand Down
11 changes: 8 additions & 3 deletions management/src/inventory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package inventory

import (
"encoding/json"
"fmt"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/contiv/cluster/management/src/collins"
"github.com/contiv/errored"
)

var description = map[AssetState]string{
Expand All @@ -15,6 +15,11 @@ var description = map[AssetState]string{
Disappeared: "Node has disappeared from monitoring subsystem. Check for possible hardware or network issues",
}

var (
errAssetExists = func(tag string) error { return errored.Errorf("asset %q already exists", tag) }
errAssetNotExists = func(tag string) error { return errored.Errorf("asset %q doesn't exists", tag) }
)

// collinsStatusMap maps the status strings to corresponding enumerated values
var collinsStatusMap = map[string]AssetStatus{
Incomplete.String(): Incomplete,
Expand Down Expand Up @@ -161,11 +166,11 @@ func (a *Asset) SetStatus(status AssetStatus, state AssetState) error {
}

if _, ok := lifecycleStatus[a.status][status]; !ok && a.status != status {
return fmt.Errorf("transition from %q to %q is not allowed", a.status, status)
return errored.Errorf("transition from %q to %q is not allowed", a.status, status)
}

if _, ok := lifecycleStates[status][state]; !ok {
return fmt.Errorf("%q is not a valid state when asset is in %q status", state, status)
return errored.Errorf("%q is not a valid state when asset is in %q status", state, status)
}

if err := a.client.SetAssetStatus(a.name, status.String(), state.String(), description[state]); err != nil {
Expand Down
Loading

0 comments on commit b7d4315

Please sign in to comment.