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 63
/
Copy pathproject_validator.go
64 lines (59 loc) · 2.23 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
package validation
import (
"context"
"github.com/lyft/flyteadmin/pkg/errors"
"github.com/lyft/flyteadmin/pkg/manager/impl/shared"
"github.com/lyft/flyteadmin/pkg/repositories"
runtimeInterfaces "github.com/lyft/flyteadmin/pkg/runtime/interfaces"
"github.com/lyft/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 maxDescriptionLength = 300
func ValidateProjectRegisterRequest(request admin.ProjectRegisterRequest) error {
if request.Project == nil {
return shared.GetMissingArgumentError(shared.Project)
}
if err := ValidateEmptyStringField(request.Project.Id, projectID); err != nil {
return err
}
if errs := validation.IsDNS1123Label(request.Project.Id); len(errs) > 0 {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid project id [%s]: %v", request.Project.Id, errs)
}
if err := ValidateEmptyStringField(request.Project.Name, projectName); err != nil {
return err
}
if err := ValidateMaxLengthStringField(request.Project.Description, projectDescription, maxDescriptionLength); err != nil {
return err
}
if request.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 {
_, 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)
}
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
}