Google Cloud IAM C++ Client  1.32.1
A C++ Client Library for Google Cloud IAM
quickstart.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 // 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 //! [START iam_quickstart]
16 #include "google/cloud/iam/iam_client.h"
17 #include <iostream>
18 #include <stdexcept>
19 
20 int main(int argc, char* argv[]) try {
21  if (argc != 2) {
22  std::cerr << "Usage: " << argv[0] << " <project-id>\n";
23  return 1;
24  }
25 
26  std::string const project_id = argv[1];
27 
28  // Create a namespace alias to make the code easier to read.
29  namespace iam = ::google::cloud::iam;
30  iam::IAMClient client(iam::MakeIAMConnection());
31  std::cout << "Service Accounts for project: " << project_id << "\n";
32  int count = 0;
33  for (auto const& service_account :
34  client.ListServiceAccounts("projects/" + project_id)) {
35  if (!service_account) {
36  throw std::runtime_error(service_account.status().message());
37  }
38  std::cout << service_account->name() << "\n";
39  ++count;
40  }
41 
42  if (count == 0) std::cout << "No Service Accounts found.\n";
43  return 0;
44 } catch (std::exception const& ex) {
45  std::cerr << "Standard exception raised: " << ex.what() << "\n";
46  return 1;
47 }
48 //! [END iam_quickstart]