Merge pull request #266 from chen-wang/master

Add service account credential
pull/281/head
Chen Wang 10 years ago
commit 3a2c4cda3c
  1. 32
      examples/tips/client.cc
  2. 3
      examples/tips/client.h
  3. 45
      examples/tips/client_main.cc

@ -36,7 +36,11 @@
#include "examples/tips/client.h"
using tech::pubsub::Topic;
using tech::pubsub::DeleteTopicRequest;
using tech::pubsub::GetTopicRequest;
using tech::pubsub::PublisherService;
using tech::pubsub::ListTopicsRequest;
using tech::pubsub::ListTopicsResponse;
namespace grpc {
namespace examples {
@ -55,6 +59,34 @@ Status Client::CreateTopic(grpc::string topic) {
return stub_->CreateTopic(&context, request, &response);
}
Status Client::ListTopics() {
ListTopicsRequest request;
ListTopicsResponse response;
ClientContext context;
return stub_->ListTopics(&context, request, &response);
}
Status Client::GetTopic(grpc::string topic) {
GetTopicRequest request;
Topic response;
ClientContext context;
request.set_topic(topic);
return stub_->GetTopic(&context, request, &response);
}
Status Client::DeleteTopic(grpc::string topic) {
DeleteTopicRequest request;
proto2::Empty response;
ClientContext context;
request.set_topic(topic);
return stub_->DeleteTopic(&context, request, &response);
}
} // namespace tips
} // namespace examples
} // namespace grpc

@ -47,6 +47,9 @@ class Client {
public:
Client(std::shared_ptr<grpc::ChannelInterface> channel);
Status CreateTopic(grpc::string topic);
Status GetTopic(grpc::string topic);
Status DeleteTopic(grpc::string topic);
Status ListTopics();
private:
std::unique_ptr<tech::pubsub::PublisherService::Stub> stub_;

@ -31,6 +31,13 @@
*
*/
#include <chrono>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <google/gflags.h>
@ -45,6 +52,20 @@
DEFINE_int32(server_port, 443, "Server port.");
DEFINE_string(server_host,
"pubsub-staging.googleapis.com", "Server host to connect to");
DEFINE_string(service_account_key_file, "",
"Path to service account json key file.");
DEFINE_string(oauth_scope, "", "Scope for OAuth tokens.");
grpc::string GetServiceAccountJsonKey() {
static grpc::string json_key;
if (json_key.empty()) {
std::ifstream json_key_file(FLAGS_service_account_key_file);
std::stringstream key_stream;
key_stream << json_key_file.rdbuf();
json_key = key_stream.str();
}
return json_key;
}
int main(int argc, char** argv) {
grpc_init();
@ -56,8 +77,15 @@ int main(int argc, char** argv) {
snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
FLAGS_server_port);
std::unique_ptr<grpc::Credentials> creds =
grpc::CredentialsFactory::ComputeEngineCredentials();
std::unique_ptr<grpc::Credentials> creds;
if (FLAGS_service_account_key_file != "") {
grpc::string json_key = GetServiceAccountJsonKey();
creds = grpc::CredentialsFactory::ServiceAccountCredentials(
json_key, FLAGS_oauth_scope, std::chrono::hours(1));
} else {
creds = grpc::CredentialsFactory::ComputeEngineCredentials();
}
std::shared_ptr<grpc::ChannelInterface> channel(
grpc::CreateTestChannel(
host_port,
@ -67,8 +95,17 @@ int main(int argc, char** argv) {
creds));
grpc::examples::tips::Client client(channel);
grpc::Status s = client.CreateTopic("test");
gpr_log(GPR_INFO, "return code %d", s.code());
grpc::Status s = client.CreateTopic("/topics/stoked-keyword-656/testtopics");
gpr_log(GPR_INFO, "return code %d, %s", s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
s = client.GetTopic("/topics/stoked-keyword-656/testtopics");
gpr_log(GPR_INFO, "return code %d, %s", s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
s = client.DeleteTopic("/topics/stoked-keyword-656/testtopics");
gpr_log(GPR_INFO, "return code %d, %s", s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
channel.reset();

Loading…
Cancel
Save