1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/bigtable/iam_policy.h"
16 #include <google/protobuf/util/message_differencer.h>
17 #include <iostream>
18 
19 namespace google {
20 namespace cloud {
21 namespace bigtable {
22 inline namespace BIGTABLE_CLIENT_NS {
IamPolicy(std::initializer_list<google::iam::v1::Binding> bindings,std::string etag,std::int32_t version)23 google::iam::v1::Policy IamPolicy(
24     std::initializer_list<google::iam::v1::Binding> bindings, std::string etag,
25     std::int32_t version) {
26   return IamPolicy(bindings.begin(), bindings.end(), std::move(etag), version);
27 }
28 
IamPolicy(std::vector<google::iam::v1::Binding> bindings,std::string etag,std::int32_t version)29 google::iam::v1::Policy IamPolicy(
30     std::vector<google::iam::v1::Binding> bindings, std::string etag,
31     std::int32_t version) {
32   google::iam::v1::Policy res;
33   for (auto& binding : bindings) {
34     *res.add_bindings() = std::move(binding);
35   }
36   res.set_version(version);
37   res.set_etag(std::move(etag));
38   return res;
39 }
40 
operator <<(std::ostream & os,google::iam::v1::Policy const & rhs)41 std::ostream& operator<<(std::ostream& os, google::iam::v1::Policy const& rhs) {
42   os << "IamPolicy={version=" << rhs.version() << ", bindings="
43      << "IamBindings={";
44   bool first = true;
45   for (auto const& binding : rhs.bindings()) {
46     os << (first ? "" : ", ") << binding;
47     first = false;
48   }
49   return os << "}, etag=" << rhs.etag() << "}";
50 }
51 
RemoveBindingFromPolicy(google::iam::v1::Policy & policy,google::protobuf::RepeatedPtrField<google::iam::v1::Binding>::iterator to_remove)52 void RemoveBindingFromPolicy(
53     google::iam::v1::Policy& policy,
54     google::protobuf::RepeatedPtrField<google::iam::v1::Binding>::iterator
55         to_remove) {
56   RemoveBindingsFromPolicyIf(
57       policy, [&to_remove](google::iam::v1::Binding const& binding) {
58         return &(*to_remove) == &binding;
59       });
60 }
61 
62 }  // namespace BIGTABLE_CLIENT_NS
63 }  // namespace bigtable
64 }  // namespace cloud
65 }  // namespace google
66