/// <inheritdoc/> public async Task DeactivateEndpointAsync(string endpointId, RegistryOperationContextModel context, CancellationToken ct) { if (string.IsNullOrEmpty(endpointId)) { throw new ArgumentException(nameof(endpointId)); } context = context.Validate(); // Get existing endpoint and compare to see if we need to patch. var twin = await _iothub.GetAsync(endpointId, null, ct); if (twin.Id != endpointId) { throw new ArgumentException("Id must be same as twin to deactivate", nameof(endpointId)); } // Convert to twin registration var registration = twin.ToEntityRegistration(true) as EndpointRegistration; if (registration == null) { throw new ResourceNotFoundException( $"{endpointId} is not an endpoint registration."); } if (registration.Activated ?? false) { await DeactivateAsync(registration, context, ct); } }
/// <inheritdoc/> public Task PurgeDisabledApplicationsAsync(TimeSpan notSeenFor, RegistryOperationContextModel context, CancellationToken ct) { context = context.Validate(); // TODO: Implement correctly return(Task.CompletedTask); }
/// <inheritdoc/> public async Task RejectApplicationAsync(string applicationId, bool force, RegistryOperationContextModel context, CancellationToken ct) { context = context.Validate(); var app = await UpdateApplicationStateAsync(applicationId, ApplicationState.Rejected, s => s == ApplicationState.New || force); }
/// <inheritdoc/> public async Task ActivateEndpointAsync(string endpointId, RegistryOperationContextModel context, CancellationToken ct) { if (string.IsNullOrEmpty(endpointId)) { throw new ArgumentException(nameof(endpointId)); } context = context.Validate(); // Get existing endpoint and compare to see if we need to patch. var twin = await _iothub.GetAsync(endpointId, null, ct); if (twin.Id != endpointId) { throw new ArgumentException("Id must be same as twin to activate", nameof(endpointId)); } // Convert to twin registration var registration = twin.ToEntityRegistration(true) as EndpointRegistration; if (registration == null) { throw new ResourceNotFoundException( $"{endpointId} is not an endpoint registration."); } if (registration.IsDisabled ?? false) { throw new ResourceInvalidStateException( $"{endpointId} is disabled - cannot activate"); } if (!(registration.Activated ?? false)) { // Activate if not yet activated try { if (string.IsNullOrEmpty(registration.SupervisorId)) { throw new ArgumentException( $"Twin {endpointId} not registered with a supervisor."); } await ActivateAsync(registration, context, ct); } catch (Exception ex) { // Try other supervisors as candidates if (!await ActivateAsync(registration, null, context, ct)) { throw ex; } } } }
/// <inheritdoc/> public async Task DisableApplicationAsync(string applicationId, RegistryOperationContextModel context, CancellationToken ct) { context = context.Validate(); var app = await _database.UpdateAsync(applicationId, (application, disabled) => { // Disable application if (!(disabled ?? false)) { application.NotSeenSince = DateTime.UtcNow; application.Updated = context; return(true, true); }
/// <inheritdoc/> public async Task ApproveApplicationAsync(string applicationId, bool force, RegistryOperationContextModel context, CancellationToken ct) { context = context.Validate(); var app = await _database.UpdateAsync(applicationId, (application, disabled) => { if (disabled ?? false) { throw new ResourceInvalidStateException("The application is disabled."); } if (application.State == ApplicationState.Approved) { return(null, null); }
/// <inheritdoc/> public async Task UnregisterApplicationAsync(string applicationId, RegistryOperationContextModel context, CancellationToken ct) { context = context.Validate(); if (string.IsNullOrEmpty(applicationId)) { throw new ArgumentNullException(nameof(applicationId), "The application id must be provided"); } while (true) { var document = await _applications.FindAsync <ApplicationDocument>(applicationId); if (document == null) { throw new ResourceNotFoundException( "A record with the specified application id does not exist."); } var application = document.Value.Clone(); try { // Try delete await _applications.DeleteAsync(document); // Success -- Notify others to clean up var app = document.Value.ToServiceModel(); // Try free record id await Try.Async(() => _index.FreeAsync(document.Value.ID)); break; } catch (ResourceOutOfDateException) { _logger.Verbose("Retry unregister application operation."); continue; } } }
/// <inheritdoc/> public async Task EnableApplicationAsync(string applicationId, RegistryOperationContextModel context, CancellationToken ct) { context = context.Validate(); var app = await UpdateEnabledDisabledAsync(applicationId, true); }