Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
attempting to update and delete rules which do not exist now return i…
Browse files Browse the repository at this point in the history
…nvalid input error
  • Loading branch information
Jake Skelcy committed Apr 11, 2018
1 parent 522ae9f commit 1baf006
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
23 changes: 12 additions & 11 deletions rules/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ const (
)

var (
errNilRuleSetSchema = errors.New("nil rule set schema")
errRuleSetNotTombstoned = errors.New("ruleset is not tombstoned")
errRuleNotFound = errors.New("rule not found")
errNoRuleSnapshots = errors.New("rule has no snapshots")
ruleActionErrorFmt = "cannot %s rule %s"
ruleSetActionErrorFmt = "cannot %s ruleset %s"
unknownOpTypeFmt = "unknown op type %v, op"
errNilRuleSetSchema = errors.New("nil rule set schema")
errRuleSetNotTombstoned = errors.New("ruleset is not tombstoned")
errRuleNotFound = errors.New("rule not found")
errNoRuleSnapshots = errors.New("rule has no snapshots")
ruleActionErrorFmt = "cannot %s rule %s"
ruleIDNotFoundErrorFmt = "no rule with id %v"
ruleSetActionErrorFmt = "cannot %s ruleset %s"
unknownOpTypeFmt = "unknown op type %v, op"
)

// Matcher matches metrics against rules to determine applicable policies.
Expand Down Expand Up @@ -782,7 +783,7 @@ func (rs *ruleSet) AddMappingRule(mrv models.MappingRuleView, meta UpdateMetadat
func (rs *ruleSet) UpdateMappingRule(mrv models.MappingRuleView, meta UpdateMetadata) error {
m, err := rs.getMappingRuleByID(mrv.ID)
if err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "update", mrv.ID))
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, mrv.ID))
}
if err := m.addSnapshot(
mrv.Name,
Expand All @@ -799,7 +800,7 @@ func (rs *ruleSet) UpdateMappingRule(mrv models.MappingRuleView, meta UpdateMeta
func (rs *ruleSet) DeleteMappingRule(id string, meta UpdateMetadata) error {
m, err := rs.getMappingRuleByID(id)
if err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "delete", id))
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, id))
}

if err := m.markTombstoned(meta); err != nil {
Expand Down Expand Up @@ -842,7 +843,7 @@ func (rs *ruleSet) AddRollupRule(rrv models.RollupRuleView, meta UpdateMetadata)
func (rs *ruleSet) UpdateRollupRule(rrv models.RollupRuleView, meta UpdateMetadata) error {
r, err := rs.getRollupRuleByID(rrv.ID)
if err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "update", rrv.ID))
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, rrv.ID))
}
targets := newRollupTargetsFromView(rrv.Targets)
if err = r.addSnapshot(
Expand All @@ -860,7 +861,7 @@ func (rs *ruleSet) UpdateRollupRule(rrv models.RollupRuleView, meta UpdateMetada
func (rs *ruleSet) DeleteRollupRule(id string, meta UpdateMetadata) error {
r, err := rs.getRollupRuleByID(id)
if err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "delete", id))
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, id))
}

if err := r.markTombstoned(meta); err != nil {
Expand Down
12 changes: 2 additions & 10 deletions rules/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3165,12 +3165,8 @@ func TestApplyMappingRuleChangesUpdateFailure(t *testing.T) {
changes.MappingRuleChanges,
helper.NewUpdateMetadata(100, "validAuthor"),
)

require.Error(t, err)
containedErr, ok := err.(xerrors.ContainedError)
require.True(t, ok)
err = containedErr.InnerError()
require.Equal(t, errRuleNotFound, err)
require.IsType(t, errors.NewInvalidInputError(""), err)
}

func TestApplyRollupRuleChangesUpdateFailure(t *testing.T) {
Expand All @@ -3182,12 +3178,8 @@ func TestApplyRollupRuleChangesUpdateFailure(t *testing.T) {
changes.RollupRuleChanges,
helper.NewUpdateMetadata(100, "validAuthor"),
)

require.Error(t, err)
containedErr, ok := err.(xerrors.ContainedError)
require.True(t, ok)
err = containedErr.InnerError()
require.Equal(t, errRuleNotFound, err)
require.IsType(t, errors.NewInvalidInputError(""), err)
}

func TestApplyRollupRuleWithInvalidOp(t *testing.T) {
Expand Down

0 comments on commit 1baf006

Please sign in to comment.