Skip to content

Commit

Permalink
Move validation under create project method
Browse files Browse the repository at this point in the history
Signed-off-by: hiteshwani29 <hiteshwani29@gmail.com>
  • Loading branch information
hiteshwani29 committed Mar 1, 2023
1 parent b38f125 commit a53757c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
8 changes: 7 additions & 1 deletion pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
cdao "github.com/paralus/paralus/internal/cluster/dao"
"github.com/paralus/paralus/internal/dao"
"github.com/paralus/paralus/internal/models"
"github.com/paralus/paralus/pkg/common"
authzv1 "github.com/paralus/paralus/proto/types/authz"
commonv3 "github.com/paralus/paralus/proto/types/commonpb/v3"
v3 "github.com/paralus/paralus/proto/types/commonpb/v3"
Expand Down Expand Up @@ -60,8 +61,13 @@ func (s *projectService) Create(ctx context.Context, project *systemv3.Project)
return nil, fmt.Errorf("missing organization in metadata")
}

err, matched := MatchStringToRegx(project.Metadata.GetName(), common.PROJECT_NAME_REGEX)
if err != nil || !matched {
return nil, fmt.Errorf("cluster name contains invalid characters. valid characters are `%s`",common.PROJECT_NAME_REGEX)
}

var org models.Organization
_, err := dao.GetByName(ctx, s.db, project.Metadata.Organization, &org)
_, err = dao.GetByName(ctx, s.db, project.Metadata.Organization, &org)
if err != nil {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"regexp"

"github.com/google/uuid"
"github.com/paralus/paralus/internal/dao"
Expand Down Expand Up @@ -33,3 +34,15 @@ func IsInternalRequest(ctx context.Context) bool {
b, ok := v.(bool)
return ok && b
}

func MatchStringToRegx(matchString, regex string) (error, bool) {
re, err := regexp.Compile(regex)
if err != nil {
_log.Errorf("Error while commpling regex %s : %s", regex, err.Error())
return err, false
}
if re.MatchString(matchString) {
return nil, true
}
return nil, false
}
8 changes: 1 addition & 7 deletions server/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package server

import (
"context"
"fmt"

"github.com/paralus/paralus/pkg/common"
"github.com/paralus/paralus/pkg/service"
systemrpc "github.com/paralus/paralus/proto/rpc/system"
v3 "github.com/paralus/paralus/proto/types/commonpb/v3"
Expand Down Expand Up @@ -35,11 +33,7 @@ func updateProjectStatus(req *systempbv3.Project, resp *systempbv3.Project, err
}

func (s *projectServer) CreateProject(ctx context.Context, req *systempbv3.Project) (*systempbv3.Project, error) {

err, matched := MatchStringToRegx(req.Metadata.GetName(), common.PROJECT_NAME_REGEX)
if err != nil || !matched {
return nil, fmt.Errorf("invalid project name. Valid project name must be like demo-proj-test3")
}

resp, err := s.Create(ctx, req)
return updateProjectStatus(req, resp, err), err
}
Expand Down
14 changes: 0 additions & 14 deletions server/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package server

import (
"regexp"

v3 "github.com/paralus/paralus/proto/types/commonpb/v3"

"google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -20,15 +18,3 @@ func getStatus(err error) *v3.Status {
ConditionStatus: v3.ConditionStatus_StatusOK,
}
}

func MatchStringToRegx(matchString, regex string) (error, bool) {
re, err := regexp.Compile(regex)
if err != nil {
_log.Errorf("Error while commpling regex %s : %s", regex, err.Error())
return err, false
}
if re.MatchString(matchString) {
return nil, true
}
return nil, false
}

0 comments on commit a53757c

Please sign in to comment.