pcl

package
v3.114.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2024 License: Apache-2.0 Imports: 26 Imported by: 18

Documentation

Index

Constants

View Source
const (
	// IntrinsicApply is the name of the apply intrinsic.
	IntrinsicApply = "__apply"
	// IntrinsicConvert is the name of the conversion intrinsic.
	IntrinsicConvert = "__convert"
)
View Source
const Invoke = "invoke"
View Source
const (
	LogicalNamePropertyKey = "__logicalName"
)

Variables

View Source
var (
	// ArchiveType represents the set of Pulumi Archive values.
	ArchiveType model.Type = model.NewOpaqueType("Archive")
	// AssetType represents the set of Pulumi Asset values.
	AssetType model.Type = model.NewOpaqueType("Asset")
	// ResourcePropertyType represents a resource property reference.
	ResourcePropertyType model.Type = model.NewOpaqueType("Property")
	// AssetOrArchiveType represents the set of Pulumi Archive values.
	AssetOrArchiveType model.Type = model.NewUnionType(ArchiveType, AssetType)
)

Functions

func AllowMissingProperties

func AllowMissingProperties(options *bindOptions)

func AllowMissingVariables

func AllowMissingVariables(options *bindOptions)

func AnnotateAttributeValue added in v3.43.0

func AnnotateAttributeValue(expr model.Expression, attributeType schema.Type) model.Expression

func AnnotateResourceInputs added in v3.43.0

func AnnotateResourceInputs(node *Resource)

func DecomposeToken

func DecomposeToken(tok string, sourceRange hcl.Range) (string, string, string, hcl.Diagnostics)

func EnumMember added in v3.30.0

func EnumMember(t *model.EnumType, value cty.Value) (*schema.Enum, bool)

EnumMember returns the name of the member that matches the given `value`. If no member if found, (nil, true) returned. If the query is nonsensical, either because no schema is associated with the EnumMember or if the type of value mismatches the type of the schema, (nil, false) is returned.

func FixupPulumiPackageTokens added in v3.39.0

func FixupPulumiPackageTokens(r *Resource)

func GenEnum added in v3.30.0

func GenEnum(
	t *model.EnumType,
	from model.Expression,
	safeEnum func(member *schema.Enum),
	unsafeEnum func(from model.Expression),
) *hcl.Diagnostic

GenEnum is a helper function when generating an enum. Given an enum, and instructions on what to do when you find a known value, and an unknown value, return a function that will generate an the given enum from the given expression.

This function should probably live in the `codegen` namespace, but cannot because of import cycles.

func GenerateMultiArguments added in v3.50.0

func GenerateMultiArguments(
	f *format.Formatter,
	w io.Writer,
	undefinedLiteral string,
	expr *model.ObjectConsExpression,
	multiArguments []*schema.Property,
)

GenerateMultiArguments takes the input bag (object) of a function invoke and spreads the values of that object into multi-argument function call. For example, { a: 1, b: 2 } with multiInputArguments: ["a", "b"] would become: 1, 2

However, when optional parameters are omitted, then <undefinedLiteral> is used where they should be. Take for example { a: 1, c: 3 } with multiInputArguments: ["a", "b", "c"], it becomes 1, <undefinedLiteral>, 3 because b was omitted and c was provided so b had to be the provided <undefinedLiteral>

func GetDiscriminatedUnionObjectMapping

func GetDiscriminatedUnionObjectMapping(t *model.UnionType) map[string]model.Type

GetDiscriminatedUnionObjectMapping calculates a map of type names to object types for a given union type.

func GetSchemaForType

func GetSchemaForType(t model.Type) (schema.Type, bool)

GetSchemaForType extracts the schema.Type associated with a model.Type, if any.

The result may be a *schema.UnionType if multiple schema types are associated with the input type.

func HasEventualTypes added in v3.66.0

func HasEventualTypes(t model.Type) bool

func IsOutputVersionInvokeCall added in v3.18.0

func IsOutputVersionInvokeCall(call *model.FunctionCallExpression) bool

Detects invoke calls that use an output version of a function.

