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();
}
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);
}
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());
}
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;
}
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());
}
Aggregations