Skip to content

Commit

Permalink
Cleanup Cloud IoT region tags. (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored and kurtisvg committed Mar 16, 2018
1 parent 7e9945c commit a58c68f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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<DeviceState> getDeviceStates(
String deviceId, String projectId, String cloudRegion, String registryName)
Expand Down Expand Up @@ -375,7 +392,9 @@ public static List<DeviceState> 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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -61,7 +61,7 @@
* </pre>
*/
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();
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -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]
}
}

0 comments on commit a58c68f

Please sign in to comment.