func LookupFunction added in v3.49.0

func LookupFunction(pkg schema.PackageReference, token string) (*schema.Function, bool, error)

Lookup a PCL invoke token in a schema.

func LookupResource added in v3.49.0

func LookupResource(pkg schema.PackageReference, token string) (*schema.Resource, bool, error)

Lookup a PCL resource token in a schema.

func LowerConversion added in v3.30.0

func LowerConversion(from model.Expression, to model.Type) model.Type

LowerConversion lowers a conversion for a specific value, such that converting `from` to a value of the returned type will produce valid code. The algorithm prioritizes safe conversions over unsafe conversions. If no conversion can be found, nil, false is returned.

This is useful because it cuts out conversion steps which the caller doesn't need to worry about. For example: Given inputs

from = string("foo") # a constant string with value "foo"
to = union(enum(string: "foo", "bar"), input(enum(string: "foo", "bar")), none)

We would receive output type:

enum(string: "foo", "bar")

since the caller can convert string("foo") to the enum directly, and does not need to consider the union.

For another example consider inputs:

from = var(string) # A variable of type string
to = union(enum(string: "foo", "bar"), string)

We would return type:

string

since var(string) can be safely assigned to string, but unsafely assigned to enum(string: "foo", "bar").

func MapProvidersAsResources added in v3.37.0

func MapProvidersAsResources(p *Program)

Remaps the "pulumi:providers:$Package" token style to "$Package:index:Provider", consistent with code generation. This mapping is consistent with how provider resources are projected into the schema and removes special casing logic to generate registering explicit providers.

The resultant program should be a shallow copy of the source with only the modified resource nodes copied.

func NewApplyCall

NewApplyCall returns a new expression that represents a call to IntrinsicApply.

func NewConvertCall

func NewConvertCall(from model.Expression, to model.Type) *model.FunctionCallExpression

NewConvertCall returns a new expression that represents a call to IntrinsicConvert.

func ParseApplyCall

func ParseApplyCall(c *model.FunctionCallExpression) (applyArgs []model.Expression,
	then *model.AnonymousFunctionExpression,
)

ParseApplyCall extracts the apply arguments and the continuation from a call to the apply intrinsic.

func ParseConvertCall

func ParseConvertCall(c *model.FunctionCallExpression) (model.Expression, model.Type)

ParseConvertCall extracts the value being converted and the type it is being converted to from a call to the convert intrinsic.

func PreferOutputVersionedInvokes added in v3.74.0

func PreferOutputVersionedInvokes(options *bindOptions)

func RecognizeOutputVersionedInvoke added in v3.18.0

func RecognizeOutputVersionedInvoke(
	expr *model.FunctionCallExpression,
) (bool, *model.ObjectConsExpression, model.Type)

Pattern matches to recognize an encoded call to an output-versioned invoke, such as `invoke(token, __convert(objCons(..)))`. If matching, returns the `args` expression and its schema-bound type.

func RecognizeTypedObjectCons added in v3.18.0

func RecognizeTypedObjectCons(theExpr model.Expression) (bool, *model.ObjectConsExpression, model.Type)

Pattern matches to recognize `__convert(objCons(..))` pattern that is used to annotate object constructors with appropriate nominal types. If the expression matches, returns true followed by the constructor expression and the appropriate type.

func RewriteApplies

func RewriteApplies(expr model.Expression, nameInfo NameInfo, applyPromises bool) (model.Expression, hcl.Diagnostics)

RewriteApplies transforms all expressions that observe the resolved values of outputs and promises into calls to the __apply intrinsic. Expressions that generate or inspect outputs or promises are passed as arguments to these calls, and are replaced by references to the corresponding parameter.

As an example, assuming that resource.id is an output, this transforms the following expression:

toJSON({
    Version = "2012-10-17"
    Statement = [{
        Effect = "Allow"
        Principal = "*"
        Action = [ "s3:GetObject" ]
        Resource = [ "arn:aws:s3:::${resource.id}/*" ]
    }]
})

into this expression:

