Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 2fb0442

Browse files
committed
Merge branch 'master' into go-1.15
2 parents e3636aa + 7260324 commit 2fb0442

File tree

90 files changed

+3741
-2939
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3741
-2939
lines changed

.github/workflows/go.yml

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ jobs:
2626
run: |
2727
go get -v -t -d ./...
2828
29+
- name: Check out terraform-provider-infracost@master
30+
uses: actions/checkout@v2
31+
with:
32+
repository: infracost/terraform-provider-infracost
33+
ref: refs/heads/master
34+
path: terraform-provider-infracost
35+
36+
- name: Install terraform-provider-infracost
37+
run: sudo make install
38+
working-directory: terraform-provider-infracost
39+
2940
- name: Run golangci-lint
3041
uses: golangci/[email protected]
3142
with:

Dockerfile

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ FROM golang:1.15 as builder
22

33
ARG ARCH=linux
44
ARG TERRAFORM_VERSION=0.12.25
5+
ARG TERRAFORM_PROVIDER_INFRACOST_VERSION=latest
56

67
# Set Environment Variables
78
SHELL ["/bin/bash", "-c"]
89
ENV HOME /app
910
ENV CGO_ENABLED 0
10-
ENV GOOS linux
1111

1212
# Install Packages
1313
RUN wget -q https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${ARCH}_amd64.zip
14-
RUN apt-get update && apt-get -y install zip -y
14+
RUN apt-get update && apt-get -y install zip jq -y
1515
RUN unzip terraform*.zip && \
1616
mv terraform /usr/local/bin && \
1717
chmod +x /usr/local/bin/terraform
1818

19-
# Build Application
2019
WORKDIR /app
20+
COPY scripts/install_provider.sh scripts/install_provider.sh
21+
RUN scripts/install_provider.sh ${TERRAFORM_PROVIDER_INFRACOST_VERSION} /usr/local/bin/
22+
23+
# Build Application
2124
COPY . .
2225
RUN make deps
2326
RUN make build
@@ -27,8 +30,8 @@ FROM alpine:3.12 as app
2730
ARG TERRAFORM_VERSION=0.12.25
2831
RUN apk --no-cache add ca-certificates terraform=${TERRAFORM_VERSION}-r0
2932
WORKDIR /root/
33+
COPY --from=builder /usr/local/bin/terraform-provider-infracost* /usr/bin/
3034
COPY --from=builder /app/build/infracost /usr/local/bin/infracost
3135
RUN chmod +x /usr/local/bin/infracost
32-
3336
ENTRYPOINT [ "infracost" ]
3437
CMD [ "--help" ]

Makefile

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
BINARY := infracost
22
ENTRYPOINT := cmd/infracost/main.go
3+
TERRAFORM_PROVIDER_INFRACOST_VERSION := latest
34

4-
.PHONY: deps run build windows linux darwin build_all clean test fmt lint
5+
ifndef $(GOOS)
6+
GOOS=$(shell go env GOOS)
7+
endif
8+
9+
ifndef $(GOARCH)
10+
GOARCH=$(shell go env GOARCH)
11+
endif
12+
13+
.PHONY: deps run build windows linux darwin build_all release install_provider clean test fmt lint
514

615
deps:
716
go mod download
@@ -28,10 +37,12 @@ release: build_all
2837
cd build; tar -czf $(BINARY)-linux-amd64.tar.gz $(BINARY)-linux-amd64
2938
cd build; tar -czf $(BINARY)-darwin-amd64.tar.gz $(BINARY)-darwin-amd64
3039

40+
install_provider:
41+
scripts/install_provider.sh $(TERRAFORM_PROVIDER_INFRACOST_VERSION)
42+
3143
clean:
3244
go clean
3345
rm -rf build/$(BINARY)*
34-
rm -rf release/$(BINARY)*
3546

