This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
project_validator.go
82 lines (75 loc) · 2.71 KB
/
project_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package validation
import (
"context"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"google.golang.org/grpc/codes"
"k8s.io/apimachinery/pkg/util/validation"
)
const projectID = "project_id"
const projectName = "project_name"
const projectDescription = "project_description"
const maxNameLength = 64
const maxDescriptionLength = 300
const maxLabelArrayLength = 16
func ValidateProjectRegisterRequest(request admin.ProjectRegisterRequest) error {
if request.Project == nil {
return shared.GetMissingArgumentError(shared.Project)
}
project := *request.Project
if err := ValidateEmptyStringField(project.Name, projectName); err != nil {
return err
}
return ValidateProject(project)
}
func ValidateProject(project admin.Project) error {
if err := ValidateEmptyStringField(project.Id, projectID); err != nil {
return err
}
if err := validateLabels(project.Labels); err != nil {
return err
}
if errs := validation.IsDNS1123Label(project.Id); len(errs) > 0 {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid project id [%s]: %v", project.Id, errs)
}
if err := ValidateMaxLengthStringField(project.Name, projectName, maxNameLength); err != nil {
return err
}
if err := ValidateMaxLengthStringField(project.Description, projectDescription, maxDescriptionLength); err != nil {
return err
}
if project.Domains != nil {
return errors.NewFlyteAdminError(codes.InvalidArgument,
"Domains are currently only set system wide. Please retry without domains included in your request.")
}
return nil
}
// Validates that a specified project and domain combination has been registered and exists in the db.
func ValidateProjectAndDomain(
ctx context.Context, db repositories.RepositoryInterface, config runtimeInterfaces.ApplicationConfiguration, projectID, domainID string) error {
project, err := db.ProjectRepo().Get(ctx, projectID)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"failed to validate that project [%s] and domain [%s] are registered, err: [%+v]",
projectID, domainID, err)
}
if *project.State != int32(admin.Project_ACTIVE) {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"project [%s] is not active", projectID)
}
var validDomain bool
domains := config.GetDomainsConfig()
for _, domain := range *domains {
if domain.ID == domainID {
validDomain = true
break
}
}
if !validDomain {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "domain [%s] is unrecognized by system", domainID)
}
return nil
}