Skip to content

Commit aa68d3b

Browse files
committed
Update examples, snippets and READMEs to use renamed core getters/setters/builders
1 parent 0366918 commit aa68d3b

File tree

64 files changed

+241
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+241
-238
lines changed

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Most `google-cloud` libraries require a project ID. There are multiple ways to
9494
* Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
9595

9696
```java
97-
Datastore datastore = DatastoreOptions.builder().projectId("PROJECT_ID").build().service();
97+
Datastore datastore = DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService();
9898
```
9999
* Specify the environment variable `GOOGLE_CLOUD_PROJECT` to be your desired project ID.
100100
* Set the project ID using the [Google Cloud SDK](https://cloud.google.com/sdk/?hl=en). To use the SDK, [download the SDK](https://cloud.google.com/sdk/?hl=en) if you haven't already, and set the project ID from the command line. For example:
@@ -128,18 +128,18 @@ Next, choose a method for authenticating API requests from within your project:
128128
```
129129
* Supply the JSON credentials file when building the service options. For example, this Storage object has the necessary permissions to interact with your Google Cloud Storage data:
130130
```java
131-
Storage storage = StorageOptions.builder()
132-
.authCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
131+
Storage storage = StorageOptions.newBuilder()
132+
.setAuthCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
133133
.build()
134-
.service();
134+
.getService();
135135
```
136136
* If running locally for development/testing, you can use Google Cloud SDK. Download the SDK if you haven't already, then login using the SDK (`gcloud auth login` in command line). Be sure to set your project ID as described above.
137137
* If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case the access token will not be automatically refreshed):
138138
```java
139-
Storage storage = StorageOptions.builder()
140-
.authCredentials(AuthCredentials.createFor("your_access_token"))
139+
Storage storage = StorageOptions.newBuilder()
140+
.setAuthCredentials(AuthCredentials.createFor("your_access_token"))
141141
.build()
142-
.service();
142+
.getService();
143143
```
144144
145145
`google-cloud` looks for credentials in the following order, stopping once it finds credentials:
@@ -175,7 +175,7 @@ import com.google.cloud.bigquery.Table;
175175
import com.google.cloud.bigquery.TableId;
176176
import com.google.cloud.bigquery.TableInfo;
177177
178-
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
178+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
179179
TableId tableId = TableId.of("dataset", "table");
180180
Table table = bigquery.getTable(tableId);
181181
if (table == null) {
@@ -216,7 +216,7 @@ import com.google.cloud.compute.Disk;
216216
import com.google.cloud.compute.DiskId;
217217
import com.google.cloud.compute.Snapshot;
218218
219-
Compute compute = ComputeOptions.defaultInstance().service();
219+
Compute compute = ComputeOptions.getDefaultInstance().getService();
220220
DiskId diskId = DiskId.of("us-central1-a", "disk-name");
221221
Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields());
222222
if (disk != null) {
@@ -243,7 +243,7 @@ import com.google.cloud.compute.InstanceInfo;
243243
import com.google.cloud.compute.MachineTypeId;
244244
import com.google.cloud.compute.NetworkId;
245245
246-
Compute compute = ComputeOptions.defaultInstance().service();
246+
Compute compute = ComputeOptions.getDefaultInstance().getService();
247247
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
248248
NetworkId networkId = NetworkId.of("default");
249249
AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
@@ -282,7 +282,7 @@ import com.google.cloud.datastore.Entity;
282282
import com.google.cloud.datastore.Key;
283283
import com.google.cloud.datastore.KeyFactory;
284284
285-
Datastore datastore = DatastoreOptions.defaultInstance().service();
285+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
286286
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
287287
Key key = keyFactory.newKey("keyName");
288288
Entity entity = Entity.newBuilder(key)
@@ -303,7 +303,7 @@ import com.google.cloud.datastore.Entity;
303303
import com.google.cloud.datastore.Key;
304304
import com.google.cloud.datastore.KeyFactory;
305305
306-
Datastore datastore = DatastoreOptions.defaultInstance().service();
306+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
307307
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
308308
Key key = keyFactory.newKey("keyName");
309309
Entity entity = datastore.get(key);
@@ -336,7 +336,7 @@ import com.google.cloud.dns.DnsOptions;
336336
import com.google.cloud.dns.Zone;
337337
import com.google.cloud.dns.ZoneInfo;
338338
339-
Dns dns = DnsOptions.defaultInstance().service();
339+
Dns dns = DnsOptions.getDefaultInstance().getService();
340340
String zoneName = "my-unique-zone";
341341
String domainName = "someexampledomain.com.";
342342
String description = "This is a google-cloud-dns sample zone.";
@@ -356,7 +356,7 @@ import com.google.cloud.dns.Zone;
356356
import java.util.Iterator;
357357
import java.util.concurrent.TimeUnit;
358358
359-
Dns dns = DnsOptions.defaultInstance().service();
359+
Dns dns = DnsOptions.getDefaultInstance().getService();
360360
String zoneName = "my-unique-zone";
361361
Zone zone = dns.getZone(zoneName);
362362
String ip = "12.13.14.15";
@@ -371,8 +371,8 @@ ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.newBuilder().add(toC
371371
Iterator<RecordSet> recordSetIterator = zone.listRecordSets().iterateAll();
372372
while (recordSetIterator.hasNext()) {
373373
RecordSet current = recordSetIterator.next();
374-
if (toCreate.name().equals(current.getName()) &&
375-
toCreate.type().equals(current.getType())) {
374+
if (toCreate.getName().equals(current.getName()) &&
375+
toCreate.getType().equals(current.getType())) {
376376
changeBuilder.delete(current);
377377
}
378378
}
@@ -410,19 +410,19 @@ import com.google.cloud.logging.Payload.StringPayload;
410410
import java.util.Collections;
411411
import java.util.Iterator;
412412
413-
LoggingOptions options = LoggingOptions.defaultInstance();
414-
try(Logging logging = options.service()) {
413+
LoggingOptions options = LoggingOptions.getDefaultInstance();
414+
try(Logging logging = options.getService()) {
415415
416416
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
417417
.setLogName("test-log")
418-
.setResource(MonitoredResource.builder("global")
419-
.addLabel("project_id", options.projectId())
418+
.setResource(MonitoredResource.newBuilder("global")
419+
.addLabel("project_id", options.getProjectId())
420420
.build())
421421
.build();
422422
logging.write(Collections.singleton(firstEntry));
423423
424424
Page<LogEntry> entries = logging.listLogEntries(
425-
EntryListOption.filter("logName=projects/" + options.projectId() + "/logs/test-log"));
425+
EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
426426
Iterator<LogEntry> entryIterator = entries.iterateAll();
427427
while (entryIterator.hasNext()) {
428428
System.out.println(entryIterator.next());
@@ -472,7 +472,7 @@ import com.google.cloud.pubsub.PubSubOptions;
472472
import com.google.cloud.pubsub.Subscription;
473473
import com.google.cloud.pubsub.SubscriptionInfo;
474474
475-
try (PubSub pubsub = PubSubOptions.defaultInstance().service()) {
475+
try (PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) {
476476
Subscription subscription =
477477
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
478478
MessageProcessor callback = new MessageProcessor() {
@@ -506,7 +506,7 @@ import com.google.cloud.resourcemanager.ResourceManagerOptions;
506506
507507
import java.util.Iterator;
508508
509-
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
509+
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
510510
Project project = resourceManager.get("some-project-id"); // Use an existing project's ID
511511
if (project != null) {
512512
Project newProject = project.toBuilder()
@@ -547,7 +547,7 @@ import com.google.cloud.storage.BlobInfo;
547547
import com.google.cloud.storage.Storage;
548548
import com.google.cloud.storage.StorageOptions;
549549
550-
Storage storage = StorageOptions.defaultInstance().service();
550+
Storage storage = StorageOptions.getDefaultInstance().getService();
551551
BlobId blobId = BlobId.of("bucket", "blob_name");
552552
BlobInfo blobInfo = BlobInfo.newBuiler(blobId).setContentType("text/plain").build();
553553
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
@@ -566,7 +566,7 @@ import com.google.cloud.storage.StorageOptions;
566566
import java.nio.ByteBuffer;
567567
import java.nio.channels.WritableByteChannel;
568568
569-
Storage storage = StorageOptions.defaultInstance().service();
569+
Storage storage = StorageOptions.getDefaultInstance().getService();
570570
BlobId blobId = BlobId.of("bucket", "blob_name");
571571
Blob blob = storage.get(blobId);
572572
if (blob != null) {
@@ -599,7 +599,7 @@ import com.google.cloud.translate.Translate.TranslateOption;
599599
import com.google.cloud.translate.TranslateOptions;
600600
import com.google.cloud.translate.Translation;
601601
602-
Translate translate = TranslateOptions.defaultInstance().service();
602+
Translate translate = TranslateOptions.getDefaultInstance().getService();
603603
604604
Detection detection = translate.detect("Hola");
605605
String detectedLanguage = detection.getLanguage();

TESTING.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Here is an example that uses the `RemoteBigQueryHelper` to create a dataset.
2727
```java
2828
RemoteBigQueryHelper bigqueryHelper =
2929
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
30-
BigQuery bigquery = bigqueryHelper.getOptions().service();
30+
BigQuery bigquery = bigqueryHelper.getOptions().getService();
3131
String dataset = RemoteBigQueryHelper.generateDatasetName();
3232
bigquery.create(DatasetInfo.newBuilder(dataset).build());
3333
```
@@ -56,7 +56,7 @@ uses the `RemoteComputeHelper` to create an address.
5656
```java
5757
RemoteComputeHelper computeHelper =
5858
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
59-
Compute compute = computeHelper.options().service();
59+
Compute compute = computeHelper.getOptions().getService();
6060
// Pick a name for the resource with low probability of clashing
6161
String addressName = RemoteComputeHelper.baseResourceName() + "address";
6262
AddressId addressId = RegionAddressId.of(REGION, addressName);
@@ -84,7 +84,7 @@ You can test against a temporary local Datastore by following these steps:
8484

8585
2. Create and use a `Datastore` object with the options given by the `LocalDatastoreHelper` instance. For example:
8686
```java
87-
Datastore localDatastore = helper.getOptions().service();
87+
Datastore localDatastore = helper.getOptions().getService();
8888
```
8989

9090
3. Run your tests.
@@ -99,12 +99,12 @@ You can test against a temporary local Datastore by following these steps:
9999
You can test against a remote Datastore emulator as well. To do this, set the `DatastoreOptions` project endpoint to the hostname of the remote machine, like the example below.
100100

101101
```java
102-
DatastoreOptions options = DatastoreOptions.builder()
103-
.projectId("my-project-id") // must match project ID specified on remote machine
104-
.host("http://<hostname of machine>:<port>")
105-
.authCredentials(AuthCredentials.noAuth())
102+
DatastoreOptions options = DatastoreOptions.newBuilder()
103+
.setProjectId("my-project-id") // must match project ID specified on remote machine
104+
.setHost("http://<hostname of machine>:<port>")
105+
.setAuthCredentials(AuthCredentials.noAuth())
106106
.build();
107-
Datastore localDatastore = options.service();
107+
Datastore localDatastore = options.getService();
108108
```
109109

110110
We recommend that you start the emulator on the remote machine using the [Google Cloud SDK](https://cloud.google.com/sdk/gcloud/reference/beta/emulators/datastore/) from command line, as shown below:
@@ -135,7 +135,7 @@ You can test against an in-memory local DNS by following these steps:
135135
For example:
136136

137137
```java
138-
Dns dns = LocalDnsHelper.getOptions().service();
138+
Dns dns = LocalDnsHelper.getOptions().getService();
139139
```
140140

141141
3. Run your tests.
@@ -164,7 +164,7 @@ uses the `RemoteLoggingHelper` to create a metric.
164164
```java
165165
RemoteLoggingHelper loggingHelper =
166166
RemoteLoggingHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
167-
Logging logging = loggingHelper.getOptions().service();
167+
Logging logging = loggingHelper.getOptions().getService();
168168
// Pick a name for the resource with low probability of clashing
169169
String metricName = RemoteLoggingHelper.formatForTest("test-metric");
170170
MetricInfo metricInfo = MetricInfo.of(name, "logName:syslog");
@@ -190,7 +190,7 @@ and `start` methods. This will bind a port for communication with the local Pub/
190190
2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For
191191
example:
192192
```java
193-
PubSub localPubsub = helper.getOptions().service();
193+
PubSub localPubsub = helper.getOptions().getService();
194194
```
195195

196196
3. Run your tests.
@@ -206,12 +206,12 @@ You can test against a remote Pub/Sub emulator as well. To do this, set the `Pub
206206
endpoint to the hostname of the remote machine, like the example below.
207207

208208
```java
209-
PubSubOptions options = PubSubOptions.builder()
210-
.projectId("my-project-id") // must match project ID specified on remote machine
211-
.host("<hostname of machine>:<port>")
212-
.authCredentials(AuthCredentials.noAuth())
209+
PubSubOptions options = PubSubOptions.newBuilder()
210+
.setProjectId("my-project-id") // must match project ID specified on remote machine
211+
.setHost("<hostname of machine>:<port>")
212+
.setAuthCredentials(AuthCredentials.noAuth())
213213
.build();
214-
PubSub localPubsub= options.service();
214+
PubSub localPubsub = options.getService();
215215
```
216216

217217
### Testing code that uses Resource Manager
@@ -232,7 +232,7 @@ You can test against an in-memory local Resource Manager by following these step
232232
2. In your program, create and use a Resource Manager service object whose host is set to `localhost` at the appropriate port. For example:
233233

234234
```java
235-
ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
235+
ResourceManager resourceManager = LocalResourceManagerHelper.getOptions().getService();
236236
```
237237

238238
3. Run your tests.
@@ -258,7 +258,7 @@ Here is an example that uses the `RemoteStorageHelper` to create a bucket.
258258
```java
259259
RemoteStorageHelper helper =
260260
RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
261-
Storage storage = helper.getOptions().service();
261+
Storage storage = helper.getOptions().getService();
262262
String bucket = RemoteStorageHelper.generateBucketName();
263263
storage.create(BucketInfo.of(bucket));
264264
```
@@ -285,7 +285,7 @@ key.
285285
that uses the `RemoteTranslateHelper` to list supported languages.
286286
```java
287287
RemoteTranslateHelper translateHelper = RemoteTranslateHelper.create(PROJECT_ID, API_KEY);
288-
Translate translate = translateHelper.getOptions().service();
288+
Translate translate = translateHelper.getOptions().getService();
289289
List<Language> languages = translate.listSupportedLanguages();
290290
```
291291

google-cloud-bigquery/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ code to create your service object:
8585
import com.google.cloud.bigquery.BigQuery;
8686
import com.google.cloud.bigquery.BigQueryOptions;
8787

88-
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
88+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
8989
```
9090

9191
For other authentication options, see the

google-cloud-compute/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ code to create your service object:
8888
import com.google.cloud.compute.Compute;
8989
import com.google.cloud.compute.ComputeOptions;
9090

91-
Compute compute = ComputeOptions.defaultInstance().service();
91+
Compute compute = ComputeOptions.getDefaultInstance().getService();
9292
```
9393

9494
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication)
@@ -116,7 +116,7 @@ RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address");
116116
Operation operation = compute.create(AddressInfo.of(addressId));
117117
// Wait for operation to complete
118118
operation = operation.waitFor();
119-
if (operation.errors() == null) {
119+
if (operation.getErrors() == null) {
120120
System.out.println("Address " + addressId + " was successfully created");
121121
} else {
122122
// inspect operation.getErrors()
@@ -153,7 +153,7 @@ operation = operation.waitFor();
153153
if (operation.getErrors() == null) {
154154
System.out.println("Disk " + diskId + " was successfully created");
155155
} else {
156-
// inspect operation.errors()
156+
// inspect operation.getErrors()
157157
throw new RuntimeException("Disk creation failed");
158158
}
159159
```
@@ -199,7 +199,7 @@ operation = operation.waitFor();
199199
if (operation.getErrors() == null) {
200200
System.out.println("Instance " + instanceId + " was successfully created");
201201
} else {
202-
// inspect operation.errors()
202+
// inspect operation.getErrors()
203203
throw new RuntimeException("Instance creation failed");
204204
}
205205
```

google-cloud-datastore/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ To make authenticated requests to Google Cloud Datastore, you must create a serv
7373
import com.google.cloud.datastore.Datastore;
7474
import com.google.cloud.datastore.DatastoreOptions;
7575

76-
Datastore datastore = DatastoreOptions.defaultInstance().service();
76+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
7777
```
7878

7979
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication) page.

google-cloud-dns/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ code to create your service object:
8686
import com.google.cloud.dns.Dns;
8787
import com.google.cloud.dns.DnsOptions;
8888

89-
Dns dns = DnsOptions.defaultInstance().service();
89+
Dns dns = DnsOptions.getDefaultInstance().getService();
9090
```
9191

9292
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication) page.

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/BigQueryExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,12 +755,12 @@ public static void main(String... args) throws Exception {
755755
printUsage();
756756
return;
757757
}
758-
BigQueryOptions.Builder optionsBuilder = BigQueryOptions.builder();
758+
BigQueryOptions.Builder optionsBuilder = BigQueryOptions.newBuilder();
759759
BigQueryAction action;
760760
String actionName;
761761
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
762762
actionName = args[1];
763-
optionsBuilder.projectId(args[0]);
763+
optionsBuilder.setProjectId(args[0]);
764764
action = ACTIONS.get(args[1]);
765765
args = Arrays.copyOfRange(args, 2, args.length);
766766
} else {
@@ -773,7 +773,7 @@ public static void main(String... args) throws Exception {
773773
printUsage();
774774
return;
775775
}
776-
BigQuery bigquery = optionsBuilder.build().service();
776+
BigQuery bigquery = optionsBuilder.build().getService();
777777
Object arg;
778778
try {
779779
arg = action.parse(args);

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/CreateTableAndLoadData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public class CreateTableAndLoadData {
4444

4545
public static void main(String... args) throws InterruptedException, TimeoutException {
46-
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
46+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
4747
TableId tableId = TableId.of("dataset", "table");
4848
Table table = bigquery.getTable(tableId);
4949
if (table == null) {

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class InsertDataAndQueryTable {
4949

5050
public static void main(String... args) throws InterruptedException {
5151
// Create a service instance
52-
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
52+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
5353

5454
// Create a dataset
5555
String datasetId = "my_dataset_id";

0 commit comments

Comments
 (0)