/
compute_utils.go
executable file
·397 lines (352 loc) · 11.2 KB
/
compute_utils.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2024 Google LLC. All Rights Reserved.
//
// 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
//
// http://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 compute contains handwritten support code for the compute service.
package alpha
import (
"bytes"
"context"
"fmt"
"net"
"net/url"
"path"
"strings"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl/operations"
)
// EncodeImageDeprecateRequest properly encodes the image deprecation request for an image.
func EncodeImageDeprecateRequest(m map[string]interface{}) map[string]interface{} {
req := make(map[string]interface{})
// Deprecate requests involve removing the "deprecated" key.
if deprecatedVal, ok := m["deprecated"]; ok {
deprecatedMap := deprecatedVal.(map[string]interface{})
for key, value := range deprecatedMap {
req[key] = value
}
}
return req
}
// WrapTargetPoolInstance wraps the instances provided by AddInstance and RemoveInstance
// in their required format.
func WrapTargetPoolInstance(m map[string]interface{}) map[string]interface{} {
is, ok := m["instances"].([]string)
if !ok {
return nil
}
wrapped := make([]interface{}, len(is))
for idx, i := range is {
wrapped[idx] = map[string]interface{}{
"instance": i,
}
}
return map[string]interface{}{
"instances": wrapped,
}
}
// WrapTargetPoolHealthCheck wraps the instances provided by AddHC and RemoveHC
// in their required format.
func WrapTargetPoolHealthCheck(m map[string]interface{}) map[string]interface{} {
hcs, ok := m["healthChecks"].([]string)
if !ok {
return nil
}
wrapped := make([]interface{}, len(hcs))
for idx, hc := range hcs {
wrapped[idx] = map[string]interface{}{
"healthCheck": hc,
}
}
return map[string]interface{}{
"healthChecks": wrapped,
}
}
// forwardingRuleEncodeCreateRequest removes the labels parameter - it cannot be supplied on create.
func forwardingRuleEncodeCreateRequest(m map[string]any) map[string]any {
// labels cannot be specified on create
delete(m, "labels")
return m
}
// forwardingRuleSetLabelsPostCreate adds a 'setLabels' operation after
// a create operation, because creation cannot set labels due to a
// long-standing bug in the API for most compute networking resources.
// createPubsubConfigs adds a patch to apply PubsubConfigs after create (if applicable).
func forwardingRuleSetLabelsPostCreate(inOps []forwardingRuleApiOperation) ([]forwardingRuleApiOperation, error) {
for _, op := range inOps {
if _, ok := op.(*createForwardingRuleOperation); ok {
return append(inOps, &updateForwardingRuleSetLabelsOperation{FieldDiffs: []*dcl.FieldDiff{{FieldName: "labels"}}}), nil
}
}
return inOps, nil
}
func canonicalizeReservationCPUPlatform(o, n interface{}) bool {
oVal, _ := o.(*string)
nVal, _ := n.(*string)
return equalReservationCPUPlatform(oVal, nVal)
}
func equalReservationCPUPlatform(o, n *string) bool {
if o == nil && n == nil {
return true
}
if o == nil || n == nil {
return false
}
if *o == "automatic" {
return true
}
if *n == "automatic" {
return true
}
return *o == *n
}
func canonicalizeIPAddressToReference(o, n interface{}) bool {
oVal, _ := o.(*string)
nVal, _ := n.(*string)
if oVal == nil && nVal == nil {
return true
}
if oVal == nil || nVal == nil {
return false
}
if isIPV4Address(*oVal) && !isIPV4Address(*nVal) {
return true
}
if isIPV4Address(*nVal) && !isIPV4Address(*oVal) {
return true
}
return dcl.NameToSelfLink(oVal, nVal)
}
func isIPV4Address(addr string) bool {
return net.ParseIP(addr) != nil
}
func canonicalizePortRange(o, n interface{}) bool {
oVal, _ := o.(*string)
nVal, _ := n.(*string)
return equalPortRanges(oVal, nVal)
}
func equalPortRanges(o, n *string) bool {
if o == nil && n == nil {
return true
}
if o == nil || n == nil {
return false
}
if strings.Contains(*o, "-") && !strings.Contains(*n, "-") {
// If one of them contains a dash but not the other, ensure that the one with a dash is 'n'.
swap := n
n = o
o = swap
}
if !strings.Contains(*o, "-") && strings.Contains(*n, "-") {
o = dcl.String(fmt.Sprintf("%s-%s", *o, *o))
}
return *o == *n
}
// Custom create method for firewall policy which waits on a ComputeGlobalOrganizationOperation.
func (op *createFirewallPolicyOperation) do(ctx context.Context, r *FirewallPolicy, c *Client) error {
c.Config.Logger.Infof("Attempting to create %v", r)
u, err := r.createURL(c.Config.BasePath)
if err != nil {
return err
}
req, err := r.marshal(c)
if err != nil {
return err
}
resp, err := dcl.SendRequest(ctx, c.Config, "POST", u, bytes.NewBuffer(req), c.Config.RetryProvider)
if err != nil {
return err
}
// Wait for object to be created.
var o operations.ComputeGlobalOrganizationOperation
if err := dcl.ParseResponse(resp.Response, &o.BaseOperation); err != nil {
return err
}
if err := o.Wait(context.WithValue(ctx, dcl.DoNotLogRequestsKey, true), c.Config, r.Parent); err != nil {
c.Config.Logger.Warningf("Creation failed after waiting for operation: %v", err)
return err
}
c.Config.Logger.Infof("Successfully waited for operation")
r.Name = &o.BaseOperation.TargetID
if _, err := c.GetFirewallPolicy(ctx, r); err != nil {
return err
}
return nil
}
// Custom update method for network which updates mtu field before updating other fields.
func (op *updateNetworkUpdateOperation) do(ctx context.Context, r *Network, c *Client) error {
_, err := c.GetNetwork(ctx, r)
if err != nil {
return err
}
u, err := r.updateURL(c.Config.BasePath, "update")
if err != nil {
return err
}
req, err := newUpdateNetworkUpdateRequest(ctx, r, c)
if err != nil {
return err
}
if mtu, ok := req["mtu"]; ok {
// Update mtu field first.
if err := performNetworkUpdate(ctx, r, c, u, map[string]interface{}{"mtu": mtu}); err != nil {
return err
}
delete(req, "mtu")
}
if err := performNetworkUpdate(ctx, r, c, u, req); err != nil {
return err
}
return nil
}
// Send the given update request to the given url on the given network with the given client in the given context and wait for the resulting operation.
func performNetworkUpdate(ctx context.Context, r *Network, c *Client, u string, req map[string]interface{}) error {
c.Config.Logger.InfoWithContextf(ctx, "Created update: %#v", req)
body, err := marshalUpdateNetworkUpdateRequest(c, req)
if err != nil {
return err
}
resp, err := dcl.SendRequest(ctx, c.Config, "PATCH", u, bytes.NewBuffer(body), c.Config.RetryProvider)
if err != nil {
return err
}
var o operations.ComputeOperation
if err := dcl.ParseResponse(resp.Response, &o); err != nil {
return err
}
err = o.Wait(context.WithValue(ctx, dcl.DoNotLogRequestsKey, true), c.Config, r.basePath(), "GET")
if err != nil {
return err
}
return nil
}
// Because the server will return both versions and instance template and expects only one to
// be set in our requests, instance template will flatten to nil if versions is non-empty.
func flattenInstanceGroupManagerInstanceTemplateWithConflict(c *Client, instanceTemplate interface{}, resource *InstanceGroupManager) *string {
if len(resource.Versions) > 0 {
c.Config.Logger.Info("flattening instance_template field to nil because versions were present")
return nil
}
return dcl.FlattenString(instanceTemplate)
}
// Because the server will return both instance_template and instance template and expects only one to
// be set in our requests, instance template will flatten to nil if instance_template is non-nil.
func flattenInstanceGroupManagerVersionsWithConflict(c *Client, Versions interface{}, resource *InstanceGroupManager) []InstanceGroupManagerVersions {
if resource.InstanceTemplate != nil {
c.Config.Logger.Info("flattening versions field to nil because instance_template was present")
return nil
}
return flattenInstanceGroupManagerVersionsSlice(c, Versions, resource)
}
func machineTypeOperations() func(fd *dcl.FieldDiff) []string {
return func(fd *dcl.FieldDiff) []string {
// We're assuming that the instance is currently running. If it isn't, this will lead to a no-op stop operation.
return []string{"updateInstanceStopOperation", "updateInstanceMachineTypeOperation", "updateInstanceStartOperation"}
}
}
func flattenPacketMirroringRegion(_ *Client, region interface{}) *string {
regionString, ok := region.(string)
if !ok {
return nil
}
return dcl.SelfLinkToName(®ionString)
}
func targetPoolHealthCheck() func(fd *dcl.FieldDiff) []string {
return func(fd *dcl.FieldDiff) []string {
var ops []string
if !dcl.IsZeroValue(fd.ToAdd) {
ops = append(ops, "updateTargetPoolAddHCOperation")
}
if !dcl.IsZeroValue(fd.ToRemove) {
ops = append(ops, "updateTargetPoolRemoveHCOperation")
}
return ops
}
}
func targetPoolInstances() func(fd *dcl.FieldDiff) []string {
return func(fd *dcl.FieldDiff) []string {
var ops []string
if !dcl.IsZeroValue(fd.ToAdd) {
ops = append(ops, "updateTargetPoolAddInstanceOperation")
}
if !dcl.IsZeroValue(fd.ToRemove) {
ops = append(ops, "updateTargetPoolRemoveInstanceOperation")
}
return ops
}
}
func flattenNetworkSelfLinkWithID(_ *Client, _ interface{}, _ *Network, r map[string]interface{}) *string {
selfLink, ok := r["selfLink"].(string)
if !ok {
return nil
}
id, ok := r["id"].(string)
if !ok {
return nil
}
u, err := url.Parse(selfLink)
if err != nil {
return nil
}
u.Path = fmt.Sprintf("%s/%s", path.Dir(u.Path), id)
selfLinkWithID := u.String()
return &selfLinkWithID
}
// Subnetwork's update operation has a custom method because a separate request must be performed for each field.
func (op *updateSubnetworkUpdateOperation) do(ctx context.Context, r *Subnetwork, c *Client) error {
_, err := c.GetSubnetwork(ctx, r)
if err != nil {
return err
}
u, err := r.updateURL(c.Config.BasePath, "update")
if err != nil {
return err
}
req, err := newUpdateSubnetworkUpdateRequest(ctx, r, c)
if err != nil {
return err
}
fingerprint := req["fingerprint"]
for field, value := range req {
if field == "fingerprint" {
continue
}
sr := map[string]interface{}{
field: value,
"fingerprint": fingerprint,
}
c.Config.Logger.InfoWithContextf(ctx, "Created update: %#v", sr)
body, err := marshalUpdateSubnetworkUpdateRequest(c, sr)
if err != nil {
return err
}
resp, err := dcl.SendRequest(ctx, c.Config, "PATCH", u, bytes.NewBuffer(body), c.Config.RetryProvider)
if err != nil {
return err
}
var o operations.ComputeOperation
if err := dcl.ParseResponse(resp.Response, &o); err != nil {
return err
}
err = o.Wait(context.WithValue(ctx, dcl.DoNotLogRequestsKey, true), c.Config, r.basePath(), "GET")
if err != nil {
return err
}
// Perform a get request to pick up the new fingerprint for the resource.
ur, err := c.GetSubnetwork(ctx, r)
if err != nil {
return err
}
fingerprint = ur.Fingerprint
}
return nil
}