-
Notifications
You must be signed in to change notification settings - Fork 19
/
gen_program.go
1063 lines (953 loc) · 32.4 KB
/
gen_program.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 2022, Pulumi Corporation. All rights reserved.
package java
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model/format"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/pulumi/pulumi-java/pkg/codegen/java/names"
)
const (
pulumiToken = "pulumi"
providersToken = "providers"
)
type generator struct {
// The formatter to use when generating code.
*format.Formatter
program *pcl.Program
// TODO
diagnostics hcl.Diagnostics
currentResourcePropertyType schema.Type
// keep track of variable identifiers which are the result of an invoke
// for example "var resourceGroup = GetResourceGroup.invokeAsync(...)"
// we will keep track of "resourceGroup" -> schema(GetResourceGroup)
//
// later on when apply a traversal sush as resourceGroup.getName(),
// we should rewrite it as resourceGroup.thenApply(getResourceGroupResult -> getResourceGroupResult.getName())
functionInvokes map[string]*schema.Function
}
func (g *generator) GenTemplateExpression(w io.Writer, expr *model.TemplateExpression) {
multiLine := false
expressions := false
for _, expr := range expr.Parts {
if lit, ok := expr.(*model.LiteralValueExpression); ok && model.StringType.AssignableFrom(lit.Type()) {
if strings.Contains(lit.Value.AsString(), "\n") {
multiLine = true
}
} else {
expressions = true
}
}
if multiLine {
g.Fgen(w, "\"\"\"\n")
} else if expressions {
g.Fgen(w, "String.format(\"")
} else {
g.Fgen(w, "\"")
}
var args []model.Expression
for _, expr := range expr.Parts {
if lit, ok := expr.(*model.LiteralValueExpression); ok && model.StringType.AssignableFrom(lit.Type()) {
if multiLine {
// no need to escape
g.Fgen(w, lit.Value.AsString())
} else {
g.Fgen(w, g.escapeString(lit.Value.AsString(), false, expressions))
}
} else {
args = append(args, expr)
g.Fgen(w, g.escapeString("%s", false, expressions))
}
}
if expressions {
// at the end of the interpolated string, append the quote
g.Fgen(w, "\"")
// emit the interpolated values, one at a time
g.Fgen(w, ", ")
for index, arg := range args {
if index == len(args)-1 {
// last element gets a closing parentheses
g.Fgenf(w, "%.v)", arg)
} else {
g.Fgenf(w, "%.v,", arg)
}
}
} else if multiLine {
g.Fgenf(w, "%s\"\"\"", g.Indent)
} else {
g.Fgen(w, "\"")
}
}
func containsFunctionCall(functionName string, nodes []pcl.Node) bool {
foundRangeCall := false
for _, node := range nodes {
diags := node.VisitExpressions(model.IdentityVisitor, func(x model.Expression) (model.Expression, hcl.Diagnostics) {
// Ignore the node if it is not a call to invoke.
call, ok := x.(*model.FunctionCallExpression)
if !ok {
return x, nil
}
if call.Name == functionName {
foundRangeCall = true
}
return x, nil
})
contract.Assert(len(diags) == 0)
}
return foundRangeCall
}
func hasIterableResources(nodes []pcl.Node) bool {
for _, node := range nodes {
switch node.(type) {
case *pcl.Resource:
resource := node.(*pcl.Resource)
if resource.Options != nil && resource.Options.Range != nil {
return true
}
}
}
return false
}
func containsRangeExpr(nodes []pcl.Node) bool {
return hasIterableResources(nodes) || containsFunctionCall("readDir", nodes)
}
func GenerateProgram(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error) {
pcl.MapProvidersAsResources(program)
// Linearize the nodes into an order appropriate for procedural code generation.
nodes := pcl.Linearize(program)
// Import Java-specific schema info.
// FIX ME: not sure what this is doing...
packages, err := program.PackageSnapshots()
if err != nil {
return nil, nil, err
}
for _, p := range packages {
if err := p.ImportLanguages(map[string]schema.Language{"java": Importer}); err != nil {
return nil, nil, err
}
}
g := &generator{
program: program,
functionInvokes: map[string]*schema.Function{},
}
g.Formatter = format.NewFormatter(g)
var index bytes.Buffer
g.genPreamble(&index, nodes)
g.Indented(func() {
g.Indented(func() {
for _, n := range nodes {
g.genNode(&index, n)
}
})
})
g.genPostamble(&index, nodes)
files := map[string][]byte{
"MyStack.java": index.Bytes(),
}
return files, g.diagnostics, nil
}
func GenerateProject(directory string, project workspace.Project, program *pcl.Program) error {
files, diagnostics, err := GenerateProgram(program)
if err != nil {
return err
}
if diagnostics.HasErrors() {
return diagnostics
}
// Set the runtime to "java" then marshal to Pulumi.yaml
project.Runtime = workspace.NewProjectRuntimeInfo("java", nil)
projectBytes, err := encoding.YAML.Marshal(project)
if err != nil {
return err
}
filesWithPackages := make(map[string][]byte)
filesWithPackages["Pulumi.yaml"] = projectBytes
for fileName, fileContents := range files {
if fileName == "MyStack.java" {
fileName = "App.java"
}
fileWithPackage := fmt.Sprintf("src/main/java/generated_program/%s", fileName)
filesWithPackages[fileWithPackage] = fileContents
}
var mavenDependenciesXML bytes.Buffer
packages, err := program.PackageSnapshots()
if err != nil {
return err
}
for _, p := range packages {
packageName := p.Name
version := p.Version
if version != nil {
dependencySection := fmt.Sprintf(
`<dependency>
<groupId>com.pulumi</groupId>
<artifactId>%s</artifactId>
<version>%s</version>
</dependency>`,
packageName, version.String(),
)
mavenDependenciesXML.WriteString(dependencySection)
}
}
mavenPomXML := bytes.NewBufferString(fmt.Sprintf(
`<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pulumi</groupId>
<artifactId>%s</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<mainClass>generated_program.App</mainClass>
<mainArgs/>
</properties>
<dependencies>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>pulumi</artifactId>
<version>(,1.0]</version>
</dependency>
%s
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-wrapper-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<mavenVersion>3.8.5</mavenVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>`,
project.Name.String(), mavenDependenciesXML.String(),
))
filesWithPackages["pom.xml"] = mavenPomXML.Bytes()
for filePath, data := range filesWithPackages {
outPath := path.Join(directory, filePath)
err := os.MkdirAll(path.Dir(outPath), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write output program: %w", err)
}
err = ioutil.WriteFile(outPath, data, 0600)
if err != nil {
return fmt.Errorf("could not write output program: %w", err)
}
}
return nil
}
func (g *generator) genImport(w io.Writer, qualifiedName string) {
g.Fprintf(w, "import %s;\n", qualifiedName)
}
func (g *generator) genIndent(w io.Writer) {
g.Fgenf(w, "%s", g.Indent)
}
func (g *generator) genNewline(w io.Writer) {
g.Fgen(w, "\n")
}
// Checks whether any of the input nodes is a configuration variable
func containConfigVariables(nodes []pcl.Node) bool {
for _, node := range nodes {
switch node.(type) {
case *pcl.ConfigVariable:
return true
}
}
return false
}
func cleanModule(module string) string {
if strings.Contains(module, "/") {
// if it is a function module
// e.g. index/getAvailabilityZones
moduleParts := strings.Split(module, "/")
// unless it is just a version e.g. core/v1
// then return it as is
if len(moduleParts) == 2 && strings.HasPrefix(moduleParts[1], "v") {
return module
}
return moduleParts[0]
}
return module
}
// Decides when to ignore a module in import definition
func ignoreModule(module string) bool {
return module == "" || module == "index"
}
// Removes dashes and replaces slash with underscore
func sanitizeImport(name string) string {
// TODO to be revised when https://github.com/pulumi/pulumi-java/issues/381 is resolved
// e.g. azure-native becomes azurenative
withoutDash := strings.ReplaceAll(name, "-", "")
// e.g. kubernetes.core/v1 becomes kubernetes.core_v1
replacedSlash := strings.ReplaceAll(withoutDash, "/", "_")
return replacedSlash
}
func pulumiImport(pkg string, module string, member string) string {
module = cleanModule(module)
if ignoreModule(module) {
return "com.pulumi." + sanitizeImport(pkg) + "." + member
} else if module == "" {
return "com.pulumi." + sanitizeImport(pkg)
}
return "com.pulumi." + sanitizeImport(pkg) + "." + sanitizeImport(module) + "." + member
}
func pulumiInputImport(pkg string, module string, member string) string {
module = cleanModule(module)
if ignoreModule(module) {
return "com.pulumi." + sanitizeImport(pkg) + ".inputs." + member
}
return "com.pulumi." + sanitizeImport(pkg) + "." + sanitizeImport(module) + ".inputs." + member
}
// Recursively derives imports from object by using its property type and
// any nested object property that it instantiates
func collectObjectImports(object *model.ObjectConsExpression, objectType *schema.ObjectType) []string {
imports := make([]string, 0)
// add imports of the type itself
fullyQualifiedTypeName := objectType.Token
nameParts := strings.Split(fullyQualifiedTypeName, ":")
objectTypeName := names.Title(nameParts[len(nameParts)-1])
pkg, module := nameParts[0], nameParts[1]
if objectType.IsInputShape() {
if !strings.HasSuffix(objectTypeName, "Args") {
objectTypeName = objectTypeName + "Args"
}
imports = append(imports, pulumiInputImport(pkg, module, objectTypeName))
} else {
imports = append(imports, pulumiImport(pkg, module, objectTypeName))
}
// then check whether one of the properties of this object is an object too
// in which case, we call this function recursively
for _, property := range object.Items {
switch property.Value.(type) {
case *model.ObjectConsExpression:
innerObject := property.Value.(*model.ObjectConsExpression)
innerObjectKey := property.Key.(*model.LiteralValueExpression).Value.AsString()
objectProperty, found := objectType.Property(innerObjectKey)
if found {
objectPropertyType := codegen.UnwrapType(objectProperty.Type)
switch objectPropertyType.(type) {
case *schema.ObjectType:
innerObjectType := objectPropertyType.(*schema.ObjectType)
// recurse into nested object
for _, importDef := range collectObjectImports(innerObject, innerObjectType) {
imports = append(imports, importDef)
}
}
}
}
}
return imports
}
// reduces Array<Input<T>> to just Array<T>
func reduceInputTypeFromArray(arrayType *schema.ArrayType) *schema.ArrayType {
elementType := arrayType.ElementType
switch elementType.(type) {
case *schema.InputType:
inputType := elementType.(*schema.InputType)
return &schema.ArrayType{ElementType: inputType.ElementType}
default:
// return as-is
return arrayType
}
}
func collectResourceImports(resource *pcl.Resource) []string {
imports := make([]string, 0)
pkg, module, name, _ := resource.DecomposeToken()
resourceImport := pulumiImport(pkg, module, name)
imports = append(imports, resourceImport)
if len(resource.Inputs) > 0 || hasCustomResourceOptions(resource) {
// import args type name
argsTypeName := resourceArgsTypeName(resource)
resourceArgsImport := pulumiImport(pkg, module, argsTypeName)
imports = append(imports, resourceArgsImport)
resourceProperties := typedResourceProperties(resource)
for _, inputProperty := range resource.Inputs {
inputType := resourceProperties[inputProperty.Name]
switch inputType.(type) {
case *schema.ObjectType:
objectType := inputType.(*schema.ObjectType)
switch inputProperty.Value.(type) {
case *model.ObjectConsExpression:
object := inputProperty.Value.(*model.ObjectConsExpression)
for _, importDef := range collectObjectImports(object, objectType) {
imports = append(imports, importDef)
}
}
case *schema.ArrayType:
arrayType := reduceInputTypeFromArray(inputType.(*schema.ArrayType))
innerArrayType := arrayType.ElementType
switch innerArrayType.(type) {
case *schema.ObjectType:
// found an Array<ElementType> type where ElementType is Object
// loop through each element of the array
// assume each element is of ElementType
arrayInnerTypeAsObject := innerArrayType.(*schema.ObjectType)
switch inputProperty.Value.(type) {
case *model.TupleConsExpression:
objects := inputProperty.Value.(*model.TupleConsExpression)
for _, arrayObject := range objects.Expressions {
switch arrayObject.(type) {
case *model.ObjectConsExpression:
object := arrayObject.(*model.ObjectConsExpression)
for _, importDef := range collectObjectImports(object, arrayInnerTypeAsObject) {
imports = append(imports, importDef)
}
}
}
}
}
}
}
}
return imports
}
func (g *generator) functionImportDef(tokenArg model.Expression) (string, string) {
token := tokenArg.(*model.TemplateExpression).Parts[0].(*model.LiteralValueExpression).Value.AsString()
tokenRange := tokenArg.SyntaxNode().Range()
// Compute the resource type from the Pulumi type token.
pkg, module, member, _ := pcl.DecomposeToken(token, tokenRange)
pkg = sanitizeImport(pkg)
module = sanitizeImport(module)
member = sanitizeImport(member)
if ignoreModule(module) {
importDef := "com.pulumi." + pkg + "." + names.Title(pkg) + "Functions"
return importDef, member
}
return pulumiImport(pkg, module, names.Title(module)+"Functions"), member
}
func (g *generator) collectFunctionCallImports(functionCall *model.FunctionCallExpression) []string {
imports := make([]string, 0)
switch functionCall.Name {
case pcl.Invoke:
fullyQualifiedFunctionImport, funcName := g.functionImportDef(functionCall.Args[0])
imports = append(imports, fullyQualifiedFunctionImport)
functionSchema, foundFunction := g.findFunctionSchema(funcName)
if foundFunction {
invokeArgumentsExpr := functionCall.Args[1]
switch invokeArgumentsExpr.(type) {
case *model.ObjectConsExpression:
argumentsExpr := invokeArgumentsExpr.(*model.ObjectConsExpression)
if functionSchema.Inputs == nil {
g.warnf(functionCall.Args[1].SyntaxNode().Range().Ptr(),
"cannot determine invoke argument type: the schema for %q has no inputs",
funcName)
return imports
}
argumentExprType := functionSchema.Inputs.InputShape
for _, importDef := range collectObjectImports(argumentsExpr, argumentExprType) {
imports = append(imports, importDef)
}
}
}
}
return imports
}
// Removes duplicate strings. Useful when collecting a distinct set of imports
func removeDuplicates(inputs []string) []string {
distinctInputs := make([]string, 0)
seenTexts := make(map[string]bool)
for _, input := range inputs {
if _, seen := seenTexts[input]; !seen {
seenTexts[input] = true
distinctInputs = append(distinctInputs, input)
}
}
return distinctInputs
}
// Collects fully qualified imports from resource or variable definitions.
// It recursively searches for imports from nested object definitions used in resource
// configuration or used in function calls
func (g *generator) collectImports(nodes []pcl.Node) []string {
imports := make([]string, 0)
for _, node := range nodes {
switch node.(type) {
case *pcl.Resource:
// collect resource imports
resource := node.(*pcl.Resource)
for _, importDef := range collectResourceImports(resource) {
imports = append(imports, importDef)
}
case *pcl.LocalVariable:
localVariable := node.(*pcl.LocalVariable)
switch localVariable.Definition.Value.(type) {
case *model.FunctionCallExpression:
// collect function invoke imports
// traverse the args and inner objects recursively
functionCall := localVariable.Definition.Value.(*model.FunctionCallExpression)
_, isInvokeCall := g.isFunctionInvoke(localVariable)
if isInvokeCall {
for _, importDef := range g.collectFunctionCallImports(functionCall) {
imports = append(imports, importDef)
}
}
}
}
}
return removeDuplicates(imports)
}
// genPreamble generates import statements, main class and stack definition.
func (g *generator) genPreamble(w io.Writer, nodes []pcl.Node) {
g.Fgen(w, "package generated_program;")
g.genNewline(w)
g.genNewline(w)
g.genImport(w, "com.pulumi.Context")
g.genImport(w, "com.pulumi.Pulumi")
g.genImport(w, "com.pulumi.core.Output")
// Write out the specific imports from used nodes
for _, importDef := range g.collectImports(nodes) {
g.genImport(w, importDef)
}
if containsFunctionCall("toJSON", nodes) {
// import static functions from the Serialization class
g.Fgen(w, "import static com.pulumi.codegen.internal.Serialization.*;\n")
}
if containsRangeExpr(nodes) {
// import the KeyedValue<T> class
g.genImport(w, "com.pulumi.codegen.internal.KeyedValue")
}
if requiresImportingCustomResourceOptions(nodes) {
g.genImport(w, "com.pulumi.resources.CustomResourceOptions")
}
if containsFunctionCall("readDir", nodes) {
g.genImport(w, "static com.pulumi.codegen.internal.Files.readDir")
}
if containsFunctionCall("fileAsset", nodes) {
g.genImport(w, "com.pulumi.asset.FileAsset")
}
if containsFunctionCall("fileArchive", nodes) {
g.genImport(w, "com.pulumi.asset.FileArchive")
}
g.genImport(w, "java.util.List")
g.genImport(w, "java.util.ArrayList")
g.genImport(w, "java.util.Map")
g.genImport(w, "java.io.File")
g.genImport(w, "java.nio.file.Files")
g.genImport(w, "java.nio.file.Paths")
g.genNewline(w)
// Emit Stack class signature
g.Fprint(w, "public class App {")
g.genNewline(w)
g.Fprint(w, " public static void main(String[] args) {\n")
g.Fgen(w, " Pulumi.run(App::stack);\n")
g.Fgen(w, " }\n")
g.genNewline(w)
g.Fprint(w, " public static void stack(Context ctx) {\n")
if containConfigVariables(nodes) {
g.Fprint(w, " final var config = ctx.config();\n")
}
}
// genPostamble closes the method and the class and declares stack output statements.
func (g *generator) genPostamble(w io.Writer, nodes []pcl.Node) {
g.Indented(func() {
g.genIndent(w)
g.Fgen(w, "}\n")
})
g.Fprint(w, "}\n")
}
// resourceTypeName computes the Java resource class name for the given resource.
func resourceTypeName(resource *pcl.Resource) string {
// Compute the resource type from the Pulumi type token.
pkg, module, member, diags := resource.DecomposeToken()
contract.Assert(len(diags) == 0)
if pkg == pulumiToken && module == providersToken {
member = "Provider"
}
return names.Title(member)
}
// resourceArgsTypeName computes the Java arguments class name for the given resource.
func resourceArgsTypeName(r *pcl.Resource) string {
return fmt.Sprintf("%sArgs", resourceTypeName(r))
}
// Returns the expression that should be emitted for a resource's "name" parameter given its base name
func makeResourceName(baseName string, suffix string) string {
if suffix == "" {
return fmt.Sprintf(`"%s"`, baseName)
}
return fmt.Sprintf(`"%s-"`, baseName) + " + " + suffix
}
func (g *generator) findFunctionSchema(function string) (*schema.Function, bool) {
function = names.LowerCamelCase(function)
for _, pkg := range g.program.PackageReferences() {
for it := pkg.Functions().Range(); it.Next(); {
if strings.HasSuffix(it.Token(), function) {
fn, err := it.Function()
if err != nil {
return nil, false
}
return fn, true
}
}
}
return nil, false
}
func getTraversalKey(traversal hcl.Traversal) string {
for _, part := range traversal {
switch part := part.(type) {
case hcl.TraverseAttr:
return cty.StringVal(part.Name).AsString()
case hcl.TraverseIndex:
return part.Key.AsString()
default:
contract.Failf("unexpected traversal part of type %T (%v)", part, part.SourceRange())
}
}
return ""
}
// Returns whether a resource has a custom resource option (other than range)
func hasCustomResourceOptions(resource *pcl.Resource) bool {
if resource.Options == nil {
return false
}
return resource.Options.IgnoreChanges != nil ||
resource.Options.DependsOn != nil ||
resource.Options.Parent != nil ||
resource.Options.Protect != nil ||
resource.Options.Provider != nil
}
// Checks whether any resource within the program nodes has a custom resource option
// in which case an import should be emitted
func requiresImportingCustomResourceOptions(programNodes []pcl.Node) bool {
for _, node := range programNodes {
switch node.(type) {
case *pcl.Resource:
resource := node.(*pcl.Resource)
if hasCustomResourceOptions(resource) {
return true
}
}
}
return false
}
func (g *generator) genCustomResourceOptions(w io.Writer, resource *pcl.Resource) {
g.Fgen(w, "CustomResourceOptions.builder()")
g.Indented(func() {
g.genNewline(w)
if resource.Options.Provider != nil {
g.genIndent(w)
g.Fgenf(w, ".provider(%v)", resource.Options.Provider)
g.genNewline(w)
}
if resource.Options.Protect != nil {
g.genIndent(w)
g.Fgenf(w, ".protect(%v)", resource.Options.Protect)
g.genNewline(w)
}
if resource.Options.Parent != nil {
g.genIndent(w)
g.Fgenf(w, ".parent(%v)", resource.Options.Parent)
g.genNewline(w)
}
if resource.Options.DependsOn != nil {
g.genIndent(w)
g.Fgenf(w, ".dependsOn(%v)", resource.Options.DependsOn)
g.genNewline(w)
}
if resource.Options.IgnoreChanges != nil {
g.genIndent(w)
g.Fgen(w, ".ignoreChanges(")
switch resource.Options.IgnoreChanges.(type) {
case *model.TupleConsExpression:
// when we have a list of expressions
// write each one of them between quotes
ignoredChanges := resource.Options.IgnoreChanges.(*model.TupleConsExpression)
for index, ignoredChange := range ignoredChanges.Expressions {
g.Fgenf(w, "\"%v\"", ignoredChange)
// write a comma between elements
// if we did not reach last expression
if index != len(ignoredChanges.Expressions)-1 {
g.Fgen(w, ", ")
}
}
default:
// ignored changes expression was NOT a list
// which is not really expected here
// we will write the expression as-is anyway
g.Fgenf(w, "\"%v\"", resource.Options.IgnoreChanges)
}
g.Fgen(w, ")")
g.genNewline(w)
}
g.genIndent(w)
g.Fgen(w, ".build()")
})
}
func typedResourceProperties(resource *pcl.Resource) map[string]schema.Type {
resourceProperties := map[string]schema.Type{}
resourceSchema := resource.Schema
if resourceSchema != nil && resourceSchema.InputProperties != nil {
for _, property := range resourceSchema.InputProperties {
if property != nil && property.Type != nil {
resourceProperties[property.Name] = codegen.UnwrapType(property.Type)
}
}
}
return resourceProperties
}
func (g *generator) genResource(w io.Writer, resource *pcl.Resource) {
resourceTypeName := resourceTypeName(resource)
resourceArgs := resourceArgsTypeName(resource)
variableName := names.LowerCamelCase(names.MakeValidIdentifier(resource.Name()))
instantiate := func(resName string) {
resourceProperties := typedResourceProperties(resource)
if len(resource.Inputs) == 0 && !hasCustomResourceOptions(resource) {
g.Fgenf(w, "new %s(%s)", resourceTypeName, resName)
} else if len(resource.Inputs) == 0 && hasCustomResourceOptions(resource) {
// generate empty resource args in this case
emptyArgs := resourceArgs + ".Empty"
g.Fgenf(w, "new %s(%s, %s, ", resourceTypeName, resName, emptyArgs)
g.genCustomResourceOptions(w, resource)
g.Fgen(w, ")")
} else {
g.Fgenf(w, "new %s(%s, %s.builder()", resourceTypeName, resName, resourceArgs)
g.Fgenf(w, "%s\n", g.Indent)
g.Indented(func() {
for _, attr := range resource.Inputs {
attributeIdent := names.MakeValidIdentifier(attr.Name)
attributeSchemaType := resourceProperties[attr.Name]
g.currentResourcePropertyType = attributeSchemaType
g.Fgenf(w, "%s.%s(%.v)\n", g.Indent, attributeIdent, g.lowerExpression(attr.Value, attr.Type()))
}
if !hasCustomResourceOptions(resource) {
g.Fgenf(w, "%s.build())", g.Indent)
} else {
g.Fgenf(w, "%s.build(), ", g.Indent)
g.genCustomResourceOptions(w, resource)
g.Fgen(w, ")")
}
})
}
}
if resource.Options != nil && resource.Options.Range != nil {
// generate list of resources
rangeType := model.ResolveOutputs(resource.Options.Range.Type())
rangeExpr := g.lowerExpression(resource.Options.Range, rangeType)
isNumericRange := model.InputType(model.NumberType).ConversionFrom(rangeExpr.Type()) != model.NoConversion
if isNumericRange {
// numeric range
g.Fgenf(w, "%sfor (var i = 0; i < %.12o; i++) {\n", g.Indent, rangeExpr)
g.Indented(func() {
// register each resource
g.Fgenf(w, "%s", g.Indent)
instantiate(makeResourceName(resource.Name(), "i"))
g.Fgenf(w, ";\n\n")
})
g.Fgenf(w, "%s\n}\n", g.Indent)
} else {
// for each-loop through the elements to creates a resource from each one
switch rangeExpr.(type) {
case *model.FunctionCallExpression:
funcCall := rangeExpr.(*model.FunctionCallExpression)
switch funcCall.Name {
case pcl.IntrinsicConvert:
firstArg := funcCall.Args[0]
switch firstArg.(type) {
case *model.ScopeTraversalExpression:
traversalExpr := firstArg.(*model.ScopeTraversalExpression)
if len(traversalExpr.Parts) == 2 {
// Meaning here we have {root}.{part} expression which the most common
// check whether {root} is actually a variable name that holds the result
// of a function invoke
if functionSchema, isInvoke := g.functionInvokes[traversalExpr.RootName]; isInvoke {
resultTypeName := names.LowerCamelCase(typeName(functionSchema.Outputs))
part := getTraversalKey(traversalExpr.Traversal.SimpleSplit().Rel)
g.genIndent(w)
g.Fgenf(w, "final var %s = ", resource.Name())
g.Fgenf(w, "%s.applyValue(%s -> {\n", traversalExpr.RootName, resultTypeName)
g.Indented(func() {
g.Fgenf(w, "%sfinal var resources = new ArrayList<%s>();\n", g.Indent, resourceTypeName)
g.Fgenf(w, "%sfor (var range : KeyedValue.of(%s.%s()) {\n", g.Indent, resultTypeName, part)
g.Indented(func() {
suffix := "range.key()"
g.Fgenf(w, "%svar resource = ", g.Indent)
instantiate(makeResourceName(resource.Name(), suffix))
g.Fgenf(w, ";\n\n")
g.Fgenf(w, "%sresources.add(resource);\n", g.Indent)
})
g.Fgenf(w, "%s}\n\n", g.Indent)
g.Fgenf(w, "%sreturn resources;\n", g.Indent)
})
g.Fgenf(w, "%s});\n\n", g.Indent)
return
}
// not an async function invoke
// wrap into range collection
g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr)
} else {
// wrap into range collection
g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr)
}
}
// wrap into range collection
g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr)
default:
// assume function call returns a Range<T>
g.Fgenf(w, "%sfor (var range : %.12o) {\n", g.Indent, rangeExpr)
}
default:
// wrap into range collection
g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr)
}
g.Indented(func() {
suffix := "range.key()"
g.Fgenf(w, "%s", g.Indent)
instantiate(makeResourceName(resource.Name(), suffix))
g.Fgenf(w, ";\n")
})
g.Fgenf(w, "%s}\n\n", g.Indent)
}
} else {
// generate single resource
g.Fgenf(w, "%svar %s = ", g.Indent, variableName)
suffix := ""
instantiate(makeResourceName(resource.Name(), suffix))
g.Fgenf(w, ";\n\n")
}
}
func (g *generator) genConfigVariable(w io.Writer, configVariable *pcl.ConfigVariable) {
g.genIndent(w)
if configVariable.DefaultValue != nil {
g.Fgenf(w, "final var %s = config.get(\"%s\").orElse(%v);",
names.MakeValidIdentifier(configVariable.Name()),
configVariable.Name(),
configVariable.DefaultValue)
} else {
g.Fgenf(w, "final var %s = config.get(\"%s\");",
names.MakeValidIdentifier(configVariable.Name()),
configVariable.Name())
}
g.genNewline(w)
}
func (g *generator) isFunctionInvoke(localVariable *pcl.LocalVariable) (*schema.Function, bool) {
value := localVariable.Definition.Value
switch value.(type) {
case *model.FunctionCallExpression:
call := value.(*model.FunctionCallExpression)
switch call.Name {
case pcl.Invoke:
args := call.Args[0]
_, schemaName := g.functionName(args)
functionSchema, foundFunction := g.findFunctionSchema(schemaName)
if foundFunction {