1818#include < typeindex>
1919#include < typeinfo>
2020
21- namespace {
22- struct Usage {
23- std::string msg;
24- };
25-
26- char const * ConsumeArg (int & argc, char * argv[]) {
27- if (argc < 2 ) {
28- return nullptr ;
21+ int main (int argc, char * argv[]) try {
22+ if (argc != 5 ) {
23+ std::string const cmd = argv[0 ];
24+ auto last_slash = std::string (cmd).find_last_of (' /' );
25+ auto program = cmd.substr (last_slash + 1 );
26+ std::cerr << " \n Usage: " << program
27+ << " <project-id> <instance-id> <cluster-id> <zone>\n\n "
28+ << " Example: " << program
29+ << " my-project my-instance my-instance-c1 us-central1-f\n " ;
30+ return 1 ;
2931 }
30- char const * result = argv[1 ];
31- std::copy (argv + 2 , argv + argc, argv + 1 );
32- argc--;
33- return result;
34- }
35-
36- std::string command_usage;
3732
38- void PrintUsage (int argc, char * argv[], std::string const & msg) {
39- std::string const cmd = argv[0 ];
40- auto last_slash = std::string (cmd).find_last_of (' /' );
41- auto program = cmd.substr (last_slash + 1 );
42- std::cerr << msg << " \n Usage: " << program << " <command> [arguments]\n\n "
43- << " Commands:\n "
44- << command_usage << " \n " ;
45- }
33+ std::string const project_id = argv[1 ];
34+ std::string const instance_id = argv[2 ];
35+ std::string const cluster_id = argv[3 ];
36+ std::string const zone = argv[4 ];
4637
47- // This full example demonstrate various instance operations
48- // by initially creating instance of type PRODUCTION
49- void RunInstanceOperations (std::string project_id, int argc, char * argv[]) {
50- if (argc != 4 ) {
51- throw Usage{" run: <project-id> <instance-id> <cluster-id> <zone>" };
52- }
53- google::cloud::bigtable::InstanceId instance_id (ConsumeArg (argc, argv));
54- google::cloud::bigtable::ClusterId cluster_id (ConsumeArg (argc, argv));
55- std::string const zone = ConsumeArg (argc, argv);
38+ // ! [aliases]
39+ namespace cbt = google::cloud::bigtable;
40+ using google::cloud::future;
41+ using google::cloud::StatusOr;
42+ // ! [aliases]
5643
57- google::cloud::bigtable::InstanceAdmin instance_admin (
58- google::cloud::bigtable::CreateDefaultInstanceAdminClient (
59- project_id, google::cloud::bigtable::ClientOptions ()));
44+ // Connect to the Cloud Bigtable admin endpoint.
45+ // ! [connect admin]
46+ cbt::InstanceAdmin instance_admin (
47+ cbt::CreateDefaultInstanceAdminClient (project_id, cbt::ClientOptions ()));
48+ // ! [connect admin]
6049
6150 std::cout << " \n Check Instance exists:\n " ;
62- auto instances = instance_admin.ListInstances ();
51+ StatusOr<cbt::InstanceList> instances = instance_admin.ListInstances ();
6352 if (!instances) {
6453 throw std::runtime_error (instances.status ().message ());
6554 }
66- auto instance_name =
67- instance_admin.project_name () + " /instances/" + instance_id.get ();
68- bool instance_exists =
69- instances->instances .end () !=
70- std::find_if (
71- instances->instances .begin (), instances->instances .end (),
72- [&instance_name](google::bigtable::admin::v2::Instance const & i) {
73- return i.name () == instance_name;
74- });
7555 if (!instances->failed_locations .empty ()) {
7656 std::cerr
7757 << " The service tells us it has no information about these locations:" ;
@@ -80,62 +60,78 @@ void RunInstanceOperations(std::string project_id, int argc, char* argv[]) {
8060 }
8161 std::cerr << " . Continuing anyway\n " ;
8262 }
63+ auto instance_name =
64+ instance_admin.project_name () + " /instances/" + instance_id;
65+ auto instance_name_it = std::find_if (
66+ instances->instances .begin (), instances->instances .end (),
67+ [&instance_name](google::bigtable::admin::v2::Instance const & i) {
68+ return i.name () == instance_name;
69+ });
70+ bool instance_exists = instance_name_it != instances->instances .end ();
71+ std::cout << " The instance " << instance_id
72+ << (instance_exists ? " does" : " does not" ) << " exist already\n " ;
8373
8474 // Create instance if does not exists
8575 if (!instance_exists) {
8676 std::cout << " \n Creating a PRODUCTION Instance: " ;
87- google::cloud::bigtable::DisplayName display_name (" Sample Instance" );
8877
8978 // production instance needs at least 3 nodes
90- auto cluster_config = google::cloud::bigtable::ClusterConfig (
91- zone, 3 , google::cloud::bigtable::ClusterConfig::HDD);
92- google::cloud::bigtable::InstanceConfig config (
93- google::cloud::bigtable::InstanceId (instance_id), display_name,
94- {{cluster_id.get (), cluster_config}});
95- config.set_type (google::cloud::bigtable::InstanceConfig::PRODUCTION);
96-
97- auto instance_details = instance_admin.CreateInstance (config).get ();
98- std::cout << " Done\n " ;
99- } else {
100- std::cout << " \n Instance " << instance_id.get () << " already exists.\n " ;
101- return ;
79+ auto cluster_config = cbt::ClusterConfig (zone, 3 , cbt::ClusterConfig::HDD);
80+ cbt::InstanceConfig config (cbt::InstanceId (instance_id),
81+ cbt::DisplayName (" Sample Instance" ),
82+ {{cluster_id, cluster_config}});
83+ config.set_type (cbt::InstanceConfig::PRODUCTION);
84+
85+ google::cloud::future<void > creation_done =
86+ instance_admin.CreateInstance (config).then (
87+ [instance_id](
88+ future<StatusOr<google::bigtable::admin::v2::Instance>> f) {
89+ auto instance = f.get ();
90+ if (!instance) {
91+ std::cerr << " Could not create instance " << instance_id
92+ << " \n " ;
93+ throw std::runtime_error (instance.status ().message ());
94+ }
95+ std::cout << " Successfully created instance: "
96+ << instance->DebugString () << " \n " ;
97+ });
98+ // Note how this blocks until the instance is created, in production code
99+ // you may want to perform this task asynchronously.
100+ creation_done.get ();
101+ std::cout << " DONE\n " ;
102102 }
103103
104104 std::cout << " \n Listing Instances:\n " ;
105- auto instances_after = instance_admin.ListInstances ();
106- if (!instances_after) {
107- throw std::runtime_error (instances_after.status ().message ());
108- }
109- for (auto const & instance : instances_after->instances ) {
110- std::cout << instance.name () << " \n " ;
105+ instances = instance_admin.ListInstances ();
106+ if (!instances) {
107+ throw std::runtime_error (instances.status ().message ());
111108 }
112- if (!instances_after ->failed_locations .empty ()) {
109+ if (!instances ->failed_locations .empty ()) {
113110 std::cerr
114111 << " The service tells us it has no information about these locations:" ;
115- for (auto const & failed_location : instances_after ->failed_locations ) {
112+ for (auto const & failed_location : instances ->failed_locations ) {
116113 std::cerr << " " << failed_location;
117114 }
118115 std::cerr << " . Continuing anyway\n " ;
119116 }
117+ for (auto const & instance : instances->instances ) {
118+ std::cout << " " << instance.name () << " \n " ;
119+ }
120+ std::cout << " DONE\n " ;
120121
121122 std::cout << " \n Get Instance:\n " ;
122- auto instance = instance_admin.GetInstance (instance_id. get () );
123+ auto instance = instance_admin.GetInstance (instance_id);
123124 if (!instance) {
124125 throw std::runtime_error (instance.status ().message ());
125126 }
126- std::string instance_detail;
127- google::protobuf::TextFormat::PrintToString (*instance, &instance_detail);
128- std::cout << " GetInstance details :\n " << instance_detail;
127+ std::cout << " Instance details :\n " << instance->DebugString () << " \n " ;
129128
130129 std::cout << " \n Listing Clusters:\n " ;
131- auto cluster_list = instance_admin.ListClusters (instance_id.get ());
130+ StatusOr<cbt::ClusterList> cluster_list =
131+ instance_admin.ListClusters (instance_id);
132132 if (!cluster_list) {
133133 throw std::runtime_error (cluster_list.status ().message ());
134134 }
135- std::cout << " Cluster Name List:\n " ;
136- for (auto const & cluster : cluster_list->clusters ) {
137- std::cout << " Cluster Name: " << cluster.name () << " \n " ;
138- }
139135 if (!cluster_list->failed_locations .empty ()) {
140136 std::cout << " The Cloud Bigtable service reports that the following "
141137 " locations are temporarily unavailable and no information "
@@ -144,49 +140,21 @@ void RunInstanceOperations(std::string project_id, int argc, char* argv[]) {
144140 std::cout << failed_location << " \n " ;
145141 }
146142 }
147- }
148-
149- } // anonymous namespace
150-
151- int main (int argc, char * argv[]) try {
152- using CommandType = std::function<void (std::string, int , char *[])>;
153-
154- std::map<std::string, CommandType> commands = {
155- {" run" , &RunInstanceOperations},
156- };
157-
158- for (auto && kv : commands) {
159- try {
160- std::string unused;
161- int fake_argc = 0 ;
162- kv.second (unused, fake_argc, argv);
163- } catch (Usage const & u) {
164- command_usage += " " ;
165- command_usage += u.msg ;
166- command_usage += " \n " ;
167- }
168- }
169-
170- if (argc < 3 ) {
171- PrintUsage (argc, argv, " Missing command and/or project-id" );
172- return 1 ;
143+ std::cout << " Cluster Name List:\n " ;
144+ for (auto const & cluster : cluster_list->clusters ) {
145+ std::cout << " Cluster Name: " << cluster.name () << " \n " ;
173146 }
147+ std::cout << " DONE\n " ;
174148
175- std::string const command_name = ConsumeArg (argc, argv);
176- std::string const project_id = ConsumeArg (argc, argv);
177-
178- auto command = commands.find (command_name);
179- if (commands.end () == command) {
180- PrintUsage (argc, argv, " Unknown command: " + command_name);
181- return 1 ;
149+ std::cout << " Deleting instance " << instance_id << " \n " ;
150+ google::cloud::Status delete_status =
151+ instance_admin.DeleteInstance (instance_id);
152+ if (!delete_status.ok ()) {
153+ throw std::runtime_error (delete_status.message ());
182154 }
183-
184- command->second (project_id, argc, argv);
155+ std::cout << " DONE\n " ;
185156
186157 return 0 ;
187- } catch (Usage const & ex) {
188- PrintUsage (argc, argv, ex.msg );
189- return 1 ;
190158} catch (std::exception const & ex) {
191159 std::cerr << " Standard C++ exception raised: " << ex.what () << " \n " ;
192160 return 1 ;
0 commit comments