Skip to content

Commit 59bf5bf

Browse files
committed
Update examples, snippets and READMEs to use renamed core getters/setters/builders
1 parent 75f4247 commit 59bf5bf

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
@@ -93,7 +93,7 @@ Most `google-cloud` libraries require a project ID. There are multiple ways to
9393
* Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
9494

9595
```java
96-
Datastore datastore = DatastoreOptions.builder().projectId("PROJECT_ID").build().service();
96+
Datastore datastore = DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService();
9797
```
9898
* Specify the environment variable `GOOGLE_CLOUD_PROJECT` to be your desired project ID.
9999
* 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:
@@ -127,18 +127,18 @@ Next, choose a method for authenticating API requests from within your project:
127127
```
128128
* 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:
129129
```java
130-
Storage storage = StorageOptions.builder()
131-
.authCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
130+
Storage storage = StorageOptions.newBuilder()
131+
.setAuthCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
132132
.build()
133-
.service();
133+
.getService();
134134
```
135135
* 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.
136136
* 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):
137137
```java
138-
Storage storage = StorageOptions.builder()
139-
.authCredentials(AuthCredentials.createFor("your_access_token"))
138+
Storage storage = StorageOptions.newBuilder()
139+
.setAuthCredentials(AuthCredentials.createFor("your_access_token"))
140140
.build()
141-
.service();
141+
.getService();
142142
```
143143
144144
`google-cloud` looks for credentials in the following order, stopping once it finds credentials:
@@ -174,7 +174,7 @@ import com.google.cloud.bigquery.Table;
174174
import com.google.cloud.bigquery.TableId;
175175
import com.google.cloud.bigquery.TableInfo;
176176
177-
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
177+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
178178
TableId tableId = TableId.of("dataset", "table");
179179
Table table = bigquery.getTable(tableId);
180180
if (table == null) {
@@ -215,7 +215,7 @@ import com.google.cloud.compute.Disk;
215215
import com.google.cloud.compute.DiskId;
216216
import com.google.cloud.compute.Snapshot;
217217
218-
Compute compute = ComputeOptions.defaultInstance().service();
218+
Compute compute = ComputeOptions.getDefaultInstance().getService();
219219
DiskId diskId = DiskId.of("us-central1-a", "disk-name");
220220
Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields());
221221
if (disk != null) {
@@ -242,7 +242,7 @@ import com.google.cloud.compute.InstanceInfo;
242242
import com.google.cloud.compute.MachineTypeId;
243243
import com.google.cloud.compute.NetworkId;
244244
245-
Compute compute = ComputeOptions.defaultInstance().service();
245+
Compute compute = ComputeOptions.getDefaultInstance().getService();
246246
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
247247
NetworkId networkId = NetworkId.of("default");
248248
AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
@@ -281,7 +281,7 @@ import com.google.cloud.datastore.Entity;
281281
import com.google.cloud.datastore.Key;
282282
import com.google.cloud.datastore.KeyFactory;
283283
284-
Datastore datastore = DatastoreOptions.defaultInstance().service();
284+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
285285
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
286286
Key key = keyFactory.newKey("keyName");
287287
Entity entity = Entity.newBuilder(key)
@@ -302,7 +302,7 @@ import com.google.cloud.datastore.Entity;
302302
import com.google.cloud.datastore.Key;
303303
import com.google.cloud.datastore.KeyFactory;
304304
305-
Datastore datastore = DatastoreOptions.defaultInstance().service();
305+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
306306
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
307307
Key key = keyFactory.newKey("keyName");
308308
Entity entity = datastore.get(key);
@@ -335,7 +335,7 @@ import com.google.cloud.dns.DnsOptions;
335335
import com.google.cloud.dns.Zone;
336336
import com.google.cloud.dns.ZoneInfo;
337337
338-
Dns dns = DnsOptions.defaultInstance().service();
338+
Dns dns = DnsOptions.getDefaultInstance().getService();
339339
String zoneName = "my-unique-zone";
340340
String domainName = "someexampledomain.com.";
341341
String description = "This is a google-cloud-dns sample zone.";
@@ -355,7 +355,7 @@ import com.google.cloud.dns.Zone;
355355
import java.util.Iterator;
356356
import java.util.concurrent.TimeUnit;
357357
358-
Dns dns = DnsOptions.defaultInstance().service();
358+
Dns dns = DnsOptions.getDefaultInstance().getService();
359359
String zoneName = "my-unique-zone";
360360
Zone zone = dns.getZone(zoneName);
361361
String ip = "12.13.14.15";
@@ -370,8 +370,8 @@ ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.newBuilder().add(toC
370370
Iterator<RecordSet> recordSetIterator = zone.listRecordSets().iterateAll();
371371
while (recordSetIterator.hasNext()) {
372372
RecordSet current = recordSetIterator.next();
373-
if (toCreate.name().equals(current.getName()) &&
374-
toCreate.type().equals(current.getType())) {
373+
if (toCreate.getName().equals(current.getName()) &&
374+
toCreate.getType().equals(current.getType())) {
375375
changeBuilder.delete(current);
376376
}
377377
}
@@ -409,19 +409,19 @@ import com.google.cloud.logging.Payload.StringPayload;
409409
import java.util.Collections;
410410
import java.util.Iterator;
411411
412-
LoggingOptions options = LoggingOptions.defaultInstance();
413-
try(Logging logging = options.service()) {
412+
LoggingOptions options = LoggingOptions.getDefaultInstance();
413+
try(Logging logging = options.getService()) {
414414
415415
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
416416
.setLogName("test-log")
417-
.setResource(MonitoredResource.builder("global")
418-
.addLabel("project_id", options.projectId())
417+
.setResource(MonitoredResource.newBuilder("global")
418+
.addLabel("project_id", options.getProjectId())
419419
.build())
420420
.build();
421421
logging.write(Collections.singleton(firstEntry));
422422
423423
Page<LogEntry> entries = logging.listLogEntries(
424-
EntryListOption.filter("logName=projects/" + options.projectId() + "/logs/test-log"));
424+
EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
425425
Iterator<LogEntry> entryIterator = entries.iterateAll();
426426
while (entryIterator.hasNext()) {
427427
System.out.println(entryIterator.next());
@@ -471,7 +471,7 @@ import com.google.cloud.pubsub.PubSubOptions;
471471
import com.google.cloud.pubsub.Subscription;
472472
import com.google.cloud.pubsub.SubscriptionInfo;
473473
474-
try (PubSub pubsub = PubSubOptions.defaultInstance().service()) {
474+
try (PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) {
475475
Subscription subscription =
476476
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
477477
MessageProcessor callback = new MessageProcessor() {
@@ -505,7 +505,7 @@ import com.google.cloud.resourcemanager.ResourceManagerOptions;
505505
506506
import java.util.Iterator;
507507
508-
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
508+
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
509509
Project project = resourceManager.get("some-project-id"); // Use an existing project's ID
510510
if (project != null) {
511511
Project newProject = project.toBuilder()
@@ -546,7 +546,7 @@ import com.google.cloud.storage.BlobInfo;
546546
import com.google.cloud.storage.Storage;
547547
import com.google.cloud.storage.StorageOptions;
548548
549-
Storage storage = StorageOptions.defaultInstance().service();
549+
Storage storage = StorageOptions.getDefaultInstance().getService();
550550
BlobId blobId = BlobId.of("bucket", "blob_name");
551551
BlobInfo blobInfo = BlobInfo.newBuiler(blobId).setContentType("text/plain").build();
552552
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
@@ -565,7 +565,7 @@ import com.google.cloud.storage.StorageOptions;
565565
import java.nio.ByteBuffer;
566566
import java.nio.channels.WritableByteChannel;
567567
568-
Storage storage = StorageOptions.defaultInstance().service();
568+
Storage storage = StorageOptions.getDefaultInstance().getService();
569569
BlobId blobId = BlobId.of("bucket", "blob_name");
570570
Blob blob = storage.get(blobId);
571571
if (blob != null) {
@@ -598,7 +598,7 @@ import com.google.cloud.translate.Translate.TranslateOption;
598598
import com.google.cloud.translate.TranslateOptions;
599599
import com.google.cloud.translate.Translation;
600600
601-
Translate translate = TranslateOptions.defaultInstance().service();
601+
Translate translate = TranslateOptions.getDefaultInstance().getService();
602602
603603
Detection detection = translate.detect("Hola");
604604
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)