Skip to content

Commit e79d5c9

Browse files
authored
refactor: make each rule implement validationrule.Interface (#341)
## Description Makes each rule in the plugin implement `validationrule.Interface` from validator@v0.1.7. --------- Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
1 parent 145fd5a commit e79d5c9

12 files changed

Lines changed: 117 additions & 43 deletions

File tree

api/v1alpha1/vspherevalidator_types.go

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55

66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
77

8+
"github.com/validator-labs/validator/pkg/validationrule"
9+
810
"github.com/validator-labs/validator-plugin-vsphere/pkg/constants"
911
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
1012
)
@@ -41,18 +43,34 @@ type VsphereAuth struct {
4143

4244
// NTPValidationRule defines the NTP validation rule
4345
type NTPValidationRule struct {
44-
// Name is the name of the NTP validation rule
45-
Name string `json:"name" yaml:"name"`
46+
validationrule.ManuallyNamed `json:"-"`
47+
48+
// RuleName is the name of the NTP validation rule
49+
RuleName string `json:"name" yaml:"name"`
4650
// ClusterName is required when the vCenter Host(s) reside beneath a Cluster in the vCenter object hierarchy
4751
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName,omitempty"`
4852
// Hosts is the list of vCenter Hosts to validate NTP configuration
4953
Hosts []string `json:"hosts" yaml:"hosts"`
5054
}
5155

56+
var _ validationrule.Interface = (*NTPValidationRule)(nil)
57+
58+
// Name returns the name of the NTPValidationRule.
59+
func (r NTPValidationRule) Name() string {
60+
return r.RuleName
61+
}
62+
63+
// SetName sets the name of the NTPValidationRule.
64+
func (r *NTPValidationRule) SetName(name string) {
65+
r.RuleName = name
66+
}
67+
5268
// ComputeResourceRule defines the compute resource validation rule
5369
type ComputeResourceRule struct {
54-
// Name is the name of the compute resource validation rule
55-
Name string `json:"name" yaml:"name"`
70+
validationrule.ManuallyNamed `json:"-"`
71+
72+
// RuleName is the name of the compute resource validation rule
73+
RuleName string `json:"name" yaml:"name"`
5674
// ClusterName is required when the vCenter Entity resides beneath a Cluster in the vCenter object hierarchy
5775
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName"`
5876
// Scope is the scope of the compute resource validation rule
@@ -63,10 +81,24 @@ type ComputeResourceRule struct {
6381
NodepoolResourceRequirements []NodepoolResourceRequirement `json:"nodepoolResourceRequirements" yaml:"nodepoolResourceRequirements"`
6482
}
6583

84+
var _ validationrule.Interface = (*ComputeResourceRule)(nil)
85+
86+
// Name returns the name of the ComputeResourceRule.
87+
func (r ComputeResourceRule) Name() string {
88+
return r.RuleName
89+
}
90+
91+
// SetName sets the name of the ComputeResourceRule.
92+
func (r *ComputeResourceRule) SetName(name string) {
93+
r.RuleName = name
94+
}
95+
6696
// EntityPrivilegeValidationRule defines the entity privilege validation rule
6797
type EntityPrivilegeValidationRule struct {
68-
// Name is the name of the entity privilege validation rule
69-
Name string `json:"name" yaml:"name"`
98+
validationrule.ManuallyNamed `json:"-"`
99+
100+
// RuleName is the name of the entity privilege validation rule
101+
RuleName string `json:"name" yaml:"name"`
70102
// Username is the username to validate against
71103
Username string `json:"username" yaml:"username"`
72104
// ClusterName is required when the vCenter Entity resides beneath a Cluster in the vCenter object hierarchy
@@ -79,18 +111,41 @@ type EntityPrivilegeValidationRule struct {
79111
Privileges []string `json:"privileges" yaml:"privileges"`
80112
}
81113

114+
var _ validationrule.Interface = (*EntityPrivilegeValidationRule)(nil)
115+
116+
// Name returns the name of the EntityPrivilegeValidationRule.
117+
func (r EntityPrivilegeValidationRule) Name() string {
118+
return r.RuleName
119+
}
120+
121+
// SetName sets the name of the EntityPrivilegeValidationRule.
122+
func (r *EntityPrivilegeValidationRule) SetName(name string) {
123+
r.RuleName = name
124+
}
125+
82126
// GenericRolePrivilegeValidationRule defines the generic role privilege validation rule
83127
type GenericRolePrivilegeValidationRule struct {
128+
validationrule.AutomaticallyNamed `json:"-"`
129+
84130
// Username is the username to validate against
85131
Username string `json:"username" yaml:"username"`
86132
// Privileges is the list of privileges to validate that the user has
87133
Privileges []string `json:"privileges" yaml:"privileges"`
88134
}
89135

136+
var _ validationrule.Interface = (*GenericRolePrivilegeValidationRule)(nil)
137+
138+
// Name returns the name of the GenericRolePrivilegeValidationRule.
139+
func (r GenericRolePrivilegeValidationRule) Name() string {
140+
return r.Username
141+
}
142+
90143
// TagValidationRule defines the tag validation rule
91144
type TagValidationRule struct {
92-
// Name is the name of the tag validation rule
93-
Name string `json:"name" yaml:"name"`
145+
validationrule.ManuallyNamed `json:"-"`
146+
147+
// RuleName is the name of the tag validation rule
148+
RuleName string `json:"name" yaml:"name"`
94149
// ClusterName is required when the vCenter Entity resides beneath a Cluster in the vCenter object hierarchy
95150
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName"`
96151
// EntityType is the type of the entity to validate
@@ -101,6 +156,18 @@ type TagValidationRule struct {
101156
Tag string `json:"tag" yaml:"tag"`
102157
}
103158

159+
var _ validationrule.Interface = (*TagValidationRule)(nil)
160+
161+
// Name returns the name of the TagValidationRule.
162+
func (r TagValidationRule) Name() string {
163+
return r.RuleName
164+
}
165+
166+
// SetName sets the name of the TagValidationRule.
167+
func (r *TagValidationRule) SetName(name string) {
168+
r.RuleName = name
169+
}
170+
104171
// NodepoolResourceRequirement defines the resource requirements for a nodepool
105172
type NodepoolResourceRequirement struct {
106173
// Name is the name of the nodepool

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ spec:
9090
description: EntityName is the name of the entity to validate
9191
type: string
9292
name:
93-
description: Name is the name of the compute resource validation
93+
description: RuleName is the name of the compute resource validation
9494
rule
9595
type: string
9696
nodepoolResourceRequirements:
@@ -155,7 +155,7 @@ spec:
155155
description: EntityType is the type of the entity to validate
156156
type: string
157157
name:
158-
description: Name is the name of the entity privilege validation
158+
description: RuleName is the name of the entity privilege validation
159159
rule
160160
type: string
161161
privileges:
@@ -190,7 +190,7 @@ spec:
190190
type: string
191191
type: array
192192
name:
193-
description: Name is the name of the NTP validation rule
193+
description: RuleName is the name of the NTP validation rule
194194
type: string
195195
required:
196196
- hosts
@@ -231,7 +231,7 @@ spec:
231231
description: EntityType is the type of the entity to validate
232232
type: string
233233
name:
234-
description: Name is the name of the tag validation rule
234+
description: RuleName is the name of the tag validation rule
235235
type: string
236236
tag:
237237
description: Tag is the tag to validate on the entity

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ spec:
9090
description: EntityName is the name of the entity to validate
9191
type: string
9292
name:
93-
description: Name is the name of the compute resource validation
93+
description: RuleName is the name of the compute resource validation
9494
rule
9595
type: string
9696
nodepoolResourceRequirements:
@@ -155,7 +155,7 @@ spec:
155155
description: EntityType is the type of the entity to validate
156156
type: string
157157
name:
158-
description: Name is the name of the entity privilege validation
158+
description: RuleName is the name of the entity privilege validation
159159
rule
160160
type: string
161161
privileges:
@@ -190,7 +190,7 @@ spec:
190190
type: string
191191
type: array
192192
name:
193-
description: Name is the name of the NTP validation rule
193+
description: RuleName is the name of the NTP validation rule
194194
type: string
195195
required:
196196
- hosts
@@ -231,7 +231,7 @@ spec:
231231
description: EntityType is the type of the entity to validate
232232
type: string
233233
name:
234-
description: Name is the name of the tag validation rule
234+
description: RuleName is the name of the tag validation rule
235235
type: string
236236
tag:
237237
description: Tag is the tag to validate on the entity

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/pkg/errors v0.9.1
1212
github.com/sirupsen/logrus v1.9.3
1313
github.com/stretchr/testify v1.9.0
14-
github.com/validator-labs/validator v0.1.6
14+
github.com/validator-labs/validator v0.1.7
1515
github.com/vmware/govmomi v0.42.0
1616
golang.org/x/exp v0.0.0-20240822175202-778ce7bba035
1717
k8s.io/api v0.31.0
@@ -82,3 +82,5 @@ require (
8282
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
8383
sigs.k8s.io/yaml v1.4.0 // indirect
8484
)
85+
86+
// replace github.com/validator-labs/validator => ../validator

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
158158
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
159159
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
160160
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
161-
github.com/validator-labs/validator v0.1.6 h1:Gckp+rYFPNYJwCU5iLgJXuS/o6yy7ayivvGzhxNOf3o=
162-
github.com/validator-labs/validator v0.1.6/go.mod h1:ssEvc9ws3kwWJ2VpIsOtgm7WmA6bdux2kkuIHbgT6oU=
161+
github.com/validator-labs/validator v0.1.7 h1:x1iBKoecChM52G7bbacMsdIQYvB84C65DRIex8TG6vQ=
162+
github.com/validator-labs/validator v0.1.7/go.mod h1:ssEvc9ws3kwWJ2VpIsOtgm7WmA6bdux2kkuIHbgT6oU=
163163
github.com/vmware/govmomi v0.42.0 h1:MbvAlVfjNBE1mHMaQ7yOSop1KLB0/93x6VAGuCtjqtI=
164164
github.com/vmware/govmomi v0.42.0/go.mod h1:1H5LWwsBif8HKZqbFp0FdoKTHyJE4FzL6ACequMKYQg=
165165
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=

internal/controller/vspherevalidator_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var _ = Describe("VsphereValidator controller", Ordered, func() {
4848
NTPValidationRules: []v1alpha1.NTPValidationRule{},
4949
TagValidationRules: []v1alpha1.TagValidationRule{
5050
{
51-
Name: "Datacenter k8s-region rule",
51+
RuleName: "Datacenter k8s-region rule",
5252
EntityType: "datacenter",
5353
EntityName: "Datacenter",
5454
Tag: "k8s-region",

0 commit comments

Comments
 (0)