-
Notifications
You must be signed in to change notification settings - Fork 129
/
service_policy_model.go
89 lines (76 loc) · 2.94 KB
/
service_policy_model.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
83
84
85
86
87
88
89
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package model
import (
"fmt"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/storage/boltz"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/ziti/controller/models"
"go.etcd.io/bbolt"
"strings"
)
type ServicePolicy struct {
models.BaseEntity
Name string
PolicyType string
Semantic string
IdentityRoles []string
ServiceRoles []string
PostureCheckRoles []string
}
func (entity *ServicePolicy) validatePolicyType() error {
if !strings.EqualFold(entity.PolicyType, db.PolicyTypeDialName) && !strings.EqualFold(entity.PolicyType, db.PolicyTypeBindName) {
msg := fmt.Sprintf("invalid policy type. valid types are '%v' and '%v'", db.PolicyTypeDialName, db.PolicyTypeBindName)
return errorz.NewFieldError(msg, "policyType", entity.PolicyType)
}
return nil
}
func (entity *ServicePolicy) toBoltEntity(checker boltz.FieldChecker) (*db.ServicePolicy, error) {
if checker == nil || checker.IsUpdated(db.FieldServicePolicyType) {
if err := entity.validatePolicyType(); err != nil {
return nil, err
}
}
policyType := db.PolicyTypeInvalid
if strings.EqualFold(entity.PolicyType, db.PolicyTypeDialName) {
policyType = db.PolicyTypeDial
} else if strings.EqualFold(entity.PolicyType, db.PolicyTypeBindName) {
policyType = db.PolicyTypeBind
}
return &db.ServicePolicy{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
Name: entity.Name,
PolicyType: policyType,
Semantic: entity.Semantic,
IdentityRoles: entity.IdentityRoles,
ServiceRoles: entity.ServiceRoles,
PostureCheckRoles: entity.PostureCheckRoles,
}, nil
}
func (entity *ServicePolicy) toBoltEntityForCreate(*bbolt.Tx, Env) (*db.ServicePolicy, error) {
return entity.toBoltEntity(nil)
}
func (entity *ServicePolicy) toBoltEntityForUpdate(_ *bbolt.Tx, _ Env, checker boltz.FieldChecker) (*db.ServicePolicy, error) {
return entity.toBoltEntity(checker)
}
func (entity *ServicePolicy) fillFrom(_ Env, _ *bbolt.Tx, boltServicePolicy *db.ServicePolicy) error {
entity.FillCommon(boltServicePolicy)
entity.Name = boltServicePolicy.Name
entity.PolicyType = string(boltServicePolicy.PolicyType)
entity.Semantic = boltServicePolicy.Semantic
entity.ServiceRoles = boltServicePolicy.ServiceRoles
entity.IdentityRoles = boltServicePolicy.IdentityRoles
entity.PostureCheckRoles = boltServicePolicy.PostureCheckRoles
return nil
}