Google Cloud Bigtable C++ Client  2.0.0
A C++ Client Library for Google Cloud Bigtable
iam_policy.h
Go to the documentation of this file.
1 // Copyright 2019 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 // https://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_BIGTABLE_IAM_POLICY_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_IAM_POLICY_H
17 
18 #include "google/cloud/bigtable/iam_binding.h"
19 #include "google/cloud/bigtable/version.h"
20 #include <list>
21 #include <string>
22 #include <vector>
23 
24 namespace google {
25 namespace cloud {
26 namespace bigtable {
28 template <class InputIt>
29 /**
30  * Create a google::iam::v1::Policy.
31  *
32  * @see
33  * https://cloud.google.com/resource-manager/reference/rest/Shared.Types/Policy
34  * for more information about a IAM policies.
35  *
36  * @see https://tools.ietf.org/html/rfc7232#section-2.3 for more information
37  * about ETags.
38  *
39  * @warning ETags are currently not used by Cloud Bigtable.
40  *
41  * @param first_binding iterator pointing to the first google::iam::v1::Binding
42  * @param last_binding iterator pointing to past last google::iam::v1::Binding
43  * @param etag used for optimistic concurrency control
44  * @param version currently unused
45  *
46  *
47  * @return The policy
48  */
49 google::iam::v1::Policy IamPolicy(InputIt first_binding, InputIt last_binding,
50  std::string etag = "",
51  std::int32_t version = 0) {
52  google::iam::v1::Policy res;
53  for (auto binding = first_binding; binding != last_binding; ++binding) {
54  *res.add_bindings() = *binding;
55  }
56  res.set_version(version);
57  res.set_etag(std::move(etag));
58  return res;
59 }
60 
61 /**
62  * Create a google::iam::v1::Policy.
63  *
64  * @see
65  * https://cloud.google.com/resource-manager/reference/rest/Shared.Types/Policy
66  * for more information about a IAM policies.
67  *
68  * @see https://tools.ietf.org/html/rfc7232#section-2.3 for more information
69  * about ETags.
70  *
71  * @warning ETags are currently not used by Cloud Bigtable.
72  *
73  * @param bindings initializer_list of google::iam::v1::Binding
74  * @param etag used for optimistic concurrency control
75  * @param version currently unused
76  *
77  * @return The policy
78  */
79 google::iam::v1::Policy IamPolicy(
80  std::initializer_list<google::iam::v1::Binding> bindings,
81  std::string etag = "", std::int32_t version = 0);
82 
83 /**
84  * Create a google::iam::v1::Policy.
85  *
86  * @see
87  * https://cloud.google.com/resource-manager/reference/rest/Shared.Types/Policy
88  * for more information about a IAM policies.
89  *
90  * @see https://tools.ietf.org/html/rfc7232#section-2.3 for more information
91  * about ETags.
92  *
93  * @warning ETags are currently not used by Cloud Bigtable.
94  *
95  * @param bindings vector of google::iam::v1::Binding
96  * @param etag used for optimistic concurrency control
97  * @param version currently unused
98  *
99  * @return The policy
100  */
101 google::iam::v1::Policy IamPolicy(
102  std::vector<google::iam::v1::Binding> bindings, std::string etag = "",
103  std::int32_t version = 0);
104 
105 std::ostream& operator<<(std::ostream& os, google::iam::v1::Policy const& rhs);
106 
107 /**
108  * Remove all bindings matching a predicate from a policy.
109  *
110  * @param policy the policy to remove from
111  * @param pred predicate indicating whether to remove a binding
112  *
113  * @tparam Functor the type of the predicate; it should be invocable with
114  * `google::iam::v1::Binding const&` and return a bool.
115  *
116  * @return number of bindings removed.
117  */
118 template <typename Functor>
119 size_t RemoveBindingsFromPolicyIf(google::iam::v1::Policy& policy,
120  Functor pred) {
121  auto& bindings = *policy.mutable_bindings();
122  auto new_end =
123  std::remove_if(bindings.begin(), bindings.end(), std::move(pred));
124  size_t res = std::distance(new_end, bindings.end());
125  for (size_t i = 0; i < res; ++i) {
126  bindings.RemoveLast();
127  }
128  return res;
129 }
130 
131 /**
132  * Remove a specific binding from a policy.
133  *
134  * @param policy the policy to remove from
135  * @param to_remove the iterator indicating the binding; it should be retrieved
136  * from the `mutable_bindings()` member
137  */
139  google::iam::v1::Policy& policy,
140  google::protobuf::RepeatedPtrField<google::iam::v1::Binding>::iterator
141  to_remove);
142 
144 } // namespace bigtable
145 } // namespace cloud
146 } // namespace google
147 
148 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_IAM_POLICY_H