Documentation ¶
Index ¶
- Variables
- type Attribute
- type Block
- func (b *Block) AttributesAsMap() map[string]*Attribute
- func (b *Block) Children() Blocks
- func (b *Block) Clone(index cty.Value) *Block
- func (b *Block) Context() *Context
- func (b *Block) FullName() string
- func (b *Block) GetAttribute(name string) *Attribute
- func (b *Block) GetAttributes() []*Attribute
- func (b *Block) GetChildBlock(name string) *Block
- func (b *Block) HasChild(childElement string) bool
- func (b *Block) HasModuleBlock() bool
- func (b *Block) InjectBlock(block *Block, name string)
- func (b *Block) IsCountExpanded() bool
- func (b *Block) Label() string
- func (b *Block) Labels() []string
- func (b *Block) LocalName() string
- func (b *Block) ModuleAddress() string
- func (b *Block) ModuleName() string
- func (b *Block) ModuleSource() string
- func (b *Block) NameLabel() string
- func (b *Block) Provider() string
- func (b *Block) Reference() *Reference
- func (b *Block) SetContext(ctx *Context)
- func (b *Block) Type() string
- func (b *Block) TypeLabel() string
- func (b *Block) Values() cty.Value
- type Blocks
- type Context
- func (c *Context) Get(parts ...string) cty.Value
- func (c *Context) Inner() *hcl.EvalContext
- func (c *Context) NewChild() *Context
- func (c *Context) Parent() *Context
- func (c *Context) Root() *Context
- func (c *Context) Set(val cty.Value, parts ...string)
- func (c *Context) SetByDot(val cty.Value, path string)
- type Evaluator
- type Module
- type ModuleCall
- type Option
- type Parser
- type Reference
- type Type
Constants ¶
This section is empty.
Variables ¶
var TypeData = Type{ // contains filtered or unexported fields }
var TypeLocal = Type{ // contains filtered or unexported fields }
var TypeModule = Type{ // contains filtered or unexported fields }
var TypeOutput = Type{ // contains filtered or unexported fields }
var TypeProvider = Type{ // contains filtered or unexported fields }
var TypeResource = Type{ // contains filtered or unexported fields }
var TypeTerraform = Type{ // contains filtered or unexported fields }
var TypeVariable = Type{ // contains filtered or unexported fields }
var ValidTypes = []Type{ TypeData, TypeLocal, TypeModule, TypeOutput, TypeProvider, TypeResource, TypeTerraform, TypeVariable, }
Functions ¶
This section is empty.
Types ¶
type Attribute ¶
type Attribute struct { // HCLAttr is the underlying hcl.Attribute that the Attribute references. HCLAttr *hcl.Attribute // Ctx is the context that the Attribute should be evaluated against. This propagates // any references from variables into the attribute. Ctx *Context // Verbose defines if the attribute should log verbose diagnostics messages to debug. Verbose bool }
Attribute provides a wrapper struct around hcl.Attribute it provides helper methods and functionality for common interactions with hcl.Attribute.
Attributes are key/value pairs that are part of a Block. For example take the following Block:
resource "aws_instance" "t3_standard" { ami = "fake_ami" instance_type = "t3.medium" credit_specification { cpu_credits = "standard" } }
"ami" & "instance_type" are the Attributes of this Block, "credit_specification" is a child Block see Block.Children for more info.
func (*Attribute) AllReferences ¶
AllReferences returns a list of References for the given Attribute. This can include the main Value Reference (see Reference method) and also a list of references used in conditional evaluation and templating.
func (*Attribute) IsIterable ¶
IsIterable returns if the attribute can be ranged over.
func (*Attribute) Reference ¶
Reference returns the pointer to a Reference struct that holds information about the Attributes referenced block. Reference achieves this by traversing the Attribute Expression in order to find the parent block. E.g. with the following HCL
resource "aws_launch_template" "foo2" { name = "foo2" } resource "some_resource" "example_with_launch_template_3" { ... name = aws_launch_template.foo2.name }
The Attribute some_resource.name would have a reference of
Reference { blockType: Type{ name: "resource", removeTypeInReference: true, } typeLabel: "aws_launch_template" nameLabel: "foo2" }
Reference is used to build up a Terraform JSON configuration file that holds information about the expressions and their parents. Infracost uses these references in resource evaluation to lookup connecting resource information.
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
Block wraps a hcl.Block type with additional methods and context. A Block is a core piece of HCL schema and represents a set of data. Most importantly a Block directly corresponds to a schema.Resource.
Blocks can represent a number of different types - see terraformSchemaV012 for a list of potential HCL blocks available.
e.g. a type resource block could look like this in HCL:
resource "aws_lb" "lb1" { load_balancer_type = "application" }
A Block can also have a set number of child Blocks, these child Blocks in turn can also have children. Blocks are recursive. The following example is represents a resource Block with child Blocks:
resource "aws_instance" "t3_standard_cpuCredits" { ami = "fake_ami" instance_type = "t3.medium" # child Block starts here credit_specification { cpu_credits = "standard" } }
See Attribute for more info about how the values of Blocks are evaluated with their Context and returned.
func NewHCLBlock ¶
NewHCLBlock returns a Block with Context and child Blocks initialised.
func (*Block) AttributesAsMap ¶
AttributesAsMap returns the Attributes of this block as a map with the attribute name as the key and the value as the Attribute.
func (*Block) Clone ¶
Clone creates a duplicate of the block and sets the returned Block's Context to include the index provided. This is primarily used when Blocks are expanded as part of a count evaluation.
func (*Block) FullName ¶
FullName returns the fully qualified Reference name as it relates to the Blocks position in the entire Terraform config tree. This includes module name. e.g.
The following resource residing in a module named "web_app":
resource "aws_instance" "t3_standard" { ami = "fake_ami" instance_type = var.instance_type }
Would have its FullName as module.web_app.aws_instance.t3_standard FullName is what Terraform uses in its JSON output file.
func (*Block) GetAttribute ¶
GetAttribute returns the given attribute with the provided name. It will return nil if the attribute is not found. If we take the following Block example:
resource "aws_instance" "t3_standard_cpuCredits" { ami = "fake_ami" instance_type = "t3.medium" credit_specification { cpu_credits = "standard" } }
ami & instance_type are both valid Attribute names that can be used to lookup Block Attributes.
func (*Block) GetAttributes ¶
GetAttributes returns a list of Attribute for this Block. Attributes are key value specification on a given Block. For example take the following hcl:
resource "aws_instance" "t3_standard_cpuCredits" { ami = "fake_ami" instance_type = "t3.medium" credit_specification { cpu_credits = "standard" } }
ami & instance_type are the Attributes of this Block and credit_specification is a child Block.
func (*Block) GetChildBlock ¶
GetChildBlock returns the first child Block that has the name provided. e.g: If the current Block looks like such:
resource "aws_instance" "t3_standard_cpuCredits" { ami = "fake_ami" instance_type = "t3.medium" credit_specification { cpu_credits = "standard" } ebs_block_device { device_name = "xvdj" } }
Then "credit_specification" & "ebs_block_device" would be valid names that could be used to retrieve child Blocks.
func (*Block) HasModuleBlock ¶
HasModuleBlock returns is the Block as a module associated with it. If it doesn't this means that this Block is part of the root Module.
func (*Block) InjectBlock ¶
InjectBlock takes a block and appends it to the Blocks childBlocks with the Block's attributes set as contextual values on the child. In most cases this is because we've expanded the block into further Blocks as part of a count or for_each.
func (*Block) IsCountExpanded ¶
IsCountExpanded returns if the Block has been expanded as part of a for_each or count evaluation.
func (*Block) ModuleAddress ¶
ModuleAddress returns the address of the module associated with this Block or "" if it is part of the root Module
func (*Block) ModuleName ¶
ModuleName returns the name of the module associated with this Block or "" if it is part of the root Module
func (*Block) ModuleSource ¶
ModuleSource returns the "source" attribute from the associated Module or "" if it is part of the root Module
func (*Block) Provider ¶
Provider returns the provider by first checking if it is explicitly set as an attribute, if it is not the first word in the snake_case name of the type is returned. E.g. the type 'aws_instance' would return provider 'aws'
func (*Block) Reference ¶
Reference returns a Reference to the given Block this can be used to when printing out full names of Blocks to stdout or a file.
func (*Block) SetContext ¶
SetContext sets the Block.context to the provided ctx. This ctx is also set on the child Blocks as a child Context. Meaning that it can be used in traversal evaluation when looking up Context variables.
func (*Block) Values ¶
Values returns the Block as a cty.Value with all the Attributes evaluated with the Block Context. This means that any variables or references will be replaced by their actual value. For example:
variable "instance_type" { default = "t3.medium" } resource "aws_instance" "t3_standard_cpucredits" { ami = "fake_ami" instance_type = var.instance_type }
Would evaluate to a cty.Value of type Object with the instance_type Attribute holding the value "t3.medium".
type Blocks ¶
type Blocks []*Block
Blocks is a helper type around a slice of blocks to provide easy access finding blocks of type.
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator provides a set of given Blocks with contextual information. Evaluator is an important step in retrieving Block values that can be used in the schema.Resource cost retrieval. Without Evaluator the Blocks provided only have shallow information within attributes and won't contain any evaluated variables or references.
func NewEvaluator ¶
func NewEvaluator( projectRootPath string, modulePath string, workingDir string, blocks Blocks, inputVars map[string]cty.Value, moduleMetadata *modules.Manifest, visitedModules map[string]struct{}, workspace string, ) *Evaluator
NewEvaluator returns an Evaluator with Context initialised with top level variables. This Context is then passed to all Blocks as child Context so that variables built in Evaluation are propagated to the Block Attributes.
func (*Evaluator) Run ¶
Run builds the Evaluator Context using all the provided Blocks. It will build up the Context to hold variable and reference information so that this can be used by Attribute evaluation. Run will also parse and build up and child modules that are referenced in the Blocks and runs child Evaluator on these Modules.
type ModuleCall ¶
type ModuleCall struct { // Name the name of the module as specified a the point of definition. Name string // Path is the path to the local directory containing the HCL for the Module. Path string // Definition is the actual Block where the ModuleCall happens in a hcl.File Definition *Block // Modules contains the parsed modules that are part of this ModuleCall. This can contain // more than one Module as it will also contain a list of the child Modules that have been // called within this Module. The Module at position 0 is the root Module. Modules []*Module }
ModuleCall represents a call to a defined Module by a parent Module.
type Option ¶
type Option func(p *Parser)
func OptionStopOnHCLError ¶
func OptionStopOnHCLError() Option
func OptionWithInputVars ¶
OptionWithInputVars takes a cmd line var input values and converts them to cty.Value It then sets these as the Parser starting inputVars which be used at the root module evaluation.
func OptionWithTFVarsPaths ¶
OptionWithTFVarsPaths takes a slice of paths and sets them on the parser relative to the Parser initialPath. Paths that don't exist will be ignored.
func OptionWithWorkspaceName ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a tool for parsing terraform templates at a given file system location.
func New ¶
New creates a new Parser with the provided options, it inits the workspace as under the default name this can be changed using Option.
func (*Parser) ParseDirectory ¶
ParseDirectory parses all the terraform files in the initalPath into Blocks and then passes them to an Evaluator to fill these Blocks with additional Context information. Parser does not parse any blocks outside the root Module. It instead leaves ModuleLoader to fetch these Modules on demand. See ModuleLoader.Load for more information.
ParseDirectory returns a list of Module that represent the Terraform Config tree.