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

Upload pending StateVersion #717

Merged
merged 3 commits into from
Jun 15, 2023

Conversation

brandonc
Copy link
Collaborator

@brandonc brandonc commented Jun 8, 2023

Description

Adds pending StateVersion support to the model as well as a new method Upload which creates and uploads and re-fetches a state version.

Refactor Notes

This change alters the behavior of requests that are made directly to IPRanges endpoint and the object store for ConfigurationVersions, RegistryModels, PolicySetVersions, (and now StateVersions). Previously, we were reusing the normal request methods for these, which unnecessarily sent an Authorization header, and additionally attempted to use jsonapi decoding (with the exception of IPRanges, which seems to have fundamentally broken error handling)

I added a new Client method, doForeignPUTRequest, which does an object-store-style application/octet-stream request with optional JSON response handling. The "foreign" part of the name reflects the fact that we aren't making API requests that have standard API features like Authorization with tokens and rate limiting.

External links

Output from tests

Including output from tests may require access to a TFE instance. Ignore this section if you have no environment to test against.

$ ENABLE_BETA=1 go test ./... -run TestStateVersions -v
=== RUN   TestStateVersionsList
=== RUN   TestStateVersionsList/without_StateVersionListOptions
=== RUN   TestStateVersionsList/without_list_options
=== RUN   TestStateVersionsList/with_list_options
=== RUN   TestStateVersionsList/without_an_organization
=== RUN   TestStateVersionsList/without_a_workspace
--- PASS: TestStateVersionsList (21.07s)
    --- PASS: TestStateVersionsList/without_StateVersionListOptions (0.00s)
    --- PASS: TestStateVersionsList/without_list_options (1.43s)
    --- PASS: TestStateVersionsList/with_list_options (1.51s)
    --- PASS: TestStateVersionsList/without_an_organization (0.00s)
    --- PASS: TestStateVersionsList/without_a_workspace (0.00s)
=== RUN   TestStateVersionsUpload
=== RUN   TestStateVersionsUpload/can_create_finalized_state_versions
=== RUN   TestStateVersionsUpload/cannot_provide_base64_state_parameter_when_uploading
=== RUN   TestStateVersionsUpload/RawState_parameter_is_required_when_uploading
--- PASS: TestStateVersionsUpload (16.26s)
    --- PASS: TestStateVersionsUpload/can_create_finalized_state_versions (6.47s)
    --- PASS: TestStateVersionsUpload/cannot_provide_base64_state_parameter_when_uploading (0.00s)
    --- PASS: TestStateVersionsUpload/RawState_parameter_is_required_when_uploading (0.00s)
=== RUN   TestStateVersionsCreate
=== RUN   TestStateVersionsCreate/can_create_pending_state_versions
=== RUN   TestStateVersionsCreate/with_valid_options
=== RUN   TestStateVersionsCreate/with_external_state_representation
=== RUN   TestStateVersionsCreate/with_the_force_flag_set
=== RUN   TestStateVersionsCreate/with_a_run_to_associate_with
    state_version_integration_test.go:348: This can only be tested with the run specific token
=== RUN   TestStateVersionsCreate/without_md5_hash
=== RUN   TestStateVersionsCreate/without_serial
=== RUN   TestStateVersionsCreate/with_invalid_workspace_id
--- PASS: TestStateVersionsCreate (37.53s)
    --- PASS: TestStateVersionsCreate/can_create_pending_state_versions (4.30s)
    --- PASS: TestStateVersionsCreate/with_valid_options (5.99s)
    --- PASS: TestStateVersionsCreate/with_external_state_representation (7.38s)
    --- PASS: TestStateVersionsCreate/with_the_force_flag_set (8.48s)
    --- SKIP: TestStateVersionsCreate/with_a_run_to_associate_with (0.00s)
    --- PASS: TestStateVersionsCreate/without_md5_hash (0.00s)
    --- PASS: TestStateVersionsCreate/without_serial (0.00s)
    --- PASS: TestStateVersionsCreate/with_invalid_workspace_id (0.00s)
=== RUN   TestStateVersionsRead
=== RUN   TestStateVersionsRead/when_the_state_version_exists
=== RUN   TestStateVersionsRead/when_the_state_version_does_not_exist
=== RUN   TestStateVersionsRead/with_invalid_state_version_id
--- PASS: TestStateVersionsRead (20.17s)
    --- PASS: TestStateVersionsRead/when_the_state_version_exists (5.68s)
    --- PASS: TestStateVersionsRead/when_the_state_version_does_not_exist (1.26s)
    --- PASS: TestStateVersionsRead/with_invalid_state_version_id (0.00s)
