/
resource.go
1007 lines (857 loc) · 30.6 KB
/
resource.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 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 main
import (
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"bitbucket.org/creachadair/stringset"
"github.com/golang/glog"
"github.com/nasa9084/go-openapi"
"gopkg.in/yaml.v2"
)
const GoPkgTerraformSdkValidation = "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
// Resource is tpgtools' model of what a information is necessary to generate a
// resource in TPG.
type Resource struct {
productMetadata *ProductMetadata
// ID is the Terraform resource id format as a pattern string. Additionally,
// import formats can be derived from it.
ID string
// If the Terraform ID format should be used instead of the DCL ID function.
// For example, resources with a regional/global cannot use the DCL ID formatter.
UseTerraformID bool
// ImportFormats are pattern format strings for importing the Terraform resource.
// TODO: if none are set, the resource does not support import.
ImportFormats []string
// AppendToBasePath is a string that will be appended to the end of the API base path.
// rarely needed in cases where the shared mm basepath does not include the version
// as in Montioring https://git.io/Jz4Wn
AppendToBasePath string
// ReplaceInBasePath contains a string replacement for the config base path,
// replacing one substring with another.
ReplaceInBasePath BasePathReplacement
// SkipInProvider is true when the resource shouldn't be included in the dclResources
// map for the provider. This is usually because it was already included through mmv1.
SkipInProvider bool
// title is the name of the resource in snake_case. For example,
// "instance", "backend_service".
title SnakeCaseTerraformResourceName
// dclTitle is the name of the resource in TitleCase, as parsed from the relevant
// DCL YAML file. For example, "Instance", "BackendService".
// This is particularly useful for acronymizations that exist in
// resource names, like OSPolicy. We store it separately from title
// because it can differ, especially in the case of split resources:
// "region_backend_service" vs "BackendService". We use this to
// create the names of DCL functions - "Apply{{dclTitle}}".
dclTitle TitleCaseResourceName
// dclStructName is the name of the resource struct in the DCL. In almost all cases
// this is == to dclTitle, but sometimes, due to (for instance) reserved words in the
// DCL conflicting with resource names, this differs. We use this to create DCL
// structs: `foo := compute.{{dclStructName}}{field1: "bar"}`.
dclStructName TitleCaseResourceName
// Description of the Terraform resource
Description string
// Lock name for a mutex to prevent concurrent API calls for a given resource
Mutex string
// Properties are the fields of a resource. Properties may be nested.
Properties []Property
// InsertTimeoutMinutes is the timeout value in minutes for the resource
// create operation
InsertTimeoutMinutes int
// UpdateTimeoutMinutes is the timeout value in minutes for the resource
// update operation
UpdateTimeoutMinutes int
// DeleteTimeoutMinutes is the timeout value in minutes for the resource
// delete operation
DeleteTimeoutMinutes int
// PreCreateFunction is the name of a function that's called before the
// Creation call for a resource- specifically, before the id is recorded.
PreCreateFunction *string
// PostCreateFunction is the name of a function that's called immediately
// after the Creation call for a resource.
PostCreateFunction *string
// PreDeleteFunction is the name of a function that's called immediately
// prior to the Delete call for a resource.
PreDeleteFunction *string
// CustomImportFunction is the name of a function that's called in place
// of the standard import template code
CustomImportFunction *string
// CustomizeDiff is a list of functions to set as the Terraform schema
// CustomizeDiff field
CustomizeDiff []string
// List of other Golang packages to import in a resources' generated Go file
additionalFileImportSet stringset.Set
// ListFields is the list of fields required for a list call.
ListFields []string
// location is one of "zone", "region", or "global".
location string
// HasProject tells us if the resource has a project field.
HasProject bool
// HasCreate tells us if the resource has a create endpoint.
HasCreate bool
// HasSweeper says if this resource has a generated sweeper.
HasSweeper bool
// These are all of the reused types.
ReusedTypes []Property
// If this resource requires a state hint to update correctly
StateHint bool
// CustomCreateDirectiveFunction is the name of a function that takes the
// object to be created and returns a list of directive to use for the apply
// call
CustomCreateDirectiveFunction *string
// SkipDeleteFunction is the name of a function that takes the
// object and config and returns a boolean for if Terraform should make
// the delete call for the resource
SkipDeleteFunction *string
// SerializationOnly defines if this resource should not generate provider files
// and only be used for serialization
SerializationOnly bool
// CustomSerializer defines the function this resource should use to serialize itself.
CustomSerializer *string
// TerraformProductName is the Product name overriden from the DCL
TerraformProductName *SnakeCaseProductName
// The array of Samples associated with the resource
Samples []Sample
// Versions specific information about this resource
versionMetadata Version
// Reference points to the rest API
Reference *Link
// Guides point to non-rest useful context for the resource.
Guides []Link
// The current schema version
SchemaVersion int
// The schema versions from 0 to the current schema version
SchemaVersions []int
// Whether to generate long form versions of resource sample tests
GenerateLongFormTests bool
}
type Link struct {
text string
url string
}
type BasePathReplacement struct {
Present bool
Old string
New string
}
func (l Link) Markdown() string {
return fmt.Sprintf("[%s](%s)", l.text, l.url)
}
func (r *Resource) fillLinksFromExtensionsMap(m map[string]interface{}) {
ref, ok := m["x-dcl-ref"].(map[string]interface{})
if ok {
r.Reference = &Link{url: ref["url"].(string), text: ref["text"].(string)}
}
gs, ok := m["x-dcl-guides"].([]interface{})
if ok {
for _, g := range gs {
guide := g.(map[interface{}]interface{})
r.Guides = append(r.Guides, Link{url: guide["url"].(string), text: guide["text"].(string)})
}
}
}
// Name is the shortname of a resource. For example, "instance".
func (r Resource) Name() SnakeCaseTerraformResourceName {
return r.title
}
func (r Resource) TitleCaseFullName() TitleCaseFullName {
return TitleCaseFullName(snakeToTitleCase(concatenateSnakeCase(r.ProductName(), r.Name())).titlecase())
}
func (r Resource) DCLTitle() TitleCaseResourceName {
return r.dclTitle
}
func (r Resource) DCLStructName() TitleCaseResourceName {
if r.dclStructName != "" {
return r.dclStructName
}
return r.dclTitle
}
// TerraformName is the Terraform resource type used in HCL configuration.
// For example, "google_compute_instance"
func (r Resource) TerraformName() SnakeCaseFullName {
googlePrefix := miscellaneousNameSnakeCase("google")
if r.TerraformProductName != nil {
if r.TerraformProductName.snakecase() == "" {
return SnakeCaseFullName(concatenateSnakeCase(googlePrefix, r.Name()))
}
return SnakeCaseFullName(concatenateSnakeCase(googlePrefix, *r.TerraformProductName, r.Name()))
}
return SnakeCaseFullName(concatenateSnakeCase(googlePrefix, r.ProductName(), r.Name()))
}
// PathType is the title-cased name of a resource preceded by its package,
// often used to namespace functions. For example, "RedisInstance".
func (r Resource) PathType() TitleCaseFullName {
return r.TitleCaseFullName()
}
// Package is the namespace of the package within the dcl
// the Package is normally a lowercase variant of ProductName
func (r Resource) Package() DCLPackageName {
return r.productMetadata.PackageName
}
func (r Resource) TitleCasePackageName() RenderedString {
return RenderedString(snakeToTitleCase(r.ProductName()).titlecase())
}
func (r Resource) DocsSection() miscellaneousNameTitleCase {
return r.productMetadata.DocsSection()
}
// ProductName is the snakecase product name of a resource. For example,
// "network_services".
func (r Resource) ProductName() SnakeCaseProductName {
return r.productMetadata.ProductName
}
func (r Resource) ProductMetadata() *ProductMetadata {
copy := *r.productMetadata
return ©
}
// DCLPackage is the package name of the DCL client library to use for this
// resource. For example, the Package "access_context_manager" at version GA would have a
// DCLPackage of "accesscontextmanager", and at beta would be "accesscontextmanager/beta".
func (r Resource) DCLPackage() DCLPackageNameWithVersion {
return r.productMetadata.PackageNameWithVersion()
}
// Updatable returns true if the resource should have an update method.
// This will avoid the error message:
// "All fields are ForceNew or Computed w/out Optional, Update is superfluous"
func (r Resource) Updatable() bool {
for _, p := range r.SchemaProperties() {
if !p.ForceNew && !(!p.Optional && p.Computed) {
return true
}
}
return false
}
// The resource has other mutable fields, besides "labels" and "terraform_labels" fields
func (r Resource) HasMutableNonLabelsFields() bool {
for _, p := range r.SchemaProperties() {
if !p.IsResourceLabels() && p.Name() != "terraform_labels" && !p.ForceNew && !(!p.Optional && p.Computed) {
return true
}
}
return false
}
// Objects are properties with sub-properties
func (r Resource) Objects() (props []Property) {
for _, v := range r.Properties {
if len(v.Properties) != 0 {
// If this property uses a reused type, add it one-time afterwards to avoid multiple creations.
if v.ref != "" {
continue
}
props = append(props, v)
props = append(props, v.Objects()...)
}
}
for _, v := range r.ReusedTypes {
props = append(props, v)
props = append(props, v.Objects()...)
}
return props
}
// SchemaProperties is the list of resource properties filtered to those included in schema.
func (r Resource) SchemaProperties() (props []Property) {
return collapsedProperties(r.Properties)
}
// Enum arrays are not arrays of strings in the DCL and require special
// conversion
func (r Resource) EnumArrays() (props []Property) {
// Top level
for _, v := range r.Properties {
if v.Type.typ.Items != nil && len(v.Type.typ.Items.Enum) > 0 {
props = append(props, v)
}
}
// Look for nested
for _, n := range r.Objects() {
for _, v := range n.Properties {
if v.Type.typ.Items != nil && len(v.Type.typ.Items.Enum) > 0 {
props = append(props, v)
}
}
}
return props
}
// AdditionalGoPackages returns a sorted list of additional Go packages to import.
func (r Resource) AdditionalFileImports() []string {
sl := make([]string, 0, len(r.additionalFileImportSet))
for k := range r.additionalFileImportSet {
sl = append(sl, k)
}
sort.Strings(sl)
return sl
}
// If this resource has a server generated field that is used to read the
// resource. This must be set during create
func (r Resource) HasServerGeneratedName() bool {
identityFields := idParts(r.ID)
for _, p := range r.Properties {
if stringInSlice(p.Name(), identityFields) {
if !p.Settable {
return true
}
}
}
return false
}
// SweeperFunctionArgs returns a list of arguments for calling a sweeper function.
func (r Resource) SweeperFunctionArgs() string {
vals := make([]string, 0)
for _, v := range r.ListFields {
vals = append(vals, fmt.Sprintf("d[\"%s\"]", v))
}
if len(vals) > 0 {
return strings.Join(vals, ",") + ","
} else {
return ""
}
}
// Returns the name of the ID function for the Terraform resource.
func (r Resource) IDFunction() string {
for _, p := range r.Properties {
if p.forwardSlashAllowed {
return "tpgresource.ReplaceVars"
}
}
return "tpgresource.ReplaceVarsForId"
}
// Check if the resource has the lables field for the resource
func (r Resource) HasLabels() bool {
for _, p := range r.Properties {
if p.IsResourceLabels() {
return true
}
}
return false
}
// Check if the resource has the annotations field for the resource
func (r Resource) HasAnnotations() bool {
for _, p := range r.Properties {
if p.IsResourceAnnotations() {
return true
}
}
return false
}
// ResourceInput is a Resource along with additional generation metadata.
type ResourceInput struct {
Resource
}
// RegisterReusedType adds a new reused type if the type does not already exist.
func (r Resource) RegisterReusedType(p Property) []Property {
found := false
for _, v := range r.ReusedTypes {
if v.ref == p.ref {
found = true
}
}
if !found {
r.ReusedTypes = append(r.ReusedTypes, p)
}
return r.ReusedTypes
}
func createResource(schema *openapi.Schema, info *openapi.Info, typeFetcher *TypeFetcher, overrides Overrides, product *ProductMetadata, version Version, location string) (*Resource, error) {
resourceTitle := strings.Split(info.Title, "/")[1]
res := Resource{
title: SnakeCaseTerraformResourceName(jsonToSnakeCase(resourceTitle).snakecase()),
dclStructName: TitleCaseResourceName(schema.Title),
dclTitle: TitleCaseResourceName(resourceTitle),
productMetadata: product,
versionMetadata: version,
Description: info.Description,
location: location,
InsertTimeoutMinutes: 20,
UpdateTimeoutMinutes: 20,
DeleteTimeoutMinutes: 20,
}
// Since the resource's "info" extension field can't be accessed, the relevant
// extensions have been copied into the schema objects.
res.fillLinksFromExtensionsMap(schema.Extension)
// Resource Override: Custom Timeout
ctd := CustomTimeoutDetails{}
ctdOk, err := overrides.ResourceOverrideWithDetails(CustomTimeout, &ctd, location)
if err != nil {
return nil, fmt.Errorf("failed to decode custom timeout details: %v", err)
}
if ctdOk {
res.InsertTimeoutMinutes = ctd.TimeoutMinutes
res.UpdateTimeoutMinutes = ctd.TimeoutMinutes
res.DeleteTimeoutMinutes = ctd.TimeoutMinutes
}
if overrides.ResourceOverride(SkipInProvider, location) {
res.SkipInProvider = true
}
crname := CustomResourceNameDetails{}
crnameOk, err := overrides.ResourceOverrideWithDetails(CustomResourceName, &crname, location)
if err != nil {
return nil, fmt.Errorf("failed to decode custom resource name details: %v", err)
}
if crnameOk {
res.title = SnakeCaseTerraformResourceName(crname.Title)
}
id, customID, err := findResourceID(schema, overrides, location)
if err != nil {
return nil, err
}
res.ID = id
res.UseTerraformID = customID
// Resource Override: Custom Import Function
cifd := CustomImportFunctionDetails{}
cifdOk, err := overrides.ResourceOverrideWithDetails(CustomImport, &cifd, location)
if err != nil {
return nil, fmt.Errorf("failed to decode custom import function details: %v", err)
}
if cifdOk {
res.CustomImportFunction = &cifd.Function
}
// Resource Override: Append to Base Path
atbpd := AppendToBasePathDetails{}
atbpOk, err := overrides.ResourceOverrideWithDetails(AppendToBasePath, &atbpd, location)
if err != nil {
return nil, fmt.Errorf("failed to decode append to base path details: %v", err)
}
if atbpOk {
res.AppendToBasePath = atbpd.String
}
// Resource Override: Replace in Base Path
ribpd := ReplaceInBasePathDetails{}
ribpOk, err := overrides.ResourceOverrideWithDetails(ReplaceInBasePath, &ribpd, location)
if err != nil {
return nil, fmt.Errorf("failed to decode replace in base path details: %v", err)
}
if ribpOk {
res.ReplaceInBasePath.Present = true
res.ReplaceInBasePath.Old = ribpd.Old
res.ReplaceInBasePath.New = ribpd.New
}
// Resource Override: Mutex
md := MutexDetails{}
mdOk, err := overrides.ResourceOverrideWithDetails(Mutex, &md, location)
if err != nil {
return nil, fmt.Errorf("failed to decode mutex details: %v", err)
}
if mdOk {
res.Mutex = md.Mutex
}
props, err := createPropertiesFromSchema(schema, typeFetcher, overrides, &res, nil, location)
if err != nil {
return nil, err
}
res.Properties = props
onlyLongFormFormat := shouldAllowForwardSlashInFormat(res.ID, res.Properties)
// Resource Override: Import formats
ifd := ImportFormatDetails{}
ifdOk, err := overrides.ResourceOverrideWithDetails(ImportFormat, &ifd, location)
if err != nil {
return nil, fmt.Errorf("failed to decode import format details: %v", err)
}
if ifdOk {
res.ImportFormats = ifd.Formats
} else {
res.ImportFormats = defaultImportFormats(res.ID, onlyLongFormFormat)
}
_, res.HasProject = schema.Properties["project"]
// Resource Override: Virtual field
for _, vfd := range overrides.ResourceOverridesWithDetails(VirtualField, location) {
vf := VirtualFieldDetails{}
if err := convert(vfd, &vf); err != nil {
return nil, fmt.Errorf("error converting type: %v", err)
}
res.Properties = append(res.Properties, readVirtualField(vf))
}
// Resource-level pre and post action functions
preCreate := PreCreateFunctionDetails{}
preCreOk, err := overrides.ResourceOverrideWithDetails(PreCreate, &preCreate, location)
if err != nil {
return nil, fmt.Errorf("failed to decode pre create function details: %v", err)
}
if preCreOk {
res.PreCreateFunction = &preCreate.Function
}
postCreate := PostCreateFunctionDetails{}
postCreOk, err := overrides.ResourceOverrideWithDetails(PostCreate, &postCreate, location)
if err != nil {
return nil, fmt.Errorf("failed to decode post create function details: %v", err)
}
if postCreOk {
res.PostCreateFunction = &postCreate.Function
}
pd := PreDeleteFunctionDetails{}
pdOk, err := overrides.ResourceOverrideWithDetails(PreDelete, &pd, location)
if err != nil {
return nil, fmt.Errorf("failed to decode pre delete function details: %v", err)
}
if pdOk {
res.PreDeleteFunction = &pd.Function
}
// Resource Override: Customize Diff
cdiff := CustomizeDiffDetails{}
cdOk, err := overrides.ResourceOverrideWithDetails(CustomizeDiff, &cdiff, location)
if err != nil {
return nil, fmt.Errorf("failed to decode customize diff details: %v", err)
}
if cdOk {
res.CustomizeDiff = cdiff.Functions
}
if res.HasLabels() {
res.CustomizeDiff = append(res.CustomizeDiff, "tpgresource.SetLabelsDiff")
}
if res.HasAnnotations() {
res.CustomizeDiff = append(res.CustomizeDiff, "tpgresource.SetAnnotationsDiff")
}
// ListFields
if parameters, ok := typeFetcher.doc.Paths["list"]; ok {
for _, param := range parameters.Parameters {
if param.Name != "" {
res.ListFields = append(res.ListFields, param.Name)
}
}
}
// Determine if a resource has a create method.
res.HasCreate, _ = schema.Extension["x-dcl-has-create"].(bool)
// Determine if a resource can use a generated sweeper or not
// We only supply a certain set of parent values to sweepers, so only generate
// one if it will actually work- resources with resource parents are not
// sweepable, in particular, such as nested resources or fine-grained
// resources. Additional special cases can be handled with overrides.
res.HasSweeper = true
validSweeperParameters := []string{"project", "region", "location", "zone", "billingAccount"}
if deleteAllInfo, ok := typeFetcher.doc.Paths["deleteAll"]; ok {
for _, p := range deleteAllInfo.Parameters {
// if any field isn't a standard sweeper parameter, don't make a sweeper
if !stringInSlice(p.Name, validSweeperParameters) {
res.HasSweeper = false
}
}
} else {
// if deleteAll wasn't found, the DCL hasn't published a sweeper
res.HasSweeper = false
}
if overrides.ResourceOverride(NoSweeper, location) {
if res.HasSweeper == false {
return nil, fmt.Errorf("superfluous NO_SWEEPER specified for %q", res.TerraformName())
}
res.HasSweeper = false
}
stateHint, ok := schema.Extension["x-dcl-uses-state-hint"].(bool)
if ok {
res.StateHint = stateHint
}
// Resource Override: CustomCreateDirectiveFunction
createDirectiveFunc := CustomCreateDirectiveDetails{}
createDirectiveFuncOk, err := overrides.ResourceOverrideWithDetails(CustomCreateDirective, &createDirectiveFunc, location)
if err != nil {
return nil, fmt.Errorf("failed to decode custom create directive function details: %v", err)
}
if createDirectiveFuncOk {
res.CustomCreateDirectiveFunction = &createDirectiveFunc.Function
}
// Resource Override: SkipDeleteFunction
skipDeleteFunc := SkipDeleteFunctionDetails{}
skipDeleteFuncOk, err := overrides.ResourceOverrideWithDetails(SkipDeleteFunction, &skipDeleteFunc, location)
if err != nil {
return nil, fmt.Errorf("failed to decode skip delete details: %v", err)
}
if skipDeleteFuncOk {
res.SkipDeleteFunction = &skipDeleteFunc.Function
}
// Resource Override: SerializationOnly
res.SerializationOnly = overrides.ResourceOverride(SerializationOnly, location)
// Resource Override: CustomSerializer
customSerializerFunc := CustomSerializerDetails{}
customSerializerFuncOk, err := overrides.ResourceOverrideWithDetails(CustomSerializer, &customSerializerFunc, location)
if err != nil {
return nil, fmt.Errorf("failed to decode custom serializer function details: %v", err)
}
if customSerializerFuncOk {
res.CustomSerializer = &customSerializerFunc.Function
}
// Resource Override: TerraformProductName
terraformProductName := TerraformProductNameDetails{}
terraformProductNameOk, err := overrides.ResourceOverrideWithDetails(TerraformProductName, &terraformProductName, location)
if err != nil {
return nil, fmt.Errorf("failed to decode terraform product name function details: %v", err)
}
if terraformProductNameOk {
scpn := SnakeCaseProductName(terraformProductName.Product)
res.TerraformProductName = &scpn
}
// Resource Override: StateUpgrade
stateUpgrade := StateUpgradeDetails{}
stateUpgradeOk, err := overrides.ResourceOverrideWithDetails(StateUpgrade, &stateUpgrade, location)
if err != nil {
return nil, fmt.Errorf("Failed to decode state upgrade details: %v", err)
}
if stateUpgradeOk {
res.SchemaVersion = stateUpgrade.SchemaVersion
res.SchemaVersions = make([]int, res.SchemaVersion)
for i := range res.SchemaVersions {
res.SchemaVersions[i] = i
}
}
if overrides.ResourceOverride(GenerateLongFormTests, location) {
res.GenerateLongFormTests = true
}
res.Samples = res.loadSamples()
return &res, nil
}
func readVirtualField(vf VirtualFieldDetails) Property {
prop := Property{
title: vf.Name,
Type: Type{&openapi.Schema{Type: vf.Type}},
}
if vf.Type == "boolean" {
def := "false"
prop.Default = &def
}
if vf.Output {
prop.Computed = true
} else {
prop.Optional = true
prop.Settable = true
}
return prop
}
func (r Resource) TestSamples() []Sample {
return r.getSamples(false)
}
func (r Resource) DocSamples() []Sample {
return r.getSamples(true)
}
func (r Resource) getSamples(docs bool) []Sample {
out := []Sample{}
if len(r.Samples) < 1 {
return out
}
var hideList []string
if docs {
hideList = r.Samples[0].DocHide
if len(r.Samples[0].DocHideConditional) > 0 {
for _, dochidec := range r.Samples[0].DocHideConditional {
if r.location == dochidec.Location {
hideList = append(hideList, dochidec.Name)
}
}
}
} else {
hideList = r.Samples[0].Testhide
if len(r.Samples[0].TestHideConditional) > 0 {
for _, testhidec := range r.Samples[0].TestHideConditional {
if r.location == testhidec.Location {
hideList = append(hideList, testhidec.Name)
}
}
}
}
for _, sample := range r.Samples {
shouldhide := false
for _, hideName := range hideList {
if sample.FileName == hideName {
shouldhide = true
}
}
if !shouldhide {
out = append(out, sample)
}
}
return out
}
func (r *Resource) getSampleAccessoryFolder() Filepath {
resourceType := strings.ToLower(r.DCLTitle().titlecase())
packageName := r.productMetadata.PackageName.lowercase()
sampleAccessoryFolder := path.Join(*tPath, packageName, "samples", resourceType)
return Filepath(sampleAccessoryFolder)
}
func (r *Resource) loadSamples() []Sample {
samples := []Sample{}
handWritten := r.loadHandWrittenSamples()
dclSamples := r.loadDCLSamples()
samples = append(samples, dclSamples...)
samples = append(samples, handWritten...)
return samples
}
func (r *Resource) loadHandWrittenSamples() []Sample {
sampleAccessoryFolder := r.getSampleAccessoryFolder()
sampleFriendlyMetaPath := path.Join(string(sampleAccessoryFolder), "meta.yaml")
samples := []Sample{}
if !pathExists(sampleFriendlyMetaPath) {
return samples
}
files, err := ioutil.ReadDir(string(sampleAccessoryFolder))
if err != nil {
glog.Exit(err)
}
for _, file := range files {
if fileName := strings.ToLower(file.Name()); !strings.HasSuffix(fileName, ".tf.tmpl") ||
strings.Contains(fileName, "_update") {
continue
}
sample := Sample{}
sampleName := strings.Split(file.Name(), ".")[0]
sampleDefinitionFile := path.Join(string(sampleAccessoryFolder), sampleName+".yaml")
var tc []byte
if pathExists(sampleDefinitionFile) {
tc, err = mergeYaml(sampleDefinitionFile, sampleFriendlyMetaPath)
} else {
tc, err = ioutil.ReadFile(sampleFriendlyMetaPath)
}
if err != nil {
glog.Exit(err)
}
err = yaml.UnmarshalStrict(tc, &sample)
if err != nil {
glog.Exit(err)
}
versionMatch := false
// if no versions provided assume all versions
if len(sample.Versions) <= 0 {
sample.HasGAEquivalent = true
versionMatch = true
} else {
for _, v := range sample.Versions {
if v == r.versionMetadata.V {
versionMatch = true
}
if v == "ga" {
sample.HasGAEquivalent = true
}
}
}
if !versionMatch {
glog.Errorf("skipping %q due to no version match", file.Name())
continue
}
sample.SamplesPath = sampleAccessoryFolder
sample.resourceReference = r
sample.FileName = file.Name()
sample.PrimaryResource = &(sample.FileName)
if sample.Name == nil || *sample.Name == "" {
sampleName = strings.Split(sample.FileName, ".")[0]
sample.Name = &sampleName
}
sample.TestSlug = RenderedString(snakeToTitleCase(miscellaneousNameSnakeCase(sampleName)).titlecase() + "HandWritten")
// The "labels" and "annotations" fields in the state are decided by the configuration.
// During importing, as the configuration is unavailableafter, the "labels" and "annotations" fields in the state will be empty.
// So add the "labels" and the "annotations" fields to the ImportStateVerifyIgnore list.
if r.HasLabels() {
sample.IgnoreRead = append(sample.IgnoreRead, "labels", "terraform_labels")
}
if r.HasAnnotations() {
sample.IgnoreRead = append(sample.IgnoreRead, "annotations")
}
samples = append(samples, sample)
}
return samples
}
func (r *Resource) loadDCLSamples() []Sample {
sampleAccessoryFolder := r.getSampleAccessoryFolder()
packagePath := r.productMetadata.PackagePath
version := r.versionMetadata.V
resourceType := r.DCLTitle()
sampleFriendlyMetaPath := path.Join(string(sampleAccessoryFolder), "meta.yaml")
samples := []Sample{}
if mode != nil && *mode == "serialization" {
return samples
}
// Samples appear in the root product folder
packagePath = Filepath(strings.Split(string(packagePath), "/")[0])
samplesPath := Filepath(path.Join(*fPath, string(packagePath), "samples"))
files, err := ioutil.ReadDir(string(samplesPath))
if err != nil {
// ignore the error if the file just doesn't exist
if !os.IsNotExist(err) {
glog.Exit(err)
}
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".yaml") {
continue
}
sample := Sample{}
sampleOGFilePath := path.Join(string(samplesPath), file.Name())
var tc []byte
if pathExists(sampleFriendlyMetaPath) {
tc, err = mergeYaml(sampleOGFilePath, sampleFriendlyMetaPath)
} else {
glog.Errorf("warning : sample meta does not exist for %v at %q", r.TerraformName(), sampleFriendlyMetaPath)
tc, err = ioutil.ReadFile(path.Join(string(samplesPath), file.Name()))
}
if err != nil {
glog.Exit(err)
}
err = yaml.UnmarshalStrict(tc, &sample)
if err != nil {
glog.Exit(err)
}
versionMatch := false
for _, v := range sample.Versions {
if v == version {
versionMatch = true
}
if v == "ga" {
sample.HasGAEquivalent = true
versionMatch = true
}
}
primaryResource := *sample.PrimaryResource
var parts []miscellaneousNameSnakeCase
parts = assertSnakeArray(strings.Split(primaryResource, "."))
primaryResourceName := snakeToTitleCase(parts[len(parts)-2])
if !versionMatch {
continue
} else if !strings.EqualFold(primaryResourceName.titlecase(), resourceType.titlecase()) {
// This scenario will occur for product folders with multiple resources
continue
}
sample.SamplesPath = samplesPath
sample.resourceReference = r
sample.FileName = file.Name()
var dependencies []Dependency
mainResource := sample.generateSampleDependencyWithName(primaryResource, "primary")
dependencies = append(dependencies, mainResource)
for _, dFileName := range sample.DependencyFileNames {
dependency := sample.generateSampleDependency(dFileName)
dependencies = append(dependencies, dependency)
}
sample.DependencyList = dependencies
sample.TestSlug = RenderedString(sampleNameToTitleCase(*sample.Name).titlecase())
// The "labels" and "annotations" fields in the state are decided by the configuration.
// During importing, as the configuration is unavailable, the "labels" and "annotations" fields in the state will be empty.
// So add the "labels" and the "annotations" fields to the ImportStateVerifyIgnore list.
if r.HasLabels() {
sample.IgnoreRead = append(sample.IgnoreRead, "labels", "terraform_labels")
}
if r.HasAnnotations() {
sample.IgnoreRead = append(sample.IgnoreRead, "annotations")
}
if r.GenerateLongFormTests {
longFormSample := sample
longFormSample.LongForm = true
var longFormDependencies []Dependency
mainResourceLongForm := longFormSample.generateSampleDependencyWithName(primaryResource, "primary")
longFormDependencies = append(longFormDependencies, mainResourceLongForm)
for _, dFileName := range longFormSample.DependencyFileNames {
longFormDependency := sample.generateSampleDependency(dFileName)
longFormDependencies = append(longFormDependencies, longFormDependency)
}
longFormSample.DependencyList = longFormDependencies
longFormSample.TestSlug += "LongForm"
samples = append(samples, longFormSample)
}