Skip to content

Commit

Permalink
Resolved comments
Browse files Browse the repository at this point in the history
Signed-off-by: hiteshwani29 <hiteshwani29@gmail.com>
  • Loading branch information
hiteshwani29 committed Mar 17, 2023
1 parent a53757c commit e003141
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
11 changes: 7 additions & 4 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import "time"
import (
"regexp"
"time"
)

// environment variables for configuration
const (
Expand Down Expand Up @@ -50,7 +53,7 @@ const (
var SessionDataKey contextKey
var SessionInternalKey contextKey

//Regex
const (
PROJECT_NAME_REGEX = `^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$`
// Regex
var (
PrjNameRX = regexp.MustCompile(`^[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]$`)
)
9 changes: 5 additions & 4 deletions pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -61,13 +62,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)
matched := common.PrjNameRX.MatchString(project.Metadata.GetName())
if !matched {
return nil, errors.New("project name contains invalid characters. Valid characters are alphanumeric and hyphen, except at the beginning or the end")
}

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: 0 additions & 13 deletions pkg/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"regexp"

"github.com/google/uuid"
"github.com/paralus/paralus/internal/dao"
Expand Down Expand Up @@ -34,15 +33,3 @@ 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
}

0 comments on commit e003141

Please sign in to comment.