Skip to content

Commit a58c68f

Browse files
jmdobrykurtisvg
authored andcommitted
Cleanup Cloud IoT region tags. (#1041)
1 parent 7e9945c commit a58c68f

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java

+34
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static Topic createIotTopic(String projectId, String topicId) throws Exce
110110
}
111111
}
112112

113+
// [START iot_create_registry]
113114
/** Create a registry for Cloud IoT. */
114115
public static void createRegistry(String cloudRegion, String projectId, String registryName,
115116
String pubsubTopicPath)
@@ -137,7 +138,9 @@ public static void createRegistry(String cloudRegion, String projectId, String r
137138
registry).execute();
138139
System.out.println("Created registry: " + reg.getName());
139140
}
141+
// [END iot_create_registry]
140142

143+
// [START iot_delete_registry]
141144
/** Delete this registry from Cloud IoT. */
142145
public static void deleteRegistry(String cloudRegion, String projectId, String registryName)
143146
throws GeneralSecurityException, IOException {
@@ -155,7 +158,9 @@ public static void deleteRegistry(String cloudRegion, String projectId, String r
155158
System.out.println("Deleting: " + registryPath);
156159
service.projects().locations().registries().delete(registryPath).execute();
157160
}
161+
// [END iot_delete_registry]
158162

163+
// [START iot_list_devices]
159164
/** Print all of the devices in this registry to standard out. */
160165
public static void listDevices(String projectId, String cloudRegion, String registryName) throws
161166
GeneralSecurityException, IOException {
@@ -194,7 +199,9 @@ public static void listDevices(String projectId, String cloudRegion, String regi
194199
System.out.println("Registry has no devices.");
195200
}
196201
}
202+
// [END iot_list_devices]
197203

204+
// [START iot_create_es_device]
198205
/** Create a device that is authenticated using ES256. */
199206
public static void createDeviceWithEs256(String deviceId, String publicKeyFilePath,
200207
String projectId, String cloudRegion, String registryName)
@@ -234,7 +241,9 @@ public static void createDeviceWithEs256(String deviceId, String publicKeyFilePa
234241

235242
System.out.println("Created device: " + createdDevice.toPrettyString());
236243
}
244+
// [END iot_create_es_device]
237245

246+
// [START iot_create_rsa_device]
238247
/** Create a device that is authenticated using RS256. */
239248
public static void createDeviceWithRs256(String deviceId, String certificateFilePath,
240249
String projectId, String cloudRegion,
@@ -274,7 +283,9 @@ public static void createDeviceWithRs256(String deviceId, String certificateFile
274283

275284
System.out.println("Created device: " + createdDevice.toPrettyString());
276285
}
286+
// [END iot_create_rsa_device]
277287

288+
// [START iot_create_unauth_device]
278289
/**
279290
* Create a device that has no credentials.
280291
*
@@ -310,7 +321,9 @@ public static void createDeviceWithNoAuth(String deviceId, String projectId, Str
310321

311322
System.out.println("Created device: " + createdDevice.toPrettyString());
312323
}
324+
// [END iot_create_unauth_device]
313325

326+
// [START iot_delete_device]
314327
/** Delete the given device from the registry. */
315328
public static void deleteDevice(String deviceId, String projectId, String cloudRegion,
316329
String registryName)
@@ -329,7 +342,9 @@ public static void deleteDevice(String deviceId, String projectId, String cloudR
329342
System.out.println("Deleting device " + devicePath);
330343
service.projects().locations().registries().devices().delete(devicePath).execute();
331344
}
345+
// [END iot_delete_device]
332346

347+
// [START iot_get_device]
333348
/** Retrieves device metadata from a registry. **/
334349
public static Device getDevice(String deviceId, String projectId, String cloudRegion,
335350
String registryName) throws GeneralSecurityException, IOException {
@@ -347,7 +362,9 @@ public static Device getDevice(String deviceId, String projectId, String cloudRe
347362
System.out.println("Retrieving device " + devicePath);
348363
return service.projects().locations().registries().devices().get(devicePath).execute();
349364
}
365+
// [END iot_get_device]
350366

367+
// [START iot_get_device_state]
351368
/** Retrieves device metadata from a registry. **/
352369
public static List<DeviceState> getDeviceStates(
353370
String deviceId, String projectId, String cloudRegion, String registryName)
@@ -375,7 +392,9 @@ public static List<DeviceState> getDeviceStates(
375392

376393
return resp.getDeviceStates();
377394
}
395+
// [END iot_get_device_state]
378396

397+
// [START iot_get_registry]
379398
/** Retrieves registry metadata from a project. **/
380399
public static DeviceRegistry getRegistry(
381400
String projectId, String cloudRegion, String registryName)
@@ -393,7 +412,9 @@ public static DeviceRegistry getRegistry(
393412

394413
return service.projects().locations().registries().get(registryPath).execute();
395414
}
415+
// [END iot_get_registry]
396416

417+
// [START iot_get_device_config]
397418
/** List all of the configs for the given device. */
398419
public static void listDeviceConfigs(
399420
String deviceId, String projectId, String cloudRegion, String registryName)
@@ -427,7 +448,9 @@ public static void listDeviceConfigs(
427448
System.out.println();
428449
}
429450
}
451+
// [END iot_get_device_config]
430452

453+
// [START iot_list_registries]
431454
/** Lists all of the registries associated with the given project. */
432455
public static void listRegistries(String projectId, String cloudRegion)
433456
throws GeneralSecurityException, IOException {
@@ -464,7 +487,9 @@ public static void listRegistries(String projectId, String cloudRegion)
464487
System.out.println("Project has no registries.");
465488
}
466489
}
490+
// [END iot_list_registries]
467491

492+
// [START iot_patch_es]
468493
/** Patch the device to add an ES256 key for authentication. */
469494
public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath, String projectId,
470495
String cloudRegion, String registryName)
@@ -503,7 +528,9 @@ public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath,
503528

