-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GCS+gRPC): implement BucketAccessControl parser
- Loading branch information
Showing
8 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
google/cloud/storage/internal/grpc_bucket_access_control_parser.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/storage/internal/grpc_bucket_access_control_parser.h" | ||
#include "google/cloud/storage/version.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace storage { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
namespace internal { | ||
|
||
google::storage::v2::BucketAccessControl GrpcBucketAccessControlParser::ToProto( | ||
BucketAccessControl const& acl) { | ||
google::storage::v2::BucketAccessControl result; | ||
result.set_role(acl.role()); | ||
result.set_id(acl.id()); | ||
result.set_entity(acl.entity()); | ||
result.set_entity_id(acl.entity_id()); | ||
result.set_email(acl.email()); | ||
result.set_domain(acl.domain()); | ||
if (acl.has_project_team()) { | ||
result.mutable_project_team()->set_project_number( | ||
acl.project_team().project_number); | ||
result.mutable_project_team()->set_team(acl.project_team().team); | ||
} | ||
return result; | ||
} | ||
|
||
BucketAccessControl GrpcBucketAccessControlParser::FromProto( | ||
google::storage::v2::BucketAccessControl acl, | ||
std::string const& bucket_name) { | ||
BucketAccessControl result; | ||
result.kind_ = "storage#bucketAccessControl"; | ||
result.bucket_ = bucket_name; | ||
result.domain_ = std::move(*acl.mutable_domain()); | ||
result.email_ = std::move(*acl.mutable_email()); | ||
result.entity_ = std::move(*acl.mutable_entity()); | ||
result.entity_id_ = std::move(*acl.mutable_entity_id()); | ||
result.id_ = std::move(*acl.mutable_id()); | ||
if (acl.has_project_team()) { | ||
result.project_team_ = ProjectTeam{ | ||
std::move(*acl.mutable_project_team()->mutable_project_number()), | ||
std::move(*acl.mutable_project_team()->mutable_team()), | ||
}; | ||
} | ||
result.role_ = std::move(*acl.mutable_role()); | ||
result.self_link_.clear(); | ||
|
||
return result; | ||
} | ||
|
||
} // namespace internal | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace storage | ||
} // namespace cloud | ||
} // namespace google |
42 changes: 42 additions & 0 deletions
42
google/cloud/storage/internal/grpc_bucket_access_control_parser.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_BUCKET_ACCESS_CONTROL_PARSER_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_BUCKET_ACCESS_CONTROL_PARSER_H | ||
|
||
#include "google/cloud/storage/internal/raw_client.h" | ||
#include "google/cloud/storage/version.h" | ||
#include <google/storage/v2/storage.pb.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace storage { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
namespace internal { | ||
|
||
struct GrpcBucketAccessControlParser { | ||
static google::storage::v2::BucketAccessControl ToProto( | ||
BucketAccessControl const& acl); | ||
static BucketAccessControl FromProto( | ||
google::storage::v2::BucketAccessControl acl, | ||
std::string const& bucket_name); | ||
}; | ||
|
||
} // namespace internal | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace storage | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_BUCKET_ACCESS_CONTROL_PARSER_H |
125 changes: 125 additions & 0 deletions
125
google/cloud/storage/internal/grpc_bucket_access_control_parser_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/storage/internal/grpc_bucket_access_control_parser.h" | ||
#include "google/cloud/storage/internal/bucket_access_control_parser.h" | ||
#include "google/cloud/testing_util/is_proto_equal.h" | ||
#include "google/cloud/testing_util/status_matchers.h" | ||
#include <google/protobuf/text_format.h> | ||
#include <gmock/gmock.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace storage { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
namespace internal { | ||
namespace { | ||
|
||
namespace storage_proto = ::google::storage::v2; | ||
using ::google::cloud::testing_util::IsProtoEqual; | ||
|
||
TEST(GrpcBucketAccessControlParser, FromProto) { | ||
storage_proto::BucketAccessControl input; | ||
EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(R"""( | ||
role: "test-role" | ||
id: "test-id" | ||
entity: "test-entity" | ||
entity_id: "test-entity-id" | ||
email: "test-email" | ||
domain: "test-domain" | ||
project_team: { | ||
project_number: "test-project-number" | ||
team: "test-team" | ||
} | ||
)""", | ||
&input)); | ||
|
||
auto const expected = BucketAccessControlParser::FromString(R"""({ | ||
"role": "test-role", | ||
"id": "test-id", | ||
"kind": "storage#bucketAccessControl", | ||
"bucket": "test-bucket", | ||
"entity": "test-entity", | ||
"entityId": "test-entity-id", | ||
"email": "test-email", | ||
"domain": "test-domain", | ||
"projectTeam": { | ||
"projectNumber": "test-project-number", | ||
"team": "test-team" | ||
} | ||
})"""); | ||
ASSERT_STATUS_OK(expected); | ||
|
||
auto actual = GrpcBucketAccessControlParser::FromProto(input, "test-bucket"); | ||
EXPECT_EQ(*expected, actual); | ||
} | ||
|
||
TEST(GrpcBucketAccessControlParser, ToProtoSimple) { | ||
auto acl = BucketAccessControlParser::FromString(R"""({ | ||
"role": "test-role", | ||
"id": "test-id", | ||
"kind": "storage#bucketAccessControl", | ||
"bucket": "test-bucket", | ||
"entity": "test-entity", | ||
"entityId": "test-entity-id", | ||
"email": "test-email", | ||
"domain": "test-domain", | ||
"projectTeam": { | ||
"projectNumber": "test-project-number", | ||
"team": "test-team" | ||
} | ||
})"""); | ||
ASSERT_STATUS_OK(acl); | ||
auto actual = GrpcBucketAccessControlParser::ToProto(*acl); | ||
|
||
storage_proto::BucketAccessControl expected; | ||
EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(R"""( | ||
role: "test-role" | ||
id: "test-id" | ||
entity: "test-entity" | ||
entity_id: "test-entity-id" | ||
email: "test-email" | ||
domain: "test-domain" | ||
project_team: { | ||
project_number: "test-project-number" | ||
team: "test-team" | ||
} | ||
)""", | ||
&expected)); | ||
|
||
EXPECT_THAT(actual, IsProtoEqual(expected)); | ||
} | ||
|
||
TEST(GrpcBucketAccessControlParser, MinimalFields) { | ||
BucketAccessControl acl; | ||
acl.set_role("test-role"); | ||
acl.set_entity("test-entity"); | ||
auto actual = GrpcBucketAccessControlParser::ToProto(acl); | ||
|
||
storage_proto::BucketAccessControl expected; | ||
EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(R"""( | ||
role: "test-role" | ||
entity: "test-entity" | ||
)""", | ||
&expected)); | ||
|
||
EXPECT_THAT(actual, IsProtoEqual(expected)); | ||
} | ||
|
||
} // namespace | ||
} // namespace internal | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace storage | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters