Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance check for duplicate projects #64

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions api/v1alpha1/customer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

const (
invalidUpdateErrorMsg string = " is an immutable field and can not be modified."
duplicateKeysErr string = "Duplicate Project Keys are not allowed"
)

// CustomerSpec defines the desired state of Customer
Expand Down Expand Up @@ -98,14 +99,9 @@ func (customer *Customer) SetReconcileStatus(reconcileStatus []metav1.Condition)
}

func (customer *Customer) IsValid() (bool, error) {
keys := make(map[string]bool)

for _, entry := range customer.Spec.Projects {
if _, value := keys[entry]; !value {
keys[entry] = true
} else {
return false, errors.New("Duplicate Project Keys are not allowed")
}
if duplicateKeysExist(customer.Spec.Projects) {
return false, errors.New(duplicateKeysErr)
}

return true, nil
Expand All @@ -124,6 +120,10 @@ func (customer *Customer) IsValidUpdate(existingCustomer Customer) (bool, error)
return false, fmt.Errorf("%s %s", "LegacyCustomer", invalidUpdateErrorMsg)
}

if duplicateKeysExist(customer.Spec.Projects) {
return false, errors.New(duplicateKeysErr)
}

return true, nil
}

Expand All @@ -138,3 +138,16 @@ func (customer *Customer) IsValidCustomerUpdate(existingCustomer Customer) (bool

return true, nil
}

func duplicateKeysExist(projectKeys []string) bool {
keys := make(map[string]bool)

for _, entry := range projectKeys {
if _, value := keys[entry]; !value {
keys[entry] = true
} else {
return true
}
}
return false
}