504529
System.out.println("Patched device is " + patchedDevice.toPrettyString());
505530
}
531+
// [END iot_patch_es]
506532

533+
// [START iot_patch_rsa]
507534
/** Patch the device to add an RSA256 key for authentication. */
508535
public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath, String projectId,
509536
String cloudRegion,
@@ -543,7 +570,9 @@ public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath,
543570

544571
System.out.println("Patched device is " + patchedDevice.toPrettyString());
545572
}
573+
// [END iot_patch_rsa]
546574

575+
// [START iot_set_device_config]
547576
/** Set a device configuration to the specified data (string, JSON) and version (0 for latest). */
548577
public static void setDeviceConfiguration(
549578
String deviceId, String projectId, String cloudRegion, String registryName,
@@ -578,7 +607,9 @@ public static void setDeviceConfiguration(
578607

579608
System.out.println("Updated: " + config.getVersion());
580609
}
610+
// [END iot_set_device_config]
581611

612+
// [START iot_get_iam_policy]
582613
/** Retrieves IAM permissions for the given registry. */
583614
public static void getIamPermissions(
584615
String projectId, String cloudRegion, String registryName)
@@ -615,7 +646,9 @@ public static void getIamPermissions(
615646
System.out.println(String.format("No policy bindings for %s", registryName));
616647
}
617648
}
649+
// [END iot_get_iam_policy]
618650

651+
// [START iot_set_iam_policy]
619652
/** Sets IAM permissions for the given registry. */
620653
public static void setIamPermissions(
621654
String projectId, String cloudRegion, String registryName,
@@ -686,6 +719,7 @@ public static void setIamPermissions(
686719
}
687720
}
688721
}
722+
// [END iot_set_iam_policy]
689723

690724
/** Entry poit for CLI. */
691725
public static void main(String[] args) throws Exception {

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.example.cloud.iot.examples;
1818

19-
// [START cloudiotcore_http_imports]
19+
// [START iot_http_includes]
2020
import com.google.api.client.http.ByteArrayContent;
2121
import com.google.api.client.http.GenericUrl;
2222
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
@@ -51,7 +51,7 @@
5151
import org.joda.time.DateTime;
5252
import org.json.JSONException;
5353
import org.json.JSONObject;
54-
// [END cloudiotcore_http_imports]
54+
// [END iot_http_includes]
5555

5656
/**
5757
* Java sample of connecting to Google Cloud IoT Core vice via HTTP, using JWT.
@@ -68,7 +68,7 @@ public class HttpExample {
6868
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
6969
static final JsonFactory JSON_FACTORY = new JacksonFactory();
7070

71-
// [START cloudiotcore_http_createjwt]
71+
// [START iot_http_jwt]
7272
/** Create a RSA-based JWT for the given project id, signed with the given private key. */
7373
private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception {
7474
DateTime now = new DateTime();
@@ -106,9 +106,9 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw
106106

107107
return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact();
108108
}
109-
// [END cloudiotcore_http_createjwt]
109+
// [END iot_http_jwt]
110110

111-
// [START cloudiotcore_http_getconfig]
111+
// [START iot_http_getconfig]
112112
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
113113
public static void getConfig(String urlPath, String token, String projectId,
114114
String cloudRegion, String registryId, String deviceId, String version)
@@ -151,9 +151,9 @@ public void initialize(HttpRequest request) {
151151

152152
System.out.println(CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8)));
153153
}
154-
// [END cloudiotcore_http_getconfig]
154+
// [END iot_http_getconfig]
155155

156-
// [START cloudiotcore_http_publishmessage]
156+
// [START iot_http_publish]
157157
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
158158
public static void publishMessage(String payload, String urlPath, String messageType,
159159
String token, String projectId, String cloudRegion, String registryId, String deviceId)
@@ -216,9 +216,9 @@ public void initialize(HttpRequest request) {
216216
System.out.println(res.getStatusCode());
217217
System.out.println(res.getStatusMessage());
218218
}
219-
// [END cloudiotcore_http_publishmessage]
219+
// [END iot_http_publish]
220220

