Files
zt/router/xgress_common/messages.go
2025-04-02 15:28:59 -04:00

76 lines
2.0 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 xgress_common
import (
"github.com/openziti/channel/v4"
"github.com/openziti/channel/v4/protobufs"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
const (
PayloadFlagsHeader uint8 = 0x10
)
func GetResultOrFailure(msg *channel.Message, err error, result protobufs.TypedMessage) error {
if err != nil {
return err
}
if msg.ContentType == int32(edge_ctrl_pb.ContentType_ErrorType) {
msg := string(msg.Body)
if msg == "" {
msg = "error state returned from controller with no message"
}
return errors.New(msg)
}
if msg.ContentType != result.GetContentType() {
return errors.Errorf("unexpected response type %v to request. expected %v", msg.ContentType, result.GetContentType())
}
return proto.Unmarshal(msg.Body, result)
}
func CheckForFailureResult(msg *channel.Message, err error, successType edge_ctrl_pb.ContentType) error {
if err != nil {
return err
}
if msg.ContentType == int32(edge_ctrl_pb.ContentType_ErrorType) {
msg := string(msg.Body)
if msg == "" {
msg = "error state returned from controller with no message"
}
return errors.New(msg)
}
if msg.ContentType != int32(successType) {
return errors.Errorf("unexpected response type %v to request. expected %v", msg.ContentType, successType)
}
return nil
}
func GetFinHeaders() map[uint8][]byte {
return map[uint8][]byte{
PayloadFlagsHeader: {0x1, 0, 0, 0},
}
}