Files
ziti/controller/internal/routes/terminator_api_model.go
T
Andrew M 3087972172 all conversion work
- remove old JSON schema, unsused code
- remove unused variables from base router functions
- rework all API entities
- rework all links
- fix lint issues
- adds error conversion logic
- update tests as needed
- fix up go test
- fix backwards compat w/ non json enroller
2020-06-09 14:11:23 -04:00

164 lines
4.5 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 (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/edge/controller/env"
"github.com/openziti/edge/controller/response"
"github.com/openziti/edge/rest_model"
"github.com/openziti/fabric/controller/models"
"github.com/openziti/fabric/controller/network"
"github.com/openziti/fabric/controller/xt"
"github.com/openziti/foundation/util/stringz"
"strings"
)
const EntityNameTerminator = "terminators"
var TerminatorLinkFactory = NewBasicLinkFactory(EntityNameTerminator)
func GetPrecedence(precedence *rest_model.TerminatorPrecedence) *xt.Precedence {
if precedence == nil {
return nil
}
if strings.EqualFold("default", string(*precedence)) {
return &xt.Precedences.Default
}
if strings.EqualFold("required", string(*precedence)) {
return &xt.Precedences.Required
}
if strings.EqualFold("failed", string(*precedence)) {
return &xt.Precedences.Failed
}
return nil
}
func MapCreateTerminatorToModel(terminator *rest_model.TerminatorCreate) *network.Terminator {
ret := &network.Terminator{
BaseEntity: models.BaseEntity{
Tags: terminator.Tags,
},
Service: stringz.OrEmpty(terminator.Service),
Router: stringz.OrEmpty(terminator.Router),
Binding: terminator.Binding,
Address: stringz.OrEmpty(terminator.Address),
}
if terminator.Cost != nil {
ret.Cost = uint16(*terminator.Cost)
}
return ret
}
func MapUpdateTerminatorToModel(id string, terminator *rest_model.TerminatorUpdate) *network.Terminator {
ret := &network.Terminator{
BaseEntity: models.BaseEntity{
Tags: terminator.Tags,
Id: id,
},
Service: stringz.OrEmpty(terminator.Service),
Router: stringz.OrEmpty(terminator.Router),
Binding: terminator.Binding,
Address: stringz.OrEmpty(terminator.Address),
}
if terminator.Cost != nil {
ret.Cost = uint16(*terminator.Cost)
}
return ret
}
func MapPatchTerminatorToModel(id string, terminator *rest_model.TerminatorPatch) *network.Terminator {
ret := &network.Terminator{
BaseEntity: models.BaseEntity{
Tags: terminator.Tags,
Id: id,
},
Service: terminator.Service,
Router: terminator.Router,
Binding: terminator.Binding,
Address: terminator.Address,
}
if terminator.Cost != nil {
ret.Cost = uint16(*terminator.Cost)
}
return ret
}
func MapTerminatorToRestEntity(ae *env.AppEnv, _ *response.RequestContext, e models.Entity) (interface{}, error) {
terminator, ok := e.(*network.Terminator)
if !ok {
err := fmt.Errorf("entity is not a Terminator \"%s\"", e.GetId())
log := pfxlog.Logger()
log.Error(err)
return nil, err
}
restModel, err := MapTerminatorToRestModel(ae, terminator)
if err != nil {
err := fmt.Errorf("could not convert to API entity \"%s\": %s", e.GetId(), err)
log := pfxlog.Logger()
log.Error(err)
return nil, err
}
return restModel, nil
}
func MapTerminatorToRestModel(ae *env.AppEnv, terminator *network.Terminator) (*rest_model.TerminatorDetail, error) {
service, err := ae.Handlers.EdgeService.Read(terminator.Service)
if err != nil {
return nil, err
}
router, err := ae.Handlers.TransitRouter.Read(terminator.Router)
if err != nil {
return nil, err
}
ret := &rest_model.TerminatorDetail{
BaseEntity: BaseEntityToRestModel(terminator, TerminatorLinkFactory),
ServiceID: &terminator.Service,
Service: ToEntityRef(service.Name, service, ServiceLinkFactory),
RouterID: &terminator.Router,
Router: ToEntityRef(router.Name, router, TransitRouterLinkFactory),
Binding: &terminator.Binding,
Address: &terminator.Address,
}
cost := rest_model.TerminatorCost(int64(terminator.Cost))
ret.Cost = &cost
dynamicCost := rest_model.TerminatorCost(xt.GlobalCosts().GetPrecedenceCost(terminator.Id))
ret.DynamicCost = &dynamicCost
precedence := xt.GlobalCosts().GetPrecedence(*ret.ID)
if precedence.IsRequired() {
ret.Precedence = "required"
} else if precedence.IsFailed() {
ret.Precedence = "failed"
} else {
ret.Precedence = "default"
}
return ret, nil
}