221-
// [START cloudiotcore_http_run]
221+
// [START iot_http_run]
222222
/** Parse arguments and publish messages. */
223223
public static void main(String[] args) throws Exception {
224224
HttpExampleOptions options = HttpExampleOptions.fromFlags(args);
@@ -280,5 +280,5 @@ public static void main(String[] args) throws Exception {
280280
}
281281
System.out.println("Finished loop successfully. Goodbye!");
282282
}
283-
// [END cloudiotcore_http_run]
283+
// [END iot_http_run]
284284
}

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.example.cloud.iot.examples;
1818

19-
// [START cloudiotcore_mqtt_imports]
19+
// [START iot_mqtt_includes]
2020
import io.jsonwebtoken.JwtBuilder;
2121
import io.jsonwebtoken.Jwts;
2222
import io.jsonwebtoken.SignatureAlgorithm;
@@ -34,7 +34,7 @@
3434
import org.eclipse.paho.client.mqttv3.MqttMessage;
3535
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
3636
import org.joda.time.DateTime;
37-
// [END cloudiotcore_mqtt_imports]
37+
// [END iot_mqtt_includes]
3838

3939
/**
4040
* Java sample of connecting to Google Cloud IoT Core vice via MQTT, using JWT.
@@ -61,7 +61,7 @@
6161
* </pre>
6262
*/
6363
public class MqttExample {
64-
// [START cloudiotcore_mqtt_createjwt]
64+
// [START iot_mqtt_jwt]
6565
/** Create a Cloud IoT Core JWT for the given project id, signed with the given RSA key. */
6666
private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception {
6767
DateTime now = new DateTime();
@@ -99,9 +99,9 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw
9999

100100
return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact();
101101
}
102-
// [END cloudiotcore_mqtt_createjwt]
102+
// [END iot_mqtt_jwt]
103103

104-
// [START cloudiotcore_mqtt_configcallback]
104+
// [START iot_mqtt_configcallback]
105105
static MqttCallback mCallback;
106106

107107
/** Attaches the callback used when configuration changes occur. */
@@ -130,11 +130,11 @@ public void deliveryComplete(IMqttDeliveryToken token) {
130130

131131
client.setCallback(mCallback);
132132
}
133-
// [END cloudiotcore_mqtt_configcallback]
133+
// [END iot_mqtt_configcallback]
134134

135135
/** Parse arguments, configure MQTT, and publish messages. */
136136
public static void main(String[] args) throws Exception {
137-
// [START cloudiotcore_mqtt_configuremqtt]
137+
// [START iot_mqtt_configuremqtt]
138138
MqttExampleOptions options = MqttExampleOptions.fromFlags(args);
139139
if (options == null) {
140140
// Could not parse.
@@ -180,9 +180,9 @@ public static void main(String[] args) throws Exception {
180180
throw new IllegalArgumentException(
181181
"Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'.");
182182
}
183-
// [END cloudiotcore_mqtt_configuremqtt]
183+
// [END iot_mqtt_configuremqtt]
184184

185-
// [START cloudiotcore_mqtt_publish]
185+
// [START iot_mqtt_publish]
186186
// Create a client, and connect to the Google MQTT bridge.
187187
MqttClient client = new MqttClient(mqttServerAddress, mqttClientId, new MemoryPersistence());
188188

@@ -238,7 +238,7 @@ public static void main(String[] args) throws Exception {
238238
options.messageType, i, options.numMessages, payload);
239239

240240
// Refresh the connection credentials before the JWT expires.
241-
// [START cloudiotcore_mqtt_jwt_refresh]
241+
// [START iot_mqtt_jwt_refresh]
242242
long secsSinceRefresh = ((new DateTime()).getMillis() - iat.getMillis()) / 1000;
243243
if (secsSinceRefresh > (options.tokenExpMins * 60)) {
244244
System.out.format("\tRefreshing token after: %d seconds\n", secsSinceRefresh);
@@ -258,7 +258,7 @@ public static void main(String[] args) throws Exception {
258258
client.connect();
259259
attachCallback(client, options.deviceId);
260260
}
261-
// [END cloudiotcore_mqtt_jwt_refresh]
261+
// [END iot_mqtt_jwt_refresh]
262262

263263
// Publish "payload" to the MQTT topic. qos=1 means at least once delivery. Cloud IoT Core
264264
// also supports qos=0 for at most once delivery.
@@ -281,6 +281,6 @@ public static void main(String[] args) throws Exception {
281281
}
282282

283283
System.out.println("Finished loop successfully. Goodbye!");
284-
// [END cloudiotcore_mqtt_publish]
284+
// [END iot_mqtt_publish]
285285
}
286286
}

0 commit comments

Comments
 (0)