use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method createRegistry.
// [START iot_create_registry]
/**
* Create a registry for Cloud IoT.
*/
public static void createRegistry(String cloudRegion, String projectId, String registryName, String pubsubTopicPath) throws GeneralSecurityException, IOException {
GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
final String fullPubsubPath = "projects/" + projectId + "/topics/" + pubsubTopicPath;
DeviceRegistry registry = new DeviceRegistry();
EventNotificationConfig notificationConfig = new EventNotificationConfig();
notificationConfig.setPubsubTopicName(fullPubsubPath);
List<EventNotificationConfig> notificationConfigs = new ArrayList<EventNotificationConfig>();
notificationConfigs.add(notificationConfig);
registry.setEventNotificationConfigs(notificationConfigs);
registry.setId(registryName);
DeviceRegistry reg = service.projects().locations().registries().create(projectPath, registry).execute();
System.out.println("Created registry: " + reg.getName());
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method patchEs256ForAuth.
// [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) throws GeneralSecurityException, IOException {
GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
publicKeyCredential.setKey(key);
publicKeyCredential.setFormat("ES256_PEM");
DeviceCredential devCredential = new DeviceCredential();
devCredential.setPublicKey(publicKeyCredential);
Device device = new Device();
device.setCredentials(Arrays.asList(devCredential));
Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method setDeviceConfiguration.
// [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, String data, long version) throws GeneralSecurityException, IOException {
GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
ModifyCloudToDeviceConfigRequest req = new ModifyCloudToDeviceConfigRequest();
req.setVersionToUpdate(version);
// Data sent through the wire has to be base64 encoded.
Base64.Encoder encoder = Base64.getEncoder();
String encPayload = encoder.encodeToString(data.getBytes("UTF-8"));
req.setBinaryData(encPayload);
DeviceConfig config = service.projects().locations().registries().devices().modifyCloudToDeviceConfig(devicePath, req).execute();
System.out.println("Updated: " + config.getVersion());
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method createDeviceWithEs256.
// [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) throws GeneralSecurityException, IOException {
GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
final String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
publicKeyCredential.setKey(key);
publicKeyCredential.setFormat("ES256_PEM");
DeviceCredential devCredential = new DeviceCredential();
devCredential.setPublicKey(publicKeyCredential);
System.out.println("Creating device with id: " + deviceId);
Device device = new Device();
device.setId(deviceId);
device.setCredentials(Arrays.asList(devCredential));
Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
System.out.println("Created device: " + createdDevice.toPrettyString());
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method getIamPermissions.
// [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) throws GeneralSecurityException, IOException {
GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
com.google.api.services.cloudiot.v1.model.Policy policy = service.projects().locations().registries().getIamPolicy(registryPath, new GetIamPolicyRequest()).execute();
System.out.println("Policy ETAG: " + policy.getEtag());
if (policy.getBindings() != null) {
for (com.google.api.services.cloudiot.v1.model.Binding binding : policy.getBindings()) {
System.out.println(String.format("Role: %s", binding.getRole()));
System.out.println("Binding members: ");
for (String member : binding.getMembers()) {
System.out.println(String.format("\t%s", member));
}
}
} else {
System.out.println(String.format("No policy bindings for %s", registryName));
}
}
Aggregations