3647
test:
3748
go test ./... $(or $(ARGS), -v -cover)

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ sudo mv /tmp/infracost-$(uname -s | tr '[:upper:]' '[:lower:]')-amd64 /usr/local
3131

3232
Generate a cost breakdown from a Terraform directory:
3333
```sh
34-
infracost --tfdir examples/small_terraform
34+
infracost --tfdir examples/terraform
3535
```
3636

3737
Check the [docs site](https://docs.infracost.io) for more details.
@@ -42,11 +42,16 @@ The [Infracost GitHub action](https://github.com/marketplace/actions/run-infraco
4242

4343
## Development
4444

45-
Install dependencies:
45+
Install Go dependencies:
4646
```sh
4747
make deps
4848
```
4949

50+
Install latest version of terraform-provider-infracost. If you want to use a local development version see [#using-a-local-version-of-terraform-provider-infracost](#using-a-local-version-of-terraform-provider-infracost)
51+
```sh
52+
make install_provider
53+
```
54+
5055
Run the code:
5156
```sh
5257
make run ARGS="--tfdir <Terraform Dir>"
@@ -67,6 +72,17 @@ Build:
6772
make build
6873
```
6974

75+
### Using a local version of terraform-provider-infracost
76+
77+
To use a local development version of terraform-provider-infracost
78+
79+
1. Fork/clone the [terraform-provider-infracost repository](https://github.com/infracost/terraform-provider-infracost)
80+
81+
2. Inside the directory that you cloned the repository run the following to install the local version in your `~/.terraform.d/plugins` directory:
82+
```sh
83+
make install
84+
```
85+
7086
## Contributing
7187

7288
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

cmd/infracost/main.go

+39-46
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"time"
67

8+
"infracost/internal/providers/terraform"
79
"infracost/pkg/config"
8-
"infracost/pkg/costs"
910
"infracost/pkg/output"
10-
"infracost/pkg/parsers/terraform"
11+
"infracost/pkg/prices"
12+
"infracost/pkg/schema"
1113

14+
"github.com/briandowns/spinner"
1215
"github.com/fatih/color"
1316
log "github.com/sirupsen/logrus"
1417
"github.com/urfave/cli/v2"
@@ -41,16 +44,15 @@ func main() {
4144
Usage: "Path to the Terraform project directory",
4245
TakesFile: true,
4346
},
44-
&cli.BoolFlag{
45-
Name: "verbose",
46-
Aliases: []string{"v"},
47-
Usage: "Verbosity",
48-
Value: false,
47+
&cli.StringFlag{
48+
Name: "log-level",
49+
Usage: "Log level (TRACE, DEBUG, INFO, WARN, ERROR)",
50+
Value: "WARN",
4951
},
5052
&cli.StringFlag{
5153
Name: "output",
5254
Aliases: []string{"o"},
53-
Usage: "Output (json|table)",
55+
Usage: "Output (json, table)",
5456
Value: "table",
5557
},
5658
&cli.BoolFlag{
@@ -70,79 +72,70 @@ func main() {
7072
return nil
7173
},
7274
Action: func(c *cli.Context) error {
73-
var planJSON []byte
74-
7575
if c.Bool("no-color") {
7676
config.Config.NoColor = true
7777
formatter.DisableColors = true
7878
color.NoColor = true
7979
}
8080

81-
logLevel := log.InfoLevel
82-
if c.Bool("verbose") {
83-
logLevel = log.DebugLevel
81+
if c.String("log-level") != "" {
82+
switch c.String("log-level") {
83+
case "TRACE":
84+
log.SetLevel(log.TraceLevel)
85+
case "DEBUG":
86+
log.SetLevel(log.DebugLevel)
87+
case "INFO":
88+
log.SetLevel(log.InfoLevel)
89+
case "WARN":
90+
log.SetLevel(log.WarnLevel)
91+
case "ERROR":
92+
log.SetLevel(log.ErrorLevel)
93+
}
8494
}
85-
log.SetLevel(logLevel)
8695

8796
if c.String("api-url") != "" {
8897
config.Config.ApiUrl = c.String("api-url")
8998
}
9099

91-
if c.String("tfjson") != "" && c.String("tfplan") != "" {
92-
color.HiRed("Please only provide one of either a Terraform Plan JSON file (tfjson) or a Terraform Plan file (tfplan)")
93-
_ = cli.ShowAppHelp(c)
94-
os.Exit(1)
95-
}
96-
97-
if c.String("tfplan") != "" && c.String("tfdir") == "" {
98-
color.HiRed("Please provide a path to the Terrafrom project (tfdir) if providing a Terraform Plan file (tfplan)\n\n")
99-
_ = cli.ShowAppHelp(c)
100-
os.Exit(1)
101-
}
102-
103-
if c.String("tfjson") == "" && c.String("tfdir") == "" {
104-
color.HiRed("Please provide either the path to the Terrafrom project (tfdir) or a Terraform Plan JSON file (tfjson)")
100+
provider := terraform.Provider()
101+
err := provider.ProcessArgs(c)
102+
if err != nil {
103+
color.HiRed(err.Error())
105104
_ = cli.ShowAppHelp(c)
106105
os.Exit(1)
107106
}
108107

109-
var err error
110-
if c.String("tfjson") != "" {
111-
planJSON, err = terraform.LoadPlanJSON(c.String("tfjson"))
112-
if err != nil {
113-
return err
114-
}
115-
} else {
116-
planFile := c.String("tfplan")
117-
planJSON, err = terraform.GeneratePlanJSON(c.String("tfdir"), planFile)
118-
if err != nil {
119-
return err
120-
}
108+
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
109+
if !c.Bool("no-color") {
110+
_ = s.Color("fgHiGreen", "bold")
121111
}
112+
s.Suffix = " Calculating costs…"
113+
s.Start()
122114

123-
resources, err := terraform.ParsePlanJSON(planJSON)
115+
resources, err := provider.LoadResources()
124116
if err != nil {
125117
return err
126118
}
127-
128-
q := costs.NewGraphQLQueryRunner(fmt.Sprintf("%s/graphql", config.Config.ApiUrl))
129-
resourceCostBreakdowns, err := costs.GenerateCostBreakdowns(q, resources)
119+
err = prices.PopulatePrices(resources)
130120
if err != nil {
131121
return err
132122
}
123+
schema.CalculateCosts(resources)
124+
schema.SortResources(resources)
133125

134126
var out []byte
135127
switch c.String("output") {
136128
case "table":
137-
out, err = output.ToTable(resourceCostBreakdowns)
129+
out, err = output.ToTable(resources)
138130
case "json":
139-
out, err = output.ToJSON(resourceCostBreakdowns)
131+
out, err = output.ToJSON(resources)
140132
default:
141133
err = cli.ShowAppHelp(c)
142134
}
143135
if err != nil {
144136
return err
145137
}
138+
s.Stop()
146139

147140
fmt.Println(string(out))
148141

examples/small_terraform/data.tf

-27
This file was deleted.

examples/small_terraform/main.tf

-30
This file was deleted.

examples/terraform/data.tf

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
# https://github.com/localstack/localstack could also be used to speed-up dev/test
2+
terraform {
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
}
7+
# TODO Enable once we have released terraform-provider-infracost
8+
# infracost = {
9+
# source = "infracost.io/infracost/infracost"
10+
# }
11+
}
12+
}
13+
214
provider "aws" {
315
region = "us-east-1"
416
s3_force_path_style = true
@@ -9,6 +21,9 @@ provider "aws" {
921
secret_key = "mock_secret_key"
1022
}
1123

24+
# TODO Enable once we have released terraform-provider-infracost
25+
# provider "infracost" {}
26+
1227
data "aws_region" "current" {}
1328

1429
variable "availability_zone_names" {

0 commit comments

Comments
 (0)