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

fix(storage): consistent project id overrides #11754

Merged

Conversation

coryan
Copy link
Member

@coryan coryan commented May 31, 2023

Use the same function to determine the project id in all functions that require one:

  • If the function consumes a project_id argument, then use that.
  • If the application provides a OverrideDefaultProject request option (i.e., via some variadic argument) then use that.
  • Otherwise use the value configured via the Options{}, including any per-call Options.
  • Use GOOGLE_CLOUD_PROJECT to initialize the Options{}, if present.
  • Return an error if no value can be found for the project id.

Fixes #11742

I apologize for the large PR. Most of the changes are in large (and maybe repetitive) tests. But I hope the tests are easy to follow.


This change is Reviewable

Use the same function to determine the project id in all functions that
require one:

- If the function consumes a `project_id` argument, then use that.
- If the application provides a `OverrideDefaultProject` request option
  (i.e., via some variadic argument) then use that.
- Otherwise use the value configured via the `Options{}`, including any
  per-call `Options`.
- Use `GOOGLE_CLOUD_PROJECT` to initialize the `Options{}`, if present.
- Return an error if no value can be found for the project id.
@product-auto-label product-auto-label bot added the api: storage Issues related to the Cloud Storage API. label May 31, 2023
@codecov
Copy link

codecov bot commented May 31, 2023

Codecov Report

Patch coverage: 99.45% and project coverage change: +0.01 🎉

Comparison is base (30f2d35) 93.77% compared to head (c060c2a) 93.79%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11754      +/-   ##
==========================================
+ Coverage   93.77%   93.79%   +0.01%     
==========================================
  Files        1831     1834       +3     
  Lines      165133   165800     +667     
==========================================
+ Hits       154852   155510     +658     
- Misses      10281    10290       +9     
Impacted Files Coverage Δ
google/cloud/storage/internal/bucket_requests.h 100.00% <ø> (ø)
google/cloud/storage/internal/hmac_key_requests.h 100.00% <ø> (ø)
.../cloud/storage/internal/service_account_requests.h 100.00% <ø> (ø)
google/cloud/storage/internal/request_project_id.h 88.23% <88.23%> (ø)
google/cloud/storage/client_bucket_test.cc 99.79% <99.45%> (-0.21%) ⬇️
...oogle/cloud/storage/client_service_account_test.cc 99.82% <99.76%> (-0.18%) ⬇️
google/cloud/storage/client.h 99.84% <100.00%> (+<0.01%) ⬆️
google/cloud/storage/client_test.cc 98.60% <100.00%> (ø)
google/cloud/storage/internal/generic_request.h 100.00% <100.00%> (ø)
...e/cloud/storage/internal/hmac_key_requests_test.cc 100.00% <100.00%> (ø)
... and 3 more

... and 6 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@coryan coryan marked this pull request as ready for review May 31, 2023 13:02
@coryan coryan requested a review from a team as a code owner May 31, 2023 13:02
Copy link
Member

@dbolduc dbolduc left a comment

Choose a reason for hiding this comment

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

Reviewed 16 of 16 files at r1, all commit messages.
Reviewable status: all files reviewed, 7 unresolved discussions (waiting on @coryan)


google/cloud/storage/client.h line 358 at r1 (raw file):

    auto opts = SpanOptions(std::forward<Options>(options)...);
    auto project_id = storage_internal::RequestProjectId(
        GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);

are we allowed to std::forward<>(options) multiple times like this?

I think it is ok because we only std::move() the google::cloud::Optionstypes in SpanOptions(...). And we ignore their values in RequestProjectId(...)


google/cloud/storage/client_bucket_test.cc line 74 at r1 (raw file):

  EXPECT_THAT(list, ElementsAre(StatusIs(StatusCode::kInvalidArgument)));
  // Ensure the errors are coming from the right source.
  EXPECT_NE(PermanentError().code(), StatusCode::kInvalidArgument);

I think it would be clearer to just check:

  EXPECT_THAT(list, ElementsAre(StatusIs(StatusCode::kInvalidArgument,
                                         HasSubstr("missing project id"))));

google/cloud/storage/client_service_account_test.cc line 136 at r1 (raw file):

  auto actual =
      client.GetServiceAccount(Options{}

nit: reclaim vertical space


google/cloud/storage/client_service_account_test.cc line 157 at r1 (raw file):

      client.GetServiceAccount(OverrideDefaultProject("override-project-id"),
                               Options{}

ditto


google/cloud/storage/internal/generic_request.h line 119 at r1 (raw file):

/**
 * Specialize for `OverrideDefaultProject` as a no-op class.

Ah. Good idea. I was worried about the request.set_multiple_options(...) calls.


google/cloud/storage/internal/request_project_id.h line 41 at r1 (raw file):

    internal::ErrorInfoBuilder ei, storage::OverrideDefaultProject const& o,
    RequestOptions&&... ro) {
  if (!o.has_value()) {

optional nit: save a line with: if (o.has_value()) return o.value();


google/cloud/storage/internal/request_project_id.h line 68 at r1 (raw file):

 * `google::cloud::Options` configured in the client. Before we introduced
 * per-call `Options` parameters GCS had "request options" as a variadic
 * list of template arguments. One of request options could be of type

s/One of/One of the/

Copy link
Member Author

@coryan coryan left a comment

Choose a reason for hiding this comment

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

PTAL

Reviewable status: 13 of 16 files reviewed, 5 unresolved discussions (waiting on @dbolduc)


google/cloud/storage/client.h line 358 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

are we allowed to std::forward<>(options) multiple times like this?

I think it is ok because we only std::move() the google::cloud::Optionstypes in SpanOptions(...). And we ignore their values in RequestProjectId(...)

std::forward and std::move do not change their parameter, they are just a fancy way to cast. So we are certainly allowed to use std::forward<Options>(options)... multiple times provided one does not accidentally trigger some UB in the second call (e.g. because the first call results in a move-from), which as you observe, we don't.


google/cloud/storage/client_bucket_test.cc line 74 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

I think it would be clearer to just check:

  EXPECT_THAT(list, ElementsAre(StatusIs(StatusCode::kInvalidArgument,
                                         HasSubstr("missing project id"))));

Part of the goal is to make sure the subsequent tests that use PermanentError()


google/cloud/storage/client_service_account_test.cc line 157 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

ditto

Done.


google/cloud/storage/internal/generic_request.h line 119 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

Ah. Good idea. I was worried about the request.set_multiple_options(...) calls.

Done.


google/cloud/storage/internal/request_project_id.h line 41 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

optional nit: save a line with: if (o.has_value()) return o.value();

Done.


google/cloud/storage/internal/request_project_id.h line 68 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

s/One of/One of the/

Done.

@coryan coryan enabled auto-merge (squash) June 1, 2023 14:04
@coryan coryan merged commit 18af25f into googleapis:main Jun 1, 2023
59 of 60 checks passed
@coryan coryan deleted the fix-storage-consistent-project-id-overrides branch June 1, 2023 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the Cloud Storage API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behavior between GOOGLE_CLOUD_PROJECT and ProjectIdOption
2 participants