Search in sources :

Example 1 with CloudIot

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());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceRegistry(com.google.api.services.cloudiot.v1.model.DeviceRegistry) JsonFactory(com.google.api.client.json.JsonFactory) ArrayList(java.util.ArrayList) EventNotificationConfig(com.google.api.services.cloudiot.v1.model.EventNotificationConfig) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 2 with CloudIot

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());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 3 with CloudIot

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());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) Base64(java.util.Base64) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) DeviceConfig(com.google.api.services.cloudiot.v1.model.DeviceConfig) ModifyCloudToDeviceConfigRequest(com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)

Example 4 with CloudIot

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());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 5 with CloudIot

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));
    }
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) GetIamPolicyRequest(com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)17 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)17 JsonFactory (com.google.api.client.json.JsonFactory)17 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)17 Device (com.google.api.services.cloudiot.v1.model.Device)6 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)5 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)4 File (java.io.File)4 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 DeviceRegistry (com.google.api.services.cloudiot.v1.model.DeviceRegistry)2 GetIamPolicyRequest (com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest)2 ArrayList (java.util.ArrayList)2 EventNotificationConfig (com.google.api.services.cloudiot.v1.model.EventNotificationConfig)1 ListDeviceStatesResponse (com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)1 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)1 SetIamPolicyRequest (com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest)1 Binding (com.google.iam.v1.Binding)1 Base64 (java.util.Base64)1