-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathinternal_compute.go
More file actions
246 lines (228 loc) · 4.84 KB
/
internal_compute.go
File metadata and controls
246 lines (228 loc) · 4.84 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package internal
import (
"fmt"
"go.temporal.io/api/compute/v1"
enums "go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/converter"
)
func computeConfigFromProto(
dc converter.DataConverter,
msg *compute.ComputeConfig,
) (*ComputeConfig, error) {
if msg == nil {
return nil, nil
}
res := &ComputeConfig{}
groups := make(map[string]*ComputeConfigScalingGroup, len(msg.ScalingGroups))
for groupName, group := range msg.ScalingGroups {
g, err := computeConfigScalingGroupFromProto(dc, group)
if err != nil {
return nil, err
}
groups[groupName] = g
}
res.ScalingGroups = groups
return res, nil
}
func validateComputeConfig(cfg *ComputeConfig) error {
if cfg == nil {
return nil
}
for groupName, group := range cfg.ScalingGroups {
err := validateComputeConfigScalingGroup(groupName, group)
if err != nil {
return err
}
}
return nil
}
func computeConfigToProto(
dc converter.DataConverter,
cc *ComputeConfig,
) (*compute.ComputeConfig, error) {
if cc == nil {
return nil, nil
}
groups := make(
map[string]*compute.ComputeConfigScalingGroup,
len(cc.ScalingGroups),
)
for groupName, group := range cc.ScalingGroups {
g, err := computeConfigScalingGroupToProto(dc, group)
if err != nil {
return nil, err
}
groups[groupName] = g
}
return &compute.ComputeConfig{
ScalingGroups: groups,
}, nil
}
func computeConfigScalingGroupFromProto(
dc converter.DataConverter,
msg *compute.ComputeConfigScalingGroup,
) (*ComputeConfigScalingGroup, error) {
if msg == nil {
return nil, nil
}
res := &ComputeConfigScalingGroup{}
msgTQTs := msg.GetTaskQueueTypes()
tqts := make([]TaskQueueType, len(msgTQTs))
for x, msgTQT := range msgTQTs {
tqts[x] = taskQueueTypeFromProto(msgTQT)
}
res.TaskQueueTypes = tqts
p, err := computeProviderFromProto(dc, msg.GetProvider())
if err != nil {
return nil, err
}
res.Provider = p
s, err := computeScalerFromProto(dc, msg.GetScaler())
if err != nil {
return nil, err
}
res.Scaler = s
return res, nil
}
func validateComputeConfigScalingGroup(
groupName string,
cfg *ComputeConfigScalingGroup,
) error {
if cfg == nil {
return nil
}
p := cfg.Provider
if p != nil {
if p.Type == "" {
return fmt.Errorf(
"compute config scaling group %s missing provider type",
groupName,
)
}
if p.Details == nil {
return fmt.Errorf(
"compute config scaling group %s missing provider details",
groupName,
)
}
}
s := cfg.Scaler
if s != nil {
if s.Type == "" {
return fmt.Errorf(
"compute config scaling group %s missing scaler type",
groupName,
)
}
if s.Details == nil {
return fmt.Errorf(
"compute config scaling group %s missing scaler details",
groupName,
)
}
}
return nil
}
func computeConfigScalingGroupToProto(
dc converter.DataConverter,
group *ComputeConfigScalingGroup,
) (*compute.ComputeConfigScalingGroup, error) {
if group == nil {
return nil, nil
}
tqts := group.TaskQueueTypes
msgTQTs := make([]enums.TaskQueueType, len(tqts))
for x, tqt := range tqts {
msgTQTs[x] = taskQueueTypeToProto(tqt)
}
provider, err := computeProviderToProto(dc, group.Provider)
if err != nil {
return nil, err
}
scaler, err := computeScalerToProto(dc, group.Scaler)
if err != nil {
return nil, err
}
return &compute.ComputeConfigScalingGroup{
TaskQueueTypes: msgTQTs,
Provider: provider,
Scaler: scaler,
}, nil
}
func computeProviderToProto(
dc converter.DataConverter,
p *ComputeProvider,
) (*compute.ComputeProvider, error) {
if p == nil {
return nil, nil
}
res := &compute.ComputeProvider{
Type: p.Type,
}
details := p.Details
enc, err := dc.ToPayload(&details)
if err != nil {
return nil, err
}
res.Details = enc
return res, nil
}
func computeScalerToProto(
dc converter.DataConverter,
s *ComputeScaler,
) (*compute.ComputeScaler, error) {
if s == nil {
return nil, nil
}
res := &compute.ComputeScaler{
Type: s.Type,
}
details := s.Details
enc, err := dc.ToPayload(&details)
if err != nil {
return nil, err
}
res.Details = enc
return res, nil
}
func computeProviderFromProto(
dc converter.DataConverter,
msg *compute.ComputeProvider,
) (*ComputeProvider, error) {
if msg == nil {
return nil, nil
}
res := &ComputeProvider{
Type: msg.GetType(),
NexusEndpoint: msg.GetNexusEndpoint(),
}
details := make(map[string]any)
if details != nil {
err := dc.FromPayload(msg.GetDetails(), details)
if err != nil {
return nil, err
}
res.Details = details
}
return res, nil
}
func computeScalerFromProto(
dc converter.DataConverter,
msg *compute.ComputeScaler,
) (*ComputeScaler, error) {
if msg == nil {
return nil, nil
}
res := &ComputeScaler{
Type: msg.GetType(),
}
details := make(map[string]any)
if details != nil {
err := dc.FromPayload(msg.GetDetails(), details)
if err != nil {
return nil, err
}
res.Details = details
}
return res, nil
}