github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/pcl/resource.go (about) 1 // Copyright 2016-2020, 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/pulumi/pulumi/pkg/v3/codegen/hcl2/model" 21 "github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax" 22 "github.com/pulumi/pulumi/pkg/v3/codegen/schema" 23 ) 24 25 // ResourceOptions represents a resource instantiation's options. 26 type ResourceOptions struct { 27 // The definition of the resource options. 28 Definition *model.Block 29 30 // An expression to range over when instantiating the resource. 31 Range model.Expression 32 // The resource's parent, if any. 33 Parent model.Expression 34 // The provider to use. 35 Provider model.Expression 36 // The explicit dependencies of the resource. 37 DependsOn model.Expression 38 // Whether or not the resource is protected. 39 Protect model.Expression 40 // A list of properties that are not considered when diffing the resource. 41 IgnoreChanges model.Expression 42 // The version of the provider for this resource. 43 Version model.Expression 44 // The plugin download URL for this resource. 45 PluginDownloadURL model.Expression 46 } 47 48 // Resource represents a resource instantiation inside of a program or component. 49 type Resource struct { 50 node 51 52 syntax *hclsyntax.Block 53 54 // The name visible to API calls related to the resource. Used as the Name argument in resource 55 // constructors, and through those calls to RegisterResource. Must not be modified during code 56 // generation to ensure that resources are not renamed (deleted and recreated). 57 logicalName string 58 59 // The definition of the resource. 60 Definition *model.Block 61 62 // Token is the type token for this resource. 63 Token string 64 65 // Schema is the schema definition for this resource, if any. 66 Schema *schema.Resource 67 68 // The type of the resource's inputs. This will always be either Any or an object type. 69 InputType model.Type 70 // The type of the resource's outputs. This will always be either Any or an object type. 71 OutputType model.Type 72 73 // The type of the resource variable. 74 VariableType model.Type 75 76 // The resource's input attributes, in source order. 77 Inputs []*model.Attribute 78 79 // The resource's options, if any. 80 Options *ResourceOptions 81 } 82 83 // SyntaxNode returns the syntax node associated with the resource. 84 func (r *Resource) SyntaxNode() hclsyntax.Node { 85 return r.syntax 86 } 87 88 // Type returns the type of the resource. 89 func (r *Resource) Type() model.Type { 90 return r.VariableType 91 } 92 93 func (r *Resource) VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics { 94 return model.VisitExpressions(r.Definition, pre, post) 95 } 96 97 func (r *Resource) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) { 98 return r.VariableType.Traverse(traverser) 99 } 100 101 // Deprecated: Name returns the variable or declaration name of the resource. 102 func (r *Resource) Name() string { 103 return r.Definition.Labels[0] 104 } 105 106 // Returns the unique name of the resource; if the resource has an unique name it is formatted with 107 // the format string and returned, otherwise the defaultValue is returned as is. 108 func (r *Resource) LogicalName() string { 109 if r.logicalName != "" { 110 return r.logicalName 111 } 112 113 return r.Name() 114 } 115 116 // DecomposeToken attempts to decompose the resource's type token into its package, module, and type. If decomposition 117 // fails, a description of the failure is returned in the diagnostics. 118 func (r *Resource) DecomposeToken() (string, string, string, hcl.Diagnostics) { 119 _, tokenRange := getResourceToken(r) 120 return DecomposeToken(r.Token, tokenRange) 121 } 122 123 // ResourceProperty represents a resource property. 124 type ResourceProperty struct { 125 Path hcl.Traversal 126 PropertyType model.Type 127 } 128 129 func (*ResourceProperty) SyntaxNode() hclsyntax.Node { 130 return syntax.None 131 } 132 133 func (p *ResourceProperty) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) { 134 propertyType, diagnostics := p.PropertyType.Traverse(traverser) 135 return &ResourceProperty{ 136 Path: append(p.Path, traverser), 137 PropertyType: propertyType.(model.Type), 138 }, diagnostics 139 } 140 141 func (p *ResourceProperty) Type() model.Type { 142 return ResourcePropertyType 143 }