-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from maier/CIRC-8780
v0.0.9 [CIRC-8780]
- Loading branch information
Showing
15 changed files
with
530 additions
and
38 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 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
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
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,62 @@ | ||
package trapcheck | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/circonus-labs/go-apiclient" | ||
) | ||
|
||
func (tc *TrapCheck) UpdateCheckTags(ctx context.Context, tags []string) (*apiclient.CheckBundle, error) { | ||
if tc.checkBundle == nil { | ||
return nil, fmt.Errorf("invalid state, check bundle is nil") | ||
} | ||
if len(tags) == 0 { | ||
return nil, nil | ||
} | ||
|
||
update := false | ||
for _, tag := range tags { | ||
if tag == "" { | ||
continue | ||
} | ||
found := false | ||
tagParts := strings.SplitN(tag, ":", 2) | ||
for j, ctag := range tc.checkBundle.Tags { | ||
if tag == ctag { | ||
found = true | ||
break | ||
} | ||
|
||
ctagParts := strings.SplitN(ctag, ":", 2) | ||
if len(tagParts) == len(ctagParts) { | ||
if tagParts[0] == ctagParts[0] { | ||
if tagParts[1] != ctagParts[1] { | ||
tc.Log.Warnf("modifying tag: new: %v old: %v", tagParts, ctagParts) | ||
tc.checkBundle.Tags[j] = tag | ||
update = true // but force update since we're modifying a tag | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
} | ||
} | ||
if !found { | ||
tc.Log.Warnf("adding missing tag: %s curr: %v", tag, tc.checkBundle.Tags) | ||
tc.checkBundle.Tags = append(tc.checkBundle.Tags, tag) | ||
update = true | ||
} | ||
} | ||
|
||
if update { | ||
b, err := tc.client.UpdateCheckBundle(tc.checkBundle) | ||
if err != nil { | ||
return nil, fmt.Errorf("api updating check bundle tags: %w", err) | ||
} | ||
return b, nil | ||
} | ||
|
||
return nil, 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,132 @@ | ||
package trapcheck | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/circonus-labs/go-apiclient" | ||
) | ||
|
||
func TestTrapCheck_UpdateCheckTags(t *testing.T) { | ||
tc := &TrapCheck{} | ||
tc.Log = &LogWrapper{ | ||
Log: log.New(io.Discard, "", log.LstdFlags), | ||
Debug: false, | ||
} | ||
|
||
tests := []struct { | ||
client API | ||
bundle *apiclient.CheckBundle | ||
want *apiclient.CheckBundle | ||
name string | ||
newTags []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "new tag", | ||
bundle: &apiclient.CheckBundle{ | ||
Tags: []string{"foo"}, | ||
}, | ||
newTags: []string{"bar"}, | ||
want: &apiclient.CheckBundle{ | ||
Tags: []string{"foo", "bar"}, | ||
}, | ||
wantErr: false, | ||
client: &APIMock{ | ||
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) { | ||
return cfg, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "new tag, ignore blank", | ||
bundle: &apiclient.CheckBundle{ | ||
Tags: []string{"foo"}, | ||
}, | ||
newTags: []string{"bar", ""}, | ||
want: &apiclient.CheckBundle{ | ||
Tags: []string{"foo", "bar"}, | ||
}, | ||
wantErr: false, | ||
client: &APIMock{ | ||
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) { | ||
return cfg, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "modify tag", | ||
bundle: &apiclient.CheckBundle{ | ||
Tags: []string{"foo:bar"}, | ||
}, | ||
newTags: []string{"foo:baz"}, | ||
want: &apiclient.CheckBundle{ | ||
Tags: []string{"foo:baz"}, | ||
}, | ||
wantErr: false, | ||
client: &APIMock{ | ||
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) { | ||
return cfg, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no change", | ||
bundle: &apiclient.CheckBundle{ | ||
Tags: []string{"foo"}, | ||
}, | ||
newTags: []string{"foo"}, | ||
want: nil, | ||
wantErr: false, | ||
client: &APIMock{ | ||
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) { | ||
return cfg, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalid (nil check bundle)", | ||
bundle: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "no tags", | ||
bundle: &apiclient.CheckBundle{}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "api error", | ||
bundle: &apiclient.CheckBundle{Tags: []string{"foo"}}, | ||
newTags: []string{"bar"}, | ||
want: nil, | ||
wantErr: true, | ||
client: &APIMock{ | ||
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) { | ||
return nil, fmt.Errorf("api error 500") | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
tc.client = tt.client | ||
tc.checkBundle = tt.bundle | ||
|
||
got, err := tc.UpdateCheckTags(context.Background(), tt.newTags) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("TrapCheck.UpdateCheckTags() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("TrapCheck.UpdateCheckTags() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.