Google Cloud IAM C++ Client  1.32.1
A C++ Client Library for Google Cloud IAM
iam_client.cc
Go to the documentation of this file.
1 // Copyright 2021 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 // Generated by the Codegen C++ plugin.
16 // If you make any local changes, they will be lost.
17 // source: google/iam/admin/v1/iam.proto
18 
19 #include "google/cloud/iam/iam_client.h"
20 #include "google/cloud/iam/iam_options.h"
21 #include "google/cloud/iam/internal/iam_option_defaults.h"
22 #include <memory>
23 #include <thread>
24 
25 namespace google {
26 namespace cloud {
27 namespace iam {
29 
30 IAMClient::IAMClient(std::shared_ptr<IAMConnection> connection)
31  : connection_(std::move(connection)) {}
32 IAMClient::~IAMClient() = default;
33 
34 StreamRange<google::iam::admin::v1::ServiceAccount>
35 IAMClient::ListServiceAccounts(std::string const& name) {
36  google::iam::admin::v1::ListServiceAccountsRequest request;
37  request.set_name(name);
38  return connection_->ListServiceAccounts(request);
39 }
40 
41 StatusOr<google::iam::admin::v1::ServiceAccount> IAMClient::GetServiceAccount(
42  std::string const& name) {
43  google::iam::admin::v1::GetServiceAccountRequest request;
44  request.set_name(name);
45  return connection_->GetServiceAccount(request);
46 }
47 
48 StatusOr<google::iam::admin::v1::ServiceAccount>
50  std::string const& name, std::string const& account_id,
51  google::iam::admin::v1::ServiceAccount const& service_account) {
52  google::iam::admin::v1::CreateServiceAccountRequest request;
53  request.set_name(name);
54  request.set_account_id(account_id);
55  *request.mutable_service_account() = service_account;
56  return connection_->CreateServiceAccount(request);
57 }
58 
59 Status IAMClient::DeleteServiceAccount(std::string const& name) {
60  google::iam::admin::v1::DeleteServiceAccountRequest request;
61  request.set_name(name);
62  return connection_->DeleteServiceAccount(request);
63 }
64 
65 StatusOr<google::iam::admin::v1::ListServiceAccountKeysResponse>
67  std::string const& name,
68  std::vector<
69  google::iam::admin::v1::ListServiceAccountKeysRequest::KeyType> const&
70  key_types) {
71  google::iam::admin::v1::ListServiceAccountKeysRequest request;
72  request.set_name(name);
73  *request.mutable_key_types() = {key_types.begin(), key_types.end()};
74  return connection_->ListServiceAccountKeys(request);
75 }
76 
77 StatusOr<google::iam::admin::v1::ServiceAccountKey>
79  std::string const& name,
80  google::iam::admin::v1::ServiceAccountPublicKeyType public_key_type) {
81  google::iam::admin::v1::GetServiceAccountKeyRequest request;
82  request.set_name(name);
83  request.set_public_key_type(public_key_type);
84  return connection_->GetServiceAccountKey(request);
85 }
86 
87 StatusOr<google::iam::admin::v1::ServiceAccountKey>
89  std::string const& name,
90  google::iam::admin::v1::ServiceAccountPrivateKeyType private_key_type,
91  google::iam::admin::v1::ServiceAccountKeyAlgorithm key_algorithm) {
92  google::iam::admin::v1::CreateServiceAccountKeyRequest request;
93  request.set_name(name);
94  request.set_private_key_type(private_key_type);
95  request.set_key_algorithm(key_algorithm);
96  return connection_->CreateServiceAccountKey(request);
97 }
98 
99 Status IAMClient::DeleteServiceAccountKey(std::string const& name) {
100  google::iam::admin::v1::DeleteServiceAccountKeyRequest request;
101  request.set_name(name);
102  return connection_->DeleteServiceAccountKey(request);
103 }
104 
105 StatusOr<google::iam::v1::Policy> IAMClient::GetIamPolicy(
106  std::string const& resource) {
107  google::iam::v1::GetIamPolicyRequest request;
108  request.set_resource(resource);
109  return connection_->GetIamPolicy(request);
110 }
111 
112 StatusOr<google::iam::v1::Policy> IAMClient::SetIamPolicy(
113  std::string const& resource, google::iam::v1::Policy const& policy) {
114  google::iam::v1::SetIamPolicyRequest request;
115  request.set_resource(resource);
116  *request.mutable_policy() = policy;
117  return connection_->SetIamPolicy(request);
118 }
119 
120 StatusOr<google::iam::v1::Policy> IAMClient::SetIamPolicy(
121  std::string const& resource, IamUpdater const& updater, Options options) {
122  internal::CheckExpectedOptions<IAMBackoffPolicyOption>(options, __func__);
123  options = iam_internal::IAMDefaultOptions(std::move(options));
124  auto backoff_policy = options.get<IAMBackoffPolicyOption>()->clone();
125  for (;;) {
126  auto recent = GetIamPolicy(resource);
127  if (!recent) {
128  return recent.status();
129  }
130  auto policy = updater(*std::move(recent));
131  if (!policy) {
132  return Status(StatusCode::kCancelled, "updater did not yield a policy");
133  }
134  auto result = SetIamPolicy(resource, *std::move(policy));
135  if (result || result.status().code() != StatusCode::kAborted) {
136  return result;
137  }
138  std::this_thread::sleep_for(backoff_policy->OnCompletion());
139  }
140 }
141 
142 StatusOr<google::iam::v1::TestIamPermissionsResponse>
143 IAMClient::TestIamPermissions(std::string const& resource,
144  std::vector<std::string> const& permissions) {
145  google::iam::v1::TestIamPermissionsRequest request;
146  request.set_resource(resource);
147  *request.mutable_permissions() = {permissions.begin(), permissions.end()};
148  return connection_->TestIamPermissions(request);
149 }
150 
151 StreamRange<google::iam::admin::v1::Role> IAMClient::QueryGrantableRoles(
152  std::string const& full_resource_name) {
153  google::iam::admin::v1::QueryGrantableRolesRequest request;
154  request.set_full_resource_name(full_resource_name);
155  return connection_->QueryGrantableRoles(request);
156 }
157 
158 StreamRange<google::iam::admin::v1::ServiceAccount>
160  google::iam::admin::v1::ListServiceAccountsRequest request) {
161  return connection_->ListServiceAccounts(std::move(request));
162 }
163 
164 StatusOr<google::iam::admin::v1::ServiceAccount> IAMClient::GetServiceAccount(
165  google::iam::admin::v1::GetServiceAccountRequest const& request) {
166  return connection_->GetServiceAccount(request);
167 }
168 
169 StatusOr<google::iam::admin::v1::ServiceAccount>
171  google::iam::admin::v1::CreateServiceAccountRequest const& request) {
172  return connection_->CreateServiceAccount(request);
173 }
174 
175 StatusOr<google::iam::admin::v1::ServiceAccount> IAMClient::PatchServiceAccount(
176  google::iam::admin::v1::PatchServiceAccountRequest const& request) {
177  return connection_->PatchServiceAccount(request);
178 }
179 
181  google::iam::admin::v1::DeleteServiceAccountRequest const& request) {
182  return connection_->DeleteServiceAccount(request);
183 }
184 
185 StatusOr<google::iam::admin::v1::UndeleteServiceAccountResponse>
187  google::iam::admin::v1::UndeleteServiceAccountRequest const& request) {
188  return connection_->UndeleteServiceAccount(request);
189 }
190 
192  google::iam::admin::v1::EnableServiceAccountRequest const& request) {
193  return connection_->EnableServiceAccount(request);
194 }
195 
197  google::iam::admin::v1::DisableServiceAccountRequest const& request) {
198  return connection_->DisableServiceAccount(request);
199 }
200 
201 StatusOr<google::iam::admin::v1::ListServiceAccountKeysResponse>
203  google::iam::admin::v1::ListServiceAccountKeysRequest const& request) {
204  return connection_->ListServiceAccountKeys(request);
205 }
206 
207 StatusOr<google::iam::admin::v1::ServiceAccountKey>
209  google::iam::admin::v1::GetServiceAccountKeyRequest const& request) {
210  return connection_->GetServiceAccountKey(request);
211 }
212 
213 StatusOr<google::iam::admin::v1::ServiceAccountKey>
215  google::iam::admin::v1::CreateServiceAccountKeyRequest const& request) {
216  return connection_->CreateServiceAccountKey(request);
217 }
218 
219 StatusOr<google::iam::admin::v1::ServiceAccountKey>
221  google::iam::admin::v1::UploadServiceAccountKeyRequest const& request) {
222  return connection_->UploadServiceAccountKey(request);
223 }
224 
226  google::iam::admin::v1::DeleteServiceAccountKeyRequest const& request) {
227  return connection_->DeleteServiceAccountKey(request);
228 }
229 
230 StatusOr<google::iam::v1::Policy> IAMClient::GetIamPolicy(
231  google::iam::v1::GetIamPolicyRequest const& request) {
232  return connection_->GetIamPolicy(request);
233 }
234 
235 StatusOr<google::iam::v1::Policy> IAMClient::SetIamPolicy(
236  google::iam::v1::SetIamPolicyRequest const& request) {
237  return connection_->SetIamPolicy(request);
238 }
239 
240 StatusOr<google::iam::v1::TestIamPermissionsResponse>
242  google::iam::v1::TestIamPermissionsRequest const& request) {
243  return connection_->TestIamPermissions(request);
244 }
245 
246 StreamRange<google::iam::admin::v1::Role> IAMClient::QueryGrantableRoles(
247  google::iam::admin::v1::QueryGrantableRolesRequest request) {
248  return connection_->QueryGrantableRoles(std::move(request));
249 }
250 
251 StreamRange<google::iam::admin::v1::Role> IAMClient::ListRoles(
252  google::iam::admin::v1::ListRolesRequest request) {
253  return connection_->ListRoles(std::move(request));
254 }
255 
256 StatusOr<google::iam::admin::v1::Role> IAMClient::GetRole(
257  google::iam::admin::v1::GetRoleRequest const& request) {
258  return connection_->GetRole(request);
259 }
260 
261 StatusOr<google::iam::admin::v1::Role> IAMClient::CreateRole(
262  google::iam::admin::v1::CreateRoleRequest const& request) {
263  return connection_->CreateRole(request);
264 }
265 
266 StatusOr<google::iam::admin::v1::Role> IAMClient::UpdateRole(
267  google::iam::admin::v1::UpdateRoleRequest const& request) {
268  return connection_->UpdateRole(request);
269 }
270 
271 StatusOr<google::iam::admin::v1::Role> IAMClient::DeleteRole(
272  google::iam::admin::v1::DeleteRoleRequest const& request) {
273  return connection_->DeleteRole(request);
274 }
275 
276 StatusOr<google::iam::admin::v1::Role> IAMClient::UndeleteRole(
277  google::iam::admin::v1::UndeleteRoleRequest const& request) {
278  return connection_->UndeleteRole(request);
279 }
280 
281 StreamRange<google::iam::admin::v1::Permission>
283  google::iam::admin::v1::QueryTestablePermissionsRequest request) {
284  return connection_->QueryTestablePermissions(std::move(request));
285 }
286 
287 StatusOr<google::iam::admin::v1::QueryAuditableServicesResponse>
289  google::iam::admin::v1::QueryAuditableServicesRequest const& request) {
290  return connection_->QueryAuditableServices(request);
291 }
292 
293 StatusOr<google::iam::admin::v1::LintPolicyResponse> IAMClient::LintPolicy(
294  google::iam::admin::v1::LintPolicyRequest const& request) {
295  return connection_->LintPolicy(request);
296 }
297 
298 } // namespace GOOGLE_CLOUD_CPP_GENERATED_NS
299 } // namespace iam
300 } // namespace cloud
301 } // namespace google