terraspec

package
v0.0.0-...-f01a3cb Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: MPL-2.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const Info tfdiags.Severity = 'I'

Info is an additional Severity level to display Info messages

Variables

This section is empty.

Functions

func BuildContextOptions

func BuildContextOptions(dir, varFile string, resolver *ProviderResolver, tsCtx *Context) (*terraform.ContextOpts, tfdiags.Diagnostics)

BuildContextOptions creates a new terraform.ContextOpts ready for instanciating a terraform Context It returns the built ContextOpts or a Diagnostics if error occured

func Compare

func Compare(one, other tfdiags.Diagnostics) int

Compare returns the difference in error numbers between one and other if result == 0, then the 2 diagnostics have same number of errors if result < 0, one has less error than other if result > 0, one has more error than other

func GetPluginFolder

func GetPluginFolder() (string, error)

GetPluginFolder tries to compute the user plugin folder for the current user. For windows: %APPDATA%/terraform.d/plugins For linux: ~/terraform.d/plugins

func InputValuesFromType

func InputValuesFromType(values map[string]cty.Value, sourceType terraform.ValueSourceType) terraform.InputValues

InputValuesFromType converts a map of values file into InputValues with the given SourceType

func IsEmptyCollection

func IsEmptyCollection(value cty.Value) bool

IsEmptyCollection returns true if value is a collection type of length 0

func IsNull

func IsNull(val cty.Value) bool

IsNull returns true if val is null or all its properties (recrusively) are null

func LoadSchemas

func LoadSchemas(opts *terraform.ContextOpts) (schemas *terraform.Schemas, diags tfdiags.Diagnostics)

LoadSchemas load all the schema required to parse hcl configs

func LookupProviderSchema

func LookupProviderSchema(schemas *terraform.Schemas, providerType string) (*terraform.ProviderSchema, error)

LookupProviderSchema searches for the schema matching the given type in the collection of known schemas

func MarshalValue

func MarshalValue(value cty.Value) []byte

MarshalValue serializes a cty.Value in hcl format

func Merge

func Merge(original, override cty.Value) cty.Value

Merge returns a new cty.Value whose attributes are set with values from override if present, or from original

func PrimitiveValue

func PrimitiveValue(val cty.Value) interface{}

PrimitiveValue will return the implied value if it's a primitive type Number will be returned as int If a non primitive is given, nil will be returned

func ProvidersMapFromConfig

func ProvidersMapFromConfig(cfg configs.Config, schema *terraform.Schemas) map[string]cty.Value

func ProvisionersFactory

func ProvisionersFactory() map[string]provisioners.Factory

ProvisionersFactory return a map of mock provisioners as provisioners are not meant to be called in the context of terraspec

Types

type Assert

type Assert struct {
	TypeName
	Value  cty.Value
	Return cty.Value
}

Assert struct contains the definition of an assertion

func NewAssert

func NewAssert(aType, aName string, aValue cty.Value, rValue cty.Value) *Assert

NewAssert is a convenient method to instanciate a new Assert struct with given parameters

func (*Assert) Key

func (a *Assert) Key() string

Key return fully qualified name of an Assert

type Context

type Context struct {
	TerraformVersion  *goversion.Version
	UserVersion       *goversion.Version
	ConfigureProvider bool
	WorkaroundOnce    sync.Once
}

Context struct holds terraspec options and internal state

type FakeResourceCreator

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

FakeResourceCreator can fake a resource creation by setting all resource attributes as defined in an assertion

func (*FakeResourceCreator) GetFake

func (f *FakeResourceCreator) GetFake(typeName string, parameters cty.Value) cty.Value

GetFake return the attributes to set to the resource terraspec fakes to create

func (*FakeResourceCreator) SetFakes

func (f *FakeResourceCreator) SetFakes(fakes []*Assert)

SetFakes populates fake data

type Mock

type Mock struct {
	TypeName
	Query         cty.Value
	Data          cty.Value
	Body          []byte
	ProviderAlias string
	// contains filtered or unexported fields
}

Mock struct contains the definition of mocked data resources

func NewMock

func NewMock(aType, aName, aProvider string, aQuery, aData cty.Value, aBody []byte) *Mock

NewMock is a convenient method to instanciate a new Mock struct with given parameters

func (*Mock) Call

func (m *Mock) Call() cty.Value

Call marks the mock as called and returns its data

func (*Mock) Called

func (m *Mock) Called() bool

Called indicates if mock was called at least once

type MockDataSourceReader

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

MockDataSourceReader can mock a call to ReadDataSource and return appropriate mocked data

func (*MockDataSourceReader) ReadDataSource

func (m *MockDataSourceReader) ReadDataSource(typeName string, config cty.Value, providerConfig cty.Value) cty.Value

ReadDataSource returns a mock response for the datasource call

func (*MockDataSourceReader) SetMock

func (m *MockDataSourceReader) SetMock(mocks []*Mock)

