forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam_bindings.cc
100 lines (87 loc) · 3.02 KB
/
iam_bindings.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2018 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/iam_bindings.h"
#include "google/cloud/internal/absl_str_join_quiet.h"
#include <algorithm>
#include <iostream>
#include <iterator>
// TODO(#5929) - remove after decommission is completed
#include "google/cloud/internal/disable_deprecation_warnings.inc"
namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
void IamBindings::AddMember(std::string const& role, std::string member) {
bindings_[role].emplace(std::move(member));
}
void IamBindings::AddMembers(google::cloud::IamBinding const& iam_binding) {
std::string const& role(iam_binding.role());
std::set<std::string> const& members = iam_binding.members();
bindings_[role].insert(members.begin(), members.end());
}
void IamBindings::AddMembers(std::string const& role,
std::set<std::string> const& members) {
bindings_[role].insert(members.begin(), members.end());
}
void IamBindings::RemoveMember(std::string const& role,
std::string const& member) {
auto it = bindings_.find(role);
if (it == bindings_.end()) {
return;
}
auto& members = it->second;
auto member_loc = members.find(member);
if (member_loc != members.end()) {
members.erase(member_loc);
}
if (members.empty()) {
bindings_.erase(it);
}
}
void IamBindings::RemoveMembers(google::cloud::IamBinding const& iam_binding) {
RemoveMembers(iam_binding.role(), iam_binding.members());
}
void IamBindings::RemoveMembers(std::string const& role,
std::set<std::string> const& members) {
auto it = bindings_.find(role);
if (it == bindings_.end()) {
return;
}
auto& binding_members = it->second;
for (auto const& member : members) {
auto member_loc = binding_members.find(member);
if (member_loc != binding_members.end()) {
binding_members.erase(member_loc);
}
}
if (binding_members.empty()) {
bindings_.erase(it);
}
}
std::ostream& operator<<(std::ostream& os, IamBindings const& rhs) {
os << "IamBindings={";
struct IamBindingFormatter {
void operator()(std::string* out,
std::pair<std::string, std::set<std::string>> const& rhs) {
out->append(rhs.first);
out->append(": [");
out->append(absl::StrJoin(rhs.second, ", "));
out->append("]");
}
};
os << absl::StrJoin(rhs, ", ", IamBindingFormatter{});
return os << "}";
}
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google