Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ google::storage::v2::Bucket ToProto(storage::BucketMetadata const& rhs) {
if (rhs.has_soft_delete_policy()) {
*result.mutable_soft_delete_policy() = ToProto(rhs.soft_delete_policy());
}
if (rhs.has_object_retention()) {
result.mutable_object_retention()->set_enabled(
rhs.object_retention().enabled);
}
return result;
}

Expand Down Expand Up @@ -161,6 +165,10 @@ storage::BucketMetadata FromProto(google::storage::v2::Bucket const& rhs,
if (rhs.has_logging()) metadata.set_logging(FromProto(rhs.logging()));
metadata.set_metageneration(rhs.metageneration());
metadata.set_name(GrpcBucketNameToId(rhs.name()));
if (rhs.has_object_retention()) {
metadata.set_object_retention(
storage::BucketObjectRetention{rhs.object_retention().enabled()});
}
if (rhs.has_owner()) {
metadata.set_owner(storage_internal::FromProto(rhs.owner()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ TEST(GrpcBucketMetadataParser, BucketAllFieldsRoundtrip) {
effective_time { seconds: 1708000496 nanos: 700000000 }
retention_duration { seconds: 864000 }
}
object_retention { enabled: true }
)pb";
EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &input));

Expand Down Expand Up @@ -254,6 +255,9 @@ TEST(GrpcBucketMetadataParser, BucketAllFieldsRoundtrip) {
"softDeletePolicy": {
"effectiveTime": "2024-02-15T12:34:56.700Z",
"retentionDurationSeconds": "864000"
},
"objectRetention": {
"mode": "Enabled"
}
})""");
ASSERT_THAT(expected, IsOk());
Expand Down Expand Up @@ -577,6 +581,23 @@ TEST(GrpcBucketMetadataParser, BucketCustomPlacementConfigRoundtrip) {
EXPECT_THAT(end, IsProtoEqual(start));
}

TEST(GrpcBucketMetadataParser, BucketObjectRetentionRoundtrip) {
google::storage::v2::Bucket input;
storage::BucketMetadata const empty_metadata = FromProto(input, Options{});
EXPECT_FALSE(empty_metadata.has_object_retention());
google::storage::v2::Bucket const empty_roundtrip = ToProto(empty_metadata);
EXPECT_FALSE(empty_roundtrip.has_object_retention());

input.mutable_object_retention()->set_enabled(false);
storage::BucketMetadata const metadata = FromProto(input, Options{});
ASSERT_TRUE(metadata.has_object_retention());
EXPECT_EQ(metadata.object_retention(), storage::BucketObjectRetention{false});

google::storage::v2::Bucket const roundtrip = ToProto(metadata);
ASSERT_TRUE(roundtrip.has_object_retention());
EXPECT_FALSE(roundtrip.object_retention().enabled());
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
Expand Down
6 changes: 6 additions & 0 deletions google/cloud/storage/internal/grpc/bucket_request_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,19 @@ google::storage::v2::CreateBucketRequest ToProto(
result.set_predefined_default_object_acl(
request.GetOption<storage::PredefinedDefaultObjectAcl>().value());
}
if (request.HasOption<storage::EnableObjectRetention>()) {
result.set_enable_object_retention(
request.GetOption<storage::EnableObjectRetention>().value());
}
*result.mutable_bucket() = storage_internal::ToProto(request.metadata());
result.mutable_bucket()->set_project("projects/" + request.project_id());
// Ignore fields commonly set by ToProto().
result.mutable_bucket()->set_name("");
result.mutable_bucket()->set_bucket_id("");
result.mutable_bucket()->clear_create_time();
result.mutable_bucket()->clear_update_time();
// REST uses only the enableObjectRetention query parameter.
result.mutable_bucket()->clear_object_retention();
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ TEST(GrpcBucketRequestParser, CreateBucketMetadataAllOptions) {
}
predefined_acl: "projectPrivate"
predefined_default_object_acl: "private"
enable_object_retention: true
)pb",
&expected));

Expand Down Expand Up @@ -167,8 +168,10 @@ TEST(GrpcBucketRequestParser, CreateBucketMetadataAllOptions) {
storage::LifecycleRule::Delete())}})
.set_logging(storage::BucketLogging{
/*.log_bucket=*/"test-log-bucket",
/*.log_object_prefix=*/"test-log-object-prefix"}));
/*.log_object_prefix=*/"test-log-object-prefix"})
.set_object_retention(storage::BucketObjectRetention{true}));
req.set_multiple_options(
storage::EnableObjectRetention(true),
storage::PredefinedAcl::ProjectPrivate(),
storage::PredefinedDefaultObjectAcl::Private(),
storage::Projection("full"), storage::UserProject("test-user-project"),
Expand Down
51 changes: 51 additions & 0 deletions google/cloud/storage/internal/grpc/object_metadata_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
#include "google/cloud/storage/internal/md5hash.h"
#include "google/cloud/storage/version.h"
#include "google/cloud/internal/big_endian.h"
#include "google/cloud/internal/make_status.h"
#include "google/cloud/internal/time_utils.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"

namespace google {
namespace cloud {
Expand All @@ -46,6 +49,51 @@ StatusOr<google::storage::v2::CustomerEncryption> ToProto(
return result;
}

StatusOr<google::storage::v2::Object::Retention> ToProto(
storage::ObjectRetention const& rhs) {
google::storage::v2::Object::Retention result;
// Accept canonical TitleCase ("Unlocked"/"Locked") and uppercase proto enum
// names.
if (rhs.mode.empty()) {
result.set_mode(google::storage::v2::Object::Retention::MODE_UNSPECIFIED);
} else if (absl::EqualsIgnoreCase(rhs.mode,
storage::ObjectRetentionUnlocked())) {
result.set_mode(google::storage::v2::Object::Retention::UNLOCKED);
} else if (absl::EqualsIgnoreCase(rhs.mode,
storage::ObjectRetentionLocked())) {
result.set_mode(google::storage::v2::Object::Retention::LOCKED);
} else {
return google::cloud::internal::InvalidArgumentError(
absl::StrCat("unrecognized object retention mode: ", rhs.mode),
GCP_ERROR_INFO());
}
if (rhs.retain_until_time != std::chrono::system_clock::time_point{}) {
*result.mutable_retain_until_time() =
google::cloud::internal::ToProtoTimestamp(rhs.retain_until_time);
}
return result;
}

storage::ObjectRetention FromProto(
google::storage::v2::Object::Retention const& rhs) {
storage::ObjectRetention result;
switch (rhs.mode()) {
case google::storage::v2::Object::Retention::UNLOCKED:
result.mode = storage::ObjectRetentionUnlocked();
break;
case google::storage::v2::Object::Retention::LOCKED:
result.mode = storage::ObjectRetentionLocked();
break;
default:
break;
}
if (rhs.has_retain_until_time()) {
result.retain_until_time =
google::cloud::internal::ToChronoTimePoint(rhs.retain_until_time());
}
return result;
}

std::string Crc32cFromProto(std::uint32_t v) {
auto endian_encoded = google::cloud::internal::EncodeBigEndian(v);
return storage::internal::Base64Encode(endian_encoded);
Expand Down Expand Up @@ -148,6 +196,9 @@ storage::ObjectMetadata FromProto(google::storage::v2::Object object,
google::cloud::internal::ToChronoTimePoint(
object.retention_expire_time()));
}
if (object.has_retention()) {
metadata.set_retention(FromProto(object.retention()));
}
metadata.set_size(static_cast<std::uint64_t>(object.size()));
metadata.set_temporary_hold(object.temporary_hold());
if (object.has_delete_time()) {
Expand Down
5 changes: 5 additions & 0 deletions google/cloud/storage/internal/grpc/object_metadata_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ StatusOr<google::storage::v2::CustomerEncryption> ToProto(
storage::CustomerEncryption FromProto(
google::storage::v2::CustomerEncryption rhs);

StatusOr<google::storage::v2::Object::Retention> ToProto(
storage::ObjectRetention const& rhs);
storage::ObjectRetention FromProto(
google::storage::v2::Object::Retention const& rhs);

std::string Crc32cFromProto(std::uint32_t);
StatusOr<std::uint32_t> Crc32cToProto(std::string const&);
std::string MD5FromProto(std::string const&);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "google/cloud/internal/sha256_hash.h"
#include "google/cloud/testing_util/is_proto_equal.h"
#include "google/cloud/testing_util/status_matchers.h"
#include "absl/strings/str_cat.h"
#include <google/protobuf/text_format.h>
#include <gmock/gmock.h>

Expand All @@ -31,6 +32,7 @@ namespace {
namespace storage_proto = ::google::storage::v2;
using ::google::cloud::internal::HexDecode;
using ::google::cloud::testing_util::IsProtoEqual;
using ::google::cloud::testing_util::StatusIs;

TEST(GrpcClientFromProto, ObjectSimple) {
storage_proto::Object input;
Expand Down Expand Up @@ -130,6 +132,13 @@ TEST(GrpcClientFromProto, ObjectSimple) {
seconds: 1714979289
nanos: 123456789
}
retention {
mode: UNLOCKED
retain_until_time {
seconds: 1714979289
nanos: 123456789
}
}
)""",
&input));

