Skip to content

Commit 11cf62f

Browse files
authored
refactor: make API entities strings (#370)
## Issue N/A ## Description - Make API entities strings, otherwise the UX is terrible - Scope GetFolder to a datacenter, trim the datacenter/vm prefix, and take the first item with the folder name as a prefix. This avoids selecting, e.g., `sp-pritam/spectro-templates` before the top-level `spectro-templates` folder. --------- Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
1 parent 28f42c5 commit 11cf62f

13 files changed

Lines changed: 50 additions & 40 deletions

File tree

api/v1alpha1/vspherevalidator_types.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/validator-labs/validator/pkg/validationrule"
1010

1111
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
12-
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter/entity"
1312
"github.com/validator-labs/validator-plugin-vsphere/pkg/constants"
1413
)
1514

@@ -82,7 +81,7 @@ type ComputeResourceRule struct {
8281
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName"`
8382

8483
// Scope is the scope of the compute resource validation rule.
85-
Scope entity.Entity `json:"scope" yaml:"scope"`
84+
Scope string `json:"scope" yaml:"scope"`
8685

8786
// EntityName is the name of the entity to validate.
8887
EntityName string `json:"entityName" yaml:"entityName"`
@@ -114,7 +113,7 @@ type PrivilegeValidationRule struct {
114113
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName,omitempty"`
115114

116115
// EntityType is the type of the vCenter entity to validate.
117-
EntityType entity.Entity `json:"entityType" yaml:"entityType"`
116+
EntityType string `json:"entityType" yaml:"entityType"`
118117

119118
// EntityName is the name of the vCenter entity to validate privileges on.
120119
EntityName string `json:"entityName" yaml:"entityName"`
@@ -165,7 +164,7 @@ type TagValidationRule struct {
165164
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName"`
166165

167166
// EntityType is the type of the vCenter entity to validate.
168-
EntityType entity.Entity `json:"entityType" yaml:"entityType"`
167+
EntityType string `json:"entityType" yaml:"entityType"`
169168

170169
// EntityName is the name of the vCenter entity to validate tags on.
171170
EntityName string `json:"entityName" yaml:"entityName"`

api/vcenter/entity/entities_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestMarshalYAML(t *testing.T) {
3939
}
4040
}
4141

42-
func TestUnMarshalYAML(t *testing.T) {
42+
func TestUnmarshalYAML(t *testing.T) {
4343
// int
4444
in := []byte("8\n")
4545
expected := ResourcePool

chart/validator-plugin-vsphere/crds/validation.spectrocloud.labs_vspherevalidators.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ spec:
123123
scope:
124124
description: Scope is the scope of the compute resource validation
125125
rule.
126-
type: integer
126+
type: string
127127
required:
128128
- entityName
129129
- name
@@ -171,7 +171,7 @@ spec:
171171
entityType:
172172
description: EntityType is the type of the vCenter entity to
173173
validate.
174-
type: integer
174+
type: string
175175
name:
176176
description: RuleName is the name of the privilege validation
177177
rule.
@@ -230,7 +230,7 @@ spec:
230230
entityType:
231231
description: EntityType is the type of the vCenter entity to
232232
validate.
233-
type: integer
233+
type: string
234234
name:
235235
description: RuleName is the name of the tag validation rule.
236236
type: string

config/crd/bases/validation.spectrocloud.labs_vspherevalidators.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ spec:
123123
scope:
124124
description: Scope is the scope of the compute resource validation
125125
rule.
126-
type: integer
126+
type: string
127127
required:
128128
- entityName
129129
- name
@@ -171,7 +171,7 @@ spec:
171171
entityType:
172172
description: EntityType is the type of the vCenter entity to
173173
validate.
174-
type: integer
174+
type: string
175175
name:
176176
description: RuleName is the name of the privilege validation
177177
rule.
@@ -230,7 +230,7 @@ spec:
230230
entityType:
231231
description: EntityType is the type of the vCenter entity to
232232
validate.
233-
type: integer
233+
type: string
234234
name:
235235
description: RuleName is the name of the tag validation rule.
236236
type: string

internal/controller/vspherevalidator_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var _ = Describe("VsphereValidator controller", Ordered, func() {
4949
TagValidationRules: []v1alpha1.TagValidationRule{
5050
{
5151
RuleName: "Datacenter k8s-region rule",
52-
EntityType: entity.Datacenter,
52+
EntityType: entity.Datacenter.String(),
5353
EntityName: "Datacenter",
5454
Tag: "k8s-region",
5555
},

pkg/validate/validate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func testRules(inputs []privilegeRuleInput) []v1alpha1.PrivilegeValidationRule {
158158
for i, input := range inputs {
159159
r := v1alpha1.PrivilegeValidationRule{
160160
RuleName: fmt.Sprintf("rule %d", i),
161-
EntityType: input.EntityType,
161+
EntityType: input.EntityType.String(),
162162
EntityName: input.EntityName,
163163
Privileges: input.Privileges,
164164
}

pkg/validators/computeresources/computeresources.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (c *ValidationService) ReconcileComputeResourceValidationRule(rule v1alpha1
127127
defer cancel()
128128

129129
var res *Usage
130-
switch rule.Scope {
130+
switch e := entity.Map[rule.Scope]; e {
131131
case entity.Cluster:
132132
res, err = clusterUsage(ctx, rule, finder)
133133
case entity.ResourcePool:
@@ -371,7 +371,7 @@ func getTotalQuantity(quantity string, numberOfNodes int) resource.Quantity {
371371
// GetScopeKey returns a formatted key depending on the scope of a rule
372372
func GetScopeKey(rule v1alpha1.ComputeResourceRule) (string, error) {
373373
var key string
374-
switch rule.Scope {
374+
switch e := entity.Map[rule.Scope]; e {
375375
case entity.Cluster:
376376
key = fmt.Sprintf("%s-%s", rule.Scope, rule.EntityName)
377377
case entity.Host:

pkg/validators/computeresources/computeresources_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
4747
rule: v1alpha1.ComputeResourceRule{
4848
RuleName: "Test Resource Validation rule",
4949
ClusterName: "DC0_C0",
50-
Scope: entity.Cluster,
50+
Scope: entity.Cluster.String(),
5151
EntityName: "DC0_C0",
5252
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
5353
{
@@ -82,7 +82,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
8282
rule: v1alpha1.ComputeResourceRule{
8383
RuleName: "Test Resource Validation rule",
8484
ClusterName: "DC0_C0",
85-
Scope: entity.Cluster,
85+
Scope: entity.Cluster.String(),
8686
EntityName: "DC0_C0",
8787
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
8888
{
@@ -117,7 +117,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
117117
rule: v1alpha1.ComputeResourceRule{
118118
RuleName: "Test Resource Validation rule",
119119
ClusterName: "DC0_C0",
120-
Scope: entity.Cluster,
120+
Scope: entity.Cluster.String(),
121121
EntityName: "DC0_C0",
122122
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
123123
{
@@ -152,7 +152,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
152152
rule: v1alpha1.ComputeResourceRule{
153153
RuleName: "Test Resource Validation rule",
154154
ClusterName: "DC0_C0",
155-
Scope: entity.Cluster,
155+
Scope: entity.Cluster.String(),
156156
EntityName: "DC0_C0",
157157
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
158158
{
@@ -186,7 +186,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
186186
name: "Host - All Resources available",
187187
rule: v1alpha1.ComputeResourceRule{
188188
RuleName: "Test Host Resource Validation rule",
189-
Scope: entity.Host,
189+
Scope: entity.Host.String(),
190190
EntityName: "DC0_C0_H0",
191191
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
192192
{
@@ -220,7 +220,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
220220
name: "Host CPU not available",
221221
rule: v1alpha1.ComputeResourceRule{
222222
RuleName: "Test Host Resource Validation rule",
223-
Scope: entity.Host,
223+
Scope: entity.Host.String(),
224224
EntityName: "DC0_C0_H0",
225225
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
226226
{
@@ -254,7 +254,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
254254
name: "Host Memory not available",
255255
rule: v1alpha1.ComputeResourceRule{
256256
RuleName: "Test Host Resource Validation rule",
257-
Scope: entity.Host,
257+
Scope: entity.Host.String(),
258258
EntityName: "DC0_C0_H0",
259259
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
260260
{
@@ -288,7 +288,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
288288
name: "Host Disk not available",
289289
rule: v1alpha1.ComputeResourceRule{
290290
RuleName: "Test Host Resource Validation rule",
291-
Scope: entity.Host,
291+
Scope: entity.Host.String(),
292292
EntityName: "DC0_C0_H0",
293293
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
294294
{
@@ -322,7 +322,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
322322
name: "Resourcepool - All Resources available",
323323
rule: v1alpha1.ComputeResourceRule{
324324
RuleName: "Test Host Resource Validation rule",
325-
Scope: entity.ResourcePool,
325+
Scope: entity.ResourcePool.String(),
326326
ClusterName: "DC0_C0",
327327
EntityName: "DC0_C0_RP0",
328328
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
@@ -357,7 +357,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
357357
name: "Resourcepool CPU not available",
358358
rule: v1alpha1.ComputeResourceRule{
359359
RuleName: "Test Host Resource Validation rule",
360-
Scope: entity.ResourcePool,
360+
Scope: entity.ResourcePool.String(),
361361
ClusterName: "DC0_C0",
362362
EntityName: "DC0_C0_RP0",
363363
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
@@ -392,7 +392,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
392392
name: "Resourcepool Memory not available",
393393
rule: v1alpha1.ComputeResourceRule{
394394
RuleName: "Test Resourcepool Resource Validation rule",
395-
Scope: entity.ResourcePool,
395+
Scope: entity.ResourcePool.String(),
396396
ClusterName: "DC0_C0",
397397
EntityName: "DC0_C0_RP0",
398398
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
@@ -427,7 +427,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
427427
name: "Resourcepool Disk not available",
428428
rule: v1alpha1.ComputeResourceRule{
429429
RuleName: "Test Resourcepool Resource Validation rule",
430-
Scope: entity.ResourcePool,
430+
Scope: entity.ResourcePool.String(),
431431
ClusterName: "DC0_C0",
432432
EntityName: "DC0_C0_RP0",
433433
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
@@ -462,7 +462,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
462462
name: "Duplicate scope resourcepool",
463463
rule: v1alpha1.ComputeResourceRule{
464464
RuleName: "Test Resourcepool Resource Validation rule",
465-
Scope: entity.ResourcePool,
465+
Scope: entity.ResourcePool.String(),
466466
ClusterName: "DC0_C1",
467467
EntityName: "DC0_C1_RP0",
468468
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
@@ -497,7 +497,7 @@ func TestReconcileComputeResourceValidationRule(t *testing.T) {
497497
name: "Duplicate scope cluster",
498498
rule: v1alpha1.ComputeResourceRule{
499499
RuleName: "Test Resourcepool Resource Validation rule",
500-
Scope: entity.Cluster,
500+
Scope: entity.Cluster.String(),
501501
EntityName: "DC0_C1",
502502
NodepoolResourceRequirements: []v1alpha1.NodepoolResourceRequirement{
503503
{

pkg/validators/privileges/privilege_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
6363
rule: v1alpha1.PrivilegeValidationRule{
6464
RuleName: "VirtualMachine.Config.AddExistingDisk",
6565
ClusterName: opts.Cluster,
66-
EntityType: entity.Cluster,
66+
EntityType: entity.Cluster.String(),
6767
EntityName: opts.Cluster,
6868
Privileges: []string{"VirtualMachine.Config.AddExistingDisk"},
6969
Propagation: v1alpha1.Propagation{
@@ -87,7 +87,7 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
8787
rule: v1alpha1.PrivilegeValidationRule{
8888
RuleName: "VirtualMachine.Config.MagicCarpet",
8989
ClusterName: opts.Cluster,
90-
EntityType: entity.Cluster,
90+
EntityType: entity.Cluster.String(),
9191
EntityName: opts.Cluster,
9292
Privileges: []string{"VirtualMachine.Config.MagicCarpet"},
9393
Propagation: v1alpha1.Propagation{

pkg/validators/tags/tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func tagIsValid(tagsManager *tags.Manager, finder *find.Finder, datacenter strin
9999
}
100100
}
101101

102-
switch rule.EntityType {
102+
switch e := entity.Map[rule.EntityType]; e {
103103
case entity.Cluster:
104104
inventoryPath = fmt.Sprintf(vcenter.ClusterInventoryPath, datacenter, rule.EntityName)
105105
case entity.Datacenter:

0 commit comments

Comments
 (0)