-
Notifications
You must be signed in to change notification settings - Fork 129
/
edge_service_model.go
129 lines (112 loc) · 4.55 KB
/
edge_service_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
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"
"time"
)
type Service struct {
models.BaseEntity
Name string `json:"name"`
MaxIdleTime time.Duration `json:"maxIdleTime"`
TerminatorStrategy string `json:"terminatorStrategy"`
RoleAttributes []string `json:"roleAttributes"`
Configs []string `json:"configs"`
EncryptionRequired bool `json:"encryptionRequired"`
}
func (entity *Service) toBoltEntity(tx *bbolt.Tx, env Env) (*db.EdgeService, error) {
if err := entity.validateConfigs(tx, env); err != nil {
return nil, err
}
edgeService := &db.EdgeService{
Service: db.Service{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
Name: entity.Name,
MaxIdleTime: entity.MaxIdleTime,
TerminatorStrategy: entity.TerminatorStrategy,
},
RoleAttributes: entity.RoleAttributes,
Configs: entity.Configs,
EncryptionRequired: entity.EncryptionRequired,
}
return edgeService, nil
}
func (entity *Service) toBoltEntityForCreate(tx *bbolt.Tx, env Env) (*db.EdgeService, error) {
return entity.toBoltEntity(tx, env)
}
func (entity *Service) validateConfigs(tx *bbolt.Tx, env Env) error {
typeMap := map[string]*db.Config{}
configStore := env.GetStores().Config
for _, id := range entity.Configs {
config, _ := configStore.LoadById(tx, id)
if config == nil {
return boltz.NewNotFoundError(db.EntityTypeConfigs, "id", id)
}
conflictConfig, found := typeMap[config.Type]
if found {
configTypeName := "<not found>"
if configType, _ := env.GetStores().ConfigType.LoadById(tx, config.Type); configType != nil {
configTypeName = configType.Name
}
msg := fmt.Sprintf("duplicate configs named %v and %v found for config type %v. Only one config of a given typed is allowed per service ",
conflictConfig.Name, config.Name, configTypeName)
return errorz.NewFieldError(msg, "configs", entity.Configs)
}
typeMap[config.Type] = config
}
return nil
}
func (entity *Service) toBoltEntityForUpdate(tx *bbolt.Tx, env Env, _ boltz.FieldChecker) (*db.EdgeService, error) {
return entity.toBoltEntity(tx, env)
}
func (entity *Service) fillFrom(_ Env, _ *bbolt.Tx, boltService *db.EdgeService) error {
entity.FillCommon(boltService)
entity.Name = boltService.Name
entity.TerminatorStrategy = boltService.TerminatorStrategy
entity.RoleAttributes = boltService.RoleAttributes
entity.Configs = boltService.Configs
entity.EncryptionRequired = boltService.EncryptionRequired
return nil
}
type ServiceDetail struct {
models.BaseEntity
Name string `json:"name"`
MaxIdleTime time.Duration `json:"maxIdleTime"`
TerminatorStrategy string `json:"terminatorStrategy"`
RoleAttributes []string `json:"roleAttributes"`
Permissions []string `json:"permissions"`
Configs []string `json:"configs"`
Config map[string]map[string]interface{} `json:"config"`
EncryptionRequired bool `json:"encryptionRequired"`
}
func (entity *ServiceDetail) toBoltEntityForCreate(*bbolt.Tx, Env) (*db.EdgeService, error) {
panic("should never be called")
}
func (entity *ServiceDetail) toBoltEntityForUpdate(*bbolt.Tx, Env, boltz.FieldChecker) (*db.EdgeService, error) {
panic("should never be called")
}
func (entity *ServiceDetail) fillFrom(_ Env, _ *bbolt.Tx, boltService *db.EdgeService) error {
entity.FillCommon(boltService)
entity.MaxIdleTime = boltService.MaxIdleTime
entity.Name = boltService.Name
entity.TerminatorStrategy = boltService.TerminatorStrategy
entity.RoleAttributes = boltService.RoleAttributes
entity.Configs = boltService.Configs
entity.EncryptionRequired = boltService.EncryptionRequired
return nil
}