2020-09-18 18:05:15 +00:00
package pbservice
import (
"github.com/hashicorp/consul/agent/structs"
2022-03-23 16:10:03 +00:00
"github.com/hashicorp/consul/proto/pbcommon"
2022-12-22 18:03:33 +00:00
"github.com/hashicorp/consul/proto/pbconfigentry"
2022-03-23 16:10:03 +00:00
"github.com/hashicorp/consul/types"
2020-09-18 18:05:15 +00:00
)
2022-03-23 16:10:03 +00:00
type CheckIDType = types . CheckID
type NodeIDType = types . NodeID
func RaftIndexToStructs ( s * pbcommon . RaftIndex ) structs . RaftIndex {
2022-05-19 21:37:52 +00:00
if s == nil {
return structs . RaftIndex { }
}
2020-09-18 18:05:15 +00:00
return structs . RaftIndex {
CreateIndex : s . CreateIndex ,
ModifyIndex : s . ModifyIndex ,
}
}
2022-03-23 16:10:03 +00:00
func NewRaftIndexFromStructs ( s structs . RaftIndex ) * pbcommon . RaftIndex {
return & pbcommon . RaftIndex {
2020-09-18 18:05:15 +00:00
CreateIndex : s . CreateIndex ,
ModifyIndex : s . ModifyIndex ,
}
}
2022-03-23 16:10:03 +00:00
func MapHeadersToStructs ( s map [ string ] * HeaderValue ) map [ string ] [ ] string {
2020-09-18 18:05:15 +00:00
t := make ( map [ string ] [ ] string , len ( s ) )
for k , v := range s {
t [ k ] = v . Value
}
return t
}
2022-03-23 16:10:03 +00:00
func NewMapHeadersFromStructs ( t map [ string ] [ ] string ) map [ string ] * HeaderValue {
s := make ( map [ string ] * HeaderValue , len ( t ) )
2020-09-18 18:05:15 +00:00
for k , v := range t {
2022-03-23 16:10:03 +00:00
s [ k ] = & HeaderValue { Value : v }
2020-09-18 18:05:15 +00:00
}
return s
}
// TODO: use mog once it supports pointers and slices
2022-03-15 14:34:46 +00:00
func CheckServiceNodeToStructs ( s * CheckServiceNode ) ( * structs . CheckServiceNode , error ) {
2020-09-18 18:05:15 +00:00
if s == nil {
2022-03-15 14:34:46 +00:00
return nil , nil
2020-09-18 18:05:15 +00:00
}
var t structs . CheckServiceNode
if s . Node != nil {
2022-03-23 16:10:03 +00:00
n := new ( structs . Node )
NodeToStructs ( s . Node , n )
t . Node = n
2020-09-18 18:05:15 +00:00
}
if s . Service != nil {
2022-03-23 16:10:03 +00:00
r := new ( structs . NodeService )
NodeServiceToStructs ( s . Service , r )
t . Service = r
2020-09-18 18:05:15 +00:00
}
t . Checks = make ( structs . HealthChecks , len ( s . Checks ) )
for i , c := range s . Checks {
if c == nil {
continue
}
2022-03-23 16:10:03 +00:00
h := new ( structs . HealthCheck )
HealthCheckToStructs ( c , h )
t . Checks [ i ] = h
2020-09-18 18:05:15 +00:00
}
2022-03-15 14:34:46 +00:00
return & t , nil
2020-09-18 18:05:15 +00:00
}
// TODO: use mog once it supports pointers and slices
func NewCheckServiceNodeFromStructs ( t * structs . CheckServiceNode ) * CheckServiceNode {
if t == nil {
return nil
}
var s CheckServiceNode
if t . Node != nil {
2022-03-23 16:10:03 +00:00
n := new ( Node )
NodeFromStructs ( t . Node , n )
s . Node = n
2020-09-18 18:05:15 +00:00
}
if t . Service != nil {
2022-03-23 16:10:03 +00:00
r := new ( NodeService )
NodeServiceFromStructs ( t . Service , r )
s . Service = r
2020-09-18 18:05:15 +00:00
}
2020-09-24 23:55:31 +00:00
s . Checks = make ( [ ] * HealthCheck , len ( t . Checks ) )
2020-09-18 18:05:15 +00:00
for i , c := range t . Checks {
if c == nil {
continue
}
2022-03-23 16:10:03 +00:00
h := new ( HealthCheck )
HealthCheckFromStructs ( c , h )
s . Checks [ i ] = h
2020-09-18 18:05:15 +00:00
}
return & s
}
// TODO: handle this with mog, once mog handles pointers
func WeightsPtrToStructs ( s * Weights ) * structs . Weights {
if s == nil {
return nil
}
var t structs . Weights
t . Passing = int ( s . Passing )
t . Warning = int ( s . Warning )
return & t
}
// TODO: handle this with mog, once mog handles pointers
func NewWeightsPtrFromStructs ( t * structs . Weights ) * Weights {
if t == nil {
return nil
}
var s Weights
s . Passing = int32 ( t . Passing )
s . Warning = int32 ( t . Warning )
return & s
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func MapStringServiceAddressToStructs ( s map [ string ] * ServiceAddress ) map [ string ] structs . ServiceAddress {
2020-09-18 18:05:15 +00:00
t := make ( map [ string ] structs . ServiceAddress , len ( s ) )
for k , v := range s {
t [ k ] = structs . ServiceAddress { Address : v . Address , Port : int ( v . Port ) }
}
return t
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func NewMapStringServiceAddressFromStructs ( t map [ string ] structs . ServiceAddress ) map [ string ] * ServiceAddress {
s := make ( map [ string ] * ServiceAddress , len ( t ) )
2020-09-18 18:05:15 +00:00
for k , v := range t {
2022-03-23 16:10:03 +00:00
s [ k ] = & ServiceAddress { Address : v . Address , Port : int32 ( v . Port ) }
2020-09-18 18:05:15 +00:00
}
return s
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func ExposePathSliceToStructs ( s [ ] * ExposePath ) [ ] structs . ExposePath {
2020-09-18 18:05:15 +00:00
t := make ( [ ] structs . ExposePath , len ( s ) )
for i , v := range s {
2022-03-23 16:10:03 +00:00
e := new ( structs . ExposePath )
ExposePathToStructs ( v , e )
t [ i ] = * e
2020-09-18 18:05:15 +00:00
}
return t
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func NewExposePathSliceFromStructs ( t [ ] structs . ExposePath ) [ ] * ExposePath {
s := make ( [ ] * ExposePath , len ( t ) )
2020-09-18 18:05:15 +00:00
for i , v := range t {
2022-03-23 16:10:03 +00:00
ep := new ( ExposePath )
ExposePathFromStructs ( & v , ep )
s [ i ] = ep
2020-09-18 18:05:15 +00:00
}
return s
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func UpstreamsToStructs ( s [ ] * Upstream ) structs . Upstreams {
2020-09-18 18:05:15 +00:00
t := make ( structs . Upstreams , len ( s ) )
for i , v := range s {
2022-03-23 16:10:03 +00:00
u := new ( structs . Upstream )
UpstreamToStructs ( v , u )
t [ i ] = * u
2020-09-18 18:05:15 +00:00
}
return t
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func NewUpstreamsFromStructs ( t structs . Upstreams ) [ ] * Upstream {
s := make ( [ ] * Upstream , len ( t ) )
2020-09-18 18:05:15 +00:00
for i , v := range t {
2022-03-23 16:10:03 +00:00
u := new ( Upstream )
UpstreamFromStructs ( & v , u )
s [ i ] = u
2020-09-18 18:05:15 +00:00
}
return s
}
// TODO: handle this with mog
2022-03-23 16:10:03 +00:00
func CheckTypesToStructs ( s [ ] * CheckType ) structs . CheckTypes {
2020-09-18 18:05:15 +00:00
t := make ( structs . CheckTypes , len ( s ) )
for i , v := range s {
if v == nil {
continue
}
2022-03-23 16:10:03 +00:00
c := new ( structs . CheckType )
CheckTypeToStructs ( v , c )
t [ i ] = c
2020-09-18 18:05:15 +00:00
}
2022-03-23 16:10:03 +00:00
return t
2020-09-18 18:05:15 +00:00
}
// TODO: handle this with mog
func NewCheckTypesFromStructs ( t structs . CheckTypes ) [ ] * CheckType {
s := make ( [ ] * CheckType , len ( t ) )
for i , v := range t {
if v == nil {
continue
}
2022-03-23 16:10:03 +00:00
newV := new ( CheckType )
CheckTypeFromStructs ( v , newV )
s [ i ] = newV
2020-09-18 18:05:15 +00:00
}
return s
}
// TODO: handle this with mog
func ConnectProxyConfigPtrToStructs ( s * ConnectProxyConfig ) * structs . ConnectProxyConfig {
if s == nil {
return nil
}
2022-03-23 16:10:03 +00:00
c := new ( structs . ConnectProxyConfig )
ConnectProxyConfigToStructs ( s , c )
return c
2020-09-18 18:05:15 +00:00
}
// TODO: handle this with mog
func NewConnectProxyConfigPtrFromStructs ( t * structs . ConnectProxyConfig ) * ConnectProxyConfig {
if t == nil {
return nil
}
2022-03-23 16:10:03 +00:00
cp := new ( ConnectProxyConfig )
ConnectProxyConfigFromStructs ( t , cp )
return cp
2020-09-18 18:05:15 +00:00
}
// TODO: handle this with mog
func ServiceConnectPtrToStructs ( s * ServiceConnect ) * structs . ServiceConnect {
if s == nil {
return nil
}
2022-03-23 16:10:03 +00:00
sc := new ( structs . ServiceConnect )
ServiceConnectToStructs ( s , sc )
return sc
2020-09-18 18:05:15 +00:00
}
// TODO: handle this with mog
func NewServiceConnectPtrFromStructs ( t * structs . ServiceConnect ) * ServiceConnect {
if t == nil {
return nil
}
2022-03-23 16:10:03 +00:00
sc := new ( ServiceConnect )
ServiceConnectFromStructs ( t , sc )
return sc
2020-09-18 18:05:15 +00:00
}
// TODO: handle this with mog
func ServiceDefinitionPtrToStructs ( s * ServiceDefinition ) * structs . ServiceDefinition {
if s == nil {
return nil
}
2022-03-23 16:10:03 +00:00
sd := new ( structs . ServiceDefinition )
ServiceDefinitionToStructs ( s , sd )
return sd
2020-09-18 18:05:15 +00:00
}
// TODO: handle this with mog
func NewServiceDefinitionPtrFromStructs ( t * structs . ServiceDefinition ) * ServiceDefinition {
if t == nil {
return nil
}
2022-03-23 16:10:03 +00:00
sd := new ( ServiceDefinition )
ServiceDefinitionFromStructs ( t , sd )
return sd
2020-09-18 18:05:15 +00:00
}
2022-12-22 18:03:33 +00:00
// EnvoyExtensionsToStructs converts the protobuf type to structs. This is copied here because it either needs to be in the local package or in structs.
func EnvoyExtensionsToStructs ( args [ ] * pbconfigentry . EnvoyExtension ) [ ] structs . EnvoyExtension {
return pbconfigentry . EnvoyExtensionsToStructs ( args )
}
// EnvoyExtensionsFromStructs converts the structs type to protobuf type. This is copied here because it either needs to be in the local package or in structs.
func EnvoyExtensionsFromStructs ( args [ ] structs . EnvoyExtension ) [ ] * pbconfigentry . EnvoyExtension {
return pbconfigentry . EnvoyExtensionsFromStructs ( args )
}