Google Cloud C++ Client  0.4.0
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 <algorithm>
17 #include <iostream>
18 #include <iterator>
19 
20 namespace google {
21 namespace cloud {
22 inline namespace GOOGLE_CLOUD_CPP_NS {
23 void IamBindings::AddMember(std::string const& role, std::string member) {
24  bindings_[role].emplace(std::move(member));
25 }
26 
27 void IamBindings::AddMembers(google::cloud::IamBinding const& iam_binding) {
28  std::string const& role(iam_binding.role());
29  std::set<std::string> const& members = iam_binding.members();
30 
31  bindings_[role].insert(members.begin(), members.end());
32 }
33 
34 void IamBindings::AddMembers(std::string const& role,
35  std::set<std::string> const& members) {
36  bindings_[role].insert(members.begin(), members.end());
37 }
38 
39 void IamBindings::RemoveMember(std::string const& role,
40  std::string const& member) {
41  auto it = bindings_.find(role);
42  if (it == bindings_.end()) {
43  return;
44  }
45 
46  auto& members = it->second;
47  auto member_loc = members.find(member);
48 
49  if (member_loc != members.end()) {
50  members.erase(member_loc);
51  }
52  if (members.empty()) {
53  bindings_.erase(it);
54  }
55 }
56 
57 void IamBindings::RemoveMembers(google::cloud::IamBinding const& iam_binding) {
58  RemoveMembers(iam_binding.role(), iam_binding.members());
59 }
60 
61 void IamBindings::RemoveMembers(std::string const& role,
62  std::set<std::string> const& members) {
63  auto it = bindings_.find(role);
64  if (it == bindings_.end()) {
65  return;
66  }
67 
68  auto& binding_members = it->second;
69  for (auto const& member : members) {
70  auto member_loc = binding_members.find(member);
71  if (member_loc != binding_members.end()) {
72  binding_members.erase(member_loc);
73  }
74  }
75  if (binding_members.empty()) {
76  bindings_.erase(it);
77  }
78 }
79 
80 std::ostream& operator<<(std::ostream& os, IamBindings const& rhs) {
81  os << "IamBindings={";
82  char const* sep = "";
83  for (auto const& kv : rhs) {
84  os << sep << kv.first << ": [";
85  char const* sep2 = "";
86  for (auto const& member : kv.second) {
87  os << sep2 << member;
88  sep2 = ", ";
89  }
90  os << "]";
91  sep = ", ";
92  }
93  return os << "}";
94 }
95 
96 } // namespace GOOGLE_CLOUD_CPP_NS
97 } // namespace cloud
98 } // namespace google
void AddMembers(google::cloud::IamBinding const &iam_binding)
Adds a new key-value pair of role and members to the container if there is none for the role of given...
Definition: iam_bindings.cc:27
#define GOOGLE_CLOUD_CPP_NS
Definition: version.h:24
Contains all the Google Cloud C++ Library APIs.
Definition: iam_bindings.cc:21
void AddMembers(std::string const &role, std::set< std::string > const &members)
Adds a new key-value pair of role and members to the container if there no existing for given role el...
Definition: iam_bindings.cc:34
void RemoveMembers(std::string const &role, std::set< std::string > const &members)
Removes the given members from given role&#39;s member set if there exists one in container.
Definition: iam_bindings.cc:61
void AddMember(std::string const &role, std::string member)
Adds a new member if a binding exists with given role otherwise inserts a new key-value pair of role ...
Definition: iam_bindings.cc:23
void RemoveMembers(google::cloud::IamBinding const &iam_binding)
Removes the given binding&#39;s member from the given binding&#39;s role&#39;s member set if there exists one in ...
Definition: iam_bindings.cc:57
void RemoveMember(std::string const &role, std::string const &member)
Removes the given member from the given role&#39;s member set if there exists one in container.
Definition: iam_bindings.cc:39
std::set< std::string > const & members() const
Definition: iam_binding.h:38
std::string const & role() const
Definition: iam_binding.h:37
Represents a container for providing users with a handful of operation to users which they can use to...
Definition: iam_bindings.h:35
Represents a Binding which associates a member with a particular role which can be used for Identity ...
Definition: iam_binding.h:32