mirror of
https://github.com/hanzozt/zt.git
synced 2026-10-30 15:04:55 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
/*
|
|
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 routes
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/hanzozt/zt/v2/controller/env"
|
|
"github.com/hanzozt/zt/v2/controller/idgen"
|
|
"github.com/hanzozt/zt/v2/controller/model"
|
|
"github.com/hanzozt/zt/v2/controller/response"
|
|
"github.com/hanzozt/zt/v2/controller/rest_model"
|
|
|
|
"github.com/hanzozt/foundation/v2/stringz"
|
|
"github.com/hanzozt/zt/v2/controller/models"
|
|
)
|
|
|
|
var FabricServiceLinkFactory = NewFabricServiceLinkFactory()
|
|
|
|
type FabricServiceLinkFactoryIml struct {
|
|
BasicFabricLinkFactory
|
|
}
|
|
|
|
func NewFabricServiceLinkFactory() *FabricServiceLinkFactoryIml {
|
|
return &FabricServiceLinkFactoryIml{
|
|
BasicFabricLinkFactory: *NewBasicFabricLinkFactory(EntityNameService),
|
|
}
|
|
}
|
|
|
|
func (factory *FabricServiceLinkFactoryIml) Links(entity LinkEntity) rest_model.Links {
|
|
links := factory.BasicFabricLinkFactory.Links(entity)
|
|
links[EntityNameTerminator] = factory.NewNestedLink(entity, EntityNameTerminator)
|
|
return links
|
|
}
|
|
|
|
func MapCreateFabricServiceToModel(service *rest_model.ServiceCreate) *model.Service {
|
|
ret := &model.Service{
|
|
BaseEntity: models.BaseEntity{
|
|
Tags: FabricTagsOrDefault(service.Tags),
|
|
},
|
|
Name: stringz.OrEmpty(service.Name),
|
|
TerminatorStrategy: service.TerminatorStrategy,
|
|
MaxIdleTime: time.Duration(service.MaxIdleTimeMillis) * time.Millisecond,
|
|
}
|
|
|
|
if ret.Id == "" {
|
|
ret.Id = idgen.New()
|
|
}
|
|
|
|
if ret.Id != "" && ret.Name == "" {
|
|
ret.Name = ret.Id
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func MapUpdateFabricServiceToModel(id string, service *rest_model.ServiceUpdate) *model.Service {
|
|
ret := &model.Service{
|
|
BaseEntity: models.BaseEntity{
|
|
Tags: FabricTagsOrDefault(service.Tags),
|
|
Id: id,
|
|
},
|
|
Name: stringz.OrEmpty(service.Name),
|
|
TerminatorStrategy: service.TerminatorStrategy,
|
|
MaxIdleTime: time.Duration(service.MaxIdleTimeMillis) * time.Millisecond,
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func MapPatchFabricServiceToModel(id string, service *rest_model.ServicePatch) *model.Service {
|
|
ret := &model.Service{
|
|
BaseEntity: models.BaseEntity{
|
|
Tags: FabricTagsOrDefault(service.Tags),
|
|
Id: id,
|
|
},
|
|
Name: service.Name,
|
|
TerminatorStrategy: service.TerminatorStrategy,
|
|
MaxIdleTime: time.Duration(service.MaxIdleTimeMillis) * time.Millisecond,
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
type FabricServiceModelMapper struct{}
|
|
|
|
func (FabricServiceModelMapper) ToApi(_ *env.AppEnv, _ *response.RequestContext, service *model.Service) (interface{}, error) {
|
|
maxIdleTime := service.MaxIdleTime.Milliseconds()
|
|
return &rest_model.ServiceDetail{
|
|
BaseEntity: FabricEntityToRestModel(service, FabricServiceLinkFactory),
|
|
Name: &service.Name,
|
|
TerminatorStrategy: &service.TerminatorStrategy,
|
|
MaxIdleTimeMillis: &maxIdleTime,
|
|
}, nil
|
|
}
|