use of com.microsoft.azure.sdk.iot.service.RegistryManager in project azure-iot-sdk-java by Azure.
the class RegistryManagerTests method registryManagerTokenRenewalWithAzureSasCredential.
@Test
public void registryManagerTokenRenewalWithAzureSasCredential() throws Exception {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
RegistryManager registryManager = new RegistryManager(iotHubConnectionStringObj.getHostName(), azureSasCredential);
RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance(registryManager);
Device device1 = Device.createDevice(testInstance.deviceId + "-1", AuthenticationType.SAS);
Device device2 = Device.createDevice(testInstance.deviceId + "-2", AuthenticationType.SAS);
Device device3 = Device.createDevice(testInstance.deviceId + "-3", AuthenticationType.SAS);
azureSasCredential.update(serviceSasToken.toString());
// add first device just to make sure that the first credential update worked
testInstance.registryManager.addDevice(device1);
// deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
// access signature that is set here.
azureSasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
testInstance.registryManager.addDevice(device2);
fail("Expected adding a device to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
} catch (IotHubUnathorizedException e) {
log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
}
// Renew the expired shared access signature
serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
azureSasCredential.update(serviceSasToken.toString());
// adding the final device should succeed since the shared access signature has been renewed
testInstance.registryManager.addDevice(device3);
}
use of com.microsoft.azure.sdk.iot.service.RegistryManager in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method cloudToDeviceTelemetryWithTokenCredential.
@Test
public void cloudToDeviceTelemetryWithTokenCredential() throws Exception {
// only run tests for standard tier hubs
Assume.assumeFalse(isBasicTierHub);
// We remove and recreate the device for a clean start
RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
registryManager.addDevice(device);
Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
// Create service client
ServiceClient serviceClient = buildServiceClientWithTokenCredential(IotHubServiceClientProtocol.AMQPS);
serviceClient.open();
Message message = new Message("some message".getBytes(StandardCharsets.UTF_8));
serviceClient.send(device.getDeviceId(), message);
Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
serviceClient.close();
registryManager.removeDevice(device.getDeviceId());
// Assert
assertEquals(0, deviceGetBefore.getCloudToDeviceMessageCount());
assertEquals(1, deviceGetAfter.getCloudToDeviceMessageCount());
registryManager.close();
}
use of com.microsoft.azure.sdk.iot.service.RegistryManager in project azure-iot-sdk-java by Azure.
the class ConfigurationManangerSample method GetAllConfiguration.
private static void GetAllConfiguration() throws Exception {
RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
try {
List<Configuration> configList = registryManager.getConfigurations(20);
System.out.println(configList.size() + " Configurations found");
for (Configuration config : configList) {
System.out.println("Configuration Id: " + config.getId());
}
} catch (IotHubException | IOException iote) {
iote.printStackTrace();
}
}
use of com.microsoft.azure.sdk.iot.service.RegistryManager in project azure-iot-sdk-java by Azure.
the class ConfigurationManangerSample method GetConfiguration.
private static Configuration GetConfiguration() throws Exception {
RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
Configuration returnConfig = null;
try {
returnConfig = registryManager.getConfiguration(SampleUtils.configurationId);
printConfiguration(returnConfig);
} catch (IotHubException | IOException iote) {
iote.printStackTrace();
}
registryManager.close();
return returnConfig;
}
use of com.microsoft.azure.sdk.iot.service.RegistryManager 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();
}
Aggregations