Skip to content

Commit

Permalink
Merge pull request #484 from hashicorp/laurenolivia/update-structs
Browse files Browse the repository at this point in the history
expose additional fields in state versions struct
  • Loading branch information
laurenolivia committed Aug 9, 2022
2 parents d5e64fd + fee64a9 commit aaa4bbb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
46 changes: 39 additions & 7 deletions state_version.go
Expand Up @@ -55,12 +55,18 @@ type StateVersionList struct {

// StateVersion represents a Terraform Enterprise state version.
type StateVersion struct {
ID string `jsonapi:"primary,state-versions"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
DownloadURL string `jsonapi:"attr,hosted-state-download-url"`
Serial int64 `jsonapi:"attr,serial"`
VCSCommitSHA string `jsonapi:"attr,vcs-commit-sha"`
VCSCommitURL string `jsonapi:"attr,vcs-commit-url"`
ID string `jsonapi:"primary,state-versions"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
DownloadURL string `jsonapi:"attr,hosted-state-download-url"`
Serial int64 `jsonapi:"attr,serial"`
VCSCommitSHA string `jsonapi:"attr,vcs-commit-sha"`
VCSCommitURL string `jsonapi:"attr,vcs-commit-url"`
ResourcesProcessed bool `jsonapi:"attr,resources-processed"`
StateVersion int `jsonapi:"attr,state-version"`
TerraformVersion string `jsonapi:"attr,terraform-version"`
Modules *StateVersionModules `jsonapi:"attr,modules"`
Providers *StateVersionProviders `jsonapi:"attr,providers"`
Resources []*StateVersionResources `jsonapi:"attr,resources"`

// Relations
Run *Run `jsonapi:"relation,run"`
Expand Down Expand Up @@ -139,7 +145,7 @@ type StateVersionCreateOptions struct {
// Optional: Specifies the run to associate the state with.
Run *Run `jsonapi:"relation,run,omitempty"`

// Optional: The external, json representation of state outputs, base64 encoded. Supplying this field
// Optional: The external, json representation of state outputs, base64 encoded. Supplying this field
// will provide more detailed output type information to TFE.
// For more information on the contents of this field: https://www.terraform.io/internals/json-format#values-representation
// about the current terraform state.
Expand All @@ -148,6 +154,32 @@ type StateVersionCreateOptions struct {
JSONStateOutputs *string `jsonapi:"attr,json-state-outputs,omitempty"`
}

type StateVersionModules struct {
Root StateVersionModuleRoot `jsonapi:"attr,root"`
}

type StateVersionModuleRoot struct {
NullResource int `jsonapi:"attr,null-resource"`
TerraformRemoteState int `jsonapi:"attr,data.terraform-remote-state"`
}

type StateVersionProviders struct {
Data ProviderData `jsonapi:"attr,provider[map]string"`
}

type ProviderData struct {
NullResource int `json:"null-resource"`
TerraformRemoteState int `json:"data.terraform-remote-state"`
}

type StateVersionResources struct {
Name string `jsonapi:"attr,name"`
Count string `jsonapi:"attr,count"`
Type int `jsonapi:"attr,type"`
Module string `jsonapi:"attr,module"`
Provider string `jsonapi:"attr,provider"`
}

// List all the state versions for a given workspace.
func (s *stateVersions) List(ctx context.Context, options *StateVersionListOptions) (*StateVersionList, error) {
if err := options.valid(); err != nil {
Expand Down
38 changes: 22 additions & 16 deletions state_version_integration_test.go
Expand Up @@ -20,15 +20,15 @@ func TestStateVersionsList(t *testing.T) {
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
t.Cleanup(orgTestCleanup)

wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
t.Cleanup(wTestCleanup)

svTest1, svTestCleanup1 := createStateVersion(t, client, 0, wTest)
defer svTestCleanup1()
t.Cleanup(svTestCleanup1)
svTest2, svTestCleanup2 := createStateVersion(t, client, 1, wTest)
defer svTestCleanup2()
t.Cleanup(svTestCleanup2)

t.Run("without StateVersionListOptions", func(t *testing.T) {
svl, err := client.StateVersions.List(ctx, nil)
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestStateVersionsCreate(t *testing.T) {
ctx := context.Background()

wTest, wTestCleanup := createWorkspace(t, client, nil)
defer wTestCleanup()
t.Cleanup(wTestCleanup)

state, err := ioutil.ReadFile("test-fixtures/state-version/terraform.tfstate")
if err != nil {
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestStateVersionsCreate(t *testing.T) {
t.Skip("This can only be tested with the run specific token")

rTest, rTestCleanup := createRun(t, client, wTest)
defer rTestCleanup()
t.Cleanup(rTestCleanup)

ctx := context.Background()
sv, err := client.StateVersions.Create(ctx, wTest.ID, StateVersionCreateOptions{
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestStateVersionsRead(t *testing.T) {
ctx := context.Background()

svTest, svTestCleanup := createStateVersion(t, client, 0, nil)
defer svTestCleanup()
t.Cleanup(svTestCleanup)

t.Run("when the state version exists", func(t *testing.T) {
sv, err := client.StateVersions.Read(ctx, svTest.ID)
Expand All @@ -333,6 +333,12 @@ func TestStateVersionsRead(t *testing.T) {
sv.Outputs = nil

assert.Equal(t, svTest, sv)
assert.NotEmpty(t, svTest, svTest.ResourcesProcessed)
assert.NotEmpty(t, svTest, svTest.StateVersion)
assert.NotEmpty(t, svTest, svTest.TerraformVersion)
assert.NotEmpty(t, svTest, svTest.Modules)
assert.NotEmpty(t, svTest, svTest.Providers)
assert.NotEmpty(t, svTest, svTest.Resources)
})

t.Run("when the state version does not exist", func(t *testing.T) {
Expand All @@ -353,7 +359,7 @@ func TestStateVersionsReadWithOptions(t *testing.T) {
ctx := context.Background()

svTest, svTestCleanup := createStateVersion(t, client, 0, nil)
defer svTestCleanup()
t.Cleanup(svTestCleanup)

// give TFC some time to process the statefile and extract the outputs.
waitForSVOutputs(t, client, svTest.ID)
Expand All @@ -375,13 +381,13 @@ func TestStateVersionsCurrent(t *testing.T) {
ctx := context.Background()

wTest1, wTest1Cleanup := createWorkspace(t, client, nil)
defer wTest1Cleanup()
t.Cleanup(wTest1Cleanup)

wTest2, wTest2Cleanup := createWorkspace(t, client, nil)
defer wTest2Cleanup()
t.Cleanup(wTest2Cleanup)

svTest, svTestCleanup := createStateVersion(t, client, 0, wTest1)
defer svTestCleanup()
t.Cleanup(svTestCleanup)

t.Run("when a state version exists", func(t *testing.T) {
sv, err := client.StateVersions.ReadCurrent(ctx, wTest1.ID)
Expand Down Expand Up @@ -418,10 +424,10 @@ func TestStateVersionsCurrentWithOptions(t *testing.T) {
ctx := context.Background()

wTest1, wTest1Cleanup := createWorkspace(t, client, nil)
defer wTest1Cleanup()
t.Cleanup(wTest1Cleanup)

svTest, svTestCleanup := createStateVersion(t, client, 0, wTest1)
defer svTestCleanup()
t.Cleanup(svTestCleanup)

// give TFC some time to process the statefile and extract the outputs.
waitForSVOutputs(t, client, svTest.ID)
Expand All @@ -443,7 +449,7 @@ func TestStateVersionsDownload(t *testing.T) {
ctx := context.Background()

svTest, svTestCleanup := createStateVersion(t, client, 0, nil)
defer svTestCleanup()
t.Cleanup(svTestCleanup)

stateTest, err := ioutil.ReadFile("test-fixtures/state-version/terraform.tfstate")
require.NoError(t, err)
Expand Down Expand Up @@ -475,10 +481,10 @@ func TestStateVersionOutputs(t *testing.T) {
ctx := context.Background()

wTest1, wTest1Cleanup := createWorkspace(t, client, nil)
defer wTest1Cleanup()
t.Cleanup(wTest1Cleanup)

sv, svTestCleanup := createStateVersion(t, client, 0, wTest1)
defer svTestCleanup()
t.Cleanup(svTestCleanup)

// give TFC some time to process the statefile and extract the outputs.
waitForSVOutputs(t, client, sv.ID)
Expand Down

0 comments on commit aaa4bbb

Please sign in to comment.