-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1563 from snyk/feat/tfstate-discovery-tfc
State discovery for Terraform cloud
- Loading branch information
Showing
8 changed files
with
155 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package hcl | ||
|
||
import ( | ||
"path" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/snyk/driftctl/pkg/iac/config" | ||
"github.com/snyk/driftctl/pkg/iac/terraform/state" | ||
"github.com/snyk/driftctl/pkg/iac/terraform/state/backend" | ||
) | ||
|
||
type CloudWorkspacesBlock struct { | ||
Name string `hcl:"name,optional"` | ||
Tags []string `hcl:"tags,optional"` | ||
Remain hcl.Body `hcl:",remain"` | ||
} | ||
|
||
type CloudBlock struct { | ||
Organization string `hcl:"organization"` | ||
Workspaces CloudWorkspacesBlock `hcl:"workspaces,block"` | ||
Remain hcl.Body `hcl:",remain"` | ||
} | ||
|
||
func (c CloudBlock) SupplierConfig(workspace string) *config.SupplierConfig { | ||
// If a workspace is specified in HCL, use it rather than the current environment | ||
if c.Workspaces.Name != "" { | ||
workspace = c.Workspaces.Name | ||
} | ||
return &config.SupplierConfig{ | ||
Key: state.TerraformStateReaderSupplier, | ||
Backend: backend.BackendKeyTFCloud, | ||
Path: path.Join(c.Organization, workspace), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package hcl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/snyk/driftctl/pkg/iac/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCloud_SupplierConfig(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
filename string | ||
want *config.SupplierConfig | ||
wantErr string | ||
}{ | ||
{ | ||
name: "test with cloud block and default workspace", | ||
filename: "testdata/cloud_block.tf", | ||
want: &config.SupplierConfig{ | ||
Key: "tfstate", | ||
Backend: "tfcloud", | ||
Path: "example_corp/default", | ||
}, | ||
}, | ||
{ | ||
name: "test with cloud block and non-default workspace", | ||
filename: "testdata/cloud_block_workspace.tf", | ||
want: &config.SupplierConfig{ | ||
Key: "tfstate", | ||
Backend: "tfcloud", | ||
Path: "example_corp/my-workspace", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
hcl, err := ParseTerraformFromHCL(tt.filename) | ||
if tt.wantErr == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tt.wantErr) | ||
return | ||
} | ||
|
||
if hcl.Cloud == nil { | ||
assert.Nil(t, tt.want) | ||
return | ||
} | ||
|
||
assert.Equal(t, *tt.want, *hcl.Cloud.SupplierConfig("default")) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package hcl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHCL_getCurrentWorkspaceName(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
dir string | ||
want string | ||
}{ | ||
{ | ||
name: "test with non-default workspace", | ||
dir: "testdata/foo_workspace", | ||
want: "foo", | ||
}, | ||
{ | ||
name: "test with non-existing directory", | ||
dir: "testdata/noenvfile", | ||
want: "default", | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
workspace := GetCurrentWorkspaceName(tt.dir) | ||
assert.Equal(t, tt.want, workspace) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
terraform { | ||
cloud { | ||
organization = "example_corp" | ||
hostname = "app.terraform.io" # Optional; defaults to app.terraform.io | ||
|
||
workspaces {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
terraform { | ||
cloud { | ||
organization = "example_corp" | ||
hostname = "app.terraform.io" # Optional; defaults to app.terraform.io | ||
|
||
workspaces { | ||
name = "my-workspace" | ||
} | ||
} | ||
} |