Google Cloud C++ Client  1.32.1
C++ Client Library for Google Cloud Platform
iam_bindings.h
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_IAM_BINDINGS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_IAM_BINDINGS_H
17 
18 #include "google/cloud/iam_binding.h"
19 #include "google/cloud/version.h"
20 // TODO(#5929) - remove after decommission is completed
21 #include "google/cloud/internal/disable_deprecation_warnings.inc"
22 #include <map>
23 #include <set>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace google {
29 namespace cloud {
30 inline namespace GOOGLE_CLOUD_CPP_NS {
31 /**
32  * Simplified view of multiple roles and their members for IAM.
33  *
34  * @deprecated this class is deprecated. Any functions that use it have also
35  * been deprecated. The class was defined before IAM conditional bindings,
36  * and does not support them. Nor will it be able to support future IAM
37  * features. Please use the alternative functions.
38  *
39  * @see [Identity and Access Management](https://cloud.google.com/iam)
40  * @see [Overview of IAM Conditions][iam-conditions]
41  *
42  * [iam-conditions]: https://cloud.google.com/iam/docs/conditions-overview
43  */
44 class IamBindings {
45  public:
46  IamBindings() = default;
47 
49  // NOLINTNEXTLINE(performance-unnecessary-value-param)
50  std::vector<IamBinding> bindings) {
51  for (auto& it : bindings) {
52  bindings_.insert({std::move(it.role()), std::move(it.members())});
53  }
54  }
55 
57  std::set<std::string> members) {
58  bindings_.insert({std::move(role), std::move(members)});
59  }
60 
61  using iterator = std::map<std::string, std::set<std::string>>::const_iterator;
62 
63  /**
64  * Returns an iterator referring to the first element in IamBindings
65  * container.
66  */
68  return bindings_.begin();
69  }
70 
71  /**
72  * Returns an iterator referring to the past-the-end element in IamBindings
73  * container.
74  */
76  return bindings_.end();
77  }
78 
79  /**
80  * Returns whether the Bindings container is empty.
81  *
82  * @return bool whether the container is empty or not.
83  */
85  return bindings_.empty();
86  }
87 
88  /**
89  * Return number of Bindings in container.
90  *
91  * @return int the size of the container.
92  */
93  GOOGLE_CLOUD_CPP_IAM_DEPRECATED std::size_t size() const {
94  return bindings_.size();
95  }
96 
98  std::map<std::string, std::set<std::string>> const& bindings() const {
99  return bindings_;
100  }
101 
102  /**
103  * Finds the members for a role.
104  */
105  GOOGLE_CLOUD_CPP_IAM_DEPRECATED iterator find(std::string const& role) const {
106  return bindings_.find(role);
107  }
108 
109  /// Returns the members for a role.
110  GOOGLE_CLOUD_CPP_IAM_DEPRECATED std::set<std::string> at(
111  std::string const& role) const {
112  auto loc = bindings_.find(role);
113  if (loc == bindings_.end()) {
114  return {};
115  }
116  return loc->second;
117  }
118 
119  /**
120  * Adds a new member if a binding exists with given role otherwise inserts
121  * a new key-value pair of role and member to the container.
122  *
123  * @param role role of the new member.
124  * @param member specifies the identity requesting access for a cloud
125  * platform resource.
126  */
127  GOOGLE_CLOUD_CPP_IAM_DEPRECATED void AddMember(std::string const& role,
128  std::string member);
129 
130  /**
131  * Adds a new key-value pair of role and members to the container if there is
132  * none for the role of given binding else appends members of given binding
133  * to the associated role's key-value entry.
134  *
135  * @param iam_binding binding representing a set of members and role for them.
136  */
138  google::cloud::IamBinding const& iam_binding);
139 
140  /**
141  * Adds a new key-value pair of role and members to the container if there no
142  * existing for given role else appends the given members to the give role's
143  * member set.
144  *
145  * @param role role of the member set to be added.
146  * @param members a set of member which are needed to be added.
147  */
149  std::string const& role, std::set<std::string> const& members);
150 
151  /**
152  * Removes the given member from the given role's member set if there exists
153  * one in container.
154  *
155  * @param role role of the member to be removed.
156  * @param member specifies the identity requesting access for a cloud
157  * platform resource.
158  */
159  GOOGLE_CLOUD_CPP_IAM_DEPRECATED void RemoveMember(std::string const& role,
160  std::string const& member);
161 
162  /**
163  * Removes the given binding's member from the given binding's role's member
164  * set if there exists one in container.
165  *
166  * @param iam_binding binding representing a set of members and role for them.
167  */
169  google::cloud::IamBinding const& iam_binding);
170 
171  /**
172  * Removes the given members from given role's member set if there exists one
173  * in container.
174  *
175  * @param role role of the member set to be removed.
176  * @param members a set of members which are needed to be removed.
177  */
179  std::string const& role, std::set<std::string> const& members);
180 
181  private:
182  std::map<std::string, std::set<std::string>> bindings_;
183 };
184 
185 inline bool operator==(IamBindings const& lhs, IamBindings const& rhs) {
186  return lhs.bindings() == rhs.bindings();
187 }
188 
189 inline bool operator<(IamBindings const& lhs, IamBindings const& rhs) {
190  return lhs.bindings() < rhs.bindings();
191 }
192 
193 inline bool operator!=(IamBindings const& lhs, IamBindings const& rhs) {
194  return std::rel_ops::operator!=(lhs, rhs);
195 }
196 
197 inline bool operator>(IamBindings const& lhs, IamBindings const& rhs) {
198  return std::rel_ops::operator>(lhs, rhs);
199 }
200 
201 inline bool operator<=(IamBindings const& lhs, IamBindings const& rhs) {
202  return std::rel_ops::operator<=(lhs, rhs);
203 }
204 
205 inline bool operator>=(IamBindings const& lhs, IamBindings const& rhs) {
206  return std::rel_ops::operator>=(lhs, rhs);
207 }
208 
209 std::ostream& operator<<(std::ostream& os, IamBindings const& rhs);
210 
211 } // namespace GOOGLE_CLOUD_CPP_NS
212 } // namespace cloud
213 } // namespace google
214 
215 #include "google/cloud/internal/diagnostics_pop.inc"
216 
217 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_IAM_BINDINGS_H