/// <inheritdoc/> public async Task <ConfigurationModel> CreateOrUpdateConfigurationAsync( ConfigurationModel configuration, bool forceUpdate) { if (string.IsNullOrEmpty(configuration.Etag)) { // First try create configuration try { var result = await _registry.AddConfigurationAsync( configuration.ToConfiguration()); return(result.ToModel()); } catch (DeviceAlreadyExistsException) { // TODO // Expected for update } catch (Exception e) { _logger.Debug(e, "Create configuration failed in CreateOrUpdate"); // Try patch } } try { // Try update configuration var result = await _registry.UpdateConfigurationAsync( configuration.ToConfiguration()); return(result.ToModel()); } catch (Exception e) { _logger.Debug(e, "Update configuration failed in CreateOrUpdate"); throw e.Rethrow(); } }
/// <inheritdoc/> public async Task <ConfigurationModel> CreateOrUpdateConfigurationAsync( ConfigurationModel configuration, bool forceUpdate, CancellationToken ct) { try { if (string.IsNullOrEmpty(configuration.Etag)) { // First try create configuration try { var added = await _registry.AddConfigurationAsync( configuration.ToConfiguration(), ct); return(added.ToModel()); } catch (DeviceAlreadyExistsException) when(forceUpdate) { // // Technically update below should now work but for // some reason it does not. // Remove and re-add in case we are forcing updates. // await _registry.RemoveConfigurationAsync(configuration.Id, ct); var added = await _registry.AddConfigurationAsync( configuration.ToConfiguration(), ct); return(added.ToModel()); } } // Try update existing configuration var result = await _registry.UpdateConfigurationAsync( configuration.ToConfiguration(), forceUpdate, ct); return(result.ToModel()); } catch (Exception e) { _logger.Verbose(e, "Update configuration failed in CreateOrUpdate"); throw e.Rethrow(); } }