azureazure-iot-hubazure-rest-apiazure-iot-dps

Is there any REST API to programmatically link IoT hub when creating Azure DPS?


Looking for a REST Api that Creates DPS as well as link to an existing IoT hub. I see that we can do via Azure CLI but looking for REST Api calls as my web app(Azure App service) needs to first create Iot hub and then use this as a link while creating DPS. Currently using Create DPS like this:

   var mydps = new {
                location = "East US", 
                type = "Microsoft.Devices/ProvisioningServices",
            };

   var content = new StringContent(JsonConvert.SerializeObject(mydps), Encoding.UTF8, "application/json");
   var requestUri = string.Format(webOptions.CreateDpsUri, someSubscriptionID, someRsourceGroupgName, somedpsName );  
   var result = await httpClient.PutAsync(requestUri, content);

Is see this open issue Support for linking an IoT Hub to an existing DPS in that it says about "Currently you can only link a hub during DPS resource creation (or updating your DPS resource creation code)." But I couldn't see this options of setting the Iot hub during the above DPS Create REST API call.

Is that sending some param link connectionString attribute would do the work or something else as i dont see any documentation related to linking Iot hub in DPS Creation using a REST call?

If REST api is not yet supported what are my options to link Iot hub in DPS. I see that the other options to link are ARM template and Azure CLI. We can use ARM template but that is just for one time deployment, not sure if i can leverage that from Web app. Same is with Azure CLI, how i can use it from web app?


Solution

  • When creating the Azure IoT Hub DPS resource, you can pass the list of IoT Hubs to link to as part of the properties.

    Example:

    {
      "location": "East US", 
      "type": "Microsoft.Devices/ProvisioningServices",
      "properties": {
         "iotHubs": [
            {
              "applyAllocationPolicy": true,
              "allocationWeight": "1",
              "connectionString": "HostName=iothub-001.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=xxxxxxxx",
              "location": "East US"
            },
            {
              "applyAllocationPolicy": true,
              "allocationWeight": "1",
              "connectionString": "HostName=iothub-002.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=xxxxxxxx",
              "location": "East US"
            }
          ]
      }
    }
    

    More on this in the documentation of the Azure IoT Hub DPS resource.