Search in sources :

Example 6 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDeviceWithRs256.

// [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, 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();
    String key = Files.toString(new File(certificateFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("RSA_X509_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 7 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method deleteRegistry.

// [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 {
    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);
    System.out.println("Deleting: " + registryPath);
    service.projects().locations().registries().delete(registryPath).execute();
}
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) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 8 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method setIamPermissions.

// [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, String member, String role) 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();
    List<com.google.api.services.cloudiot.v1.model.Binding> bindings = policy.getBindings();
    boolean addNewRole = true;
    if (bindings != null) {
        for (com.google.api.services.cloudiot.v1.model.Binding binding : bindings) {
            if (binding.getRole().equals(role)) {
                List<String> members = binding.getMembers();
                members.add(member);
                binding.setMembers(members);
                addNewRole = false;
            }
        }
    } else {
        bindings = new ArrayList<>();
    }
    if (addNewRole) {
        com.google.api.services.cloudiot.v1.model.Binding bind = new com.google.api.services.cloudiot.v1.model.Binding();
        bind.setRole(role);
        List<String> members = new ArrayList<>();
        members.add(member);
        bind.setMembers(members);
        bindings.add(bind);
    }
    policy.setBindings(bindings);
    SetIamPolicyRequest req = new SetIamPolicyRequest().setPolicy(policy);
    policy = service.projects().locations().registries().setIamPolicy(registryPath, req).execute();
    System.out.println("Policy ETAG: " + policy.getEtag());
    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 mem : binding.getMembers()) {
            System.out.println(String.format("\t%s", mem));
        }
    }
}
Also used : Binding(com.google.iam.v1.Binding) CloudIot(com.google.api.services.cloudiot.v1.CloudIot) SetIamPolicyRequest(com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest) JsonFactory(com.google.api.client.json.JsonFactory) ArrayList(java.util.ArrayList) 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)

Example 9 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method getRegistry.

// [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) 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);
    return service.projects().locations().registries().get(registryPath).execute();
}
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) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 10 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method patchRsa256ForAuth.

// [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, 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("RSA_X509_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)

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