Search in sources :

Example 1 with JobProperties

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

the class DeviceManagerImportExportWithIdentitySample method ExportDevices.

public static void ExportDevices() throws IOException, IotHubException, InterruptedException {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(sourceHubConnectionString);
    // 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.createForExportJob(blobContainerUri, false, StorageAuthenticationType.IDENTITY, identity);
    JobProperties exportJob = registryManager.exportDevices(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)

Example 2 with JobProperties

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

the class DeviceManagerExportSample method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting export sample...");
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(SampleUtils.storageConnectionString);
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(DeviceManagerExportSample.sampleContainerName);
    container.createIfNotExists();
    String containerSasUri = SampleUtils.getContainerSasUri(container);
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    JobProperties exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
    while (true) {
        exportJob = registryManager.getJob(exportJob.getJobId());
        if (exportJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
            break;
        }
        Thread.sleep(500);
    }
    for (ListBlobItem blobItem : container.listBlobs()) {
        if (blobItem instanceof CloudBlob) {
            CloudBlob blob = (CloudBlob) blobItem;
            blob.download(new FileOutputStream(SampleUtils.exportFileLocation + blob.getName()));
        }
    }
    registryManager.close();
    System.out.println("Export job completed. Results are in " + SampleUtils.exportFileLocation);
}
Also used : CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) FileOutputStream(java.io.FileOutputStream) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Example 3 with JobProperties

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

the class JobPropertiesTest method fromJobPropertiesParser.

// Tests_SRS_SERVICE_SDK_JAVA_JOB_PROPERTIES_34_003: [This method shall convert the provided parser into a JobProperty object and return it.]
@Test
public void fromJobPropertiesParser() {
    // arrange
    JobPropertiesParser parser = Deencapsulation.newInstance(JobPropertiesParser.class);
    parser.setEndTimeUtc(new Date(System.currentTimeMillis()));
    parser.setStartTimeUtc(new Date(System.currentTimeMillis()));
    parser.setFailureReason("failureReason");
    parser.setInputBlobContainerUri("inputContainerUri");
    parser.setOutputBlobContainerUri("outputContainerUri");
    parser.setProgress(0);
    parser.setExcludeKeysInExport(false);
    parser.setJobId("jobId");
    parser.setStatus(JobProperties.JobStatus.COMPLETED.toString());
    parser.setType(JobProperties.JobType.IMPORT.toString());
    // act
    JobProperties jobProperties = jobPropertiesConstructorWithParser(parser);
    // assert
    assertEquals(parser.getInputBlobContainerUri(), jobProperties.getInputBlobContainerUri());
    assertEquals(parser.getOutputBlobContainerUri(), jobProperties.getOutputBlobContainerUri());
    assertEquals(parser.isExcludeKeysInExport(), jobProperties.getExcludeKeysInExport());
    assertEquals(parser.getType(), jobProperties.getType().toString());
    assertEquals(parser.getStatus(), jobProperties.getStatus().toString());
    assertEquals(parser.getProgress(), jobProperties.getProgress());
    assertEquals(parser.getJobIdFinal(), jobProperties.getJobId());
    assertEquals(parser.getFailureReason(), jobProperties.getFailureReason());
    assertEquals(parser.getEndTimeUtc(), jobProperties.getEndTimeUtc());
    assertEquals(parser.getStartTimeUtc(), jobProperties.getStartTimeUtc());
}
Also used : JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) JobPropertiesParser(com.microsoft.azure.sdk.iot.deps.serializer.JobPropertiesParser) Date(java.util.Date) Test(org.junit.Test)

Example 4 with JobProperties

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

the class ExportImportTests method runExportJob.

