Search in sources :

Example 31 with RegistryManager

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);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 32 with RegistryManager

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();
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) Test(org.junit.Test)

Example 33 with RegistryManager

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();
    }
}
Also used : Configuration(com.microsoft.azure.sdk.iot.service.Configuration) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 34 with RegistryManager

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;
}
Also used : Configuration(com.microsoft.azure.sdk.iot.service.Configuration) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 35 with RegistryManager

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

RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)39 IOException (java.io.IOException)20 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)19 Device (com.microsoft.azure.sdk.iot.service.Device)15 Test (org.junit.Test)14 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)7 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)6 AzureSasCredential (com.azure.core.credential.AzureSasCredential)5 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)5 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)5 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)5 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)5 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)5 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)5 RegistryStatistics (com.microsoft.azure.sdk.iot.service.RegistryStatistics)4 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)3 Configuration (com.microsoft.azure.sdk.iot.service.Configuration)3 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)3 Message (com.microsoft.azure.sdk.iot.service.Message)3 Module (com.microsoft.azure.sdk.iot.service.Module)3