SetMock populates mock data

func (*MockDataSourceReader) SetProviderConfig

func (m *MockDataSourceReader) SetProviderConfig(providerConfigs map[string]cty.Value)

SetProviderConfig populates mock data

func (*MockDataSourceReader) UnmatchedCalls

func (m *MockDataSourceReader) UnmatchedCalls() []cty.Value

UnmatchedCalls returns the list of all data source calls that were not mocked

type MockProvisionerInterface

type MockProvisionerInterface struct {
}

MockProvisionerInterface is a NoOp implementation of terraform's provisioners.Interface

func (*MockProvisionerInterface) Close

func (p *MockProvisionerInterface) Close() error

Close shuts down the plugin process if applicable.

func (*MockProvisionerInterface) GetSchema

GetSchema returns the schema for the provisioner configuration.

func (*MockProvisionerInterface) ProvisionResource

ProvisionResource runs the provisioner with provided configuration. ProvisionResource blocks until the execution is complete. If the returned diagnostics contain any errors, the resource will be left in a tainted state.

func (*MockProvisionerInterface) Stop

func (p *MockProvisionerInterface) Stop() error

Stop is called to interrupt the provisioner.

Stop should not block waiting for in-flight actions to complete. It should take any action it wants and return immediately acknowledging it has received the stop request. Terraform will not make any further API calls to the provisioner after Stop is called.

The error returned, if non-nil, is assumed to mean that signaling the stop somehow failed and that the user should expect potentially waiting a longer period of time.

func (*MockProvisionerInterface) ValidateProvisionerConfig

ValidateProvisionerConfig allows the provisioner to validate the configuration values.

type ProviderInterface

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

ProviderInterface implements providers.Interface for the purpose of testing described config

func (*ProviderInterface) ApplyResourceChange

ApplyResourceChange takes the planned state for a resource, which may yet contain unknown computed values, and applies the changes returning the final state.

func (*ProviderInterface) Close

func (m *ProviderInterface) Close() error

Close shuts down the plugin process if applicable.

func (*ProviderInterface) Configure

Configure configures and initialized the provider.

func (*ProviderInterface) GetSchema

GetSchema returns the complete schema for the provider.

func (*ProviderInterface) ImportResourceState

ImportResourceState requests that the given resource be imported.

func (*ProviderInterface) PlanResourceChange

PlanResourceChange takes the current state and proposed state of a resource, and returns the planned final state.

func (*ProviderInterface) PrepareProviderConfig

PrepareProviderConfig allows the provider to validate the configuration values, and set or override any values with defaults.

func (*ProviderInterface) ReadDataSource

ReadDataSource returns the data source's current state.

func (*ProviderInterface) ReadResource

ReadResource refreshes a resource and returns its current state.

func (*ProviderInterface) Stop

func (m *ProviderInterface) Stop() error

Stop is called when the provider should halt any in-flight actions.

Stop should not block waiting for in-flight actions to complete. It should take any action it wants and return immediately acknowledging it has received the stop request. Terraform will not make any further API calls to the provider after Stop is called.

The error returned, if non-nil, is assumed to mean that signaling the stop somehow failed and that the user should expect potentially waiting a longer period of time.

func (*ProviderInterface) UpgradeResourceState

UpgradeResourceState is called when the state loader encounters an instance state whose schema version is less than the one reported by the currently-used version of the corresponding provider, and the upgraded result is used for any further processing.

func (*ProviderInterface) ValidateDataSourceConfig

ValidateDataSourceConfig allows the provider to validate the data source configuration values.

func (*ProviderInterface) ValidateResourceTypeConfig

ValidateResourceTypeConfig allows the provider to validate the resource configuration values.

type ProviderResolver

type ProviderResolver struct {
	KnownPlugins      map[addrs.Provider]discovery.PluginMeta
	DataSourceReader  *MockDataSourceReader
	ResourceCreator   *FakeResourceCreator
	ConfigureProvider bool
}

ProviderResolver is reponsible for finding all provider implementations that can be instanciated

func BuildProviderResolver

func BuildProviderResolver(dir string, configureProvider bool) (*ProviderResolver, error)

BuildProviderResolver returns a ProviderResolver able to find all providers provided by plugins

func (*ProviderResolver) ResolveProviders

func (r *ProviderResolver) ResolveProviders() map[addrs.Provider]providers.Factory

ResolveProviders returns a map of factory capable of instanciating the required plugin to serve the provider

type Spec

type Spec struct {
	Asserts          []*Assert
	Rejects          []*TypeName
	Mocks            []*Mock
	DataSourceReader *MockDataSourceReader
	Terraspec        *TerraspecConfig
}

Spec struct contains the assertions described in .tfspec file

func ParseSpec

func ParseSpec(spec []byte, filename string, schemas *terraform.Schemas) (*Spec, hcl.Diagnostics)

ParseSpec parses the spec contained in the []byte parameter and returns the resulting Spec or a Diagnostics if error occured in the process

