Google Cloud C++ Client  0.4.0
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 #include <map>
21 #include <set>
22 #include <vector>
23 
24 namespace google {
25 namespace cloud {
26 inline namespace GOOGLE_CLOUD_CPP_NS {
27 /**
28  * Represents a container for providing users with a handful of operation to
29  * users which they can use to add and remove members to Binding which is used
30  * for defining IAM Policy for Cloud Platform Resources.
31  *
32  * For more information about a Binding please refer to:
33  * https://cloud.google.com/resource-manager/reference/rest/Shared.Types/Binding
34  */
35 class IamBindings {
36  public:
37  IamBindings() = default;
38 
39  explicit IamBindings(std::vector<IamBinding> bindings) {
40  for (auto& it : bindings) {
41  bindings_.insert({std::move(it.role()), std::move(it.members())});
42  }
43  }
44 
45  IamBindings(std::string const& role, std::set<std::string> const& members) {
46  bindings_.insert({std::move(role), std::move(members)});
47  }
48 
49  using iterator = std::map<std::string, std::set<std::string>>::const_iterator;
50 
51  /**
52  * Returns an iterator referring to the first element in IamBindings
53  * container.
54  */
55  iterator begin() const { return bindings_.begin(); }
56 
57  /**
58  * Returns an iterator referring to the past-the-end element in IamBindings
59  * container.
60  */
61  iterator end() const { return bindings_.end(); }
62 
63  /**
64  * Returns whether the Bindings container is empty.
65  *
66  * @return bool whether the container is empty or not.
67  */
68  bool empty() const { return bindings_.empty(); }
69 
70  /**
71  * Return number of Bindings in container.
72  *
73  * @return int the size of the container.
74  */
75  std::size_t size() const { return bindings_.size(); }
76 
77  std::map<std::string, std::set<std::string>> const& bindings() const {
78  return bindings_;
79  }
80 
81  /**
82  * Finds the members for a role.
83  */
84  iterator find(std::string const& role) const { return bindings_.find(role); }
85 
86  /// Returns the members for a role.
87  std::set<std::string> at(std::string const& role) const {
88  auto loc = bindings_.find(role);
89  if (loc == bindings_.end()) {
90  return {};
91  }
92  return loc->second;
93  }
94 
95  /**
96  * Adds a new member if a binding exists with given role otherwise inserts
97  * a new key-value pair of role and member to the container.
98  *
99  * @param role role of the new member.
100  * @param member specifies the identity requesting access for a cloud
101  * platform resource.
102  */
103  void AddMember(std::string const& role, std::string member);
104 
105  /**
106  * Adds a new key-value pair of role and members to the container if there is
107  * none for the role of given binding else appends members of given binding
108  * to the associated role's key-value entry.
109  *
110  * @param iam_binding binding representing a set of members and role for them.
111  */
112  void AddMembers(google::cloud::IamBinding const& iam_binding);
113 
114  /**
115  * Adds a new key-value pair of role and members to the container if there no
116  * existing for given role else appends the given members to the give role's
117  * member set.
118  *
119  * @param role role of the member set to be added.
120  * @param members a set of member which are needed to be added.
121  */
122  void AddMembers(std::string const& role,
123  std::set<std::string> const& members);
124 
125  /**
126  * Removes the given member from the given role's member set if there exists
127  * one in container.
128  *
129  * @param role role of the member to be removed.
130  * @param member specifies the identity requesting access for a cloud
131  * platform resource.
132  */
133  void RemoveMember(std::string const& role, std::string const& member);
134 
135  /**
136  * Removes the given binding's member from the given binding's role's member
137  * set if there exists one in container.
138  *
139  * @param iam_binding binding representing a set of members and role for them.
140  */
141  void RemoveMembers(google::cloud::IamBinding const& iam_binding);
142 
143  /**
144  * Removes the given members from given role's member set if there exists one
145  * in container.
146  *
147  * @param role role of the member set to be removed.
148  * @param members a set of members which are needed to be removed.
149  */
150  void RemoveMembers(std::string const& role,
151  std::set<std::string> const& members);
152 
153  bool operator==(IamBindings const& rhs) const {
154  return bindings_ == rhs.bindings_;
155  }
156 
157  bool operator<(IamBindings const& rhs) const {
158  return bindings_ < rhs.bindings_;
159  }
160 
161  bool operator!=(IamBindings const& rhs) const {
162  return std::rel_ops::operator!=(*this, rhs);
163  }
164 
165  bool operator>(IamBindings const& rhs) const {
166  return std::rel_ops::operator>(*this, rhs);
167  }
168 
169  bool operator<=(IamBindings const& rhs) const {
170  return std::rel_ops::operator<=(*this, rhs);
171  }
172 
173  bool operator>=(IamBindings const& rhs) const {
174  return std::rel_ops::operator>=(*this, rhs);
175  }
176 
177  private:
178  std::map<std::string, std::set<std::string>> bindings_;
179 };
180 
181 std::ostream& operator<<(std::ostream& os, IamBindings const& rhs);
182 
183 } // namespace GOOGLE_CLOUD_CPP_NS
184 } // namespace cloud
185 } // namespace google
186 
187 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_IAM_BINDINGS_H_
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
bool operator<(IamBindings const &rhs) const
Definition: iam_bindings.h:157
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
IamBindings(std::vector< IamBinding > bindings)
Definition: iam_bindings.h:39
std::set< std::string > at(std::string const &role) const
Returns the members for a role.
Definition: iam_bindings.h:87
bool operator>(IamBindings const &rhs) const
Definition: iam_bindings.h:165
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
std::size_t size() const
Return number of Bindings in container.
Definition: iam_bindings.h:75
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
iterator end() const
Returns an iterator referring to the past-the-end element in IamBindings container.
Definition: iam_bindings.h:61
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
IamBindings(std::string const &role, std::set< std::string > const &members)
Definition: iam_bindings.h:45
std::map< std::string, std::set< std::string > > const & bindings() const
Definition: iam_bindings.h:77
bool empty() const
Returns whether the Bindings container is empty.
Definition: iam_bindings.h:68
bool operator>=(IamBindings const &rhs) const
Definition: iam_bindings.h:173
std::set< std::string > const & members() const
Definition: iam_binding.h:38
iterator find(std::string const &role) const
Finds the members for a role.
Definition: iam_bindings.h:84
std::string const & role() const
Definition: iam_binding.h:37
bool operator<=(IamBindings const &rhs) const
Definition: iam_bindings.h:169
Represents a container for providing users with a handful of operation to users which they can use to...
Definition: iam_bindings.h:35
iterator begin() const
Returns an iterator referring to the first element in IamBindings container.
Definition: iam_bindings.h:55
bool operator!=(IamBindings const &rhs) const
Definition: iam_bindings.h:161
bool operator==(IamBindings const &rhs) const
Definition: iam_bindings.h:153
Represents a Binding which associates a member with a particular role which can be used for Identity ...
Definition: iam_binding.h:32