diff --git a/state_version.go b/state_version.go index 39173aafb..587286eb7 100644 --- a/state_version.go +++ b/state_version.go @@ -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"` @@ -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. @@ -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 { diff --git a/state_version_integration_test.go b/state_version_integration_test.go index 37cf688ae..09b6144cb 100644 --- a/state_version_integration_test.go +++ b/state_version_integration_test.go @@ -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) @@ -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 { @@ -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{ @@ -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) @@ -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) { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)