Expand Down Expand Up @@ -204,7 +213,11 @@ TEST(GrpcClientFromProto, ObjectSimple) {
"etag": "test-etag",
"softDeleteTime": "2024-03-04T12:34:56.987654321Z",
"hardDeleteTime": "2024-03-11T12:34:56.987654321Z",
"timeFinalized": "2024-05-06T07:08:09.123456789Z"
"timeFinalized": "2024-05-06T07:08:09.123456789Z",
"retention": {
"mode": "Unlocked",
"retainUntilTime": "2024-05-06T07:08:09.123456789Z"
}
})""");
ASSERT_STATUS_OK(expected);

Expand All @@ -222,6 +235,7 @@ TEST(GrpcClientFromProto, ObjectWithoutFinalizeTime) {
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &input));
storage::ObjectMetadata const actual = FromProto(input, Options{});
EXPECT_FALSE(actual.has_finalize_time());
EXPECT_FALSE(actual.has_retention());
}

TEST(GrpcClientFromProto, ObjectCustomerEncryptionRoundtrip) {
Expand Down Expand Up @@ -275,6 +289,59 @@ TEST(GrpcClientFromProto, MD5Roundtrip) {
}
}

TEST(GrpcClientFromProto, ObjectRetentionRoundtrip) {
std::chrono::system_clock::time_point const tp =
std::chrono::system_clock::time_point{} +
std::chrono::seconds(1714979289) + std::chrono::microseconds(123456);
struct TestCase {
std::string text_proto;
storage::ObjectRetention expected;
} cases[] = {
{R"pb(
mode: UNLOCKED
retain_until_time { seconds: 1714979289 nanos: 123456000 }
)pb",
storage::ObjectRetention{storage::ObjectRetentionUnlocked(), tp}},
{R"pb(
mode: LOCKED
retain_until_time { seconds: 1714979289 nanos: 123456000 }
)pb",
storage::ObjectRetention{storage::ObjectRetentionLocked(), tp}},
{R"pb(
mode: MODE_UNSPECIFIED
)pb",
storage::ObjectRetention{}},
};
for (auto const& test : cases) {
SCOPED_TRACE(absl::StrCat("Testing with text_proto=", test.text_proto));
google::storage::v2::Object::Retention start;
ASSERT_TRUE(
google::protobuf::TextFormat::ParseFromString(test.text_proto, &start));
storage::ObjectRetention const middle = FromProto(start);
EXPECT_EQ(middle, test.expected);
StatusOr<google::storage::v2::Object::Retention> const end =
ToProto(middle);
ASSERT_STATUS_OK(end);
EXPECT_THAT(*end, IsProtoEqual(start));
}

// Verify uppercase and lowercase mode strings match.
StatusOr<google::storage::v2::Object::Retention> const unlocked_upper =
ToProto(storage::ObjectRetention{"UNLOCKED", tp});
ASSERT_STATUS_OK(unlocked_upper);
EXPECT_EQ(unlocked_upper->mode(),
google::storage::v2::Object::Retention::UNLOCKED);

StatusOr<google::storage::v2::Object::Retention> const locked_lower =
ToProto(storage::ObjectRetention{"locked", tp});
ASSERT_STATUS_OK(locked_lower);
EXPECT_EQ(locked_lower->mode(),
google::storage::v2::Object::Retention::LOCKED);

EXPECT_THAT(ToProto(storage::ObjectRetention{"test-only-invalid", tp}),
StatusIs(StatusCode::kInvalidArgument));
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
Expand Down
Loading
Loading