public static async Task DeleteDeviceInExternalSystemActivity([ActivityTrigger] DeviceDeleteOrchestrationInput input, TraceWriter log) { var externalDeviceRegistry = Utils.ResolveExternalDeviceRegistry(); var deviceId = input.Device["deviceId"].ToString(); try { await externalDeviceRegistry.DeleteExternalDevice(input.Device); log.Info($"Device '{deviceId}' from IoT Hub '{input.IoTHubName}' deleted"); Utils.TelemetryClient?.TrackEvent(Utils.Event_ExternalDeviceDeleted, new Dictionary <string, string>() { { "deviceId", deviceId }, { "iothubname", input.IoTHubName } }); } catch (Exception ex) { log.Error($"Failed to device {deviceId} from IoT Hub '{input.IoTHubName}': {ex.Message}"); Utils.TelemetryClient?.TrackEvent(Utils.Event_ExternalDeviceDeletionError, new Dictionary <string, string>() { { "deviceId", deviceId }, { "iothubname", input.IoTHubName }, { "errorMessage", ex.Message }, }); throw; } }
public static async Task <HttpResponseMessage> ExternalRegistrySynchronizer_EventGridListener( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req, [OrchestrationClient] DurableOrchestrationClient starter, TraceWriter log) { var eventGridResponse = await Utils.HandleEventGridRequest(req); if (eventGridResponse != null) { return(eventGridResponse); } var orchestrationInstances = new List <string>(); var payload = await req.Content.ReadAsStringAsync(); var events = JsonConvert.DeserializeObject <List <JObject> >(payload); if (events != null) { var externalDeviceRegistry = Utils.ResolveExternalDeviceRegistry(); foreach (var gridEvent in events) { var data = gridEvent["data"]; var deviceId = data["deviceId"].ToString(); var iotHubName = data["hubName"].ToString(); var operationType = data["opType"].ToString(); switch (operationType.ToLowerInvariant()) { case "devicecreated": { var twinProperties = new Dictionary <string, string>(); if (data["twin"]?["tags"] != null) { var deviceTwinTags = data["twin"]["tags"]; foreach (var requiredProperty in externalDeviceRegistry.GetRequiredTwinProperties()) { if (deviceTwinTags[requiredProperty] != null) { twinProperties.Add(requiredProperty, deviceTwinTags[requiredProperty].ToString()); } else { twinProperties.Clear(); break; } } } // create device if all properties are there if (twinProperties.Count > 0) { try { externalDeviceRegistry.ValidateTwinProperties(twinProperties); // Create the device if all properties are there await CreateExternalDeviceActivity(new CreateExternalDeviceInput() { DeviceId = deviceId, IotHubName = iotHubName, Properties = twinProperties }, log); } catch (Exception ex) { log.Warning($"Direct device creation for device {deviceId} in iothub {iotHubName} failed, starting an orchestration. {ex.ToString()}"); // failed to create the device immediately, starts an orchestration to retry more times var deviceForwarderRequest = new DeviceCreateOrchestrationInput(deviceId, iotHubName); var instanceId = await starter.StartNewAsync(nameof(ExternalRegistrySynchronizer_CreateDeviceOrchestration), deviceForwarderRequest); orchestrationInstances.Add(instanceId); } } else { // Otherwise start orchestration to wait until the device twin properties exist var deviceForwarderRequest = new DeviceCreateOrchestrationInput(deviceId, iotHubName); var instanceId = await starter.StartNewAsync(nameof(ExternalRegistrySynchronizer_CreateDeviceOrchestration), deviceForwarderRequest); orchestrationInstances.Add(instanceId); } break; } case "devicedeleted": { var deleteDeviceInput = new DeviceDeleteOrchestrationInput(data, iotHubName); try { await DeleteDeviceInExternalSystemActivity(deleteDeviceInput, log); } catch (Exception ex) { log.Warning($"Direct device delete for device {deviceId} in iothub {iotHubName} failed, starting an orchestration. {ex.ToString()}"); // failed to delete the device, starts an orchestration to retry more times var instanceId = await starter.StartNewAsync(nameof(ExternalRegistrySynchronizer_DeleteDeviceOrchestration), deleteDeviceInput); orchestrationInstances.Add(instanceId); } break; } } } } if (orchestrationInstances.Count == 1) { return(starter.CreateCheckStatusResponse(req, orchestrationInstances.First())); } var res = new HttpResponseMessage(System.Net.HttpStatusCode.OK); return(res); }