-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GraphQL] List entity config & state resources (#4808)
* Expose `EntityConfig` & `EntityState` resources through GraphQL service * Add `Fields()` method to `EntityConfig` & `EntityState` resources so that they implement the `Fielder` interface; allows users to filter with field selectors. * Complication: The core/v3 package previous checked that each resource in the package had a unique `RBACName()`, however, since we don't want distinct names for the config & state resources this proved to be problematic. As such I've disabled the uniqueness tests for the time being. (Same as: #4807) Signed-off-by: James Phillips <[email protected]>
- Loading branch information
1 parent
e2d8098
commit f3bc8f5
Showing
27 changed files
with
2,690 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package v3 | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
corev2 "github.com/sensu/sensu-go/api/core/v2" | ||
) | ||
|
||
var entityConfigRBACName = (&corev2.Entity{}).RBACName() | ||
|
||
func (e *EntityConfig) rbacName() string { | ||
return entityConfigRBACName | ||
} | ||
|
||
func (e *EntityConfig) Fields() map[string]string { | ||
fields := map[string]string{ | ||
"entity_config.name": e.Metadata.Name, | ||
"entity_config.namespace": e.Metadata.Namespace, | ||
"entity_config.deregister": strconv.FormatBool(e.Deregister), | ||
"entity_config.entity_class": e.EntityClass, | ||
"entity_config.subscriptions": strings.Join(e.Subscriptions, ","), | ||
} | ||
MergeMapWithPrefix(fields, e.Metadata.Labels, "entity_config.labels.") | ||
return fields | ||
} | ||
|
||
// MergeMapWithPrefix merges contents of one map into another using a prefix. | ||
func MergeMapWithPrefix(a map[string]string, b map[string]string, prefix string) { | ||
for k, v := range b { | ||
a[prefix+k] = v | ||
} | ||
} |
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,60 @@ | ||
package v3 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
v2 "github.com/sensu/sensu-go/api/core/v2" | ||
) | ||
|
||
func TestEntityConfigFields(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args Fielder | ||
wantKey string | ||
want string | ||
}{ | ||
{ | ||
name: "exposes name", | ||
args: FixtureEntityConfig("my-agent"), | ||
wantKey: "entity_config.name", | ||
want: "my-agent", | ||
}, | ||
{ | ||
name: "exposes deregister", | ||
args: &EntityConfig{Metadata: &v2.ObjectMeta{}, Deregister: true}, | ||
wantKey: "entity_config.deregister", | ||
want: "true", | ||
}, | ||
{ | ||
name: "exposes class", | ||
args: &EntityConfig{Metadata: &v2.ObjectMeta{}, EntityClass: "agent"}, | ||
wantKey: "entity_config.entity_class", | ||
want: "agent", | ||
}, | ||
{ | ||
name: "exposes subscriptions", | ||
args: &EntityConfig{Metadata: &v2.ObjectMeta{}, Subscriptions: []string{"www", "unix"}}, | ||
wantKey: "entity_config.subscriptions", | ||
want: "www,unix", | ||
}, | ||
{ | ||
name: "exposes labels", | ||
args: &EntityConfig{ | ||
Metadata: &v2.ObjectMeta{ | ||
Labels: map[string]string{"region": "philadelphia"}, | ||
}, | ||
}, | ||
wantKey: "entity_config.labels.region", | ||
want: "philadelphia", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.args.Fields() | ||
if !reflect.DeepEqual(got[tt.wantKey], tt.want) { | ||
t.Errorf("EntityConfig.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,19 @@ | ||
package v3 | ||
|
||
import ( | ||
corev2 "github.com/sensu/sensu-go/api/core/v2" | ||
) | ||
|
||
var entityStateRBACName = (&corev2.Entity{}).RBACName() | ||
|
||
func (*EntityState) rbacName() string { | ||
return entityStateRBACName | ||
} | ||
|
||
func (e *EntityState) Fields() map[string]string { | ||
fields := map[string]string{ | ||
"entity_state.name": e.Metadata.Name, | ||
"entity_state.namespace": e.Metadata.Namespace, | ||
} | ||
return fields | ||
} |
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,36 @@ | ||
package v3 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestEntityStateFields(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args Fielder | ||
wantKey string | ||
want string | ||
}{ | ||
{ | ||
name: "exposes name", | ||
args: FixtureEntityState("my-agent"), | ||
wantKey: "entity_state.name", | ||
want: "my-agent", | ||
}, | ||
{ | ||
name: "exposes deregister", | ||
args: FixtureEntityState("my-agent"), | ||
wantKey: "entity_state.namespace", | ||
want: "default", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.args.Fields() | ||
if !reflect.DeepEqual(got[tt.wantKey], tt.want) { | ||
t.Errorf("EntityState.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,7 @@ | ||
package v3 | ||
|
||
// 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 | ||
} |
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
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
Oops, something went wrong.