Google Cloud C++ Client  1.32.1
C++ Client Library for Google Cloud Platform
iam_bindings.cc
Go to the documentation of this file.
1 // Copyright 2018 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/iam_bindings.h"
16 #include "google/cloud/internal/absl_str_join_quiet.h"
17 #include <algorithm>
18 #include <iostream>
19 #include <iterator>
20 // TODO(#5929) - remove after decommission is completed
21 #include "google/cloud/internal/disable_deprecation_warnings.inc"
22 
23 namespace google {
24 namespace cloud {
25 inline namespace GOOGLE_CLOUD_CPP_NS {
26 void IamBindings::AddMember(std::string const& role, std::string member) {
27  bindings_[role].emplace(std::move(member));
28 }
29 
30 void IamBindings::AddMembers(google::cloud::IamBinding const& iam_binding) {
31  std::string const& role(iam_binding.role());
32  std::set<std::string> const& members = iam_binding.members();
33 
34  bindings_[role].insert(members.begin(), members.end());
35 }
36 
37 void IamBindings::AddMembers(std::string const& role,
38  std::set<std::string> const& members) {
39  bindings_[role].insert(members.begin(), members.end());
40 }
41 
42 void IamBindings::RemoveMember(std::string const& role,
43  std::string const& member) {
44  auto it = bindings_.find(role);
45  if (it == bindings_.end()) {
46  return;
47  }
48 
49  auto& members = it->second;
50  auto member_loc = members.find(member);
51 
52  if (member_loc != members.end()) {
53  members.erase(member_loc);
54  }
55  if (members.empty()) {
56  bindings_.erase(it);
57  }
58 }
59 
60 void IamBindings::RemoveMembers(google::cloud::IamBinding const& iam_binding) {
61  RemoveMembers(iam_binding.role(), iam_binding.members());
62 }
63 
64 void IamBindings::RemoveMembers(std::string const& role,
65  std::set<std::string> const& members) {
66  auto it = bindings_.find(role);
67  if (it == bindings_.end()) {
68  return;
69  }
70 
71  auto& binding_members = it->second;
72  for (auto const& member : members) {
73  auto member_loc = binding_members.find(member);
74  if (member_loc != binding_members.end()) {
75  binding_members.erase(member_loc);
76  }
77  }
78  if (binding_members.empty()) {
79  bindings_.erase(it);
80  }
81 }
82 
83 std::ostream& operator<<(std::ostream& os, IamBindings const& rhs) {
84  os << "IamBindings={";
85  struct IamBindingFormatter {
86  void operator()(std::string* out,
87  std::pair<std::string, std::set<std::string>> const& rhs) {
88  out->append(rhs.first);
89  out->append(": [");
90  out->append(absl::StrJoin(rhs.second, ", "));
91  out->append("]");
92  }
93  };
94  os << absl::StrJoin(rhs, ", ", IamBindingFormatter{});
95  return os << "}";
96 }
97 
98 } // namespace GOOGLE_CLOUD_CPP_NS
99 } // namespace cloud
100 } // namespace google