|
| 1 | +// Copyright 2026, Pulumi Corporation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package pcl |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/hashicorp/hcl/v2" |
| 19 | + "github.com/hashicorp/hcl/v2/hclsyntax" |
| 20 | + "github.com/zclconf/go-cty/cty" |
| 21 | + |
| 22 | + "github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model" |
| 23 | + "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" |
| 24 | +) |
| 25 | + |
| 26 | +// IsOptionalResource reports whether the given traversable is a |
| 27 | +// conditionally-created resource — a resource, read-resource, or component whose |
| 28 | +// variable type is optional because it ranges over a boolean. Such a resource is |
| 29 | +// `null`/`undefined` when the condition is false, so dereferencing one of its |
| 30 | +// properties is only well-defined when the resource exists. |
| 31 | +func IsOptionalResource(t model.Traversable) bool { |
| 32 | + switch t.(type) { |
| 33 | + case *Resource, *ReadResource, *Component: |
| 34 | + return model.IsOptionalType(model.GetTraversableType(t)) |
| 35 | + default: |
| 36 | + return false |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// optionalResourceRoot reports whether expr is a property access (a traversal of |
| 41 | +// at least one attribute) rooted at a conditionally-created resource. It returns |
| 42 | +// a reference to the bare resource root so callers can guard the access against |
| 43 | +// `null`. |
| 44 | +func optionalResourceRoot(expr model.Expression) (*model.ScopeTraversalExpression, bool) { |
| 45 | + traversal, ok := expr.(*model.ScopeTraversalExpression) |
| 46 | + if !ok || len(traversal.Traversal) < 2 || len(traversal.Parts) == 0 { |
| 47 | + return nil, false |
| 48 | + } |
| 49 | + if !IsOptionalResource(traversal.Parts[0]) { |
| 50 | + return nil, false |
| 51 | + } |
| 52 | + rootRef := &model.ScopeTraversalExpression{ |
| 53 | + RootName: traversal.RootName, |
| 54 | + Traversal: traversal.Traversal[:1], |
| 55 | + Parts: traversal.Parts[:1], |
| 56 | + } |
| 57 | + mustTypecheck(rootRef) |
| 58 | + return rootRef, true |
| 59 | +} |
| 60 | + |
| 61 | +// mustTypecheck typechecks an expression built during rewriting, where the |
| 62 | +// operands are well-formed by construction, and asserts that it does so cleanly. |
| 63 | +func mustTypecheck(expr model.Expression) { |
| 64 | + diags := expr.Typecheck(false) |
| 65 | + contract.Assertf(!diags.HasErrors(), "constructing optional-resource guard: %v", diags) |
| 66 | +} |
| 67 | + |
| 68 | +// RewriteOptionalResourceGuards guards dereferences of conditionally-created |
| 69 | +// (boolean `range`) resources so the generated code is well-defined when the |
| 70 | +// resource was not created. Any expression that reads a property off such a |
| 71 | +// resource is wrapped as `root != null ? expr : null`, short-circuiting the |
| 72 | +// access — and any apply over it — to `null` when the resource is absent. |
| 73 | +// |
| 74 | +// The guard is built from the resource references themselves rather than the |
| 75 | +// original range condition so it narrows the optional type for type checkers. |
| 76 | +// It must run before applies are rewritten so the apply is generated inside the |
| 77 | +// guarded branch. |
| 78 | +func RewriteOptionalResourceGuards(expr model.Expression) model.Expression { |
| 79 | + var roots []*model.ScopeTraversalExpression |
| 80 | + seen := map[string]bool{} |
| 81 | + collect := func(e model.Expression) (model.Expression, hcl.Diagnostics) { |
| 82 | + if rootRef, ok := optionalResourceRoot(e); ok && !seen[rootRef.RootName] { |
| 83 | + seen[rootRef.RootName] = true |
| 84 | + roots = append(roots, rootRef) |
| 85 | + } |
| 86 | + return e, nil |
| 87 | + } |
| 88 | + _, _ = model.VisitExpression(expr, model.IdentityVisitor, collect) |
| 89 | + if len(roots) == 0 { |
| 90 | + return expr |
| 91 | + } |
| 92 | + |
| 93 | + condition := model.Expression(notNull(roots[0])) |
| 94 | + for _, root := range roots[1:] { |
| 95 | + and := &model.BinaryOpExpression{ |
| 96 | + LeftOperand: condition, |
| 97 | + Operation: hclsyntax.OpLogicalAnd, |
| 98 | + RightOperand: notNull(root), |
| 99 | + } |
| 100 | + mustTypecheck(and) |
| 101 | + condition = and |
| 102 | + } |
| 103 | + |
| 104 | + guarded := &model.ConditionalExpression{ |
| 105 | + Condition: condition, |
| 106 | + TrueResult: expr, |
| 107 | + FalseResult: nullLiteral(), |
| 108 | + } |
| 109 | + mustTypecheck(guarded) |
| 110 | + return guarded |
| 111 | +} |
| 112 | + |
| 113 | +// notNull builds a `ref != null` comparison. |
| 114 | +func notNull(ref model.Expression) *model.BinaryOpExpression { |
| 115 | + op := &model.BinaryOpExpression{ |
| 116 | + LeftOperand: ref, |
| 117 | + Operation: hclsyntax.OpNotEqual, |
| 118 | + RightOperand: nullLiteral(), |
| 119 | + } |
| 120 | + mustTypecheck(op) |
| 121 | + return op |
| 122 | +} |
| 123 | + |
| 124 | +func nullLiteral() model.Expression { |
| 125 | + lit := &model.LiteralValueExpression{Value: cty.NullVal(cty.DynamicPseudoType)} |
| 126 | + mustTypecheck(lit) |
| 127 | + return lit |
| 128 | +} |
0 commit comments