This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
652 lines (550 loc) · 16.3 KB
/
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package yandex
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net"
"sort"
"strings"
"text/template"
"time"
"github.com/c2h5oh/datasize"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/yandex-cloud/go-genproto/yandex/cloud/access"
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1/awscompatibility"
"github.com/yandex-cloud/go-genproto/yandex/cloud/resourcemanager/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
"github.com/yandex-cloud/go-sdk/sdkresolvers"
)
type instanceAction int
const (
instanceActionStop instanceAction = iota
instanceActionStart
instanceActionRestart
)
const defaultTimeFormat = time.RFC3339
const defaultListSize = 1000
type Policy struct {
Bindings []*access.AccessBinding
}
func getZone(d *schema.ResourceData, config *Config) (string, error) {
res, ok := d.GetOk("zone")
if !ok {
if config.Zone != "" {
return config.Zone, nil
}
return "", fmt.Errorf("cannot determine zone: please set 'zone' key in this resource or at provider level")
}
return res.(string), nil
}
func getCloudID(d *schema.ResourceData, config *Config) (string, error) {
res, ok := d.GetOk("cloud_id")
if !ok {
if config.CloudID != "" {
return config.CloudID, nil
}
return "", fmt.Errorf("cannot determine cloud_id: please set 'cloud_id' key in this resource or at provider level")
}
return res.(string), nil
}
func getFolderID(d *schema.ResourceData, config *Config) (string, error) {
res, ok := d.GetOk("folder_id")
if !ok {
if config.FolderID != "" {
return config.FolderID, nil
}
return "", fmt.Errorf("cannot determine folder_id: please set 'folder_id' key in this resource or at provider level")
}
return res.(string), nil
}
func cloudIDOfFolderID(config *Config, folderID string) (string, error) {
folder, err := config.sdk.ResourceManager().Folder().Get(config.Context(), &resourcemanager.GetFolderRequest{
FolderId: folderID,
})
if err != nil {
return "", err
}
return folder.CloudId, nil
}
func lockCloudByFolderID(config *Config, folderID string) (func(), error) {
cloudID, err := cloudIDOfFolderID(config, folderID)
if err != nil {
return nil, fmt.Errorf("error getting cloud ID of `folder_id` %s: %s", folderID, err)
}
c := CloudIamUpdater{cloudID: cloudID}
mutexKey := c.GetMutexKey()
mutexKV.Lock(mutexKey)
return func() {
mutexKV.Unlock(mutexKey)
}, nil
}
func createTemporaryStaticAccessKey(roleID string, config *Config) (accessKey, secretKey string, cleanup func(), err error) {
op, err := config.sdk.WrapOperation(config.sdk.IAM().ServiceAccount().Create(context.Background(), &iam.CreateServiceAccountRequest{
FolderId: config.FolderID,
Name: acctest.RandomWithPrefix("tmp-sa-"),
}))
if err != nil {
return
}
protoMetadata, err := op.Metadata()
if err != nil {
return
}
md, ok := protoMetadata.(*iam.CreateServiceAccountMetadata)
if !ok {
err = fmt.Errorf("could not get temporary service account ID from create operation metadata")
return
}
saID := md.ServiceAccountId
err = op.Wait(context.Background())
if err != nil {
return
}
deleteSa := func() {
op, err := config.sdk.WrapOperation(config.sdk.IAM().ServiceAccount().Delete(context.Background(), &iam.DeleteServiceAccountRequest{
ServiceAccountId: saID,
}))
if err != nil {
log.Printf("[WARN] error deleting temporary service account: %s", err)
return
}
err = op.Wait(context.Background())
if err != nil {
log.Printf("[WARN] error deleting temporary service account: %s", err)
}
}
createKey := func() (*awscompatibility.CreateAccessKeyResponse, error) {
op, err = config.sdk.WrapOperation(config.sdk.ResourceManager().Folder().SetAccessBindings(context.Background(), &access.SetAccessBindingsRequest{
ResourceId: config.FolderID,
AccessBindings: []*access.AccessBinding{
{
RoleId: roleID,
Subject: &access.Subject{
Id: saID,
Type: "serviceAccount",
},
},
},
}))
if err != nil {
return nil, err
}
err = op.Wait(context.Background())
if err != nil {
return nil, err
}
sak, err := config.sdk.IAM().AWSCompatibility().AccessKey().Create(context.Background(), &awscompatibility.CreateAccessKeyRequest{
ServiceAccountId: saID,
})
if err != nil {
return nil, err
}
return sak, err
}
sak, err := createKey()
if err != nil {
deleteSa()
return
}
accessKey = sak.AccessKey.KeyId
secretKey = sak.Secret
cleanup = func() {
_, err := config.sdk.IAM().AWSCompatibility().AccessKey().Delete(context.Background(), &awscompatibility.DeleteAccessKeyRequest{
AccessKeyId: sak.AccessKey.Id,
})
if err != nil {
log.Printf("[WARN] error deleting temporary access key: %s", err)
}
deleteSa()
}
return
}
func handleNotFoundError(err error, d *schema.ResourceData, resourceName string) error {
if isStatusWithCode(err, codes.NotFound) {
log.Printf("[WARN] Removing %s because resource doesn't exist anymore", resourceName)
d.SetId("")
return nil
}
return fmt.Errorf("error reading %s: %s", resourceName, err)
}
func isStatusWithCode(err error, code codes.Code) bool {
grpcStatus, ok := status.FromError(err)
return ok && grpcStatus.Code() == code
}
func isRequestIDPresent(err error) (string, bool) {
st, ok := status.FromError(err)
if ok {
for _, d := range st.Details() {
if reqInfo, ok := d.(*errdetails.RequestInfo); ok {
return reqInfo.RequestId, true
}
}
}
return "", false
}
func convertStringArrToInterface(sslice []string) []interface{} {
islice := make([]interface{}, len(sslice))
for i, str := range sslice {
islice[i] = str
}
return islice
}
func mergeSchemas(a, b map[string]*schema.Schema) map[string]*schema.Schema {
merged := make(map[string]*schema.Schema, len(a)+len(b))
for k, v := range a {
merged[k] = v
}
for k, v := range b {
merged[k] = v
}
return merged
}
func roleMemberToAccessBinding(role, member string) *access.AccessBinding {
chunks := strings.SplitN(member, ":", 2)
return &access.AccessBinding{
RoleId: role,
Subject: &access.Subject{
Type: chunks[0],
Id: chunks[1],
},
}
}
func mergeBindings(bindings []*access.AccessBinding) []*access.AccessBinding {
bm := rolesToMembersMap(bindings)
var rb []*access.AccessBinding
for role, members := range bm {
for member := range members {
rb = append(rb, roleMemberToAccessBinding(role, member))
}
}
return rb
}
// Map a role to a map of members, allowing easy merging of multiple bindings.
func rolesToMembersMap(bindings []*access.AccessBinding) map[string]map[string]bool {
bm := make(map[string]map[string]bool)
// Get each binding
for _, b := range bindings {
// Initialize members map
if _, ok := bm[b.RoleId]; !ok {
bm[b.RoleId] = make(map[string]bool)
}
// Get each member (user/principal) for the binding
m := canonicalMember(b)
bm[b.RoleId][m] = true
}
return bm
}
func roleToMembersList(role string, bindings []*access.AccessBinding) []string {
var members []string
for _, b := range bindings {
if b.RoleId != role {
continue
}
m := canonicalMember(b)
members = append(members, m)
}
return members
}
func removeRoleFromBindings(roleForRemove string, bindings []*access.AccessBinding) []*access.AccessBinding {
bm := rolesToMembersMap(bindings)
var rb []*access.AccessBinding
for role, members := range bm {
if role == roleForRemove {
continue
}
for member := range members {
rb = append(rb, roleMemberToAccessBinding(role, member))
}
}
return rb
}
func (p Policy) String() string {
result := ""
for i, binding := range p.Bindings {
result = result + fmt.Sprintf("\n#:%d role:%-27s\taccount:%-24s\ttype:%s",
i, binding.RoleId, binding.Subject.Id, binding.Subject.Type)
}
return result + "\n"
}
func convertStringSet(set *schema.Set) []string {
s := make([]string, set.Len())
for i, v := range set.List() {
s[i] = v.(string)
}
return s
}
func convertStringMap(v map[string]interface{}) map[string]string {
m := make(map[string]string)
if v == nil {
return m
}
for k, val := range v {
m[k] = val.(string)
}
return m
}
func shouldSuppressDiffForPolicies(k, old, new string, d *schema.ResourceData) bool {
var oldPolicy, newPolicy Policy
if err := json.Unmarshal([]byte(old), &oldPolicy); err != nil {
log.Printf("[ERROR] Could not unmarshal old policy %s: %v", old, err)
return false
}
if err := json.Unmarshal([]byte(new), &newPolicy); err != nil {
log.Printf("[ERROR] Could not unmarshal new policy %s: %v", new, err)
return false
}
oldPolicy.Bindings = mergeBindings(oldPolicy.Bindings)
newPolicy.Bindings = mergeBindings(newPolicy.Bindings)
if len(newPolicy.Bindings) != len(oldPolicy.Bindings) {
return false
}
sort.Sort(sortableBindings(newPolicy.Bindings))
sort.Sort(sortableBindings(oldPolicy.Bindings))
for pos, newBinding := range newPolicy.Bindings {
oldBinding := oldPolicy.Bindings[pos]
if oldBinding.RoleId != newBinding.RoleId {
return false
}
if oldBinding.Subject.Type != newBinding.Subject.Type {
return false
}
if oldBinding.Subject.Id != newBinding.Subject.Id {
return false
}
}
return true
}
type sortableBindings []*access.AccessBinding
func (b sortableBindings) Len() int {
return len(b)
}
func (b sortableBindings) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b sortableBindings) Less(i, j int) bool {
return b.String(i) < b.String(j)
}
func (b sortableBindings) String(i int) string {
return fmt.Sprintf("%s\x00%s\x00%s", b[i].RoleId, b[i].Subject.Type, b[i].Subject.Id)
}
func validateIPV4CidrBlocks(v interface{}, k string) (warnings []string, errors []error) {
_, _, err := net.ParseCIDR(v.(string))
if err != nil {
errors = append(errors, fmt.Errorf("%q is not a valid IP CIDR range: %s", k, err))
}
return
}
// Primary use to store value from API in state file as Gigabytes
func toGigabytes(bytesCount int64) int {
return int((datasize.ByteSize(bytesCount) * datasize.B).GBytes())
}
func toGigabytesInFloat(bytesCount int64) float64 {
return (datasize.ByteSize(bytesCount) * datasize.B).GBytes()
}
// Primary use to send byte value to API
func toBytes(gigabytesCount int) int64 {
return int64((datasize.ByteSize(gigabytesCount) * datasize.GB).Bytes())
}
func toBytesFromFloat(gigabytesCountF float64) int64 {
return int64(gigabytesCountF * float64(datasize.GB))
}
func (action instanceAction) String() string {
switch action {
case instanceActionStop:
return "Stop"
case instanceActionStart:
return "Start"
case instanceActionRestart:
return "Restart"
default:
return "Unknown"
}
}
func getTimestamp(protots *timestamp.Timestamp) (string, error) {
if protots == nil {
return "", nil
}
ts, err := ptypes.Timestamp(protots)
if err != nil {
return "", fmt.Errorf("failed to convert protobuf timestamp: %s", err)
}
return ts.Format(defaultTimeFormat), nil
}
func stringSliceToLower(s []string) []string {
var ret []string
for _, v := range s {
ret = append(ret, strings.ToLower(v))
}
return ret
}
func getEnumValueMapKeys(m map[string]int32) []string {
return getEnumValueMapKeysExt(m, false)
}
func getEnumValueMapKeysExt(m map[string]int32, skipDefault bool) []string {
keys := make([]string, 0, len(m))
for k, v := range m {
if v == 0 && skipDefault {
continue
}
keys = append(keys, k)
}
return keys
}
func getJoinedKeys(keys []string) string {
return "`" + strings.Join(keys, "`, `") + "`"
}
func checkOneOf(d *schema.ResourceData, keys ...string) error {
var gotKey bool
for _, key := range keys {
_, ok := d.GetOk(key)
if ok {
if gotKey {
return fmt.Errorf("only one of %s can be provided", getJoinedKeys(keys))
}
gotKey = true
}
}
if !gotKey {
return fmt.Errorf("one of %s should be provided", getJoinedKeys(keys))
}
return nil
}
type objectResolverFunc func(name string, opts ...sdkresolvers.ResolveOption) ycsdk.Resolver
// this function can be only used to resolve objects that belong to some folder (have folder_id attribute)
// do not use this function to resolve cloud (or similar objects) ID by name.
func resolveObjectID(ctx context.Context, config *Config, d *schema.ResourceData, resolverFunc objectResolverFunc) (string, error) {
name, ok := d.GetOk("name")
if !ok {
return "", fmt.Errorf("non empty name should be provided")
}
folderID, err := getFolderID(d, config)
if err != nil {
return "", err
}
return resolveObjectIDByNameAndFolderID(ctx, config, name.(string), folderID, resolverFunc)
}
func resolveObjectIDByNameAndFolderID(ctx context.Context, config *Config, name, folderID string, resolverFunc objectResolverFunc) (string, error) {
if name == "" {
return "", fmt.Errorf("non empty name should be provided")
}
var objectID string
resolver := resolverFunc(name, sdkresolvers.Out(&objectID), sdkresolvers.FolderID(folderID))
err := config.sdk.Resolve(ctx, resolver)
if err != nil {
return "", err
}
return objectID, nil
}
func getSnapshotMinStorageSize(snapshotID string, config *Config) (size int64, err error) {
ctx := config.Context()
snapshot, err := config.sdk.Compute().Snapshot().Get(ctx, &compute.GetSnapshotRequest{
SnapshotId: snapshotID,
})
if err != nil {
return 0, fmt.Errorf("Error on retrieve snapshot properties: %s", err)
}
return snapshot.DiskSize, nil
}
func getImageMinStorageSize(imageID string, config *Config) (size int64, err error) {
ctx := config.Context()
image, err := config.sdk.Compute().Image().Get(ctx, &compute.GetImageRequest{
ImageId: imageID,
})
if err != nil {
return 0, fmt.Errorf("Error on retrieve image properties: %s", err)
}
return image.MinDiskSize, nil
}
func templateConfig(tmpl string, ctx ...map[string]interface{}) string {
p := make(map[string]interface{})
for _, c := range ctx {
for k, v := range c {
p[k] = v
}
}
b := &bytes.Buffer{}
err := template.Must(template.New("").Parse(tmpl)).Execute(b, p)
if err != nil {
panic(fmt.Errorf("failed to execute config template: %v", err))
}
return b.String()
}
func getResourceID(n string, s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[n]
if !ok {
return "", fmt.Errorf("terraform resource '%s' not found", n)
}
if rs.Primary.ID == "" {
return "", fmt.Errorf("no ID is set for terraform resource '%s'", n)
}
return rs.Primary.ID, nil
}
type schemaGetHelper struct {
pathPrefix string
d *schema.ResourceData
}
func schemaHelper(d *schema.ResourceData, path string) *schemaGetHelper {
return &schemaGetHelper{
pathPrefix: path,
d: d,
}
}
func (h *schemaGetHelper) AppendPath(path string) *schemaGetHelper {
return &schemaGetHelper{
pathPrefix: h.pathPrefix + path,
d: h.d,
}
}
func (h *schemaGetHelper) Get(key string) interface{} {
return h.d.Get(h.pathPrefix + key)
}
func (h *schemaGetHelper) GetOk(key string) (interface{}, bool) {
return h.d.GetOk(h.pathPrefix + key)
}
func (h *schemaGetHelper) GetString(key string) string {
return h.d.Get(h.pathPrefix + key).(string)
}
func (h *schemaGetHelper) GetInt(key string) int {
return h.d.Get(h.pathPrefix + key).(int)
}
func convertResourceToDataSource(resource *schema.Resource) *schema.Resource {
return recursivelyUpdateResource(resource, func(schema *schema.Schema) {
schema.Computed = true
schema.Required = false
schema.Optional = false
schema.ForceNew = false
schema.Default = nil
schema.ValidateFunc = nil
})
}
func recursivelyUpdateResource(resource *schema.Resource, callback func(*schema.Schema)) *schema.Resource {
attributes := make(map[string]*schema.Schema)
for key, attributeSchema := range resource.Schema {
copyOfAttributeSchema := *attributeSchema
callback(©OfAttributeSchema)
if copyOfAttributeSchema.Elem != nil {
switch elem := copyOfAttributeSchema.Elem.(type) {
case *schema.Schema:
elementCopy := *elem
copyOfAttributeSchema.Elem = &elementCopy
case *schema.Resource:
copyOfAttributeSchema.Elem = recursivelyUpdateResource(elem, callback)
default:
log.Printf("[ERROR] Unexpected Elem type %T for attribute %s!\n", elem, key)
}
}
attributes[key] = ©OfAttributeSchema
}
return &schema.Resource{Schema: attributes}
}