__apply(resource.id, eval(id, toJSON({
    Version = "2012-10-17"
    Statement = [{
        Effect = "Allow"
        Principal = "*"
        Action = [ "s3:GetObject" ]
        Resource = [ "arn:aws:s3:::${id}/*" ]
    }]
})))

Here is a more advanced example, assuming that resource is an object whose properties are all outputs, this expression:

"v: ${resource[resource.id]}"

is transformed into this expression:

__apply(__apply(resource.id,eval(id, resource[id])),eval(id, "v: ${id}"))

This form is amenable to code generation for targets that require that outputs are resolved before their values are accessible (e.g. Pulumi's JS/TS libraries).

func RewriteAppliesWithSkipToJSON added in v3.107.0

func RewriteAppliesWithSkipToJSON(
	expr model.Expression,
	nameInfo NameInfo,
	applyPromises bool,
	skipToJSON bool,
) (model.Expression, hcl.Diagnostics)

func RewriteConversions

func RewriteConversions(x model.Expression, to model.Type) (model.Expression, hcl.Diagnostics)

RewriteConversions wraps automatic conversions indicated by the HCL2 spec and conversions to schema-annotated types in calls to the __convert intrinsic.

Note that the result is a bit out of line with the HCL2 spec, as static conversions may happen earlier than they would at runtime. For example, consider the case of a tuple of strings that is being converted to a list of numbers:

[a, b, c]

Calling RewriteConversions on this expression with a destination type of list(number) would result in this IR:

[__convert(a), __convert(b), __convert(c)]

If any of these conversions fail, the evaluation of the tuple itself fails. The HCL2 evaluation semantics, however, would convert the tuple _after_ it has been evaluated. The IR that matches these semantics is

__convert([a, b, c])

This transform uses the former representation so that it can appropriately insert calls to `__convert` in the face of schema-annotated types. There is a reasonable argument to be made that RewriteConversions should not be responsible for propagating schema annotations, and that this pass should be split in two: one pass would insert conversions that match HCL2 evaluation semantics, and another would insert calls to some separate intrinsic in order to propagate schema information.

func RewritePropertyReferences

func RewritePropertyReferences(expr model.Expression) model.Expression

func SkipInvokeTypechecking added in v3.75.0

func SkipInvokeTypechecking(options *bindOptions)

func SkipRangeTypechecking added in v3.76.0

func SkipRangeTypechecking(options *bindOptions)

func SkipResourceTypechecking

func SkipResourceTypechecking(options *bindOptions)

func SortedFunctionParameters added in v3.50.0

func SortedFunctionParameters(expr *model.FunctionCallExpression) []*schema.Property

SortedFunctionParameters returns a list of properties of the input type from the schema for an invoke function call which has multi argument inputs. We assume here that the expression is an invoke which has it's args (2nd parameter) annotated with the original schema type. The original schema type has properties sorted. This is important because model.ObjectType has no guarantee of property order.

func SortedStringKeys added in v3.61.1

func SortedStringKeys[V any](m map[string]V) []string

func UnwrapOption added in v3.66.0

func UnwrapOption(exprType model.Type) model.Type

UnwrapOption returns type T if the input is an Option(T)

func VariableAccessed added in v3.78.0

func VariableAccessed(variableName string, expr model.Expression) bool

VariableAccessed returns whether the given variable name is accessed in the given expression.

Types

type BindOption

type BindOption func(*bindOptions)

func Cache

func Cache(cache *PackageCache) BindOption

func ComponentBinder added in v3.58.0

func ComponentBinder(binder ComponentProgramBinder) BindOption

func DirPath added in v3.58.0

func DirPath(path string) BindOption

func Loader

func Loader(loader schema.Loader) BindOption

func NonStrictBindOptions added in v3.76.0

func NonStrictBindOptions() []BindOption

NonStrictBindOptions returns a set of bind options that make the binder lenient about type checking. Changing errors into warnings when possible

func PluginHost

func PluginHost(host plugin.Host) BindOption

type Component

type Component struct {

	// The inner Program that makes up this Component.
	Program *Program

	// The type of the resource variable.
	VariableType model.Type

	// The definition of the component.
	Definition *model.Block

	// The component's input attributes, in source order.
	Inputs []*model.Attribute

	// The component resource's options, if any.
	Options *ResourceOptions
	// contains filtered or unexported fields
}

Component represents a component reference in a program.

func (*Component) DeclarationName added in v3.77.0

func (c *Component) DeclarationName() string

DeclarationName returns the name of the component to use in the generated program's declaration. It is the name of the component's directory, with invalid characters replaced with underscores.

func (*Component) DirPath added in v3.58.0

func (c *Component) DirPath() string

func (*Component) LogicalName added in v3.58.0

func (c *Component) LogicalName() string

func (*Component) Name added in v3.58.0

func (c *Component) Name() string

func (*Component) SyntaxNode added in v3.58.0

func (c *Component) SyntaxNode() hclsyntax.Node

SyntaxNode returns the syntax node associated with the component.

func (*Component) Traverse added in v3.58.0

func (c *Component) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics)