func ReadSpec

func ReadSpec(filename string, schemas *terraform.Schemas) (*Spec, tfdiags.Diagnostics)

ReadSpec reads the .tfspec file and returns the resulting Spec or a Diagnostics if error occured in the process

func (*Spec) ResetMocks

func (s *Spec) ResetMocks()

ResetMocks reset state related to mock calls

func (*Spec) Validate

func (s *Spec) Validate(plan *plans.Plan) (tfdiags.Diagnostics, error)

Validate checks all the assertions of this Spec against the given terraform Plan. It return all failed assertion in a Diagnostics and an error if a technical error happened while testing the plan

func (*Spec) ValidateMocks

func (s *Spec) ValidateMocks() tfdiags.Diagnostics

ValidateMocks checks all mocks were called as expected

type TerraspecConfig

type TerraspecConfig struct {
	Workspace string
}

TerraspecConfig is a global element for a spec with common configuration similar to terraform hcl element.

type TerraspecDiagnostic

type TerraspecDiagnostic struct {
	tfdiags.Diagnostic
}

TerraspecDiagnostic is an assertion diagnostic, either a success or error

func AssertErrorDiags

func AssertErrorDiags(path cty.Path, expected, got interface{}) *TerraspecDiagnostic

AssertErrorDiags returns a diagnostic at Error level to indicate the user a given assertion failed

func ErrorDiags

func ErrorDiags(path cty.Path, detail string) *TerraspecDiagnostic

ErrorDiags returns a diagnostic at Error level with given error message

func RejectErrorDiags

func RejectErrorDiags(path cty.Path, rejected, got interface{}) *TerraspecDiagnostic

RejectErrorDiags returns a diagnostic at Error level to indicate the user a given reject assertion failed

func RejectSuccessDiags

func RejectSuccessDiags(path cty.Path, message string, rejected interface{}) *TerraspecDiagnostic

RejectSuccessDiags returns a diagnostic at Info level to indicate the user a given reject assertion succeeded

func RejectValueErrorDiags

func RejectValueErrorDiags(path cty.Path, key, rejected, got cty.Value) *TerraspecDiagnostic

RejectValueErrorDiags returns a diagnostic at Error level toi indicate the user a reject assertion failed

func SuccessDiags

func SuccessDiags(path cty.Path, value interface{}) *TerraspecDiagnostic

SuccessDiags creates a diagnostic at Info level to indicate the user a given assertion matches

type TypeName

type TypeName struct {
	Type string
	Name string
}

TypeName struct holds the type and name of an hcl block

func (*TypeName) Key

func (a *TypeName) Key() string

Key return fully qualified name

type WrappedProviderInterface

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

WrappedProviderInterface implements providers.Interface by wrapping a true provider.Interface and only call allowed method

func (*WrappedProviderInterface) ApplyResourceChange

ApplyResourceChange takes the planned state for a resource, which may yet contain unknown computed values, and applies the changes returning the final state.

func (*WrappedProviderInterface) Close

func (w *WrappedProviderInterface) Close() error

Close shuts down the plugin process if applicable.

func (*WrappedProviderInterface) Configure

Configure configures and initialized the provider.

func (*WrappedProviderInterface) GetSchema

GetSchema returns the complete schema for the provider.

func (*WrappedProviderInterface) ImportResourceState

ImportResourceState requests that the given resource be imported.

func (*WrappedProviderInterface) PlanResourceChange

PlanResourceChange takes the current state and proposed state of a resource, and returns the planned final state.

func (*WrappedProviderInterface) PrepareProviderConfig

PrepareProviderConfig allows the provider to validate the configuration values, and set or override any values with defaults.

func (*WrappedProviderInterface) ReadDataSource

ReadDataSource returns the data source's current state.

func (*WrappedProviderInterface) ReadResource

ReadResource refreshes a resource and returns its current state.

func (*WrappedProviderInterface) Stop

func (w *WrappedProviderInterface) Stop() error

Stop is called when the provider should halt any in-flight actions.

Stop should not block waiting for in-flight actions to complete. It should take any action it wants and return immediately acknowledging it has received the stop request. Terraform will not make any further API calls to the provider after Stop is called.

The error returned, if non-nil, is assumed to mean that signaling the stop somehow failed and that the user should expect potentially waiting a longer period of time.

func (*WrappedProviderInterface) UpgradeResourceState

UpgradeResourceState is called when the state loader encounters an instance state whose schema version is less than the one reported by the currently-used version of the corresponding provider, and the upgraded result is used for any further processing.

func (*WrappedProviderInterface) ValidateDataSourceConfig

ValidateDataSourceConfig allows the provider to validate the data source configuration values.

func (*WrappedProviderInterface) ValidateResourceTypeConfig

ValidateResourceTypeConfig allows the provider to validate the resource configuration values.

Jump to

Keyboard shortcuts

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