Skip to content

Commit

Permalink
Enhance check for duplicate projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedwaleedmalik committed Feb 10, 2021
1 parent e8bf337 commit 46d60e7
Showing 1 changed file with 20 additions and 7 deletions.
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
}

0 comments on commit 46d60e7

Please sign in to comment.