terraform

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package terraform includes functionality related to reading Terraform plan files. The plan schema is defined here according to the JSON output format described at https://www.terraform.io/docs/internals/json-format.html

The found resources are then transformed into query.Resource that can be utilized further.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoQueries       = errors.New("no terraform entities found, looks empty")
	ErrNoKnownProvider = errors.New("terraform providers are not yet supported")
	ErrNoProviders     = errors.New("no valid providers found")
)

Errors that might be returned from procesing the HCL

Functions

func ExtractQueriesFromHCL

func ExtractQueriesFromHCL(fs afero.Fs, providerInitializers []ProviderInitializer, modPath string, u usage.Usage, inputs map[string]interface{}) ([]query.Resource, string, error)

ExtractQueriesFromHCL returns the resources found in the module identified by the modPath.

Types

type Configuration

type Configuration struct {
	ProviderConfig map[string]ProviderConfig `json:"provider_config"`
	RootModule     ConfigurationModule       `json:"root_module"`
}

Configuration is a Terraform plan configuration.

type ConfigurationModule

type ConfigurationModule struct {
	Resources   []ConfigurationResource `json:"resources"`
	Variables   map[string]Variable     `json:"variables"`
	ModuleCalls map[string]struct {
		Module *ConfigurationModule `json:"module"`
	} `json:"module_calls"`
}

ConfigurationModule is used to configure a module.

type ConfigurationResource

type ConfigurationResource struct {
	Address           string `json:"address"`
	ProviderConfigKey string `json:"provider_config_key"`
	// Expressions are really similar to the ProviderConfigExpression but we cannot use it (as map[string]ProviderConfigExpression)
	// as some examples do not match, some are not map[string]ProviderConfigExpression but map[string][]interface{} and some constant_value are not
	// string but other types
	Expressions map[string]interface{} `json:"expressions"`
}

ConfigurationResource is used to configure a single reosurce.

type Module

type Module struct {
	Address      string     `json:"address"`
	Resources    []Resource `json:"resources"`
	ChildModules []*Module  `json:"child_modules"`
}

Module is a collection of resources.

type Plan

type Plan struct {
	Configuration Configuration       `json:"configuration"`
	PriorState    *State              `json:"prior_state"`
	PlannedValues Values              `json:"planned_values"`
	Variables     map[string]Variable `json:"variables"`
	// contains filtered or unexported fields
}

Plan is a representation of a Terraform plan file.

func NewPlan

func NewPlan(providerInitializers ...ProviderInitializer) *Plan

NewPlan returns an empty Plan.

func (*Plan) ExtractPlannedQueries

func (p *Plan) ExtractPlannedQueries() ([]query.Resource, error)

ExtractPlannedQueries extracts a query.Resource slice from the `planned_values` part of the Plan.

func (*Plan) ExtractPriorQueries

func (p *Plan) ExtractPriorQueries() ([]query.Resource, error)

ExtractPriorQueries extracts a query.Resource slice from the `prior_state` part of the Plan.

func (*Plan) Read

func (p *Plan) Read(r io.Reader) error

Read reads the Plan file from the provider io.Reader.

func (*Plan) SetUsage

func (p *Plan) SetUsage(u usage.Usage)

SetUsage will set the usage of the plan

type Provider

type Provider interface {
	// Name returns the common name of this Provider.
	Name() string

	// ResourceComponents returns price component queries for the given Resource. Nil may be returned
	// which signifies a resource that is not supported by this Provider.
	// It also expects all the resources in case it needs to check the configuration of another
	// resource
	ResourceComponents(rss map[string]Resource, res Resource) []query.Component
}

Provider represents a Terraform provider. It extracts price queries from Terraform resources.

type ProviderConfig

type ProviderConfig struct {
	Name        string                              `json:"name"`
	Alias       string                              `json:"alias"`
	Expressions map[string]ProviderConfigExpression `json:"expressions"`
}

ProviderConfig is configuration of a provider with the given Name.

func (*ProviderConfig) UnmarshalJSON

func (cfg *ProviderConfig) UnmarshalJSON(b []byte) error

UnmarshalJSON handles the logic of Unmarshaling a ProviderConfig as we have some edge cases we want to not unmarshal as they are not standard/needed and would make things more complex

type ProviderConfigExpression

type ProviderConfigExpression struct {
	ConstantValue string   `json:"constant_value",mapstructure:"constant_value"`
	References    []string `json:"references",mapstructure:"references"`
}

ProviderConfigExpression is a single configuration variable of a ProviderConfig.

type ProviderInitializer

type ProviderInitializer struct {
	// MatchNames contains the names that this ProviderInitializer will match. Most providers will only
	// have one name (such as `aws`) but some might use multiple names to refer to the same provider
	// implementation (such as `google` and `google-beta`).
	MatchNames []string

	// Provider initializes a Provider instance given the values defined in the config and returns it.
	// If a provider must be ignored (related to version constraints, etc), please return nil to avoid using it.
	Provider func(values map[string]interface{}) (Provider, error)
}

ProviderInitializer is used to initialize a Provider for each provider name that matches one of the MatchNames.

type Resource

type Resource struct {
	Address string `json:"address"`
	// This value is 99% of the time an integer, but it can also be
	// a string, the implementation on TF side is of 'addrs.InstanceKey'
	// which can be of type 'IntKey' or 'StringKey'
	Index        interface{}            `json:"index"`
	Mode         string                 `json:"mode"`
	Type         string                 `json:"type"`
	Name         string                 `json:"name"`
	ProviderName string                 `json:"provider_name"`
	Values       map[string]interface{} `json:"values"`
}

Resource is a single Terraform resource definition.

type State

type State struct {
	Values Values `json:"values"`
}

State is a collection of resource modules.

type Values

type Values struct {
	RootModule Module `json:"root_module"`
}

Values is a tree of modules and resources within.

type Variable

type Variable struct {
	Value interface{} `json:"value"`
}

Variable is a Terraform variable declaration.

Jump to

Keyboard shortcuts

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