1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- #include " google/cloud/iam/iam_credentials_client.h"
15+ #include " google/cloud/iam/credentials/v1/ iam_credentials_client.h"
1616#include " google/cloud/spanner/admin/instance_admin_client.h"
1717#include " google/cloud/common_options.h"
18+ #include " google/cloud/credentials.h"
1819#include " google/cloud/grpc_options.h"
1920#include " google/cloud/internal/getenv.h"
2021#include " google/cloud/log.h"
@@ -80,18 +81,10 @@ google::cloud::StatusOr<std::string> HttpGet(std::string const& url,
8081 return buffer;
8182}
8283
83- // TODO(#6185) - this should be done by the generated code
84- std::set<std::string> DefaultTracingComponents () {
85- return absl::StrSplit (
86- google::cloud::internal::GetEnv (" GOOGLE_CLOUD_CPP_ENABLE_TRACING" )
87- .value_or (" " ),
88- ' ,' );
89- }
90-
9184google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken (
92- google::cloud::iam ::IAMCredentialsClient client,
85+ google::cloud::iam_credentials_v1 ::IAMCredentialsClient client,
9386 std::vector<std::string> const & argv) {
94- namespace iam = ::google::cloud::iam ;
87+ namespace iam = ::google::cloud::iam_credentials_v1 ;
9588 return [](iam::IAMCredentialsClient client,
9689 std::string const & service_account, std::string const & project_id) {
9790 google::protobuf::Duration duration;
@@ -109,14 +102,13 @@ google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken(
109102 << " , which will expire around " << absl::FromChrono (expiration)
110103 << std::endl;
111104
112- auto credentials = grpc::CompositeChannelCredentials (
113- grpc::SslCredentials ({}),
114- grpc::AccessTokenCredentials (token->access_token ()));
105+ auto credentials = google::cloud::MakeAccessTokenCredentials (
106+ token->access_token (), expiration);
115107
116108 google::cloud::spanner_admin::InstanceAdminClient admin (
117109 google::cloud::spanner_admin::MakeInstanceAdminConnection (
118- google::cloud::Options{}. set <google::cloud::GrpcCredentialOption>(
119- credentials)));
110+ google::cloud::Options{}
111+ . set <google::cloud::UnifiedCredentialsOption>( credentials)));
120112 for (auto config : admin.ListInstanceConfigs (
121113 google::cloud::Project (project_id).FullName ())) {
122114 if (!config) throw std::move (config).status ();
@@ -127,8 +119,9 @@ google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken(
127119 }(std::move (client), argv.at (0 ), argv.at (1 ));
128120}
129121
130- void UseAccessTokenUntilExpired (google::cloud::iam::IAMCredentialsClient client,
131- std::vector<std::string> const & argv) {
122+ void UseAccessTokenUntilExpired (
123+ google::cloud::iam_credentials_v1::IAMCredentialsClient client,
124+ std::vector<std::string> const & argv) {
132125 auto token = UseAccessToken (std::move (client), argv);
133126 auto const & project_id = argv.at (1 );
134127 auto const expiration =
@@ -139,13 +132,12 @@ void UseAccessTokenUntilExpired(google::cloud::iam::IAMCredentialsClient client,
139132 << absl::FromChrono (expiration) << " )" << std::endl;
140133
141134 auto iteration = [=](bool expired) {
142- auto credentials = grpc::CompositeChannelCredentials (
143- grpc::SslCredentials ({}),
144- grpc::AccessTokenCredentials (token.access_token ()));
135+ auto credentials = google::cloud::MakeAccessTokenCredentials (
136+ token.access_token (), expiration);
145137 google::cloud::spanner_admin::InstanceAdminClient admin (
146138 google::cloud::spanner_admin::MakeInstanceAdminConnection (
147- google::cloud::Options{}. set <google::cloud::GrpcCredentialOption>(
148- credentials)));
139+ google::cloud::Options{}
140+ . set <google::cloud::UnifiedCredentialsOption>( credentials)));
149141 for (auto config : admin.ListInstanceConfigs (
150142 google::cloud::Project (project_id).FullName ())) {
151143 // kUnauthenticated receives special treatment, it is the error received
@@ -178,9 +170,10 @@ void UseAccessTokenUntilExpired(google::cloud::iam::IAMCredentialsClient client,
178170 }
179171}
180172
181- void UseIdTokenHttp (google::cloud::iam::IAMCredentialsClient client,
182- std::vector<std::string> const & argv) {
183- namespace iam = ::google::cloud::iam;
173+ void UseIdTokenHttp (
174+ google::cloud::iam_credentials_v1::IAMCredentialsClient client,
175+ std::vector<std::string> const & argv) {
176+ namespace iam = ::google::cloud::iam_credentials_v1;
184177 [](iam::IAMCredentialsClient client, std::string const & service_account,
185178 std::string const & hello_world_url) {
186179 auto token = client.GenerateIdToken (
@@ -203,9 +196,10 @@ void UseIdTokenHttp(google::cloud::iam::IAMCredentialsClient client,
203196 }(std::move (client), argv.at (0 ), argv.at (1 ));
204197}
205198
206- void UseIdTokenGrpc (google::cloud::iam::IAMCredentialsClient client,
207- std::vector<std::string> const & argv) {
208- namespace iam = ::google::cloud::iam;
199+ void UseIdTokenGrpc (
200+ google::cloud::iam_credentials_v1::IAMCredentialsClient client,
201+ std::vector<std::string> const & argv) {
202+ namespace iam = ::google::cloud::iam_credentials_v1;
209203 [](iam::IAMCredentialsClient client, std::string const & service_account,
210204 std::string const & url) {
211205 auto token = client.GenerateIdToken (
@@ -245,6 +239,7 @@ void UseIdTokenGrpc(google::cloud::iam::IAMCredentialsClient client,
245239
246240void AutoRun (std::vector<std::string> const & argv) {
247241 namespace examples = ::google::cloud::testing_util;
242+ namespace iam = ::google::cloud::iam_credentials_v1;
248243 using ::google::cloud::internal::GetEnv;
249244
250245 if (!argv.empty ()) throw examples::Usage{" auto" };
@@ -265,17 +260,13 @@ void AutoRun(std::vector<std::string> const& argv) {
265260 auto const hello_world_grpc_url =
266261 GetEnv (" GOOGLE_CLOUD_CPP_TEST_HELLO_WORLD_GRPC_URL" ).value_or (" " );
267262
268- auto client = google::cloud::iam::IAMCredentialsClient (
269- google::cloud::iam::MakeIAMCredentialsConnection (
270- google::cloud::Options{}
271- .set <google::cloud::TracingComponentsOption>(
272- DefaultTracingComponents ())
273- .set <google::cloud::GrpcTracingOptionsOption>(
274- // There are some credentials returned by RPCs. On an error
275- // these are printed. This truncates them, making the output
276- // safe, and yet useful for debugging.
277- google::cloud::TracingOptions{}.SetOptions (
278- " truncate_string_field_longer_than=32" ))));
263+ auto client = iam::IAMCredentialsClient (iam::MakeIAMCredentialsConnection (
264+ google::cloud::Options{}.set <google::cloud::GrpcTracingOptionsOption>(
265+ // There are some credentials returned by RPCs. On an error
266+ // these are printed. This truncates them, making the output
267+ // safe, and yet useful for debugging.
268+ google::cloud::TracingOptions{}.SetOptions (
269+ " truncate_string_field_longer_than=32" ))));
279270
280271 std::cout << " \n Running UseAccessToken() example" << std::endl;
281272 UseAccessToken (client, {test_iam_service_account, project_id});
@@ -294,9 +285,10 @@ void AutoRun(std::vector<std::string> const& argv) {
294285
295286int main (int argc, char * argv[]) { // NOLINT(bugprone-exception-escape)
296287 using ::google::cloud::testing_util::Example;
288+ namespace iam = ::google::cloud::iam_credentials_v1;
297289
298- using ClientCommand = std::function<void (
299- google::cloud::iam::IAMCredentialsClient, std::vector<std::string> argv)>;
290+ using ClientCommand = std::function<void (iam::IAMCredentialsClient,
291+ std::vector<std::string> argv)>;
300292
301293 auto make_entry = [](std::string name,
302294 std::vector<std::string> const & arg_names,
@@ -308,11 +300,8 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
308300 for (auto const & a : arg_names) usage += " <" + a + " >" ;
309301 throw google::cloud::testing_util::Usage{std::move (usage)};
310302 }
311- auto client = google::cloud::iam::IAMCredentialsClient (
312- google::cloud::iam::MakeIAMCredentialsConnection (
313- google::cloud::Options{}
314- .set <google::cloud::TracingComponentsOption>(
315- DefaultTracingComponents ())));
303+ auto client =
304+ iam::IAMCredentialsClient (iam::MakeIAMCredentialsConnection ());
316305 command (client, std::move (argv));
317306 };
318307 return google::cloud::testing_util::Commands::value_type (std::move (name),
0 commit comments