=== RUN   TestStateVersionsReadWithOptions
=== RUN   TestStateVersionsReadWithOptions/when_the_state_version_exists
--- PASS: TestStateVersionsReadWithOptions (19.12s)
    --- PASS: TestStateVersionsReadWithOptions/when_the_state_version_exists (1.25s)
=== RUN   TestStateVersionsCurrent
=== RUN   TestStateVersionsCurrent/when_a_state_version_exists
=== RUN   TestStateVersionsCurrent/when_a_state_version_does_not_exist
=== RUN   TestStateVersionsCurrent/with_invalid_workspace_id
--- PASS: TestStateVersionsCurrent (24.83s)
    --- PASS: TestStateVersionsCurrent/when_a_state_version_exists (1.50s)
    --- PASS: TestStateVersionsCurrent/when_a_state_version_does_not_exist (1.43s)
    --- PASS: TestStateVersionsCurrent/with_invalid_workspace_id (0.00s)
=== RUN   TestStateVersionsCurrentWithOptions
=== RUN   TestStateVersionsCurrentWithOptions/when_the_state_version_exists
--- PASS: TestStateVersionsCurrentWithOptions (19.65s)
    --- PASS: TestStateVersionsCurrentWithOptions/when_the_state_version_exists (1.37s)
=== RUN   TestStateVersionsDownload
=== RUN   TestStateVersionsDownload/when_the_state_version_exists
=== RUN   TestStateVersionsDownload/with_an_invalid_url
--- PASS: TestStateVersionsDownload (17.36s)
    --- PASS: TestStateVersionsDownload/when_the_state_version_exists (0.10s)
    --- PASS: TestStateVersionsDownload/with_an_invalid_url (1.59s)
PASS
ok  	github.com/hashicorp/go-tfe	176.768s
...

@brandonc brandonc force-pushed the brandonc/allow_state_version_create_without_state branch 2 times, most recently from 669a681 to d0e9758 Compare June 13, 2023 23:48
@brandonc brandonc marked this pull request as ready for review June 13, 2023 23:50
@brandonc brandonc requested a review from a team as a code owner June 13, 2023 23:50
@brandonc brandonc changed the title WIP Upload pending StateVersion Jun 13, 2023
@brandonc brandonc force-pushed the brandonc/allow_state_version_create_without_state branch from d0e9758 to 2b188a3 Compare June 14, 2023 17:00
Copy link
Contributor

@sebasslash sebasslash left a comment

Choose a reason for hiding this comment

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

Requesting changes on minor stuff. Otherwise, looks 👍

state_version.go Outdated
Comment on lines 276 to 284
func (s *stateVersions) putURL(ctx context.Context, putURL string, data []byte) error {
reader := bytes.NewReader(data)
req, err := s.client.NewRequest("PUT", putURL, reader)
if err != nil {
return err
}

return req.Do(ctx, nil)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This code is duplicated in UploadTarGzip() so I think this should belong as a Client receiver method:

// or don't export this
func (c *Client) PutArchivistURL(ctx context.Context, archivistURL string, reader io.Reader) error {
  req, err := c.NewRequest("PUT", archivistURL, reader)
  // error handling omitted
  return req.Do(ctx, nil)
}

It would eliminate the code duplication with UploadTarGzip() in RegistryModules, ConfigurationVersions and Policies

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK. I don't think it should be exported but definitely shared.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This turned into an adventure! See my notes in the PR description under "Refactor Notes"

state_version.go Outdated Show resolved Hide resolved
state_version.go Show resolved Hide resolved
examples/state_versions/main.go Show resolved Hide resolved
@brandonc brandonc force-pushed the brandonc/allow_state_version_create_without_state branch 2 times, most recently from 11da9a3 to 729c2de Compare June 14, 2023 23:39
I added response header hook support to IPRanges. Previously, it was not possible to encounter an HTTP status error on IPRanges because this condition could never be met:

`if resp.StatusCode < 200 && resp.StatusCode >= 400 {`
@brandonc brandonc force-pushed the brandonc/allow_state_version_create_without_state branch from 729c2de to 778c6d8 Compare June 15, 2023 02:42
Copy link
Contributor

@sebasslash sebasslash left a comment

Choose a reason for hiding this comment

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

I like this refactor, much cleaner than my original suggestion. 🚀

@brandonc brandonc merged commit 214be72 into main Jun 15, 2023
@brandonc brandonc deleted the brandonc/allow_state_version_create_without_state branch June 15, 2023 21:02
@github-actions
Copy link

Reminder to the contributor that merged this PR: if your changes have added important functionality or fixed a relevant bug, open a follow-up PR to update CHANGELOG.md with a note on your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants