Skip to content

Commit

Permalink
Add Fields() methods to corev2 resources (#4774)
Browse files Browse the repository at this point in the history
Signed-off-by: James Phillips <[email protected]>
(cherry picked from commit b913fb2)
  • Loading branch information
jamesdphillips authored and c-kruse committed Sep 28, 2022
1 parent 638b93a commit 25cace3
Show file tree
Hide file tree
Showing 31 changed files with 273 additions and 38 deletions.
5 changes: 5 additions & 0 deletions api/core/v2/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func APIKeyFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent the resource.
func (a *APIKey) Fields() map[string]string {
return APIKeyFields(a)
}

// SetNamespace sets the namespace of the resource.
func (a *APIKey) SetNamespace(namespace string) {}

Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAPIKeyValidate(t *testing.T) {
func TestAPIKeyFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand Down Expand Up @@ -73,9 +73,9 @@ func TestAPIKeyFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := APIKeyFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("APIKeyFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("APIKey.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func AssetFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent that resource
func (a *Asset) Fields() map[string]string {
return AssetFields(a)
}

// SetNamespace sets the namespace of the resource.
func (a *Asset) SetNamespace(namespace string) {
a.Namespace = namespace
Expand Down
4 changes: 2 additions & 2 deletions api/core/v2/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestValidateName_GH3344(t *testing.T) {
func TestAssetFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestAssetFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AssetFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("AssetFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/check_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,8 @@ func CheckConfigFields(r Resource) map[string]string {

return fields
}

// CheckConfigFields returns a set of fields that represent that resource
func (c *CheckConfig) Fields() map[string]string {
return CheckConfigFields(c)
}
18 changes: 15 additions & 3 deletions api/core/v2/check_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestSortCheckConfigsByName(t *testing.T) {
func TestCheckConfigFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand All @@ -166,6 +166,18 @@ func TestCheckConfigFields(t *testing.T) {
wantKey: "check.round_robin",
want: "true",
},
{
name: "exposes pipelines",
args: &CheckConfig{
Pipelines: []*ResourceReference{{
Name: "xxx",
Type: "yyy",
APIVersion: "zzz",
}},
},
wantKey: "check.pipelines",
want: "zzz.yyy(Name=xxx)",
},
{
name: "exposes labels",
args: &CheckConfig{
Expand All @@ -179,9 +191,9 @@ func TestCheckConfigFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := CheckConfigFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("CheckConfigFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("CheckConfig.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ func EntityFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent that resource
func (e *Entity) Fields() map[string]string {
return EntityFields(e)
}

// SetNamespace sets the namespace of the resource.
func (e *Entity) SetNamespace(namespace string) {
e.Namespace = namespace
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestEntityMarshal(t *testing.T) {
func TestEntityFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand Down Expand Up @@ -85,9 +85,9 @@ func TestEntityFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EntityFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("EntityFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("Entity.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ func EventFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent that resource
func (e *Event) Fields() map[string]string {
return EventFields(e)
}

func isSilenced(e *Event) string {
if len(e.Check.Silenced) > 0 {
return "true"
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func TestUnmarshalID(t *testing.T) {
func TestEventFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand Down Expand Up @@ -816,9 +816,9 @@ func TestEventFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EventFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("EventFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("Event.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func ExtensionFields(r Resource) map[string]string {
return fields
}

// ExtensionFields returns a set of fields that represent that resource
func (e *Extension) Fields() map[string]string {
return ExtensionFields(e)
}

// SetNamespace sets the namespace of the resource.
func (e *Extension) SetNamespace(namespace string) {
e.Namespace = namespace
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestExtensionFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand All @@ -31,9 +31,9 @@ func TestExtensionFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ExtensionFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("ExtensionFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("Extension.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions api/core/v2/fielder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v2

// Fielder includes a set of fields that represent a resource.
type Fielder interface {
// Fields returns a set of fields that represent the resource.
Fields() map[string]string
}
8 changes: 6 additions & 2 deletions api/core/v2/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/sensu/sensu-go/api/core/v2/internal/js"
stringsutil "github.com/sensu/sensu-go/api/core/v2/internal/stringutil"
utilstrings "github.com/sensu/sensu-go/api/core/v2/internal/stringutil"
)

Expand Down Expand Up @@ -173,10 +172,15 @@ func EventFilterFields(r Resource) map[string]string {
"filter.action": resource.Action,
"filter.runtime_assets": strings.Join(resource.RuntimeAssets, ","),
}
stringsutil.MergeMapWithPrefix(fields, resource.ObjectMeta.Labels, "filter.labels.")
utilstrings.MergeMapWithPrefix(fields, resource.ObjectMeta.Labels, "filter.labels.")
return fields
}

// Fields returns a set of fields that represent that resource
func (e *EventFilter) Fields() map[string]string {
return EventFilterFields(e)
}

// SetNamespace sets the namespace of the resource.
func (f *EventFilter) SetNamespace(namespace string) {
f.Namespace = namespace
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestEventFilterValidate(t *testing.T) {
func TestEventFilterFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand Down Expand Up @@ -78,9 +78,9 @@ func TestEventFilterFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EventFilterFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("EventFilterFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("EventFilter.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ func HandlerFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent that resource
func (h *Handler) Fields() map[string]string {
return HandlerFields(h)
}

// SetNamespace sets the namespace of the resource.
func (h *Handler) SetNamespace(namespace string) {
h.Namespace = namespace
Expand Down
4 changes: 2 additions & 2 deletions api/core/v2/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestSortHandlersByName(t *testing.T) {
func TestHandlerFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestHandlerFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := HandlerFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("HandlerFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ func HookConfigFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent that resource
func (h *HookConfig) Fields() map[string]string {
return HookConfigFields(h)
}

// SetNamespace sets the namespace of the resource.
func (c *HookConfig) SetNamespace(namespace string) {
c.Namespace = namespace
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestHookUnmarshal_GH1520(t *testing.T) {
func TestHookConfigFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand All @@ -135,9 +135,9 @@ func TestHookConfigFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := HookConfigFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("HookConfigFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("HookConfig.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func MutatorFields(r Resource) map[string]string {
return fields
}

// Fields returns a set of fields that represent that resource
func (m *Mutator) Fields() map[string]string {
return MutatorFields(m)
}

// SetNamespace sets the namespace of the resource.
func (m *Mutator) SetNamespace(namespace string) {
m.Namespace = namespace
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestSortMutatorsByName(t *testing.T) {
func TestMutatorFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand All @@ -93,9 +93,9 @@ func TestMutatorFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := MutatorFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("MutatorFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("Mutator.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions api/core/v2/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func NamespaceFields(r Resource) map[string]string {
}
}

// Fields returns a set of fields that represent that resource
func (n *Namespace) Fields() map[string]string {
return NamespaceFields(n)
}

// SetNamespace sets the namespace of the resource.
func (n *Namespace) SetNamespace(namespace string) {
}
Expand Down
6 changes: 3 additions & 3 deletions api/core/v2/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestNamespaceFields(t *testing.T) {
tests := []struct {
name string
args Resource
args Fielder
wantKey string
want string
}{
Expand All @@ -21,9 +21,9 @@ func TestNamespaceFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NamespaceFields(tt.args)
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("NamespaceFields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
t.Errorf("Namespace.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
Expand Down
Loading

0 comments on commit 25cace3

Please sign in to comment.