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

views: add cloud view #35929

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

bschaatsbergen
Copy link
Member

@bschaatsbergen bschaatsbergen commented Oct 30, 2024

Closes #35159

The cloud backend implements a retry mechanism for its client, go-tfe. Currently, retry messages from this logic were not rendered through a view, so the -json flag was not respected. This resulted in mixed (non-pure JSON) output in the terminal whenever retry messages were printed.

This PR introduces a new view: cloud, which handles rendering output in human-readable or machine-readable format from cloud-specific operations.

The PR works around the current lack of native support for views in the backends. To work around this limitation, we have introduced a ViewType field in the CLI options, which we are now passing down the to the cloud backend.

 $ go test -run "TestNewCloud" -v
=== RUN   TestNewCloudHuman_RetryLog
=== RUN   TestNewCloudHuman_RetryLog/first_retry,_no_output
=== RUN   TestNewCloudHuman_RetryLog/second_retry,_server_error
=== RUN   TestNewCloudHuman_RetryLog/subsequent_retry_with_elapsed_time
=== RUN   TestNewCloudHuman_RetryLog/retry_with_429_status,_no_output
--- PASS: TestNewCloudHuman_RetryLog (0.00s)
    --- PASS: TestNewCloudHuman_RetryLog/first_retry,_no_output (0.00s)
    --- PASS: TestNewCloudHuman_RetryLog/second_retry,_server_error (0.00s)
    --- PASS: TestNewCloudHuman_RetryLog/subsequent_retry_with_elapsed_time (0.00s)
    --- PASS: TestNewCloudHuman_RetryLog/retry_with_429_status,_no_output (0.00s)
=== RUN   TestNewCloud_unsupportedViewDiagnostics
--- PASS: TestNewCloud_unsupportedViewDiagnostics (0.00s)
=== RUN   TestNewCloud_humanViewOutput
=== RUN   TestNewCloud_humanViewOutput/no_param
=== RUN   TestNewCloud_humanViewOutput/single_param
--- PASS: TestNewCloud_humanViewOutput (0.00s)
    --- PASS: TestNewCloud_humanViewOutput/no_param (0.00s)
    --- PASS: TestNewCloud_humanViewOutput/single_param (0.00s)
=== RUN   TestNewCloud_humanViewDiagnostics
--- PASS: TestNewCloud_humanViewDiagnostics (0.00s)
=== RUN   TestNewCloudJSON_RetryLog
=== RUN   TestNewCloudJSON_RetryLog/attempt_0,_no_output
=== RUN   TestNewCloudJSON_RetryLog/attempt_1,_server_error
=== RUN   TestNewCloudJSON_RetryLog/subsequent_retry_with_elapsed_time
=== RUN   TestNewCloudJSON_RetryLog/retry_with_429_status,_no_output
--- PASS: TestNewCloudJSON_RetryLog (0.00s)
    --- PASS: TestNewCloudJSON_RetryLog/attempt_0,_no_output (0.00s)
    --- PASS: TestNewCloudJSON_RetryLog/attempt_1,_server_error (0.00s)
    --- PASS: TestNewCloudJSON_RetryLog/subsequent_retry_with_elapsed_time (0.00s)
    --- PASS: TestNewCloudJSON_RetryLog/retry_with_429_status,_no_output (0.00s)
=== RUN   TestNewCloud_jsonViewOutput
=== RUN   TestNewCloud_jsonViewOutput/no_param
=== RUN   TestNewCloud_jsonViewOutput/single_param
--- PASS: TestNewCloud_jsonViewOutput (0.00s)
    --- PASS: TestNewCloud_jsonViewOutput/no_param (0.00s)
    --- PASS: TestNewCloud_jsonViewOutput/single_param (0.00s)
=== RUN   TestNewCloud_jsonViewDiagnostics
--- PASS: TestNewCloud_jsonViewDiagnostics (0.00s)
PASS
ok      github.com/hashicorp/terraform/internal/command/views   0.902s

The cloud backend includes a retry hook that relies on a go-tfe implementation. Since these retry messages were not rendered through a view, the `-json` flag was not respected, leading to mixed (non-pure JSON) output in the terminal whenever retry messages were printed.

Before passing the init view to the cloud backend, we ensure the retryLog hook is included in the init view.

Signed-off-by: Bruno Schaatsbergen <[email protected]>
@bschaatsbergen bschaatsbergen force-pushed the b/fix-retry-messages-in-cloud-backend-json branch from f9d9df0 to f979759 Compare November 1, 2024 10:44
@bschaatsbergen bschaatsbergen force-pushed the b/fix-retry-messages-in-cloud-backend-json branch from ecbb274 to cb2d019 Compare November 1, 2024 11:38
@bschaatsbergen bschaatsbergen force-pushed the b/fix-retry-messages-in-cloud-backend-json branch from cb2d019 to a909304 Compare November 1, 2024 11:45
The cloud view is responsible for rendering messages related to cloud operations, presenting them to the user in either a human-readable format or as JSON output in the terminal.

Signed-off-by: Bruno Schaatsbergen <[email protected]>
…based on view type.

Signed-off-by: Bruno Schaatsbergen <[email protected]>
Signed-off-by: Bruno Schaatsbergen <[email protected]>
Signed-off-by: Bruno Schaatsbergen <[email protected]>
Added a comment to explain the rationale behind the guard statement, which prevents potential nil errors during backend initialization and retryLogHook call.

Signed-off-by: Bruno Schaatsbergen <[email protected]>
Signed-off-by: Bruno Schaatsbergen <[email protected]>
@bschaatsbergen bschaatsbergen changed the title views: move retryLog hook to init view views: add cloud view Nov 5, 2024
@bschaatsbergen bschaatsbergen force-pushed the b/fix-retry-messages-in-cloud-backend-json branch from 932541a to c71a1a7 Compare November 6, 2024 12:52
@bschaatsbergen bschaatsbergen force-pushed the b/fix-retry-messages-in-cloud-backend-json branch from fc57ccc to 94c732b Compare November 6, 2024 20:00
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.

Print retryLogHook messages to stderr
1 participant