private static List<ExportImportDevice> runExportJob(Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
    Boolean excludeKeys = false;
    String containerSasUri = getContainerSasUri(exportContainer);
    boolean exportJobScheduled = false;
    JobProperties exportJob = null;
    while (!exportJobScheduled) {
        try {
            if (storageAuthenticationType.isPresent()) {
                JobProperties exportJobProperties = JobProperties.createForExportJob(containerSasUri, excludeKeys, storageAuthenticationType.get());
                exportJob = registryManager.exportDevices(exportJobProperties);
            } else {
                exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
            }
            exportJobScheduled = true;
        } catch (IotHubTooManyDevicesException e) {
            // test is being throttled, wait a while and try again
            Thread.sleep(10 * 1000);
        }
    }
    JobProperties.JobStatus jobStatus;
    long startTime = System.currentTimeMillis();
    while (true) {
        exportJob = registryManager.getJob(exportJob.getJobId());
        jobStatus = exportJob.getStatus();
        if (jobStatus == JobProperties.JobStatus.COMPLETED || jobStatus == JobProperties.JobStatus.FAILED) {
            break;
        }
        if (System.currentTimeMillis() - startTime > EXPORT_JOB_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for the export job to complete");
        }
        Thread.sleep(100);
    }
    String exportedDevicesJson = "";
    for (BlobItem blobItem : exportContainer.listBlobs()) {
        BlobInputStream stream = exportContainer.getBlobClient(blobItem.getName()).openInputStream();
        try (Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8.name())) {
            exportedDevicesJson = scanner.next();
        }
    }
    List<ExportImportDevice> result = new ArrayList<>();
    Scanner scanner = new Scanner(exportedDevicesJson);
    while (scanner.hasNextLine()) {
        String exportImportDeviceJson = scanner.nextLine();
        ExportImportDeviceParser parser = new ExportImportDeviceParser(exportImportDeviceJson);
        ExportImportDevice device = Deencapsulation.newInstance(ExportImportDevice.class, new Class[] { ExportImportDeviceParser.class }, parser);
        device.setImportMode(ImportMode.CreateOrUpdate);
        result.add(device);
    }
    scanner.close();
    if (jobStatus != JobProperties.JobStatus.COMPLETED) {
        Assert.fail("The export job was not completed successfully");
    }
    return result;
}
Also used : Scanner(java.util.Scanner) JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) ArrayList(java.util.ArrayList) BlobInputStream(com.azure.storage.blob.specialized.BlobInputStream) IotHubTooManyDevicesException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException) BlobItem(com.azure.storage.blob.models.BlobItem) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)

Example 5 with JobProperties

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

the class JobPropertiesTest method toJobPropertiesParser.

// Tests_SRS_SERVICE_SDK_JAVA_JOB_PROPERTIES_34_002: [This method shall convert this into a JobPropertiesParser object and return it.]
@Test
public void toJobPropertiesParser() {
    // arrange
    JobProperties jobProperties = new JobProperties();
    jobProperties.setEndTimeUtc(new Date(System.currentTimeMillis()));
    jobProperties.setStartTimeUtc(new Date(System.currentTimeMillis()));
    jobProperties.setFailureReason("failureReason");
    jobProperties.setInputBlobContainerUri("inputContainerUri");
    jobProperties.setOutputBlobContainerUri("outputContainerUri");
    jobProperties.setProgress(0);
    jobProperties.setExcludeKeysInExport(false);
    jobProperties.setJobIdFinal("jobId");
    jobProperties.setStatus(JobProperties.JobStatus.COMPLETED);
    jobProperties.setType(JobProperties.JobType.IMPORT);
    // act
    JobPropertiesParser parser = toJobPropertiesParser(jobProperties);
    // assert
    assertEquals(parser.getInputBlobContainerUri(), jobProperties.getInputBlobContainerUri());
    assertEquals(parser.getOutputBlobContainerUri(), jobProperties.getOutputBlobContainerUri());
    assertEquals(parser.isExcludeKeysInExport(), jobProperties.getExcludeKeysInExport());
    assertEquals(parser.getType().toUpperCase(), jobProperties.getType().toString());
    assertEquals(parser.getStatus(), jobProperties.getStatus().toString());
    assertEquals(parser.getProgress(), jobProperties.getProgress());
    assertEquals(parser.getJobIdFinal(), jobProperties.getJobId());
    assertEquals(parser.getFailureReason(), jobProperties.getFailureReason());
    assertEquals(parser.getEndTimeUtc(), jobProperties.getEndTimeUtc());
    assertEquals(parser.getStartTimeUtc(), jobProperties.getStartTimeUtc());
}
Also used : JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) JobPropertiesParser(com.microsoft.azure.sdk.iot.deps.serializer.JobPropertiesParser) Date(java.util.Date) Test(org.junit.Test)

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