Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generator): generate authentication example #10138

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "generator/integration_tests/golden/golden_kitchen_sink_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: GoldenKitchenSinkClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::golden::GoldenKitchenSinkClient(
google::cloud::golden::MakeGoldenKitchenSinkConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: GoldenKitchenSinkClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::golden::GoldenKitchenSinkClient(
google::cloud::golden::MakeGoldenKitchenSinkConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"
});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "generator/integration_tests/golden/golden_thing_admin_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: GoldenThingAdminClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::golden::GoldenThingAdminClient(
google::cloud::golden::MakeGoldenThingAdminConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: GoldenThingAdminClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::golden::GoldenThingAdminClient(
google::cloud::golden::MakeGoldenThingAdminConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"
});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
39 changes: 35 additions & 4 deletions generator/internal/sample_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ Status SampleGenerator::GenerateHeader() {
HeaderLocalIncludes({
vars("client_header_path"),
"google/cloud/common_options.h",
"google/cloud/credentials.h",
"google/cloud/internal/getenv.h",
"google/cloud/testing_util/example_driver.h",
});
HeaderSystemIncludes({"iostream", "string", "vector"});
HeaderSystemIncludes({"iostream", "fstream", "string", "vector"});

HeaderPrint(R"""(
// main-dox-marker: $client_class_name$
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -59,23 +62,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::$product_namespace$::$client_class_name$(
google::cloud::$product_namespace$::Make$connection_class_name$(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: $client_class_name$
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::$product_namespace$::$client_class_name$(
google::cloud::$product_namespace$::Make$connection_class_name$(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"
});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "google/cloud/accessapproval/access_approval_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: AccessApprovalClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,50 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::accessapproval::AccessApprovalClient(
google::cloud::accessapproval::MakeAccessApprovalConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: AccessApprovalClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::accessapproval::AccessApprovalClient(
google::cloud::accessapproval::MakeAccessApprovalConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet(
{"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "google/cloud/accesscontextmanager/access_context_manager_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: AccessContextManagerClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -37,23 +41,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
auto client = google::cloud::accesscontextmanager::AccessContextManagerClient(
google::cloud::accesscontextmanager::MakeAccessContextManagerConnection(
options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: AccessContextManagerClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::accesscontextmanager::AccessContextManagerClient(
google::cloud::accesscontextmanager::MakeAccessContextManagerConnection(
options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet(
{"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Loading