Ejemplo n.º 1
0
        public async Task <IActionResult> AddOrUpdateOperationSettings(OperationChaosSetting operationChaosSetting)
        {
            var chaosSettings = await _httpClient.GetGeneralChaosSettings();

            if (chaosSettings.OperationChaosSettings == null)
            {
                chaosSettings.OperationChaosSettings = new List <OperationChaosSetting>();
            }
            var index = chaosSettings.OperationChaosSettings.FindIndex(x => x.Id == operationChaosSetting.Id);

            if (index >= 0)
            {
                chaosSettings.OperationChaosSettings[index] = operationChaosSetting;
            }
            else
            {
                if (chaosSettings.OperationChaosSettings.Any(x => x.OperationKey == operationChaosSetting.OperationKey))
                {
                    PopulateLists();
                    ModelState.AddModelError("OperationKey", "There is already a setting using that operation key");
                    return(View("OperationSettings", operationChaosSetting));
                }

                operationChaosSetting.Id = Guid.NewGuid();
                chaosSettings.OperationChaosSettings.Add(operationChaosSetting);
            }

            await _httpClient.UpdateGeneralChaosSettings(chaosSettings);

            return(RedirectToAction("Index"));
        }
        private static Task <HttpResponseMessage> GetHttpResponseMessage(Context context, CancellationToken token)
        {
            OperationChaosSetting chaosSettings = context.GetOperationChaosSettings();

            if (chaosSettings == null)
            {
                return(NoHttpResponse);
            }

            int statusCode = chaosSettings.StatusCode;

            if (statusCode < 200)
            {
                return(NoHttpResponse);
            }

            try
            {
                return(Task.FromResult(new HttpResponseMessage((HttpStatusCode)statusCode)));
            }
            catch
            {
                return(NoHttpResponse);
            }
        }
        private static Task <Double> GetInjectionRate(Context context, CancellationToken token)
        {
            OperationChaosSetting chaosSettings = context.GetOperationChaosSettings();

            if (chaosSettings == null)
            {
                return(NoInjectionRate);
            }

            return(Task.FromResult(chaosSettings.InjectionRate));
        }
        private static Task <bool> GetEnabled(Context context, CancellationToken token)
        {
            OperationChaosSetting chaosSettings = context.GetOperationChaosSettings();

            if (chaosSettings == null)
            {
                return(NotEnabled);
            }

            return(Task.FromResult(chaosSettings.Enabled));
        }
        private static Task <TimeSpan> GetLatency(Context context, CancellationToken token)
        {
            OperationChaosSetting chaosSettings = context.GetOperationChaosSettings();

            if (chaosSettings == null)
            {
                return(NoLatency);
            }

            int milliseconds = chaosSettings.LatencyMs;

            if (milliseconds <= 0)
            {
                return(NoLatency);
            }

            return(Task.FromResult(TimeSpan.FromMilliseconds(milliseconds)));
        }
        private static Task <Exception> GetException(Context context, CancellationToken token)
        {
            OperationChaosSetting chaosSettings = context.GetOperationChaosSettings();

            if (chaosSettings == null)
            {
                return(NoExceptionResult);
            }

            string exceptionName = chaosSettings.Exception;

            if (String.IsNullOrWhiteSpace(exceptionName))
            {
                return(NoExceptionResult);
            }

            try
            {
                Type exceptionType = Type.GetType(exceptionName);
                if (exceptionType == null)
                {
                    return(NoExceptionResult);
                }

                if (!typeof(Exception).IsAssignableFrom(exceptionType))
                {
                    return(NoExceptionResult);
                }

                var instance = Activator.CreateInstance(exceptionType);
                return(Task.FromResult(instance as Exception));
            }
            catch
            {
                return(NoExceptionResult);
            }
        }