/// <inheritdoc/>
 public Task UpdateEndpointAsync(string endpointId,
                                 EndpointRegistrationUpdateModel request, CancellationToken ct)
 {
     return(Task.FromException <EndpointInfoModel>(new ResourceNotFoundException()));
 }
Ejemplo n.º 2
0
 /// <inheritdoc/>
 public Task UpdateEndpointAsync(string endpointId,
                                 EndpointRegistrationUpdateModel request, CancellationToken ct)
 {
     return(_client.UpdateEndpointAsync(endpointId,
                                        Map <EndpointRegistrationUpdateApiModel>(request), ct));
 }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task UpdateEndpointAsync(string endpointId,
                                              EndpointRegistrationUpdateModel request, CancellationToken ct)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrEmpty(endpointId))
            {
                throw new ArgumentException(nameof(endpointId));
            }
            var context = request.Context.Validate();

            while (true)
            {
                try {
                    // 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 patch",
                                                    nameof(endpointId));
                    }

                    // Convert to twin registration
                    var registration = twin.ToRegistration(true) as EndpointRegistration;
                    if (registration == null)
                    {
                        throw new ResourceNotFoundException(
                                  $"{endpointId} is not a endpoint registration.");
                    }

                    if (registration.IsDisabled ?? false)
                    {
                        throw new ResourceInvalidStateException(
                                  $"{endpointId} is disabled - cannot update.");
                    }

                    // Update registration from update request
                    var patched = registration.ToServiceModel();

                    var duplicate = false;
                    if (request.User != null)
                    {
                        patched.Registration.Endpoint.User = new CredentialModel();

                        if (request.User.Type != null)
                        {
                            // Change token type?  Always duplicate since id changes.
                            duplicate = request.User.Type !=
                                        patched.Registration.Endpoint.User.Type;

                            patched.Registration.Endpoint.User.Type =
                                (CredentialType)request.User.Type;
                        }
                        if ((patched.Registration.Endpoint.User.Type
                             ?? CredentialType.None) != CredentialType.None)
                        {
                            patched.Registration.Endpoint.User.Value =
                                request.User.Value;
                        }
                        else
                        {
                            patched.Registration.Endpoint.User.Value = null;
                        }
                    }

                    var existing = duplicate ? null : registration;
                    var update   = patched.ToEndpointRegistration(registration.IsDisabled);

                    // Patch
                    await _iothub.PatchAsync(existing.Patch(update), false, ct);

                    await _broker.NotifyAllAsync(
                        l => l.OnEndpointUpdatedAsync(context, patched));

                    return;
                }
                catch (ResourceOutOfDateException ex) {
                    _logger.Debug(ex, "Retrying updating endpoint...");
                    continue;
                }
            }
        }
 /// <summary>
 /// Create from service model
 /// </summary>
 /// <param name="model"></param>
 public EndpointRegistrationUpdateApiModel(EndpointRegistrationUpdateModel model)
 {
     User = model.User == null ?
            null : new CredentialApiModel(model.User);
 }