Skip to content

Commit 8943ff6

Browse files
refactor: rename CloudDriver -> VCenterDriver (#361)
## Issue N/A ## Description Routine maintenance to improve naming and tidy code. --------- Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com> Co-authored-by: Ahmad Ibrahim <ahm.ibr@hotmail.com>
1 parent 3fb6f51 commit 8943ff6

23 files changed

Lines changed: 197 additions & 205 deletions

File tree

api/vcenter/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Package vcenter contains vCenter object types.
22
package vcenter
33

4+
import "net/url"
5+
46
// Account contains vCenter account details.
57
type Account struct {
68
// Insecure controls whether to validate the vCenter server's certificate.
@@ -16,6 +18,11 @@ type Account struct {
1618
Host string `json:"host" yaml:"host"`
1719
}
1820

21+
// Userinfo returns a vCenter account's credentials in Userinfo format.
22+
func (a Account) Userinfo() *url.Userinfo {
23+
return url.UserPassword(a.Username, a.Password)
24+
}
25+
1926
// Entity represents a vCenter entity, referenceable via govmomi.
2027
type Entity int
2128

build

internal/controller/vspherevalidator_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (r *VsphereValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Req
9595
// Always update the expected result count in case the validator's rules have changed
9696
vr.Spec.ExpectedResults = validator.Spec.ResultCount()
9797

98-
// Initialize Vsphere driver
98+
// Initialize vCenter auth
9999
if validator.Spec.Auth.SecretName == "" && validator.Spec.Auth.Account == nil {
100100
l.Error(errCredentialsRequired, "failed to reconcile VsphereValidator with empty credentials")
101101
return ctrl.Result{}, errCredentialsRequired

pkg/validate/validate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ func Validate(ctx context.Context, spec v1alpha1.VsphereValidatorSpec, log logr.
3737
return resp
3838
}
3939

40-
// Create a new vSphere driver
41-
driver, err := vsphere.NewVSphereDriver(*spec.Auth.Account, spec.Datacenter, log)
40+
// Create a new vCenter driver
41+
driver, err := vsphere.NewVCenterDriver(*spec.Auth.Account, spec.Datacenter, log)
4242
if err != nil {
43-
resp.AddResult(vrr, fmt.Errorf("failed to create vSphere driver: %w", err))
43+
resp.AddResult(vrr, fmt.Errorf("failed to create vCenter driver: %w", err))
4444
return resp
4545
}
4646

4747
// Get the authorization manager from the driver
4848
authManager := object.NewAuthorizationManager(driver.Client.Client)
4949
if authManager == nil {
50-
resp.AddResult(vrr, errors.New("invalid vSphere driver; failed to get vim25 authorization manager"))
50+
resp.AddResult(vrr, errors.New("invalid vCenter driver; failed to get vim25 authorization manager"))
5151
return resp
5252
}
5353

@@ -61,7 +61,7 @@ func Validate(ctx context.Context, spec v1alpha1.VsphereValidatorSpec, log logr.
6161
tagsManager := vtags.NewManager(driver.RestClient)
6262

6363
// Get the current user
64-
userName, err := driver.GetCurrentVmwareUser(ctx)
64+
userName, err := driver.CurrentUser(ctx)
6565
if err != nil {
6666
resp.AddResult(vrr, fmt.Errorf("failed to get current user: %w", err))
6767
return resp

pkg/validators/computeresources/computeresources.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ var (
3636
// ValidationService is a service that validates compute resource rules
3737
type ValidationService struct {
3838
log logr.Logger
39-
driver *vsphere.CloudDriver
39+
driver *vsphere.VCenterDriver
4040
}
4141

4242
// NewValidationService creates a new ValidationService
43-
func NewValidationService(log logr.Logger, driver *vsphere.CloudDriver) *ValidationService {
43+
func NewValidationService(log logr.Logger, driver *vsphere.VCenterDriver) *ValidationService {
4444
return &ValidationService{
4545
log: log,
4646
driver: driver,
@@ -105,7 +105,7 @@ type Usage struct {
105105
}
106106

107107
// ReconcileComputeResourceValidationRule reconciles the compute resource rule
108-
func (c *ValidationService) ReconcileComputeResourceValidationRule(rule v1alpha1.ComputeResourceRule, finder *find.Finder, driver *vsphere.CloudDriver, seenScopes map[string]bool) (*types.ValidationRuleResult, error) {
108+
func (c *ValidationService) ReconcileComputeResourceValidationRule(rule v1alpha1.ComputeResourceRule, finder *find.Finder, driver *vsphere.VCenterDriver, seenScopes map[string]bool) (*types.ValidationRuleResult, error) {
109109

110110
vr := buildValidationResult(rule, constants.ValidationTypeComputeResources)
111111

@@ -212,7 +212,7 @@ func hostUsage(ctx context.Context, rule v1alpha1.ComputeResourceRule, finder *f
212212
return &res, nil
213213
}
214214

215-
func resourcePoolUsage(ctx context.Context, rule v1alpha1.ComputeResourceRule, finder *find.Finder, driver *vsphere.CloudDriver) (*Usage, error) {
215+
func resourcePoolUsage(ctx context.Context, rule v1alpha1.ComputeResourceRule, finder *find.Finder, driver *vsphere.VCenterDriver) (*Usage, error) {
216216
var res Usage
217217

218218
// cpu & memory

pkg/validators/ntp/ntp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
// ValidationService is a service that validates NTP rules
2323
type ValidationService struct {
2424
log logr.Logger
25-
driver *vsphere.CloudDriver
25+
driver *vsphere.VCenterDriver
2626
datacenter string
2727
}
2828

2929
// NewValidationService creates a new ValidationService
30-
func NewValidationService(log logr.Logger, driver *vsphere.CloudDriver, datacenter string) *ValidationService {
30+
func NewValidationService(log logr.Logger, driver *vsphere.VCenterDriver, datacenter string) *ValidationService {
3131
return &ValidationService{
3232
log: log,
3333
driver: driver,

pkg/validators/privileges/privilege.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ var errRequiredPrivilegesNotFound = errors.New("one or more required privileges
2525
// PrivilegeValidationService is a service that validates user privileges
2626
type PrivilegeValidationService struct {
2727
log logr.Logger
28-
driver *vsphere.CloudDriver
28+
driver *vsphere.VCenterDriver
2929
datacenter string
3030
authManager *object.AuthorizationManager
3131
userName string
3232
}
3333

3434
// NewPrivilegeValidationService creates a new PrivilegeValidationService
35-
func NewPrivilegeValidationService(log logr.Logger, driver *vsphere.CloudDriver, datacenter string, authManager *object.AuthorizationManager, userName string) *PrivilegeValidationService {
35+
func NewPrivilegeValidationService(log logr.Logger, driver *vsphere.VCenterDriver, datacenter string, authManager *object.AuthorizationManager, userName string) *PrivilegeValidationService {
3636
return &PrivilegeValidationService{
3737
log: log,
3838
driver: driver,
@@ -50,10 +50,7 @@ func (s *PrivilegeValidationService) ReconcilePrivilegeRule(rule v1alpha1.Privil
5050
ctx, cancel := context.WithCancel(context.Background())
5151
defer cancel()
5252

53-
valid, failures, err := s.driver.ValidateUserPrivilegeOnEntities(ctx, s.authManager, s.datacenter, finder, rule)
54-
if !valid {
55-
vr.Condition.Failures = failures
56-
}
53+
vr.Condition.Failures, err = s.driver.ValidateUserPrivilegeOnEntities(ctx, s.authManager, s.datacenter, finder, rule)
5754

5855
if len(vr.Condition.Failures) > 0 {
5956
vr.State = util.Ptr(vapi.ValidationFailed)

pkg/validators/privileges/privilege_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
3434
ctx, cancel := context.WithCancel(context.Background())
3535
defer cancel()
3636

37-
userName, err := vcSim.Driver.GetCurrentVmwareUser(ctx)
37+
username, err := vcSim.Driver.CurrentUser(ctx)
3838
if err != nil {
3939
t.Fatal("Error in getting current VMware user from username")
4040
}
4141

42-
validationService := NewPrivilegeValidationService(log, vcSim.Driver, "DC0", authManager, userName)
42+
validationService := NewPrivilegeValidationService(log, vcSim.Driver, "DC0", authManager, username)
4343

4444
testCases := []struct {
4545
name string
@@ -51,7 +51,7 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
5151
name: "All privileges available",
5252
rule: v1alpha1.PrivilegeValidationRule{
5353
RuleName: "VirtualMachine.Config.AddExistingDisk",
54-
Username: userName,
54+
Username: username,
5555
ClusterName: "DC0_C0",
5656
EntityType: vcenter.Cluster,
5757
EntityName: "DC0_C0",
@@ -62,9 +62,9 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
6262
expectedResult: types.ValidationRuleResult{Condition: &vapi.ValidationCondition{
6363
ValidationType: "vsphere-privileges",
6464
ValidationRule: "validation-cluster-DC0_C0",
65-
Message: fmt.Sprintf("All required vsphere-privileges permissions were found for account: %s", userName),
65+
Message: fmt.Sprintf("All required vsphere-privileges permissions were found for account: %s", username),
6666
Details: []string{},
67-
Failures: nil,
67+
Failures: []string{},
6868
Status: corev1.ConditionTrue,
6969
},
7070
State: util.Ptr(vapi.ValidationSucceeded),
@@ -74,7 +74,7 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
7474
name: "Certain privilege not available",
7575
rule: v1alpha1.PrivilegeValidationRule{
7676
RuleName: "VirtualMachine.Config.AddExistingDisk",
77-
Username: userName,
77+
Username: username,
7878
ClusterName: "DC0_C0",
7979
EntityType: vcenter.Cluster,
8080
EntityName: "DC0_C0",
@@ -85,7 +85,7 @@ func TestPrivilegeValidationService_ReconcilePrivilegeRule(t *testing.T) {
8585
expectedResult: types.ValidationRuleResult{Condition: &vapi.ValidationCondition{
8686
ValidationType: "vsphere-privileges",
8787
ValidationRule: "validation-cluster-DC0_C0",
88-
Message: fmt.Sprintf("One or more required privileges was not found, or a condition was not met for account: %s", userName),
88+
Message: fmt.Sprintf("One or more required privileges was not found, or a condition was not met for account: %s", username),
8989
Details: []string{},
9090
Failures: []string{"user: admin2@vsphere.local does not have privilege: VirtualMachine.Config.DestroyExistingDisk on entity type: cluster with name: DC0_C0"},
9191
Status: corev1.ConditionFalse,

pkg/validators/tags/tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewValidationService(log logr.Logger) *ValidationService {
4343
}
4444

4545
// ReconcileTagRules reconciles the tag rules
46-
func (s *ValidationService) ReconcileTagRules(tagsManager *tags.Manager, finder *find.Finder, driver *vsphere.CloudDriver, rule v1alpha1.TagValidationRule) (*vapitypes.ValidationRuleResult, error) {
46+
func (s *ValidationService) ReconcileTagRules(tagsManager *tags.Manager, finder *find.Finder, driver *vsphere.VCenterDriver, rule v1alpha1.TagValidationRule) (*vapitypes.ValidationRuleResult, error) {
4747
vr := buildValidationResult(rule, constants.ValidationTypeTag)
4848

4949
valid, err := tagIsValid(tagsManager, finder, driver.Datacenter, rule)

pkg/vcsim/vcsim.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func init() {
3131
// VCSimulator is used to mock interactions with a vCenter server
3232
type VCSimulator struct {
3333
Account vcenter.Account
34-
Driver *vsphere.CloudDriver
34+
Driver *vsphere.VCenterDriver
3535
log logr.Logger
3636
}
3737

@@ -74,7 +74,7 @@ func (v *VCSimulator) Start() {
7474
log.Fatalf("failed to create vCenter simulator: %s", err)
7575
}
7676

77-
v.Driver, err = vsphere.NewVSphereDriver(v.Account, "DC0", v.log)
77+
v.Driver, err = vsphere.NewVCenterDriver(v.Account, "DC0", v.log)
7878
if err != nil {
7979
log.Fatalf("failed to create driver for vCenter simulator: %s", err)
8080
}
@@ -165,7 +165,7 @@ func (v *VCSimulator) createVCenterSimulator(model *simulator.Model) (func(), er
165165

166166
model.Service.RegisterEndpoints = true
167167
model.Service.Listen = &url.URL{
168-
User: url.UserPassword(v.Account.Username, v.Account.Password),
168+
User: v.Account.Userinfo(),
169169
Host: host,
170170
}
171171
model.Service.TLS = new(tls.Config)

0 commit comments

Comments
 (0)