Skip to content

Commit

Permalink
Merge pull request #170 from overmindtech/164-fail-with-a-nice-error-…
Browse files Browse the repository at this point in the history
…when-pointed-at-a-statefile

164 fail with a nice error when pointed at a statefile
  • Loading branch information
dylanratcliffe authored Feb 9, 2024
2 parents a33d8dc + 37f7db8 commit a958d95
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ jobs:
contents: write
packages: write

env:
GOEXPERIMENT: loopvar

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ jobs:
runs-on: ubuntu-latest
env:
CGO_ENABLED: 0
GOEXPERIMENT: loopvar

steps:
- name: Checkout
Expand All @@ -29,8 +28,6 @@ jobs:
golangci:
name: lint
runs-on: ubuntu-latest
env:
GOEXPERIMENT: loopvar

steps:
- name: Checkout
Expand Down
24 changes: 24 additions & 0 deletions cmd/submitplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,25 @@ func (g *plannedChangeGroups) Add(typ string, item *sdp.MappedItemDiff) {
groups[typ] = append(list, item)
}

// Checks if the supplied JSON bytes are a state file. It's a common mistake to
// pass a state file to Overmind rather than a plan file since the commands to
// create them are similar
func isStateFile(bytes []byte) bool {
fields := make(map[string]interface{})

err := json.Unmarshal(bytes, &fields)

if err != nil {
return false
}

if _, exists := fields["values"]; exists {
return true
}

return false
}

func mappedItemDiffsFromPlan(ctx context.Context, fileName string, lf log.Fields) ([]*sdp.MappedItemDiff, error) {
// read results from `terraform show -json ${tfplan file}`
planJSON, err := os.ReadFile(fileName)
Expand All @@ -289,6 +308,11 @@ func mappedItemDiffsFromPlan(ctx context.Context, fileName string, lf log.Fields
return nil, err
}

// Check that we haven't been passed a state file
if isStateFile(planJSON) {
return nil, fmt.Errorf("'%v' appears to be a state file, not a plan file", fileName)
}

var plan Plan
err = json.Unmarshal(planJSON, &plan)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cmd/submitplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"github.com/stretchr/testify/require"
)

func TestWithStateFile(t *testing.T) {
_, err := mappedItemDiffsFromPlan(context.Background(), "testdata/state.json", logrus.Fields{})

if err == nil {
t.Error("Expected error when running with state file, got none")
}
}

func TestMappedItemDiffsFromPlan(t *testing.T) {
mappedItemDiffs, err := mappedItemDiffsFromPlan(context.Background(), "testdata/plan.json", logrus.Fields{})

Expand Down
11 changes: 11 additions & 0 deletions cmd/testdata/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"format_version": "1.0",
"terraform_version": "1.5.7",
"values": {
"outputs": {},
"root_module": {
"resources": [],
"child_modules": []
}
}
}

0 comments on commit a958d95

Please sign in to comment.