From dcca7a597674b4cf740671593ab68950310d59ea Mon Sep 17 00:00:00 2001 From: Hugo Rosnet Date: Thu, 12 Aug 2021 11:01:49 +0200 Subject: [PATCH] provider: rearrange files to ease reading/addition The known providers are now put into a separated files, which helps with adding new supported one to the default list. --- estimation.go | 11 +++-------- go.sum | 2 ++ provider.go | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 provider.go diff --git a/estimation.go b/estimation.go index ab6e158..f47580e 100644 --- a/estimation.go +++ b/estimation.go @@ -6,7 +6,6 @@ import ( "github.com/spf13/afero" - "github.com/cycloidio/terracost/aws" "github.com/cycloidio/terracost/cost" "github.com/cycloidio/terracost/terraform" ) @@ -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...) @@ -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) @@ -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) diff --git a/go.sum b/go.sum index d605d9c..8053258 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/provider.go b/provider.go new file mode 100644 index 0000000..4e871b2 --- /dev/null +++ b/provider.go @@ -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 +}