Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose additional fields in state versions struct #484

Merged
merged 16 commits into from Aug 9, 2022
Merged
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"`
Comment on lines +64 to +69
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandonc I can confirm the new attributes are exposed in the response body. Is that sufficient? Do you have any suggestions?

{
   "data": {
      "id": "sv-DYXpEz7SQBrRJEHV",
      "type": "state-versions",
      "attributes": {
         "created-at": "2022-07-06T17:54:23.254Z",
         "size": 699,
         "hosted-state-download-url": "https://tfcdev-2c13224a.ngrok.io/_archivist/v1/object/dmF1bHQ6djE6MkpXVGFwQmhqWnRheGdGZEpjUVV4UGFCRFhyVm9nTTV3a044SVF3eldHTGtFVHNpSXpYOVRBWHRNNkU0SVlxMVVObjZPajRId0drUGlXN0N4dTdxRmpTeGV6Yk56SmtIUHRCbVJsN3pGNGhpSWswMnNJUWJ4RDhMcEdnMERwZk5ycFRpemhaVWlyVmZXWmZsYkJxYnVQU0tpOGJrOTYvL21NOWswU1ZQRXkxeWF2WTBxTElJK0gzTlBYYmRpbThsUlpxeDZldUVJQ1FYMEN3VHJXS1d4cmVqRzMrT2xHWVl4NktYWlNpTStTWWhzZ2lDYXExMGZLSjN4ekxJVWNYdjVwaDJtM1hCYWlDUEUzNGtaV0luSDZwbTVzZmcxNG5ZQWVQVkJHZzhUWjFBQk1KRFFrd3BDY0dya3ZjRlM4UktITmpKNjlEMG01NXI1QTJtT3dtd3JCMFlwek50bzY5bXkrMnk1V1FPMW9TM2t1c3BITFpocmNMaFNlOTJ4VjVOVmlhSTVKbnVjdHlNTERaU0gxbkRpMWpCTy9XSlNYQT0",
         "modules": {
            "root": {
               "random_pet": 3
            }
         },
         "providers": {
            "provider[\"registry.terraform.io/hashicorp/random\"]": {
               "random_pet": 3
            }
         },
         "resources": [
            {
               "name": "animal_trio",
               "type": "random_pet",
               "count": 3,
               "module": "root",
               "provider": "provider[\"registry.terraform.io/hashicorp/random\"]"
            }
         ],
         "resources-processed": true,
         "serial": 0,
         "state-version": 4,
         "terraform-version": "1.2.4",
         "vcs-commit-url": null,
         "vcs-commit-sha": null
      },
      "relationships": {
         "run": {
            "data": {
               "id": "run-T3EUTgV3yzSR91GE",
               "type": "runs"
            }
         },
         "next-state-version": {
            "data": null
         },
         "previous-state-version": {
            "data": null
         },
         "created-by": {
            "data": {
               "id": "user-wJVRjZegtRxpexoa",
               "type": "users"
            },
            "links": {
               "self": "/api/v2/users/user-wJVRjZegtRxpexoa",
               "related": "/api/v2/runs/run-T3EUTgV3yzSR91GE/created-by"
            }
         },
         "workspace": {
            "data": {
               "id": "ws-LRczoXxb9a3XSmLh",
               "type": "workspaces"
            }
         },
         "outputs": {
            "data": [
               {
                  "id": "wsout-cJjt3xxikeKDGA1g",
                  "type": "state-version-outputs"
               }
            ],
            "links": {
               "related": "/api/v2/state-versions/sv-DYXpEz7SQBrRJEHV/outputs"
            }
         }
      },
      "links": {
         "self": "/api/v2/state-versions/sv-DYXpEz7SQBrRJEHV"
      }
   }
}


// 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