示例#1
0
        public async Task <ActionConfirmation> Create(Service service)
        {
            var validatorConfirmation = _serviceValidator.Validate(service, ActionType.Create);

            if (!validatorConfirmation.WasSuccessful)
            {
                return(validatorConfirmation);
            }

            if (_consensusContext.IsLeader())
            {
                var consensusResult = await _logReplication.DistributeEntry(service, MethodType.Create);

                if (consensusResult.WasSuccessful)
                {
                    _serviceRepository.Create(service);
                    if (ShouldExecuteHealthChecking(service))
                    {
                        _serviceHealthWorker.RunHealthChecker(service);
                    }
                }
                else
                {
                    return(ActionConfirmation.CreateError($"Distribute item to other services fails: '{consensusResult.Message}'."));
                }
            }
            else
            {
                _serviceRepository.Create(service);
            }

            return(ActionConfirmation.CreateSuccessful());
        }
        public ActionConfirmation Validate(KvProperty kvProperty, ActionType actionType)
        {
            var errors = new List <ValidationError>();

            if (string.IsNullOrWhiteSpace(kvProperty.Key))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(kvProperty.Id),
                    Message   = "K/V property key wasn't specified"
                });
            }

            if (actionType == ActionType.Create && _kvPropertyRepository.Get(kvProperty.Key) != null)
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(kvProperty.Key),
                    Message   = $"K/V property with id '{kvProperty.Key}' already exists."
                });
            }

            if (errors.Any())
            {
                return(ActionConfirmation.CreateError("There was an validation errors.", errors.ToArray()));
            }

            return(ActionConfirmation.CreateSuccessful());
        }
示例#3
0
        public async Task <ActionConfirmation> Delete(string id)
        {
            var service = _serviceRepository.Get(id);

            if (_consensusContext.IsLeader())
            {
                var consensusResult = await _logReplication.DistributeEntry(service, MethodType.Delete);

                if (consensusResult.WasSuccessful)
                {
                    _serviceRepository.Remove(id);
                    _serviceHealthWorker.StopHealthChecker(id);
                }
                else
                {
                    return(ActionConfirmation.CreateError($"Distribute item to other services fails: '{consensusResult.Message}'."));
                }
            }
            else
            {
                _serviceRepository.Remove(id);
            }

            return(ActionConfirmation.CreateSuccessful());
        }
示例#4
0
        public async Task <ActionConfirmation> Update(KvProperty kvProperty)
        {
            var validatorConfirmation = _kvPropertyValidator.Validate(kvProperty, ActionType.Update);

            if (!validatorConfirmation.WasSuccessful)
            {
                return(validatorConfirmation);
            }

            if (_consensusContext.IsLeader())
            {
                var consensusResult = await _logReplication.DistributeEntry(kvProperty, MethodType.Update);

                if (consensusResult.WasSuccessful)
                {
                    _kvPropertyRepository.Update(kvProperty);
                }
                else
                {
                    return(ActionConfirmation.CreateError($"Distribute item to other services fails: '{consensusResult.Message}'."));
                }
            }
            else
            {
                _kvPropertyRepository.Update(kvProperty);
            }

            return(ActionConfirmation.CreateSuccessful());
        }
示例#5
0
        public ActionConfirmation Validate(Service service, ActionType actionType)
        {
            var errors = new List <ValidationError>();

            var idPattern = new Regex("^[a-zA-Z0-9-]*$");

            if (!idPattern.IsMatch(service.Id))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Id),
                    Message   = $"Service id contains unacceptable characters (only alphanumeric letters and dash is acceptable)."
                });
            }

            if (actionType == ActionType.Create && _serviceRepository.Get(service.Id) != null)
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Id),
                    Message   = $"Service with id '{service.Id}' already exists."
                });
            }

            if (string.IsNullOrWhiteSpace(service.Id))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Id),
                    Message   = "Service id wasn't specified"
                });
            }

            if (string.IsNullOrWhiteSpace(service.ServiceType))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.ServiceType),
                    Message   = "Service type wasn't specified"
                });
            }

            if (string.IsNullOrWhiteSpace(service.Address))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Address),
                    Message   = "Service address wasn't specified"
                });
            }

            if (errors.Any())
            {
                return(ActionConfirmation.CreateError("There was an validation errors.", errors.ToArray()));
            }

            return(ActionConfirmation.CreateSuccessful());
        }
        public async Task <ActionConfirmation <T> > SendRequest <T>(HttpMethod httpMethod, string path, string id, string query, T objectData) where T : class
        {
            var neutrinoAddress = GetNeutrinoLeaderAddress();
            var url             = neutrinoAddress.AppendPathSegment(path);

            if (!string.IsNullOrWhiteSpace(id))
            {
                url = url.AppendPathSegment(id);
            }

            if (!string.IsNullOrWhiteSpace(query))
            {
                if (query.StartsWith("?"))
                {
                    url.Query = query;
                }
                else
                {
                    url = url.AppendPathSegment(query);
                }
            }

            var responseMessage = await SendRequest(() => CreateHttpRequestMessage(httpMethod, url, objectData));

            if (responseMessage.IsSuccessStatusCode)
            {
                var objectStringFromResponse = await responseMessage.Content.ReadAsStringAsync();

                var objectFromResponse = default(T);

                if (!string.IsNullOrWhiteSpace(objectStringFromResponse))
                {
                    objectFromResponse = JsonConvert.DeserializeObject <T>(objectStringFromResponse);
                }

                return(ActionConfirmation <T> .CreateSuccessful(objectFromResponse));
            }

            var errorFromResponse = await responseMessage.Content.ReadAsStringAsync();

            var errorMessage = $"Request finished with status code: '{responseMessage.StatusCode}. Message from server: {errorFromResponse}.";

            return(ActionConfirmation.CreateError <T>(errorMessage));
        }
        public async Task <ActionConfirmation> SendRequest(HttpMethod httpMethod, string path, string id = null)
        {
            var neutrinoAddress = GetNeutrinoLeaderAddress();
            var url             = neutrinoAddress.AppendPathSegment(path);

            if (!string.IsNullOrWhiteSpace(id))
            {
                url = url.AppendPathSegment(id);
            }

            var responseMessage = await SendRequest(() => CreateHttpRequestMessage(httpMethod, url, null));

            if (responseMessage.IsSuccessStatusCode)
            {
                return(ActionConfirmation.CreateSuccessful());
            }

            var errorFromResponse = await responseMessage.Content.ReadAsStringAsync();

            var errorMessage = $"Request finished with status code: '{responseMessage.StatusCode}. Message from server: {errorFromResponse}.";

            return(ActionConfirmation.CreateError(errorMessage));
        }