Skip to content

Commit

Permalink
Fix PR comments
Browse files Browse the repository at this point in the history
 - add validation
 - fix template
 - add unittest for validation
  • Loading branch information
adiantum committed Jun 6, 2024
1 parent bf5b8b3 commit 70b8ec8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
16 changes: 16 additions & 0 deletions pkg/api/v1alpha1/nutanixdatacenterconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ func TestGetNutanixDatacenterConfigValidConfig(t *testing.T) {
assert.Contains(t, err.Error(), "NutanixDatacenterConfig credentialRef name is not set or is empty")
},
},
{
name: "datacenterconfig-valid-failure-domains",
fileName: "testdata/nutanix/datacenterconfig-valid-failuredomains.yaml",
assertions: func(t *testing.T, dcConf *v1alpha1.NutanixDatacenterConfig) {
assert.NoError(t, dcConf.Validate())
},
},
{
name: "datecenterconfig-invalid-failure-domains",
fileName: "testdata/nutanix/datacenterconfig-invalid-failuredomains.yaml",
assertions: func(t *testing.T, dcConf *v1alpha1.NutanixDatacenterConfig) {
err := dcConf.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "NutanixDatacenterConfig.Spec.FailureDomains.Subnets: missing subnet UUID: default/eksa-unit-test")
},
},
}

for _, test := range tests {
Expand Down
33 changes: 33 additions & 0 deletions pkg/api/v1alpha1/nutanixdatacenterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,42 @@ func (in *NutanixDatacenterConfig) Validate() error {
}
}

if in.Spec.FailureDomains != nil && len(in.Spec.FailureDomains) != 0 {
dccName := in.Namespace + "/" + in.Name
validateClusterResourceIdentifier := createValidateNutanixResourceFunc("NutanixDatacenterConfig.Spec.FailureDomains.Cluster", "cluster", dccName)
validateSubnetResourceIdentifier := createValidateNutanixResourceFunc("NutanixDatacenterConfig.Spec.FailureDomains.Subnets", "subnet", dccName)
for _, fd := range in.Spec.FailureDomains {
if err := validateClusterResourceIdentifier(&fd.Cluster); err != nil {
return err
}

for _, subnet := range fd.Subnets {
if err := validateSubnetResourceIdentifier(&subnet); err != nil {
return err
}
}
}
}

return nil
}

func createValidateNutanixResourceFunc(msgPrefix, entityName, mfstName string) (func (*NutanixResourceIdentifier) error) {
return func (ntnxRId *NutanixResourceIdentifier) error {
if ntnxRId.Type != NutanixIdentifierName && ntnxRId.Type != NutanixIdentifierUUID {
return fmt.Errorf("%s: invalid identifier type for %s: %s", msgPrefix, entityName, ntnxRId.Type)
}

if ntnxRId.Type == NutanixIdentifierName && (ntnxRId.Name == nil || *ntnxRId.Name == "") {
return fmt.Errorf("%s: missing %s name: %s", msgPrefix, entityName, mfstName)
} else if ntnxRId.Type == NutanixIdentifierUUID && (ntnxRId.UUID == nil || *ntnxRId.UUID == "") {
return fmt.Errorf("%s: missing %s UUID: %s", msgPrefix, entityName, mfstName)
}

return nil
}
}

// SetDefaults sets default values for the NutanixDatacenterConfig object.
func (in *NutanixDatacenterConfig) SetDefaults() {
if in.Spec.CredentialRef == nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/providers/nutanix/config/cp-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ spec:
{{- if (eq $value.Cluster.Type "uuid") }}
type: "uuid"
uuid: "{{ $value.Cluster.UUID }}"
{{- end}}
{{- if (eq $value.Cluster.Type "name") }}
{{- else if (eq $value.Cluster.Type "name") }}
type: "name"
name: "{{ $value.Cluster.Name }}"
{{- end}}
Expand All @@ -23,8 +22,7 @@ spec:
{{- if (eq .Type "uuid") }}
- type: "uuid"
uuid: "{{ .UUID }}"
{{- end}}
{{- if (eq .Type "name") }}
{{- else if (eq .Type "name") }}
- type: "name"
name: "{{ .Name }}"
{{- end}}
Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/nutanix/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ func generateNoProxyList(clusterSpec *cluster.Spec) []string {
return noProxyList
}

func generateNutanixFailureDomains(FailureDomains []v1alpha1.NutanixDatacenterFailureDomain) []capxv1beta1.NutanixFailureDomain {
func generateNutanixFailureDomains(eksNutanixFailureDomains []v1alpha1.NutanixDatacenterFailureDomain) []capxv1beta1.NutanixFailureDomain {
var failureDomains []capxv1beta1.NutanixFailureDomain
for _, fd := range FailureDomains {
for _, fd := range eksNutanixFailureDomains {

subnets := []capxv1beta1.NutanixResourceIdentifier{}
for _, subnet := range fd.Subnets {
Expand Down

0 comments on commit 70b8ec8

Please sign in to comment.