|
14 | 14 |
|
15 | 15 | #include "google/cloud/storage/internal/object_acl_requests.h" |
16 | 16 | #include "google/cloud/storage/internal/nljson.h" |
| 17 | +#include "google/cloud/storage/internal/patch_builder.h" |
17 | 18 | #include <iostream> |
18 | 19 |
|
19 | 20 | namespace google { |
@@ -44,6 +45,66 @@ std::ostream& operator<<(std::ostream& os, ObjectAclRequest const& r) { |
44 | 45 | return os << "}"; |
45 | 46 | } |
46 | 47 |
|
| 48 | +PatchObjectAclRequest::PatchObjectAclRequest( |
| 49 | + std::string bucket, std::string object, std::string entity, |
| 50 | + ObjectAccessControl const& original, ObjectAccessControl const& new_acl) |
| 51 | + : GenericObjectRequest(std::move(bucket), std::move(object)), |
| 52 | + entity_(std::move(entity)) { |
| 53 | + PatchBuilder build_patch; |
| 54 | + build_patch.AddStringField("bucket", original.bucket(), new_acl.bucket()); |
| 55 | + build_patch.AddStringField("domain", original.domain(), new_acl.domain()); |
| 56 | + build_patch.AddStringField("email", original.email(), new_acl.email()); |
| 57 | + build_patch.AddStringField("entity", original.entity(), new_acl.entity()); |
| 58 | + build_patch.AddStringField("entityId", original.entity_id(), |
| 59 | + new_acl.entity_id()); |
| 60 | + build_patch.AddStringField("etag", original.etag(), new_acl.etag()); |
| 61 | + build_patch.AddIntField("generation", original.generation(), |
| 62 | + new_acl.generation()); |
| 63 | + build_patch.AddStringField("id", original.id(), new_acl.id()); |
| 64 | + build_patch.AddStringField("kind", original.kind(), new_acl.kind()); |
| 65 | + build_patch.AddStringField("object", original.object(), new_acl.object()); |
| 66 | + |
| 67 | + if (original.project_team() != new_acl.project_team()) { |
| 68 | + auto empty = [](ProjectTeam const& p) { |
| 69 | + return p.project_number.empty() and p.team.empty(); |
| 70 | + }; |
| 71 | + if (empty(new_acl.project_team())) { |
| 72 | + if (not empty(original.project_team())) { |
| 73 | + build_patch.RemoveField("projectTeam"); |
| 74 | + } |
| 75 | + } else { |
| 76 | + PatchBuilder project_team_patch; |
| 77 | + project_team_patch |
| 78 | + .AddStringField("project_number", |
| 79 | + original.project_team().project_number, |
| 80 | + new_acl.project_team().project_number) |
| 81 | + .AddStringField("team", original.project_team().team, |
| 82 | + new_acl.project_team().team); |
| 83 | + build_patch.AddSubPatch("projectTeam", project_team_patch); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + build_patch.AddStringField("role", original.role(), new_acl.role()); |
| 88 | + build_patch.AddStringField("selfLink", original.self_link(), |
| 89 | + new_acl.self_link()); |
| 90 | + payload_ = build_patch.ToString(); |
| 91 | +} |
| 92 | + |
| 93 | +PatchObjectAclRequest::PatchObjectAclRequest( |
| 94 | + std::string bucket, std::string object, std::string entity, |
| 95 | + ObjectAccessControlPatchBuilder const& patch) |
| 96 | + : GenericObjectRequest(std::move(bucket), std::move(object)), |
| 97 | + entity_(std::move(entity)), |
| 98 | + payload_(patch.BuildPatch()) {} |
| 99 | + |
| 100 | +std::ostream& operator<<(std::ostream& os, PatchObjectAclRequest const& r) { |
| 101 | + os << "ObjectAclRequest={bucket_name=" << r.bucket_name() |
| 102 | + << ", object_name=" << r.object_name() << ", entity=" << r.entity(); |
| 103 | + r.DumpOptions(os, ", "); |
| 104 | + os << ", payload=" << r.payload(); |
| 105 | + return os << "}"; |
| 106 | +} |
| 107 | + |
47 | 108 | } // namespace internal |
48 | 109 | } // namespace STORAGE_CLIENT_NS |
49 | 110 | } // namespace storage |
|
0 commit comments