Просмотр исходного кода

add name check for env groups as well

Mohammed Nafees 3 лет назад
Родитель
Сommit
0ef756fa9f

+ 3 - 3
api/server/handlers/stack/add_application.go

@@ -82,16 +82,16 @@ func (p *StackAddApplicationHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 
 	appResources = append(appResources, newResources...)
 
-	resValidator := make(map[string]bool)
+	nameValidator := make(map[string]bool)
 
 	for _, res := range appResources {
-		if _, ok := resValidator[res.Name]; ok {
+		if _, ok := nameValidator[res.Name]; ok {
 			p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("duplicate app resource name: %s", res.Name),
 				http.StatusBadRequest))
 			return
 		}
 
-		resValidator[res.Name] = true
+		nameValidator[res.Name] = true
 	}
 
 	envGroups, err := stacks.CloneEnvGroups(latestRevision.EnvGroups)

+ 12 - 0
api/server/handlers/stack/add_env_group.go

@@ -88,6 +88,18 @@ func (p *StackAddEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 
 	envGroups = append(envGroups, newEnvGroups...)
 
+	nameValidator := make(map[string]bool)
+
+	for _, eg := range envGroups {
+		if _, ok := nameValidator[eg.Name]; ok {
+			p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("duplicate env group name: %s", eg.Name),
+				http.StatusBadRequest))
+			return
+		}
+
+		nameValidator[eg.Name] = true
+	}
+
 	newRevision := &models.StackRevision{
 		StackID:        stack.ID,
 		RevisionNumber: latestRevision.RevisionNumber + 1,

+ 15 - 3
api/server/handlers/stack/create.go

@@ -67,16 +67,16 @@ func (p *StackCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	resValidator := make(map[string]bool)
+	nameValidator := make(map[string]bool)
 
 	for _, res := range resources {
-		if _, ok := resValidator[res.Name]; ok {
+		if _, ok := nameValidator[res.Name]; ok {
 			p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("duplicate app resource name: %s", res.Name),
 				http.StatusBadRequest))
 			return
 		}
 
-		resValidator[res.Name] = true
+		nameValidator[res.Name] = true
 	}
 
 	envGroups, err := getEnvGroupModels(req.EnvGroups, proj.ID, cluster.ID, namespace)
@@ -86,6 +86,18 @@ func (p *StackCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	nameValidator = make(map[string]bool)
+
+	for _, eg := range envGroups {
+		if _, ok := nameValidator[eg.Name]; ok {
+			p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("duplicate env group name: %s", eg.Name),
+				http.StatusBadRequest))
+			return
+		}
+
+		nameValidator[eg.Name] = true
+	}
+
 	// write stack to the database with creating status
 	stack := &models.Stack{
 		ProjectID: proj.ID,