Skip to content

Commit

Permalink
refactor: imported all necessary internal terraform libs under intern…
Browse files Browse the repository at this point in the history
…al/terraform/ (#184)
  • Loading branch information
hbollon committed Jul 8, 2021
1 parent f632586 commit 0db9033
Show file tree
Hide file tree
Showing 494 changed files with 49,208 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Expand Up @@ -5,6 +5,10 @@ run:
# Disable tests to avoid redundancy with test workflow
tests: false

# Dirs to ignore
skip-dirs:
- internal/terraform

issues:
# Maximum issues count per one linter
max-issues-per-linter: 0
Expand Down
6 changes: 3 additions & 3 deletions db/db.go
Expand Up @@ -9,11 +9,11 @@ import (
"strings"

"github.com/camptocamp/terraboard/config"
"github.com/camptocamp/terraboard/internal/terraform/addrs"
"github.com/camptocamp/terraboard/internal/terraform/states"
"github.com/camptocamp/terraboard/internal/terraform/states/statefile"
"github.com/camptocamp/terraboard/state"
"github.com/camptocamp/terraboard/types"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statefile"
log "github.com/sirupsen/logrus"

ctyJson "github.com/zclconf/go-cty/cty/json"
Expand Down
28 changes: 22 additions & 6 deletions go.mod
Expand Up @@ -5,25 +5,41 @@ go 1.15
require (
cloud.google.com/go v0.76.0 // indirect
cloud.google.com/go/storage v1.12.0
github.com/agext/levenshtein v1.2.3 // indirect
github.com/agext/levenshtein v1.2.3
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0
github.com/apparentlymart/go-versions v1.0.1
github.com/aws/aws-sdk-go v1.37.2
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/go-test/deep v1.0.3
github.com/google/go-cmp v0.5.4
github.com/hashicorp/errwrap v1.1.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-hclog v0.15.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/hashicorp/go-slug v0.6.0 // indirect
github.com/hashicorp/go-tfe v0.12.0
github.com/hashicorp/go-uuid v1.0.1
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl/v2 v2.10.0
github.com/hashicorp/terraform v0.15.0
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jessevdk/go-flags v1.4.0
github.com/lib/pq v1.9.0 // indirect
github.com/machinebox/graphql v0.2.2
github.com/matryer/is v1.4.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/copystructure v1.2.0
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/panicwrap v1.0.0
github.com/pmezard/go-difflib v1.0.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/afero v1.5.1 // indirect
github.com/zclconf/go-cty v1.8.1
github.com/spf13/afero v1.2.2
github.com/zclconf/go-cty v1.8.4
go.opencensus.io v0.22.6 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/mod v0.4.1
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/oauth2 v0.0.0-20210201163806-010130855d6c // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Expand Up @@ -371,6 +371,8 @@ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.9.1 h1:eOy4gREY0/ZQHNItlfuEZqtcQbXIxzojlP301hDpnac=
github.com/hashicorp/hcl/v2 v2.9.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.10.0 h1:1S1UnuhDGlv3gRFV4+0EdwB+znNP5HmcGbIqwnSCByg=
github.com/hashicorp/hcl/v2 v2.10.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.0/go.mod h1:ncdBp14cuox2iFOq3kDiquKU6fqsTBc3W6JvZwjxxsE=
Expand Down Expand Up @@ -667,6 +669,7 @@ github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a/go.mod h1:X
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.5.1 h1:VHu76Lk0LSP1x254maIu2bplkWpfBWI+B+6fdoZprcg=
github.com/spf13/afero v1.5.1/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down Expand Up @@ -716,6 +719,8 @@ github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.8.1 h1:SI0LqNeNxAgv2WWqWJMlG2/Ad/6aYJ7IVYYMigmfkuI=
github.com/zclconf/go-cty v1.8.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.8.4 h1:pwhhz5P+Fjxse7S7UriBrMu6AUJSZM5pKqGem1PjGAs=
github.com/zclconf/go-cty v1.8.4/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
github.com/zclconf/go-cty-yaml v1.0.2/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
Expand Down
12 changes: 12 additions & 0 deletions internal/terraform/addrs/count_attr.go
@@ -0,0 +1,12 @@
package addrs

// CountAttr is the address of an attribute of the "count" object in
// the interpolation scope, like "count.index".
type CountAttr struct {
referenceable
Name string
}

func (ca CountAttr) String() string {
return "count." + ca.Name
}
17 changes: 17 additions & 0 deletions internal/terraform/addrs/doc.go
@@ -0,0 +1,17 @@
// Package addrs contains types that represent "addresses", which are
// references to specific objects within a Terraform configuration or
// state.
//
// All addresses have string representations based on HCL traversal syntax
// which should be used in the user-interface, and also in-memory
// representations that can be used internally.
//
// For object types that exist within Terraform modules a pair of types is
// used. The "local" part of the address is represented by a type, and then
// an absolute path to that object in the context of its module is represented
// by a type of the same name with an "Abs" prefix added, for "absolute".
//
// All types within this package should be treated as immutable, even if this
// is not enforced by the Go compiler. It is always an implementation error
// to modify an address object in-place after it is initially constructed.
package addrs
12 changes: 12 additions & 0 deletions internal/terraform/addrs/for_each_attr.go
@@ -0,0 +1,12 @@
package addrs

// ForEachAttr is the address of an attribute referencing the current "for_each" object in
// the interpolation scope, addressed using the "each" keyword, ex. "each.key" and "each.value"
type ForEachAttr struct {
referenceable
Name string
}

func (f ForEachAttr) String() string {
return "each." + f.Name
}
50 changes: 50 additions & 0 deletions internal/terraform/addrs/input_variable.go
@@ -0,0 +1,50 @@
package addrs

import (
"fmt"
)

// InputVariable is the address of an input variable.
type InputVariable struct {
referenceable
Name string
}

func (v InputVariable) String() string {
return "var." + v.Name
}

// Absolute converts the receiver into an absolute address within the given
// module instance.
func (v InputVariable) Absolute(m ModuleInstance) AbsInputVariableInstance {
return AbsInputVariableInstance{
Module: m,
Variable: v,
}
}

// AbsInputVariableInstance is the address of an input variable within a
// particular module instance.
type AbsInputVariableInstance struct {
Module ModuleInstance
Variable InputVariable
}

// InputVariable returns the absolute address of the input variable of the
// given name inside the receiving module instance.
func (m ModuleInstance) InputVariable(name string) AbsInputVariableInstance {
return AbsInputVariableInstance{
Module: m,
Variable: InputVariable{
Name: name,
},
}
}

func (v AbsInputVariableInstance) String() string {
if len(v.Module) == 0 {
return v.Variable.String()
}

return fmt.Sprintf("%s.%s", v.Module.String(), v.Variable.String())
}
135 changes: 135 additions & 0 deletions internal/terraform/addrs/instance_key.go
@@ -0,0 +1,135 @@
package addrs

import (
"fmt"

"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
)

// InstanceKey represents the key of an instance within an object that
// contains multiple instances due to using "count" or "for_each" arguments
// in configuration.
//
// IntKey and StringKey are the two implementations of this type. No other
// implementations are allowed. The single instance of an object that _isn't_
// using "count" or "for_each" is represented by NoKey, which is a nil
// InstanceKey.
type InstanceKey interface {
instanceKeySigil()
String() string

// Value returns the cty.Value of the appropriate type for the InstanceKey
// value.
Value() cty.Value
}

// ParseInstanceKey returns the instance key corresponding to the given value,
// which must be known and non-null.
//
// If an unknown or null value is provided then this function will panic. This
// function is intended to deal with the values that would naturally be found
// in a hcl.TraverseIndex, which (when parsed from source, at least) can never
// contain unknown or null values.
func ParseInstanceKey(key cty.Value) (InstanceKey, error) {
switch key.Type() {
case cty.String:
return StringKey(key.AsString()), nil
case cty.Number:
var idx int
err := gocty.FromCtyValue(key, &idx)
return IntKey(idx), err
default:
return NoKey, fmt.Errorf("either a string or an integer is required")
}
}

// NoKey represents the absense of an InstanceKey, for the single instance
// of a configuration object that does not use "count" or "for_each" at all.
var NoKey InstanceKey

// IntKey is the InstanceKey representation representing integer indices, as
// used when the "count" argument is specified or if for_each is used with
// a sequence type.
type IntKey int

func (k IntKey) instanceKeySigil() {
}

func (k IntKey) String() string {
return fmt.Sprintf("[%d]", int(k))
}

func (k IntKey) Value() cty.Value {
return cty.NumberIntVal(int64(k))
}

// StringKey is the InstanceKey representation representing string indices, as
// used when the "for_each" argument is specified with a map or object type.
type StringKey string

func (k StringKey) instanceKeySigil() {
}

func (k StringKey) String() string {
// FIXME: This isn't _quite_ right because Go's quoted string syntax is
// slightly different than HCL's, but we'll accept it for now.
return fmt.Sprintf("[%q]", string(k))
}

func (k StringKey) Value() cty.Value {
return cty.StringVal(string(k))
}

// InstanceKeyLess returns true if the first given instance key i should sort
// before the second key j, and false otherwise.
func InstanceKeyLess(i, j InstanceKey) bool {
iTy := instanceKeyType(i)
jTy := instanceKeyType(j)

switch {
case i == j:
return false
case i == NoKey:
return true
case j == NoKey:
return false
case iTy != jTy:
// The ordering here is arbitrary except that we want NoKeyType
// to sort before the others, so we'll just use the enum values
// of InstanceKeyType here (where NoKey is zero, sorting before
// any other).
return uint32(iTy) < uint32(jTy)
case iTy == IntKeyType:
return int(i.(IntKey)) < int(j.(IntKey))
case iTy == StringKeyType:
return string(i.(StringKey)) < string(j.(StringKey))
default:
// Shouldn't be possible to get down here in practice, since the
// above is exhaustive.
return false
}
}

func instanceKeyType(k InstanceKey) InstanceKeyType {
if _, ok := k.(StringKey); ok {
return StringKeyType
}
if _, ok := k.(IntKey); ok {
return IntKeyType
}
return NoKeyType
}

// InstanceKeyType represents the different types of instance key that are
// supported. Usually it is sufficient to simply type-assert an InstanceKey
// value to either IntKey or StringKey, but this type and its values can be
// used to represent the types themselves, rather than specific values
// of those types.
type InstanceKeyType rune

const (
NoKeyType InstanceKeyType = 0
IntKeyType InstanceKeyType = 'I'
StringKeyType InstanceKeyType = 'S'
)
48 changes: 48 additions & 0 deletions internal/terraform/addrs/local_value.go
@@ -0,0 +1,48 @@
package addrs

import (
"fmt"
)

// LocalValue is the address of a local value.
type LocalValue struct {
referenceable
Name string
}

func (v LocalValue) String() string {
return "local." + v.Name
}

// Absolute converts the receiver into an absolute address within the given
// module instance.
func (v LocalValue) Absolute(m ModuleInstance) AbsLocalValue {
return AbsLocalValue{
Module: m,
LocalValue: v,
}
}

// AbsLocalValue is the absolute address of a local value within a module instance.
type AbsLocalValue struct {
Module ModuleInstance
LocalValue LocalValue
}

// LocalValue returns the absolute address of a local value of the given
// name within the receiving module instance.
func (m ModuleInstance) LocalValue(name string) AbsLocalValue {
return AbsLocalValue{
Module: m,
LocalValue: LocalValue{
Name: name,
},
}
}

func (v AbsLocalValue) String() string {
if len(v.Module) == 0 {
return v.LocalValue.String()
}
return fmt.Sprintf("%s.%s", v.Module.String(), v.LocalValue.String())
}

0 comments on commit 0db9033

Please sign in to comment.