func (*Component) Type added in v3.58.0

func (c *Component) Type() model.Type

func (*Component) VisitExpressions added in v3.58.0

func (c *Component) VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics

type ComponentProgramBinder added in v3.58.0

type ComponentProgramBinder = func(ComponentProgramBinderArgs) (*Program, hcl.Diagnostics, error)

func ComponentProgramBinderFromFileSystem added in v3.58.0

func ComponentProgramBinderFromFileSystem() ComponentProgramBinder

ComponentProgramBinderFromFileSystem returns the default component program binder which uses the file system to parse and bind PCL files into a program.

type ComponentProgramBinderArgs added in v3.58.0

type ComponentProgramBinderArgs struct {
	AllowMissingVariables        bool
	AllowMissingProperties       bool
	SkipResourceTypecheck        bool
	SkipInvokeTypecheck          bool
	SkipRangeTypecheck           bool
	PreferOutputVersionedInvokes bool
	BinderDirPath                string
	BinderLoader                 schema.Loader
	ComponentSource              string
	ComponentNodeRange           hcl.Range
}

type ConfigVariable

type ConfigVariable struct {

	// The variable definition.
	Definition *model.Block
	// The default value for the config variable, if any.
	DefaultValue model.Expression
	Description  string
	// Whether the config variable is nullable
	Nullable bool
	// contains filtered or unexported fields
}

ConfigVariable represents a program- or component-scoped input variable. The value for a config variable may come from stack configuration or component inputs, respectively, and may have a default value.

func (*ConfigVariable) LogicalName added in v3.46.0

func (cv *ConfigVariable) LogicalName() string

func (*ConfigVariable) Name

func (cv *ConfigVariable) Name() string

func (*ConfigVariable) SyntaxNode

func (cv *ConfigVariable) SyntaxNode() hclsyntax.Node

SyntaxNode returns the syntax node associated with the config variable.

func (*ConfigVariable) Traverse

func (cv *ConfigVariable) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics)

func (*ConfigVariable) Type

func (cv *ConfigVariable) Type() model.Type

Type returns the type of the config variable.

func (*ConfigVariable) VisitExpressions

func (cv *ConfigVariable) VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics

type LocalVariable

type LocalVariable struct {

	// The variable definition.
	Definition *model.Attribute
	// contains filtered or unexported fields
}

LocalVariable represents a program- or component-scoped local variable.

func (*LocalVariable) LogicalName added in v3.31.0

func (lv *LocalVariable) LogicalName() string

func (*LocalVariable) Name

func (lv *LocalVariable) Name() string

func (*LocalVariable) SyntaxNode

func (lv *LocalVariable) SyntaxNode() hclsyntax.Node

SyntaxNode returns the syntax node associated with the local variable.

func (*LocalVariable) Traverse

func (lv *LocalVariable) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics)

func (*LocalVariable) Type

func (lv *LocalVariable) Type() model.Type

Type returns the type of the local variable.

func (*LocalVariable) VisitExpressions

