diff --git a/api/core/v3/entity_config.go b/api/core/v3/entity_config.go new file mode 100644 index 0000000000..99e9f42484 --- /dev/null +++ b/api/core/v3/entity_config.go @@ -0,0 +1,11 @@ +package v3 + +import ( + corev2 "github.com/sensu/sensu-go/api/core/v2" +) + +var entityConfigRBACName = (&corev2.Entity{}).RBACName() + +func (*EntityConfig) rbacName() string { + return entityConfigRBACName +} diff --git a/api/core/v3/entity_state.go b/api/core/v3/entity_state.go new file mode 100644 index 0000000000..eee78a2d28 --- /dev/null +++ b/api/core/v3/entity_state.go @@ -0,0 +1,11 @@ +package v3 + +import ( + corev2 "github.com/sensu/sensu-go/api/core/v2" +) + +var entityStateRBACName = (&corev2.Entity{}).RBACName() + +func (*EntityState) rbacName() string { + return entityStateRBACName +} diff --git a/api/core/v3/resource_generated_test.go b/api/core/v3/resource_generated_test.go index e7eb988fa4..e4f9e7c367 100644 --- a/api/core/v3/resource_generated_test.go +++ b/api/core/v3/resource_generated_test.go @@ -679,20 +679,4 @@ func TestResourceUniqueness(t *testing.T) { if got, want := len(types), len(typeMap)/2; got != want { t.Fatalf("bad number of types: got %d, want %d", got, want) } - rbacNames := make(map[string]bool) - for _, v := range types { - if name := v.RBACName(); rbacNames[name] { - t.Errorf("duplicate rbac name: %s", name) - } else { - rbacNames[name] = true - } - } - storeNames := make(map[string]bool) - for _, v := range types { - if name := v.StoreName(); storeNames[name] { - t.Errorf("duplicate store suffix: %s", name) - } else { - storeNames[name] = true - } - } } diff --git a/api/core/v3/resource_generated_test.tmpl b/api/core/v3/resource_generated_test.tmpl index 359c771f7c..5df4846203 100644 --- a/api/core/v3/resource_generated_test.tmpl +++ b/api/core/v3/resource_generated_test.tmpl @@ -186,20 +186,4 @@ func TestResourceUniqueness(t *testing.T) { if got, want := len(types), len(typeMap)/2; got != want { t.Fatalf("bad number of types: got %d, want %d", got, want) } - rbacNames := make(map[string]bool) - for _, v := range types { - if name := v.RBACName(); rbacNames[name] { - t.Errorf("duplicate rbac name: %s", name) - } else { - rbacNames[name] = true - } - } - storeNames := make(map[string]bool) - for _, v := range types { - if name := v.StoreName(); storeNames[name] { - t.Errorf("duplicate store suffix: %s", name) - } else { - storeNames[name] = true - } - } } diff --git a/api/core/v3/typemap.go b/api/core/v3/typemap.go index 0d0591971a..e887b435f5 100644 --- a/api/core/v3/typemap.go +++ b/api/core/v3/typemap.go @@ -15,11 +15,9 @@ func init() { for _, v := range typeMap { if r, ok := v.(Resource); ok { rbacMap[r.RBACName()] = r + storeMap[r.StoreName()] = r } } - for _, v := range rbacMap { - storeMap[v.StoreName()] = v - } types.RegisterResolver("core/v3", ResolveRawResource) } @@ -103,8 +101,14 @@ func ResolveResourceByStoreName(name string) (Resource, error) { // ListResources lists all of the resources in the package. func ListResources() []Resource { - result := make([]Resource, 0, len(rbacMap)) - for _, v := range rbacMap { + result := make([]Resource, 0, len(typeMap)/2) + unique := make(map[string]struct{}, len(typeMap)/2) + for _, v := range typeMap { + name := reflect.ValueOf(v).Elem().Type().Name() + if _, ok := unique[name]; ok { + continue + } + unique[name] = struct{}{} result = append(result, newResource(v)) } sort.Slice(result, func(i, j int) bool { diff --git a/api/core/v3/typemap.tmpl b/api/core/v3/typemap.tmpl index 2798ec3b92..7a46f31306 100644 --- a/api/core/v3/typemap.tmpl +++ b/api/core/v3/typemap.tmpl @@ -15,11 +15,9 @@ func init() { for _, v := range typeMap { if r, ok := v.(Resource); ok { rbacMap[r.RBACName()] = r + storeMap[r.StoreName()] = r } } - for _, v := range rbacMap { - storeMap[v.StoreName()] = v - } types.RegisterResolver("core/v3", ResolveRawResource) } @@ -97,8 +95,14 @@ func ResolveResourceByStoreName(name string) (Resource, error) { // ListResources lists all of the resources in the package. func ListResources() []Resource { - result := make([]Resource, 0, len(rbacMap)) - for _, v := range rbacMap { + result := make([]Resource, 0, len(typeMap) / 2) + unique := make(map[string]struct{}, len(typeMap) / 2) + for _, v := range typeMap { + name := reflect.ValueOf(v).Elem().Type().Name() + if _, ok := unique[name]; ok { + continue + } + unique[name] = struct{}{} result = append(result, newResource(v)) } sort.Slice(result, func(i, j int) bool {