From 03e0042fd85503da6741cac583084dd25f8b1918 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 27 Feb 2018 16:51:44 -0800 Subject: [PATCH] Cleanup Cloud IoT region tags. (#1041) --- .../iot/examples/DeviceRegistryExample.java | 34 +++++++++++++++++++ .../cloud/iot/examples/HttpExample.java | 20 +++++------ .../cloud/iot/examples/MqttExample.java | 24 ++++++------- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java index fb8d6a4ad09..32b8362822b 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java @@ -110,6 +110,7 @@ public static Topic createIotTopic(String projectId, String topicId) throws Exce } } + // [START iot_create_registry] /** Create a registry for Cloud IoT. */ public static void createRegistry(String cloudRegion, String projectId, String registryName, String pubsubTopicPath) @@ -137,7 +138,9 @@ public static void createRegistry(String cloudRegion, String projectId, String r registry).execute(); System.out.println("Created registry: " + reg.getName()); } + // [END iot_create_registry] + // [START iot_delete_registry] /** Delete this registry from Cloud IoT. */ public static void deleteRegistry(String cloudRegion, String projectId, String registryName) throws GeneralSecurityException, IOException { @@ -155,7 +158,9 @@ public static void deleteRegistry(String cloudRegion, String projectId, String r System.out.println("Deleting: " + registryPath); service.projects().locations().registries().delete(registryPath).execute(); } + // [END iot_delete_registry] + // [START iot_list_devices] /** Print all of the devices in this registry to standard out. */ public static void listDevices(String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { @@ -194,7 +199,9 @@ public static void listDevices(String projectId, String cloudRegion, String regi System.out.println("Registry has no devices."); } } + // [END iot_list_devices] + // [START iot_create_es_device] /** Create a device that is authenticated using ES256. */ public static void createDeviceWithEs256(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, String registryName) @@ -234,7 +241,9 @@ public static void createDeviceWithEs256(String deviceId, String publicKeyFilePa System.out.println("Created device: " + createdDevice.toPrettyString()); } + // [END iot_create_es_device] + // [START iot_create_rsa_device] /** Create a device that is authenticated using RS256. */ public static void createDeviceWithRs256(String deviceId, String certificateFilePath, String projectId, String cloudRegion, @@ -274,7 +283,9 @@ public static void createDeviceWithRs256(String deviceId, String certificateFile System.out.println("Created device: " + createdDevice.toPrettyString()); } + // [END iot_create_rsa_device] + // [START iot_create_unauth_device] /** * Create a device that has no credentials. * @@ -310,7 +321,9 @@ public static void createDeviceWithNoAuth(String deviceId, String projectId, Str System.out.println("Created device: " + createdDevice.toPrettyString()); } + // [END iot_create_unauth_device] + // [START iot_delete_device] /** Delete the given device from the registry. */ public static void deleteDevice(String deviceId, String projectId, String cloudRegion, String registryName) @@ -329,7 +342,9 @@ public static void deleteDevice(String deviceId, String projectId, String cloudR System.out.println("Deleting device " + devicePath); service.projects().locations().registries().devices().delete(devicePath).execute(); } + // [END iot_delete_device] + // [START iot_get_device] /** Retrieves device metadata from a registry. **/ public static Device getDevice(String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { @@ -347,7 +362,9 @@ public static Device getDevice(String deviceId, String projectId, String cloudRe System.out.println("Retrieving device " + devicePath); return service.projects().locations().registries().devices().get(devicePath).execute(); } + // [END iot_get_device] + // [START iot_get_device_state] /** Retrieves device metadata from a registry. **/ public static List getDeviceStates( String deviceId, String projectId, String cloudRegion, String registryName) @@ -375,7 +392,9 @@ public static List getDeviceStates( return resp.getDeviceStates(); } + // [END iot_get_device_state] + // [START iot_get_registry] /** Retrieves registry metadata from a project. **/ public static DeviceRegistry getRegistry( String projectId, String cloudRegion, String registryName) @@ -393,7 +412,9 @@ public static DeviceRegistry getRegistry( return service.projects().locations().registries().get(registryPath).execute(); } + // [END iot_get_registry] + // [START iot_get_device_config] /** List all of the configs for the given device. */ public static void listDeviceConfigs( String deviceId, String projectId, String cloudRegion, String registryName) @@ -427,7 +448,9 @@ public static void listDeviceConfigs( System.out.println(); } } + // [END iot_get_device_config] + // [START iot_list_registries] /** Lists all of the registries associated with the given project. */ public static void listRegistries(String projectId, String cloudRegion) throws GeneralSecurityException, IOException { @@ -464,7 +487,9 @@ public static void listRegistries(String projectId, String cloudRegion) System.out.println("Project has no registries."); } } + // [END iot_list_registries] + // [START iot_patch_es] /** Patch the device to add an ES256 key for authentication. */ public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, String registryName) @@ -503,7 +528,9 @@ public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath, System.out.println("Patched device is " + patchedDevice.toPrettyString()); } + // [END iot_patch_es] + // [START iot_patch_rsa] /** Patch the device to add an RSA256 key for authentication. */ public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, @@ -543,7 +570,9 @@ public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath, System.out.println("Patched device is " + patchedDevice.toPrettyString()); } + // [END iot_patch_rsa] + // [START iot_set_device_config] /** Set a device configuration to the specified data (string, JSON) and version (0 for latest). */ public static void setDeviceConfiguration( String deviceId, String projectId, String cloudRegion, String registryName, @@ -578,7 +607,9 @@ public static void setDeviceConfiguration( System.out.println("Updated: " + config.getVersion()); } + // [END iot_set_device_config] + // [START iot_get_iam_policy] /** Retrieves IAM permissions for the given registry. */ public static void getIamPermissions( String projectId, String cloudRegion, String registryName) @@ -615,7 +646,9 @@ public static void getIamPermissions( System.out.println(String.format("No policy bindings for %s", registryName)); } } + // [END iot_get_iam_policy] + // [START iot_set_iam_policy] /** Sets IAM permissions for the given registry. */ public static void setIamPermissions( String projectId, String cloudRegion, String registryName, @@ -686,6 +719,7 @@ public static void setIamPermissions( } } } + // [END iot_set_iam_policy] /** Entry poit for CLI. */ public static void main(String[] args) throws Exception { diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java index de636b31f43..18ad0aa7239 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java @@ -16,7 +16,7 @@ package com.example.cloud.iot.examples; -// [START cloudiotcore_http_imports] +// [START iot_http_includes] import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler; @@ -51,7 +51,7 @@ import org.joda.time.DateTime; import org.json.JSONException; import org.json.JSONObject; -// [END cloudiotcore_http_imports] +// [END iot_http_includes] /** * Java sample of connecting to Google Cloud IoT Core vice via HTTP, using JWT. @@ -68,7 +68,7 @@ public class HttpExample { static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); static final JsonFactory JSON_FACTORY = new JacksonFactory(); - // [START cloudiotcore_http_createjwt] + // [START iot_http_jwt] /** Create a RSA-based JWT for the given project id, signed with the given private key. */ private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception { DateTime now = new DateTime(); @@ -106,9 +106,9 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact(); } - // [END cloudiotcore_http_createjwt] + // [END iot_http_jwt] - // [START cloudiotcore_http_getconfig] + // [START iot_http_getconfig] /** Publish an event or state message using Cloud IoT Core via the HTTP API. */ public static void getConfig(String urlPath, String token, String projectId, String cloudRegion, String registryId, String deviceId, String version) @@ -151,9 +151,9 @@ public void initialize(HttpRequest request) { System.out.println(CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8))); } - // [END cloudiotcore_http_getconfig] + // [END iot_http_getconfig] - // [START cloudiotcore_http_publishmessage] + // [START iot_http_publish] /** Publish an event or state message using Cloud IoT Core via the HTTP API. */ public static void publishMessage(String payload, String urlPath, String messageType, String token, String projectId, String cloudRegion, String registryId, String deviceId) @@ -216,9 +216,9 @@ public void initialize(HttpRequest request) { System.out.println(res.getStatusCode()); System.out.println(res.getStatusMessage()); } - // [END cloudiotcore_http_publishmessage] + // [END iot_http_publish] - // [START cloudiotcore_http_run] + // [START iot_http_run] /** Parse arguments and publish messages. */ public static void main(String[] args) throws Exception { HttpExampleOptions options = HttpExampleOptions.fromFlags(args); @@ -280,5 +280,5 @@ public static void main(String[] args) throws Exception { } System.out.println("Finished loop successfully. Goodbye!"); } - // [END cloudiotcore_http_run] + // [END iot_http_run] } diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java index 99d3fe58aea..dea1c5a0016 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java @@ -16,7 +16,7 @@ package com.example.cloud.iot.examples; -// [START cloudiotcore_mqtt_imports] +// [START iot_mqtt_includes] import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; @@ -34,7 +34,7 @@ import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.joda.time.DateTime; -// [END cloudiotcore_mqtt_imports] +// [END iot_mqtt_includes] /** * Java sample of connecting to Google Cloud IoT Core vice via MQTT, using JWT. @@ -61,7 +61,7 @@ * */ public class MqttExample { - // [START cloudiotcore_mqtt_createjwt] + // [START iot_mqtt_jwt] /** Create a Cloud IoT Core JWT for the given project id, signed with the given RSA key. */ private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception { DateTime now = new DateTime(); @@ -99,9 +99,9 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact(); } - // [END cloudiotcore_mqtt_createjwt] + // [END iot_mqtt_jwt] - // [START cloudiotcore_mqtt_configcallback] + // [START iot_mqtt_configcallback] static MqttCallback mCallback; /** Attaches the callback used when configuration changes occur. */ @@ -130,11 +130,11 @@ public void deliveryComplete(IMqttDeliveryToken token) { client.setCallback(mCallback); } - // [END cloudiotcore_mqtt_configcallback] + // [END iot_mqtt_configcallback] /** Parse arguments, configure MQTT, and publish messages. */ public static void main(String[] args) throws Exception { - // [START cloudiotcore_mqtt_configuremqtt] + // [START iot_mqtt_configuremqtt] MqttExampleOptions options = MqttExampleOptions.fromFlags(args); if (options == null) { // Could not parse. @@ -180,9 +180,9 @@ public static void main(String[] args) throws Exception { throw new IllegalArgumentException( "Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'."); } - // [END cloudiotcore_mqtt_configuremqtt] + // [END iot_mqtt_configuremqtt] - // [START cloudiotcore_mqtt_publish] + // [START iot_mqtt_publish] // Create a client, and connect to the Google MQTT bridge. MqttClient client = new MqttClient(mqttServerAddress, mqttClientId, new MemoryPersistence()); @@ -238,7 +238,7 @@ public static void main(String[] args) throws Exception { options.messageType, i, options.numMessages, payload); // Refresh the connection credentials before the JWT expires. - // [START cloudiotcore_mqtt_jwt_refresh] + // [START iot_mqtt_jwt_refresh] long secsSinceRefresh = ((new DateTime()).getMillis() - iat.getMillis()) / 1000; if (secsSinceRefresh > (options.tokenExpMins * 60)) { System.out.format("\tRefreshing token after: %d seconds\n", secsSinceRefresh); @@ -258,7 +258,7 @@ public static void main(String[] args) throws Exception { client.connect(); attachCallback(client, options.deviceId); } - // [END cloudiotcore_mqtt_jwt_refresh] + // [END iot_mqtt_jwt_refresh] // Publish "payload" to the MQTT topic. qos=1 means at least once delivery. Cloud IoT Core // also supports qos=0 for at most once delivery. @@ -281,6 +281,6 @@ public static void main(String[] args) throws Exception { } System.out.println("Finished loop successfully. Goodbye!"); - // [END cloudiotcore_mqtt_publish] + // [END iot_mqtt_publish] } }