-
Notifications
You must be signed in to change notification settings - Fork 25
/
control-point-id.go
126 lines (112 loc) · 3.91 KB
/
control-point-id.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
package selectors
import (
cmdv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/cmd/v1"
flowcontrolpointsv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/flowcontrol/controlpoints/v1"
)
// ControlPointID is the struct that represents a ControlPoint.
//
// Agent group is implied.
// Type is ignored.
type ControlPointID struct {
ControlPoint string
Service string
}
// TypedControlPointID is the struct that represents a FlowControlPoint.
//
// Agent group is implied.
//
// Note: We need to mirror flowcontrolpointsv1.FlowControlPoint, because
// protobuf-generated struct cannot be used as map keys.
type TypedControlPointID struct {
ControlPointID
Type string
}
// TypedGlobalControlPointID is ControlPointID with explicit agent group.
//
// Note: We need to mirror cmdv1.GlobalFlowControlPoint, because
// protobuf-generated struct cannot be used as map keys.
type TypedGlobalControlPointID struct {
TypedControlPointID
AgentGroup string
}
// GlobalControlPointID is just like TypedGlobalControlPointID but embedding the ControlPointID instead of TypedControlPointID.
//
// Useful for defining a control point to find, without having to specify the source.
type GlobalControlPointID struct {
ControlPointID
AgentGroup string
}
// NewControlPointID returns a controlPointID.
func NewControlPointID(controlPoint, service string) ControlPointID {
return ControlPointID{
ControlPoint: controlPoint,
Service: service,
}
}
// WithType returns the controlpoint as TypedControlPointID.
func (cp ControlPointID) WithType(controlPointType string) TypedControlPointID {
return TypedControlPointID{
ControlPointID: cp,
Type: controlPointType,
}
}
// String returns the string representation of the control point.
func (cp ControlPointID) String() string {
return cp.Service + "/" + cp.ControlPoint
}
// NewTypedControlPointID returns a typedControlPointID.
func NewTypedControlPointID(controlPoint, controlPointType, service string) TypedControlPointID {
return TypedControlPointID{
ControlPointID: NewControlPointID(controlPoint, service),
Type: controlPointType,
}
}
// ToProto returns protobuf representation of control point.
func (cp *TypedControlPointID) ToProto() *flowcontrolpointsv1.FlowControlPoint {
return &flowcontrolpointsv1.FlowControlPoint{
ControlPoint: cp.ControlPoint,
Type: cp.Type,
Service: cp.Service,
}
}
// InAgentGroup returns the controlpoint as TypedGlobalControlPointID with given agent group.
func (cp TypedControlPointID) InAgentGroup(agentGroup string) TypedGlobalControlPointID {
return TypedGlobalControlPointID{
TypedControlPointID: cp,
AgentGroup: agentGroup,
}
}
// TypedControlPointIDFromProto creates TypedControlPointID from protobuf representation.
func TypedControlPointIDFromProto(protoCP *flowcontrolpointsv1.FlowControlPoint) TypedControlPointID {
return TypedControlPointID{
ControlPointID: ControlPointID{
Service: protoCP.GetService(),
ControlPoint: protoCP.GetControlPoint(),
},
Type: protoCP.GetType(),
}
}
// ToProto returns protobuf representation of control point.
func (cp *TypedGlobalControlPointID) ToProto() *cmdv1.GlobalFlowControlPoint {
return &cmdv1.GlobalFlowControlPoint{
FlowControlPoint: &flowcontrolpointsv1.FlowControlPoint{
ControlPoint: cp.ControlPoint,
Type: cp.Type,
Service: cp.Service,
},
AgentGroup: cp.AgentGroup,
}
}
// TypedGlobalControlPointIDFromProto creates ControlPointID from protobuf representation.
func TypedGlobalControlPointIDFromProto(protoCP *cmdv1.GlobalFlowControlPoint) TypedGlobalControlPointID {
return TypedGlobalControlPointID{
TypedControlPointID: TypedControlPointID{
ControlPointID: ControlPointID{
ControlPoint: protoCP.FlowControlPoint.GetControlPoint(),
Service: protoCP.FlowControlPoint.GetService(),
},
Type: protoCP.FlowControlPoint.GetType(),
},
AgentGroup: protoCP.GetAgentGroup(),
}
}