I'm trying to change the "status" (running or stopped) and the "restartPolicy" of an IoT Edge module programmatically in order to stop a module without having to recreate the whole deployment for the Device.
I've seen that the Edge Agent's Twin has the modules' deployment information in his desiredProperties and I've tried to patch that by using the following code (which uses the Microsoft.Azure.Devices NuGet packages)
public async Task ShutdownModule(string deviceId, string moduleId)
{
var twinEdgeAgent = await _registryManager.GetTwinAsync(deviceId, "$edgeAgent");
var patchJson = $"{{\"properties\":{{\"desired\":{{\"modules\":{{\"{moduleId}\":{{\"status\": \"stopped\", \"restartPolicy\": \"never\"}}}}}}}}}}";
await _registryManager.UpdateTwinAsync(deviceId, "$edgeAgent", patchJson, twinEdgeAgent.ETag);
}
Unfortunately this doesn't work and I'm getting an UnauthorizedException
with the message "ErrorCode:SystemModuleModifyUnauthorizedAccess;Unauthorized to modify reserved module.". It looks like that I can't change the desired properties of the Edge Agent module.
Is there a way to change this property without having to recreate the whole deployment JSON, or at least is there a way to get this deployment JSON so that I can modify the properties I need to change?