Skip to content

Commit

Permalink
provider: rearrange files to ease reading/addition
Browse files Browse the repository at this point in the history
The known providers are now put into a separated files, which helps with
adding new supported one to the default list.
  • Loading branch information
Hugo Rosnet committed Aug 30, 2021
1 parent b140b76 commit dcca7a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
11 changes: 3 additions & 8 deletions estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/afero"

"github.com/cycloidio/terracost/aws"
"github.com/cycloidio/terracost/cost"
"github.com/cycloidio/terracost/terraform"
)
Expand All @@ -16,9 +15,7 @@ import (
// It uses the Backend to retrieve the pricing data.
func EstimateTerraformPlan(ctx context.Context, backend Backend, plan io.Reader, providerInitializers ...terraform.ProviderInitializer) (*cost.Plan, error) {
if len(providerInitializers) == 0 {
providerInitializers = []terraform.ProviderInitializer{
aws.TerraformProviderInitializer,
}
providerInitializers = getDefaultProviders()
}

tfplan := terraform.NewPlan(providerInitializers...)
Expand All @@ -27,7 +24,7 @@ func EstimateTerraformPlan(ctx context.Context, backend Backend, plan io.Reader,
}

priorQueries, err := tfplan.ExtractPriorQueries()
if err != nil && err != terraform.ErrNoQueries {
if err != nil {
return nil, err
}
prior, err := cost.NewState(ctx, backend, priorQueries)
Expand All @@ -52,9 +49,7 @@ func EstimateTerraformPlan(ctx context.Context, backend Backend, plan io.Reader,
// It uses the Backend to retrieve the pricing data.
func EstimateHCL(ctx context.Context, backend Backend, fs afero.Fs, path string, providerInitializers ...terraform.ProviderInitializer) (*cost.Plan, error) {
if len(providerInitializers) == 0 {
providerInitializers = []terraform.ProviderInitializer{
aws.TerraformProviderInitializer,
}
providerInitializers = getDefaultProviders()
}

plannedQueries, err := terraform.ExtractQueriesFromHCL(fs, providerInitializers, path)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
Expand Down Expand Up @@ -679,6 +680,7 @@ golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
golang.org/x/tools v0.0.0-20201028111035-eafbe7b904eb h1:KVWk3RW1AZlxWum4tYqegLgwJHb5oouozcGM8HfNQaw=
golang.org/x/tools v0.0.0-20201028111035-eafbe7b904eb/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
16 changes: 16 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package terracost

import (
"github.com/cycloidio/terracost/aws"
"github.com/cycloidio/terracost/terraform"
)

// defaultProviders are the currently known and supported terraform providers
var defaultProviders = []terraform.ProviderInitializer{
aws.TerraformProviderInitializer,
}

// getDefaultProviders will return the default supported providers of terracost
func getDefaultProviders() []terraform.ProviderInitializer {
return defaultProviders
}

0 comments on commit dcca7a5

Please sign in to comment.