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

feat(storage): per-operation options / Bucket #9209

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions google/cloud/storage/bucket_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::internal::CurrentOptions;
using ::google::cloud::storage::testing::canonical_errors::TransientError;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
Expand Down Expand Up @@ -76,6 +77,8 @@ TEST_F(BucketTest, CreateBucket) {
EXPECT_CALL(*mock_, CreateBucket)
.WillOnce(Return(StatusOr<BucketMetadata>(TransientError())))
.WillOnce([&expected](internal::CreateBucketRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.metadata().name());
EXPECT_EQ("US", r.metadata().location());
EXPECT_EQ("STANDARD", r.metadata().storage_class());
Expand All @@ -85,7 +88,8 @@ TEST_F(BucketTest, CreateBucket) {
auto client = ClientForMock();
auto actual = client.CreateBucket(
"test-bucket-name",
BucketMetadata().set_location("US").set_storage_class("STANDARD"));
BucketMetadata().set_location("US").set_storage_class("STANDARD"),
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_EQ(expected, *actual);
}
Expand Down Expand Up @@ -135,11 +139,14 @@ TEST_F(BucketTest, GetBucketMetadata) {
EXPECT_CALL(*mock_, GetBucketMetadata)
.WillOnce(Return(StatusOr<BucketMetadata>(TransientError())))
.WillOnce([&expected](internal::GetBucketMetadataRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("foo-bar-baz", r.bucket_name());
return make_status_or(expected);
});
auto client = ClientForMock();
auto actual = client.GetBucketMetadata("foo-bar-baz");
auto actual = client.GetBucketMetadata(
"foo-bar-baz", Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_EQ(expected, *actual);
}
Expand Down Expand Up @@ -167,11 +174,14 @@ TEST_F(BucketTest, DeleteBucket) {
EXPECT_CALL(*mock_, DeleteBucket)
.WillOnce(Return(StatusOr<internal::EmptyResponse>(TransientError())))
.WillOnce([](internal::DeleteBucketRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("foo-bar-baz", r.bucket_name());
return make_status_or(internal::EmptyResponse{});
});
auto client = ClientForMock();
auto status = client.DeleteBucket("foo-bar-baz");
auto status = client.DeleteBucket(
"foo-bar-baz", Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(status);
}

Expand Down Expand Up @@ -214,6 +224,8 @@ TEST_F(BucketTest, UpdateBucket) {
EXPECT_CALL(*mock_, UpdateBucket)
.WillOnce(Return(StatusOr<BucketMetadata>(TransientError())))
.WillOnce([&expected](internal::UpdateBucketRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.metadata().name());
EXPECT_EQ("US", r.metadata().location());
EXPECT_EQ("STANDARD", r.metadata().storage_class());
Expand All @@ -222,7 +234,8 @@ TEST_F(BucketTest, UpdateBucket) {
auto client = ClientForMock();
auto actual = client.UpdateBucket(
"test-bucket-name",
BucketMetadata().set_location("US").set_storage_class("STANDARD"));
BucketMetadata().set_location("US").set_storage_class("STANDARD"),
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_EQ(expected, *actual);
}
Expand Down Expand Up @@ -273,14 +286,17 @@ TEST_F(BucketTest, PatchBucket) {
EXPECT_CALL(*mock_, PatchBucket)
.WillOnce(Return(StatusOr<BucketMetadata>(TransientError())))
.WillOnce([&expected](internal::PatchBucketRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.bucket());
EXPECT_THAT(r.payload(), HasSubstr("STANDARD"));
return make_status_or(expected);
});
auto client = ClientForMock();
auto actual = client.PatchBucket(
"test-bucket-name",
BucketMetadataPatchBuilder().SetStorageClass("STANDARD"));
BucketMetadataPatchBuilder().SetStorageClass("STANDARD"),
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_EQ(expected, *actual);
}
Expand Down Expand Up @@ -321,11 +337,14 @@ TEST_F(BucketTest, GetNativeBucketIamPolicy) {
EXPECT_CALL(*mock_, GetNativeBucketIamPolicy)
.WillOnce(Return(StatusOr<NativeIamPolicy>(TransientError())))
.WillOnce([&expected](internal::GetBucketIamPolicyRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.bucket_name());
return make_status_or(expected);
});
auto client = ClientForMock();
auto actual = client.GetNativeBucketIamPolicy("test-bucket-name");
auto actual = client.GetNativeBucketIamPolicy(
"test-bucket-name", Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_EQ(0, actual->version());
EXPECT_EQ("XYZ=", actual->etag());
Expand Down Expand Up @@ -359,12 +378,16 @@ TEST_F(BucketTest, SetNativeBucketIamPolicy) {
.WillOnce(Return(StatusOr<NativeIamPolicy>(TransientError())))
.WillOnce(
[&expected](internal::SetNativeBucketIamPolicyRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.bucket_name());
EXPECT_THAT(r.json_payload(), HasSubstr("test-user"));
return make_status_or(expected);
});
auto client = ClientForMock();
auto actual = client.SetNativeBucketIamPolicy("test-bucket-name", expected);
auto actual = client.SetNativeBucketIamPolicy(
"test-bucket-name", expected,
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_EQ(0, actual->version());
EXPECT_EQ("XYZ=", actual->etag());
Expand Down Expand Up @@ -412,13 +435,16 @@ TEST_F(BucketTest, TestBucketIamPermissions) {
TransientError())))
.WillOnce(
[&expected](internal::TestBucketIamPermissionsRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.bucket_name());
EXPECT_THAT(r.permissions(), ElementsAre("storage.buckets.delete"));
return make_status_or(expected);
});
auto client = ClientForMock();
auto actual = client.TestBucketIamPermissions("test-bucket-name",
{"storage.buckets.delete"});
auto actual = client.TestBucketIamPermissions(
"test-bucket-name", {"storage.buckets.delete"},
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_THAT(*actual, ElementsAreArray(expected.permissions));
}
Expand Down Expand Up @@ -464,12 +490,15 @@ TEST_F(BucketTest, LockBucketRetentionPolicy) {
.WillOnce(Return(StatusOr<BucketMetadata>(TransientError())))
.WillOnce(
[expected](internal::LockBucketRetentionPolicyRequest const& r) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
EXPECT_EQ("test-bucket-name", r.bucket_name());
EXPECT_EQ(42, r.metageneration());
return make_status_or(expected);
});
auto client = ClientForMock();
auto metadata = client.LockBucketRetentionPolicy("test-bucket-name", 42U);
auto metadata = client.LockBucketRetentionPolicy(
"test-bucket-name", 42U, Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(metadata);
EXPECT_EQ(expected, *metadata);
}
Expand Down
12 changes: 12 additions & 0 deletions google/cloud/storage/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ class Client {
template <typename... Options>
ListBucketsReader ListBucketsForProject(std::string const& project_id,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::ListBucketsRequest request(project_id);
request.set_multiple_options(std::forward<Options>(options)...);
auto client = raw_client_;
Expand Down Expand Up @@ -430,6 +431,7 @@ class Client {
*/
template <typename... Options>
ListBucketsReader ListBuckets(Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
auto const& project_id = raw_client_->client_options().project_id();
return ListBucketsForProject(project_id, std::forward<Options>(options)...);
}
Expand Down Expand Up @@ -502,6 +504,7 @@ class Client {
std::string project_id,
BucketMetadata metadata,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
metadata.set_name(std::move(bucket_name));
internal::CreateBucketRequest request(std::move(project_id),
std::move(metadata));
Expand All @@ -526,6 +529,7 @@ class Client {
template <typename... Options>
StatusOr<BucketMetadata> GetBucketMetadata(std::string const& bucket_name,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::GetBucketMetadataRequest request(bucket_name);
request.set_multiple_options(std::forward<Options>(options)...);
return raw_client_->GetBucketMetadata(request);
Expand All @@ -548,6 +552,7 @@ class Client {
*/
template <typename... Options>
Status DeleteBucket(std::string const& bucket_name, Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::DeleteBucketRequest request(bucket_name);
request.set_multiple_options(std::forward<Options>(options)...);
return raw_client_->DeleteBucket(request).status();
Expand Down Expand Up @@ -590,6 +595,7 @@ class Client {
StatusOr<BucketMetadata> UpdateBucket(std::string bucket_name,
BucketMetadata metadata,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
metadata.set_name(std::move(bucket_name));
internal::UpdateBucketRequest request(std::move(metadata));
request.set_multiple_options(std::forward<Options>(options)...);
Expand Down Expand Up @@ -639,6 +645,7 @@ class Client {
BucketMetadata const& original,
BucketMetadata const& updated,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::PatchBucketRequest request(std::move(bucket_name), original,
updated);
request.set_multiple_options(std::forward<Options>(options)...);
Expand Down Expand Up @@ -682,6 +689,7 @@ class Client {
StatusOr<BucketMetadata> PatchBucket(
std::string bucket_name, BucketMetadataPatchBuilder const& builder,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::PatchBucketRequest request(std::move(bucket_name), builder);
request.set_multiple_options(std::forward<Options>(options)...);
return raw_client_->PatchBucket(request);
Expand Down Expand Up @@ -723,6 +731,7 @@ class Client {
template <typename... Options>
StatusOr<NativeIamPolicy> GetNativeBucketIamPolicy(
std::string const& bucket_name, Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::GetBucketIamPolicyRequest request(bucket_name);
request.set_multiple_options(std::forward<Options>(options)...);
return raw_client_->GetNativeBucketIamPolicy(request);
Expand Down Expand Up @@ -778,6 +787,7 @@ class Client {
StatusOr<NativeIamPolicy> SetNativeBucketIamPolicy(
std::string const& bucket_name, NativeIamPolicy const& iam_policy,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::SetNativeBucketIamPolicyRequest request(bucket_name, iam_policy);
request.set_multiple_options(std::forward<Options>(options)...);
return raw_client_->SetNativeBucketIamPolicy(request);
Expand Down Expand Up @@ -814,6 +824,7 @@ class Client {
StatusOr<std::vector<std::string>> TestBucketIamPermissions(
std::string bucket_name, std::vector<std::string> permissions,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::TestBucketIamPermissionsRequest request(std::move(bucket_name),
std::move(permissions));
request.set_multiple_options(std::forward<Options>(options)...);
Expand Down Expand Up @@ -874,6 +885,7 @@ class Client {
StatusOr<BucketMetadata> LockBucketRetentionPolicy(
std::string const& bucket_name, std::uint64_t metageneration,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::LockBucketRetentionPolicyRequest request(bucket_name,
metageneration);
request.set_multiple_options(std::forward<Options>(options)...);
Expand Down