Skip to content

Commit 88c8638

Browse files
authored
impl(common): introduce an abstraction for a Cloud location (#12659)
Introduce `google::cloud::Location` to model a location resource like `"projects/project-id/locations/location-id"`, and use this in places where we previously worked directly with strings. After this change has made it into a release, we can update the quickstart programs (and the scaffolding code that generates them) to use `Location`. Part of #12631.
1 parent a773840 commit 88c8638

17 files changed

Lines changed: 348 additions & 78 deletions

examples/batch_logging.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// [START batch_job_logs]
1818
#include "google/cloud/batch/v1/batch_client.h"
1919
#include "google/cloud/logging/v2/logging_service_v2_client.h"
20+
#include "google/cloud/location.h"
2021
#include "google/cloud/project.h"
2122

2223
// [END batch_job_logs]
@@ -45,17 +46,17 @@ void JobLogs(std::vector<std::string> const& argv) {
4546
// [START batch_job_logs]
4647
[](std::string const& project_id, std::string const& location_id,
4748
std::string const& job_id) {
48-
auto const name = "projects/" + project_id + "/locations/" + location_id +
49-
"/jobs/" + job_id;
49+
auto const project = google::cloud::Project(project_id);
50+
auto const location = google::cloud::Location(project, location_id);
51+
auto const name = location.FullName() + "/jobs/" + job_id;
5052
auto batch = google::cloud::batch_v1::BatchServiceClient(
5153
google::cloud::batch_v1::MakeBatchServiceConnection());
5254
auto job = batch.GetJob(name);
5355
if (!job) throw std::move(job).status();
5456

5557
auto logging = google::cloud::logging_v2::LoggingServiceV2Client(
5658
google::cloud::logging_v2::MakeLoggingServiceV2Connection());
57-
auto const project = google::cloud::Project(project_id);
58-
auto const log_name = "projects/" + project_id + "/logs/batch_task_logs";
59+
auto const log_name = project.FullName() + "/logs/batch_task_logs";
5960
google::logging::v2::ListLogEntriesRequest request;
6061
request.mutable_resource_names()->Add(project.FullName());
6162
request.set_filter("logName=\"" + log_name +
@@ -78,7 +79,9 @@ google::cloud::batch::v1::Job CreateTestJob(
7879
std::string const& project_id, std::string const& location_id,
7980
std::string const& job_id) {
8081
google::cloud::batch::v1::CreateJobRequest request;
81-
request.set_parent("projects/" + project_id + "/locations/" + location_id);
82+
request.set_parent(
83+
google::cloud::Location(google::cloud::Project(project_id), location_id)
84+
.FullName());
8285
request.set_job_id(job_id);
8386
// Most of the job description is fixed in this example; use a string to
8487
// initialize it.

google/cloud/bigtable/admin/integration_tests/instance_admin_integration_test.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "google/cloud/internal/background_threads_impl.h"
2525
#include "google/cloud/internal/getenv.h"
2626
#include "google/cloud/internal/random.h"
27+
#include "google/cloud/location.h"
2728
#include "google/cloud/project.h"
2829
#include "google/cloud/status_or.h"
2930
#include "google/cloud/testing_util/integration_test.h"
@@ -73,16 +74,16 @@ class InstanceAdminIntegrationTest
7374

7475
StatusOr<std::vector<std::string>> ListInstances(
7576
BigtableInstanceAdminClient& client) {
76-
auto const project_name = Project(project_id_).FullName();
77-
auto sor = client.ListInstances(project_name);
77+
auto const project = Project(project_id_);
78+
auto sor = client.ListInstances(project.FullName());
7879
if (!sor) return std::move(sor).status();
7980
auto resp = *std::move(sor);
8081

8182
// If either zone_a_ or zone_b_ are in the list of failed locations then we
8283
// cannot proceed.
8384
EXPECT_THAT(resp.failed_locations(),
84-
Not(AnyOf(Contains(project_name + "/locations/" + zone_a_),
85-
Contains(project_name + "/locations/" + zone_b_))));
85+
Not(AnyOf(Contains(Location(project, zone_a_).FullName()),
86+
Contains(Location(project, zone_b_).FullName()))));
8687
std::vector<std::string> names;
8788
names.reserve(resp.instances_size());
8889
auto& instances = *resp.mutable_instances();
@@ -118,21 +119,22 @@ class InstanceAdminIntegrationTest
118119
};
119120

120121
btadmin::CreateInstanceRequest IntegrationTestConfig(
121-
std::string const& project, std::string const& instance_id,
122-
std::string const& location,
122+
std::string const& project_id, std::string const& instance_id,
123+
std::string const& location_id,
123124
btadmin::Instance::Type type = btadmin::Instance::DEVELOPMENT,
124125
int32_t serve_nodes = 0) {
126+
auto const project = Project(project_id);
127+
auto const location = Location(project, location_id);
125128
// The description cannot exceed 30 characters
126129
auto const display_name = ("IT " + instance_id).substr(0, 30);
127-
auto const project_name = Project(project).FullName();
128130

129131
btadmin::Cluster c;
130-
c.set_location(project_name + "/locations/" + location);
132+
c.set_location(location.FullName());
131133
c.set_serve_nodes(serve_nodes);
132134
c.set_default_storage_type(btadmin::StorageType::HDD);
133135

134136
btadmin::CreateInstanceRequest r;
135-
r.set_parent(std::move(project_name));
137+
r.set_parent(project.FullName());
136138
r.set_instance_id(instance_id);
137139
r.mutable_instance()->set_type(type);
138140
r.mutable_instance()->set_display_name(std::move(display_name));
@@ -326,7 +328,7 @@ TEST_F(InstanceAdminIntegrationTest, CreateListGetDeleteInstanceTest) {
326328
TEST_F(InstanceAdminIntegrationTest, CreateListGetDeleteClusterTest) {
327329
auto const instance_id = RandomInstanceId(generator_);
328330
auto const cluster_id = instance_id + "-cl2";
329-
auto const project_name = Project(project_id_).FullName();
331+
auto const project = Project(project_id_);
330332
auto const instance_name = bigtable::InstanceName(project_id_, instance_id);
331333
auto const cluster_name =
332334
bigtable::ClusterName(project_id_, instance_id, cluster_id);
@@ -339,7 +341,7 @@ TEST_F(InstanceAdminIntegrationTest, CreateListGetDeleteClusterTest) {
339341

340342
// Create cluster
341343
btadmin::Cluster c;
342-
c.set_location(project_name + "/locations/" + zone_b_);
344+
c.set_location(Location(project, zone_b_).FullName());
343345
c.set_serve_nodes(3);
344346
c.set_default_storage_type(btadmin::StorageType::HDD);
345347
auto cluster = client_.CreateCluster(instance_name, cluster_id, c).get();

google/cloud/bigtable/admin/integration_tests/table_admin_integration_test.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "google/cloud/internal/getenv.h"
2323
#include "google/cloud/internal/random.h"
2424
#include "google/cloud/internal/retry_loop.h"
25+
#include "google/cloud/location.h"
2526
#include "google/cloud/project.h"
2627
#include "google/cloud/testing_util/chrono_literals.h"
2728
#include "google/cloud/testing_util/scoped_environment.h"
@@ -251,7 +252,7 @@ TEST_F(TableAdminIntegrationTest, WaitForConsistencyCheck) {
251252
// to create an instance with at least 2 clusters to test it.
252253
auto const id = TableTestEnvironment::RandomInstanceId();
253254
auto const random_table_id = RandomTableId();
254-
auto const project_name = Project(project_id()).FullName();
255+
auto const project = Project(project_id());
255256
auto const instance_name = bigtable::InstanceName(project_id(), id);
256257
auto const table_name =
257258
bigtable::TableName(project_id(), id, random_table_id);
@@ -270,20 +271,18 @@ TEST_F(TableAdminIntegrationTest, WaitForConsistencyCheck) {
270271
in.set_display_name(std::move(display_name));
271272

272273
btadmin::Cluster c1;
273-
c1.set_location(project_name + "/locations/" +
274-
TableTestEnvironment::zone_a());
274+
c1.set_location(Location(project, TableTestEnvironment::zone_a()).FullName());
275275
c1.set_serve_nodes(3);
276276
c1.set_default_storage_type(btadmin::StorageType::HDD);
277277

278278
btadmin::Cluster c2;
279-
c2.set_location(project_name + "/locations/" +
280-
TableTestEnvironment::zone_b());
279+
c2.set_location(Location(project, TableTestEnvironment::zone_b()).FullName());
281280
c2.set_serve_nodes(3);
282281
c2.set_default_storage_type(btadmin::StorageType::HDD);
283282

284283
// Create the new instance.
285284
auto instance = instance_admin_client
286-
.CreateInstance(project_name, id, std::move(in),
285+
.CreateInstance(project.FullName(), id, std::move(in),
287286
{{id + "-c1", std::move(c1)},
288287
{id + "-c2", std::move(c2)}})
289288
.get();

google/cloud/bigtable/examples/bigtable_hello_instance_admin.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "google/cloud/bigtable/testing/random_names.h"
2424
#include "google/cloud/internal/getenv.h"
2525
#include "google/cloud/internal/random.h"
26+
#include "google/cloud/location.h"
2627
#include "google/cloud/log.h"
2728
#include "google/cloud/project.h"
2829
#include <algorithm>
@@ -48,6 +49,7 @@ void BigtableHelloInstance(std::vector<std::string> const& argv) {
4849
namespace cbt = ::google::cloud::bigtable;
4950
namespace cbta = ::google::cloud::bigtable_admin;
5051
using ::google::cloud::future;
52+
using ::google::cloud::Location;
5153
using ::google::cloud::Project;
5254
using ::google::cloud::Status;
5355
using ::google::cloud::StatusOr;
@@ -61,7 +63,8 @@ void BigtableHelloInstance(std::vector<std::string> const& argv) {
6163

6264
//! [check instance exists]
6365
std::cout << "\nCheck Instance exists:\n";
64-
std::string const project_name = Project(project_id).FullName();
66+
auto const project = Project(project_id);
67+
std::string const project_name = project.FullName();
6568
StatusOr<google::bigtable::admin::v2::ListInstancesResponse> instances =
6669
instance_admin.ListInstances(project_name);
6770
if (!instances) throw std::move(instances).status();
@@ -93,7 +96,7 @@ void BigtableHelloInstance(std::vector<std::string> const& argv) {
9396

9497
// production instance needs at least 3 nodes
9598
google::bigtable::admin::v2::Cluster c;
96-
c.set_location(project_name + "/locations/" + zone);
99+
c.set_location(Location(project, zone).FullName());
97100
c.set_serve_nodes(3);
98101
c.set_default_storage_type(google::bigtable::admin::v2::HDD);
99102

google/cloud/bigtable/examples/bigtable_instance_admin_snippets.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "google/cloud/bigtable/testing/random_names.h"
2121
#include "google/cloud/internal/absl_str_join_quiet.h"
2222
#include "google/cloud/internal/getenv.h"
23+
#include "google/cloud/location.h"
2324
#include "google/cloud/log.h"
2425
#include "google/cloud/project.h"
2526
#include <iterator>
@@ -34,20 +35,22 @@ void CreateInstance(
3435
//! [create instance] [START bigtable_create_prod_instance]
3536
namespace cbta = ::google::cloud::bigtable_admin;
3637
using ::google::cloud::future;
38+
using ::google::cloud::Location;
3739
using ::google::cloud::Project;
3840
using ::google::cloud::StatusOr;
3941
[](cbta::BigtableInstanceAdminClient instance_admin,
4042
std::string const& project_id, std::string const& instance_id,
4143
std::string const& zone) {
42-
std::string project_name = Project(project_id).FullName();
44+
auto const project = Project(project_id);
45+
std::string project_name = project.FullName();
4346
std::string cluster_id = instance_id + "-c1";
4447

4548
google::bigtable::admin::v2::Instance in;
4649
in.set_type(google::bigtable::admin::v2::Instance::PRODUCTION);
4750
in.set_display_name("Put description here");
4851

4952
google::bigtable::admin::v2::Cluster cluster;
50-
cluster.set_location(project_name + "/locations/" + zone);
53+
cluster.set_location(Location(project, zone).FullName());
5154
cluster.set_serve_nodes(3);
5255
cluster.set_default_storage_type(google::bigtable::admin::v2::HDD);
5356

@@ -76,21 +79,23 @@ void CreateDevInstance(
7679
//! [create dev instance] [START bigtable_create_dev_instance]
7780
namespace cbta = ::google::cloud::bigtable_admin;
7881
using ::google::cloud::future;
82+
using ::google::cloud::Location;
7983
using ::google::cloud::Project;
8084
using ::google::cloud::StatusOr;
8185

8286
[](cbta::BigtableInstanceAdminClient instance_admin,
8387
std::string const& project_id, std::string const& instance_id,
8488
std::string const& zone) {
85-
std::string project_name = Project(project_id).FullName();
89+
auto const project = Project(project_id);
90+
std::string project_name = project.FullName();
8691
std::string cluster_id = instance_id + "-c1";
8792

8893
google::bigtable::admin::v2::Instance in;
8994
in.set_type(google::bigtable::admin::v2::Instance::DEVELOPMENT);
9095
in.set_display_name("Put description here");
9196

9297
google::bigtable::admin::v2::Cluster cluster;
93-
cluster.set_location(project_name + "/locations/" + zone);
98+
cluster.set_location(Location(project, zone).FullName());
9499
cluster.set_serve_nodes(0);
95100
cluster.set_default_storage_type(google::bigtable::admin::v2::HDD);
96101

@@ -119,13 +124,15 @@ void CreateReplicatedInstance(
119124
// [START bigtable_create_replicated_cluster]
120125
namespace cbta = ::google::cloud::bigtable_admin;
121126
using ::google::cloud::future;
127+
using ::google::cloud::Location;
122128
using ::google::cloud::Project;
123129
using ::google::cloud::StatusOr;
124130

125131
[](cbta::BigtableInstanceAdminClient instance_admin,
126132
std::string const& project_id, std::string const& instance_id,
127133
std::string const& zone_a, std::string const& zone_b) {
128-
std::string project_name = Project(project_id).FullName();
134+
auto const project = Project(project_id);
135+
std::string project_name = project.FullName();
129136
std::string c1 = instance_id + "-c1";
130137
std::string c2 = instance_id + "-c2";
131138

@@ -134,12 +141,12 @@ void CreateReplicatedInstance(
134141
in.set_display_name("Put description here");
135142

136143
google::bigtable::admin::v2::Cluster cluster1;
137-
cluster1.set_location(project_name + "/locations/" + zone_a);
144+
cluster1.set_location(Location(project, zone_a).FullName());
138145
cluster1.set_serve_nodes(3);
139146
cluster1.set_default_storage_type(google::bigtable::admin::v2::HDD);
140147

141148
google::bigtable::admin::v2::Cluster cluster2;
142-
cluster2.set_location(project_name + "/locations/" + zone_b);
149+
cluster2.set_location(Location(project, zone_b).FullName());
143150
cluster2.set_serve_nodes(3);
144151
cluster2.set_default_storage_type(google::bigtable::admin::v2::HDD);
145152

@@ -296,16 +303,17 @@ void CreateCluster(
296303
namespace cbt = ::google::cloud::bigtable;
297304
namespace cbta = ::google::cloud::bigtable_admin;
298305
using ::google::cloud::future;
306+
using ::google::cloud::Location;
299307
using ::google::cloud::Project;
300308
using ::google::cloud::StatusOr;
301309
[](cbta::BigtableInstanceAdminClient instance_admin,
302310
std::string const& project_id, std::string const& instance_id,
303311
std::string const& cluster_id, std::string const& zone) {
304-
std::string project_name = Project(project_id).FullName();
312+
auto const project = Project(project_id);
305313
std::string instance_name = cbt::InstanceName(project_id, instance_id);
306314

307315
google::bigtable::admin::v2::Cluster c;
308-
c.set_location(project_name + "/locations/" + zone);
316+
c.set_location(Location(project, zone).FullName());
309317
c.set_serve_nodes(3);
310318
c.set_default_storage_type(google::bigtable::admin::v2::HDD);
311319

google/cloud/bigtable/instance_admin.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/bigtable/instance_admin.h"
16+
#include "google/cloud/location.h"
1617
#include <sstream>
1718
#include <string>
1819
#include <type_traits>
@@ -52,8 +53,8 @@ future<StatusOr<btadmin::Instance>> InstanceAdmin::CreateInstance(
5253
auto request = std::move(instance_config).as_proto();
5354
request.set_parent(project_name());
5455
for (auto& kv : *request.mutable_clusters()) {
55-
kv.second.set_location(project_name() + "/locations/" +
56-
kv.second.location());
56+
kv.second.set_location(
57+
Location(project_name(), kv.second.location()).FullName());
5758
}
5859
return connection_->CreateInstance(request);
5960
}
@@ -63,7 +64,7 @@ future<StatusOr<btadmin::Cluster>> InstanceAdmin::CreateCluster(
6364
std::string const& cluster_id) {
6465
google::cloud::internal::OptionsSpan span(options_);
6566
auto cluster = std::move(cluster_config).as_proto();
66-
cluster.set_location(project_name() + "/locations/" + cluster.location());
67+
cluster.set_location(Location(project_name(), cluster.location()).FullName());
6768
btadmin::CreateClusterRequest request;
6869
request.mutable_cluster()->Swap(&cluster);
6970
request.set_parent(InstanceName(instance_id));

google/cloud/bigtable/instance_admin_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "google/cloud/bigtable/instance_admin.h"
1616
#include "google/cloud/bigtable/admin/mocks/mock_bigtable_instance_admin_connection.h"
1717
#include "google/cloud/bigtable/testing/mock_policies.h"
18+
#include "google/cloud/location.h"
19+
#include "google/cloud/project.h"
1820
#include "google/cloud/testing_util/status_matchers.h"
1921
#include <gmock/gmock.h>
2022

@@ -73,7 +75,7 @@ auto const kProfileName =
7375
"projects/the-project/instances/the-instance/appProfiles/the-profile";
7476

7577
std::string LocationName(std::string const& location) {
76-
return kProjectName + ("/locations/" + location);
78+
return Location(Project(kProjectName), location).FullName();
7779
}
7880

7981
Status FailingStatus() { return Status(StatusCode::kPermissionDenied, "fail"); }

0 commit comments

Comments
 (0)