func (lv *LocalVariable) VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics

type NameInfo

type NameInfo interface {
	Format(name string) string
}

type Node

type Node interface {
	model.Definition

	// Name returns the lexical name of the node.
	Name() string

	// Type returns the type of the node.
	Type() model.Type

	// VisitExpressions visits the expressions that make up the node's body.
	VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics
	// contains filtered or unexported methods
}

Node represents a single definition in a program or component. Nodes may be config, locals, resources, components, or outputs.

func Linearize

func Linearize(p *Program) []Node

Linearize performs a topological sort of the nodes in the program so that they can be processed by tools that need to see all of a node's dependencies before the node itself (e.g. a code generator for a programming language that requires variables to be defined before they can be referenced). The sort is stable, and nodes are kept in source order as much as possible.

func SourceOrderNodes

func SourceOrderNodes(nodes []Node) []Node

type OutputVariable

type OutputVariable struct {

	// The definition of the output.
	Definition *model.Block
	// The value of the output.
	Value model.Expression
	// contains filtered or unexported fields
}

OutputVariable represents a program- or component-scoped output variable.

func (*OutputVariable) LogicalName added in v3.31.0

func (ov *OutputVariable) LogicalName() string

Returns the ID of the output, if the output has an ID it is returned surrounded by double quotes, otherwise the defaultValue is returned as is.

func (*OutputVariable) Name

func (ov *OutputVariable) Name() string

func (*OutputVariable) SyntaxNode

func (ov *OutputVariable) SyntaxNode() hclsyntax.Node

SyntaxNode returns the syntax node associated with the output variable.

func (*OutputVariable) Traverse

func (ov *OutputVariable) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics)

func (*OutputVariable) Type

func (ov *OutputVariable) Type() model.Type

Type returns the type of the output variable.

func (*OutputVariable) VisitExpressions

func (ov *OutputVariable) VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics

type PackageCache

type PackageCache struct {
	// contains filtered or unexported fields
}

func NewPackageCache

func NewPackageCache() *PackageCache

type PackageInfo added in v3.40.2

type PackageInfo struct {
	// contains filtered or unexported fields
}

type Program

type Program struct {
	Nodes []Node
	// contains filtered or unexported fields
}

Program represents a semantically-analyzed Pulumi HCL2 program.

func BindDirectory added in v3.70.0

func BindDirectory(
	directory string,
	loader schema.ReferenceLoader,
	extraOptions ...BindOption,
) (*Program, hcl.Diagnostics, error)

Used by language plugins to bind a PCL program in the given directory.

func BindProgram

func BindProgram(files []*syntax.File, opts ...BindOption) (*Program, hcl.Diagnostics, error)

BindProgram performs semantic analysis on the given set of HCL2 files that represent a single program. The given host, if any, is used for loading any resource plugins necessary to extract schema information.

func (*Program) BindExpression

func (p *Program) BindExpression(node hclsyntax.Node) (model.Expression, hcl.Diagnostics)

BindExpression binds an HCL2 expression in the top-level context of the program.

func (*Program) CollectComponents added in v3.58.0

func (p *Program) CollectComponents() map[string]*Component

CollectComponents finds all used components in a program and recursively searches of nested components from sub programs.

func (*Program) CollectNestedPackageSnapshots added in v3.59.0

func (p *Program) CollectNestedPackageSnapshots() (map[string]*schema.Package, error)

func (*Program) ConfigVariables added in v3.58.0

func (p *Program) ConfigVariables() []*ConfigVariable

ConfigVariables returns the config variable nodes of the program

func (*Program) NewDiagnosticWriter

func (p *Program) NewDiagnosticWriter(w io.Writer, width uint, color bool) hcl.DiagnosticWriter

NewDiagnosticWriter creates a new hcl.DiagnosticWriter for use with diagnostics generated by the program.

func (*Program) OutputVariables added in v3.58.0

func (p *Program) OutputVariables() []*OutputVariable

OutputVariables returns the output variable nodes of the program

func (*Program) PackageReferences added in v3.33.2

