Search in sources :

Example 6 with JobProperties

use of com.microsoft.azure.sdk.iot.service.JobProperties in project azure-iot-sdk-java by Azure.

the class ExportImportTests method runImportJob.

private static void runImportJob(List<ExportImportDevice> devices, ImportMode importMode, Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
    // Creating the json string to be submitted for import using the specified importMode
    StringBuilder devicesToAdd = new StringBuilder();
    for (int i = 0; i < devices.size(); i++) {
        devices.get(i).setImportMode(importMode);
        ExportImportDeviceParser parser = Deencapsulation.invoke(devices.get(i), "toExportImportDeviceParser");
        devicesToAdd.append(parser.toJson());
        if (i < devices.size() - 1) {
            devicesToAdd.append("\r\n");
        }
    }
    byte[] blobToImport = devicesToAdd.toString().getBytes(StandardCharsets.UTF_8);
    // Creating the Azure storage blob and uploading the serialized string of devices
    InputStream stream = new ByteArrayInputStream(blobToImport);
    String importBlobName = "devices.txt";
    BlockBlobClient importBlob = importContainer.getBlobClient(importBlobName).getBlockBlobClient();
    importBlob.delete();
    importBlob.upload(stream, blobToImport.length);
    // Starting the import job
    boolean importJobScheduled = false;
    JobProperties importJob = null;
    while (!importJobScheduled) {
        try {
            if (storageAuthenticationType.isPresent()) {
                // For a given StorageAuthenticationType, create JobProperties and pass it
                JobProperties importJobProperties = JobProperties.createForImportJob(getContainerSasUri(importContainer), getContainerSasUri(importContainer), storageAuthenticationType.get());
                importJob = registryManager.importDevices(importJobProperties);
            } else {
                importJob = registryManager.importDevices(getContainerSasUri(importContainer), getContainerSasUri(importContainer));
            }
            importJobScheduled = true;
        } catch (IotHubTooManyDevicesException e) {
            // test is being throttled, wait a while and try again
            Thread.sleep(10 * 1000);
        }
    }
    // Waiting for the import job to complete
    long startTime = System.currentTimeMillis();
    while (true) {
        importJob = registryManager.getJob(importJob.getJobId());
        if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED || importJob.getStatus() == JobProperties.JobStatus.FAILED) {
            break;
        }
        if (System.currentTimeMillis() - startTime > IMPORT_JOB_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for the import job to complete");
        }
        Thread.sleep(100);
    }
    // Checking the result of the import job
    if (importJob.getStatus() != JobProperties.JobStatus.COMPLETED) {
        Assert.fail("The import job was not completed successfully for " + importMode + " operation.");
    }
}
Also used : JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInputStream(com.azure.storage.blob.specialized.BlobInputStream) InputStream(java.io.InputStream) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser) IotHubTooManyDevicesException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)

Example 7 with JobProperties

use of com.microsoft.azure.sdk.iot.service.JobProperties in project azure-iot-sdk-java by Azure.

the class DeviceManagerImportExportWithIdentitySample method ImportDevices.

public static void ImportDevices() throws IOException, IotHubException, InterruptedException {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(destinationHubConnectionString);
    // If StorageAuthenticationType is set to IdentityBased and userAssignedIdentity property is
    // not null, the jobs will use user defined managed identity. If the IoT hub is not
    // configured with the user defined managed identity specified in userAssignedIdentity,
    // the job will fail.
    // If StorageAuthenticationType is set to IdentityBased the userAssignedIdentity property is
    // null, the jobs will use system defined identity. If the IoT hub is not configured with the
    // user defined managed identity, the job will fail.
    // If StorageAuthenticationType is set to IdentityBased and neither user defined nor system defined
    // managed identities are configured on the hub, the job will fail.
    ManagedIdentity identity = new ManagedIdentity();
    identity.setUserAssignedIdentity(userDefinedManagedIdentityResourceId);
    JobProperties jobProperties = JobProperties.createForImportJob(blobContainerUri, blobContainerUri, StorageAuthenticationType.IDENTITY, identity);
    JobProperties exportJob = registryManager.importDevices(jobProperties);
    while (true) {
        exportJob = registryManager.getJob(exportJob.getJobId());
        if (exportJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
            break;
        }
        Thread.sleep(500);
    }
    registryManager.close();
}
Also used : JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) ManagedIdentity(com.microsoft.azure.sdk.iot.deps.serializer.ManagedIdentity)

Aggregations

JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)7 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 BlobInputStream (com.azure.storage.blob.specialized.BlobInputStream)2 ExportImportDeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)2 JobPropertiesParser (com.microsoft.azure.sdk.iot.deps.serializer.JobPropertiesParser)2 ManagedIdentity (com.microsoft.azure.sdk.iot.deps.serializer.ManagedIdentity)2 IotHubTooManyDevicesException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)2 Date (java.util.Date)2 Test (org.junit.Test)2 BlobItem (com.azure.storage.blob.models.BlobItem)1 BlockBlobClient (com.azure.storage.blob.specialized.BlockBlobClient)1 ExportImportDevice (com.microsoft.azure.sdk.iot.service.ExportImportDevice)1 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)1 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)1 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)1 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)1 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1