func (p *Program) PackageReferences() []schema.PackageReference

PackageReferences returns the list of package referenced used by this program.

func (*Program) PackageSnapshots added in v3.33.2

func (p *Program) PackageSnapshots() ([]*schema.Package, error)

PackageSnapshots returns the list of packages schemas used by this program. If a referenced package is partial, its returned value is a snapshot that contains only the package members referenced by the program. Otherwise, its returned value is the full package definition.

func (*Program) Packages

func (p *Program) Packages() []*schema.Package

Packages returns the list of package referenced used by this program.

func (*Program) Source added in v3.46.1

func (p *Program) Source() map[string]string

func (*Program) WriteSource added in v3.59.0

func (p *Program) WriteSource(fs afero.Fs) error

WriteSource writes the source files of the program, including those files of used components into the provided file system.

type Resource

type Resource struct {

	// The definition of the resource.
	Definition *model.Block

	// When set to true, allows traversing unknown properties through a resource. i.e. `resource.unknownProperty`
	// will be valid and the type of the traversal is dynamic. This property is set to false by default
	LenientTraversal bool

	// Token is the type token for this resource.
	Token string

	// Schema is the schema definition for this resource, if any.
	Schema *schema.Resource

	// The type of the resource's inputs. This will always be either Any or an object type.
	InputType model.Type
	// The type of the resource's outputs. This will always be either Any or an object type.
	OutputType model.Type

	// The type of the resource variable.
	VariableType model.Type

	// The resource's input attributes, in source order.
	Inputs []*model.Attribute

	// The resource's options, if any.
	Options *ResourceOptions
	// contains filtered or unexported fields
}

Resource represents a resource instantiation inside of a program or component.

func (*Resource) DecomposeToken

func (r *Resource) DecomposeToken() (string, string, string, hcl.Diagnostics)

DecomposeToken attempts to decompose the resource's type token into its package, module, and type. If decomposition fails, a description of the failure is returned in the diagnostics.

func (*Resource) LogicalName added in v3.31.0

func (r *Resource) LogicalName() string

Returns the unique name of the resource; if the resource has an unique name it is formatted with the format string and returned, otherwise the defaultValue is returned as is.

func (*Resource) Name deprecated

func (r *Resource) Name() string

Deprecated: Name returns the variable or declaration name of the resource.

func (*Resource) SyntaxNode

func (r *Resource) SyntaxNode() hclsyntax.Node

SyntaxNode returns the syntax node associated with the resource.

func (*Resource) Traverse

func (r *Resource) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics)

func (*Resource) Type

func (r *Resource) Type() model.Type

Type returns the type of the resource.

func (*Resource) VisitExpressions

func (r *Resource) VisitExpressions(pre, post model.ExpressionVisitor) hcl.Diagnostics

type ResourceOptions

type ResourceOptions struct {
	// The definition of the resource options.
	Definition *model.Block

	// An expression to range over when instantiating the resource.
	Range model.Expression
	// The resource's parent, if any.
	Parent model.Expression
	// The provider to use.
	Provider model.Expression
	// The explicit dependencies of the resource.
	DependsOn model.Expression
	// Whether or not the resource is protected.
	Protect model.Expression
	// Whether the resource should be left in the cloud provider
	// when it's deleted from the Pulumi state.
	RetainOnDelete model.Expression
	// A list of properties that are not considered when diffing the resource.
	IgnoreChanges model.Expression
	// The version of the provider for this resource.
	Version model.Expression
	// The plugin download URL for this resource.
	PluginDownloadURL model.Expression
}

ResourceOptions represents a resource instantiation's options.

type ResourceProperty

type ResourceProperty struct {
	Path         hcl.Traversal
	PropertyType model.Type
}

ResourceProperty represents a resource property.

func (*ResourceProperty) SyntaxNode

func (*ResourceProperty) SyntaxNode() hclsyntax.Node

func (*ResourceProperty) Traverse

func (p *ResourceProperty) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics)

func (*ResourceProperty) Type

func (p *ResourceProperty) Type() model.Type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL