Exemplo n.º 1
0
        public virtual ITroubleshooterResult Troubleshoot(
            TroubleshooterContext context)
        {
            if (context == null)
            {
                return(TroubleshooterResult.FailResult("The context object passed to the troubleshooter is null."));
            }

            ItemModel configuration = context.Configuration as ItemModel;

            if (configuration == null)
            {
                return(TroubleshooterResult.FailResult("The configuration object on the troubleshooter context is null or could not be converted into an item model."));
            }

            IConverter <ItemModel, PipelineStep> converter = configuration.GetConverter <PipelineStep>(context.ItemModelRepository);

            if (converter == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("No pipeline step converter was resolved. Item id: {0}", configuration.GetItemId().ToString())));
            }

            ConvertResult <PipelineStep> convertResult = converter.Convert(configuration);

            if (!convertResult.WasConverted)
            {
                return(TroubleshooterResult.FailResult("The configuration object on the troubleshooter context could not be converted into a pipeline step."));
            }

            return(this.Troubleshoot(convertResult.ConvertedValue, context));
        }
        //private readonly IFormDataProvider _formDataProvider;

        //public FormsEndpointTroubleshooter(IFormDataProvider formDataProvider)
        //{
        //    this._formDataProvider = formDataProvider;
        //}

        protected override ITroubleshooterResult Troubleshoot(Endpoint endpoint, TroubleshooterContext context)
        {
            FormsSettings plugin = endpoint.GetPlugin <FormsSettings>();

            if (plugin == null)
            {
                return((ITroubleshooterResult)TroubleshooterResult.FailResult(string.Format("Endpoint plugin is missing. Plugin type: {0}", (object)"FormsSettings"), (Exception)null));
            }
            var formDataProvider = (IFormDataProvider)Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService(typeof(IFormDataProvider));

            if (formDataProvider == null)
            {
                return((ITroubleshooterResult)TroubleshooterResult.FailResult(string.Format("Forms Data Provider is missing. Plugin type: {0}", (object)"FormsSettings"), (Exception)null));
            }

            try
            {
                var results = formDataProvider.GetEntries(Guid.Empty, DateTime.MinValue, DateTime.MinValue);
                if (results.Count > 0)
                {
                    throw new Exception("Unexpected results.");
                }
            }
            catch (Exception ex)
            {
                this.Logger.Error(ex.StackTrace);
                return((ITroubleshooterResult)TroubleshooterResult.FailResult(string.Format("{0} {1} {2}", (object)"Exception was thrown.", (object)"Read more in log file.", (object)ex.Message), ex));
            }
            finally
            {
                //dbConnection?.Dispose();
            }
            return((ITroubleshooterResult)TroubleshooterResult.SuccessResult("Database connection was successfully established."));
        }
        protected override ITroubleshooterResult Troubleshoot(Endpoint endpoint, TroubleshooterContext context)
        {
            var settings = endpoint.GetPlugin <GitHubSettings>();

            if (settings == null)
            {
                return(TroubleshooterResult.FailResult("The endpoint is missing the plugin GitHubSettings."));
            }

            try
            {
                var client  = new GitHubClient(new ProductHeaderValue(GitHubEndpointConverter.AppName));
                var apiInfo = client.GetLastApiInfo();

                if (apiInfo == null)
                {
                    return(TroubleshooterResult.SuccessResult(Task.Run(() => GetRateInfo(client)).Result));
                }
                else
                {
                    return(TroubleshooterResult.SuccessResult(GetLastRateInfo(apiInfo.RateLimit)));
                }
            }
            catch (Exception ex) {
                return(TroubleshooterResult.FailResult("API Error", ex));
            }
        }
Exemplo n.º 4
0
        protected override ITroubleshooterResult Troubleshoot(PipelineStep pipelineStep, TroubleshooterContext context)
        {
            EndpointSettings endpointSettings = pipelineStep.GetEndpointSettings();

            if (endpointSettings == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("Pipeline step is missing a plugin. \n Plugin: {0}", typeof(EndpointSettings).FullName)));
            }

            Endpoint endpointFrom = endpointSettings.EndpointFrom;

            if (endpointFrom == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("Pipeline step is missing a value. \n Property: {0}", Templates.BaseEndpointPipelineStep.FieldNames.EndpointFrom)));
            }

            return(Task.Run(async() => await this.TroubleshootReadData(endpointFrom, pipelineStep, context)).Result);
        }
        protected override ITroubleshooterResult Troubleshoot(Endpoint endpoint, TroubleshooterContext context)
        {
            var settings = endpoint.GetPlugin <TextFileSettings>();

            if (settings == null)
            {
                return(TroubleshooterResult.FailResult("The endpoint is missing the plugin TextFileSettings."));
            }
            if (string.IsNullOrWhiteSpace(settings.Path))
            {
                return(TroubleshooterResult.FailResult("The endpoint does not have a file path specified on it."));
            }
            if (!File.Exists(settings.Path))
            {
                return(TroubleshooterResult.FailResult(
                           "The path specified on the endpoint either " +
                           "points to a file that does not exist " +
                           "or the process does not have permission to read the file."));
            }
            return(TroubleshooterResult.SuccessResult("The specified file exists and can be accessed."));
        }
Exemplo n.º 6
0
        protected async Task <ITroubleshooterResult> TroubleshootReadData(Endpoint endpoint, PipelineStep pipelineStep, TroubleshooterContext context)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }

            if (pipelineStep == null)
            {
                throw new ArgumentNullException(nameof(pipelineStep));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var repositorySettings = Context.GetPlugin <RepositorySettings>();

            if (repositorySettings == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("Context is missing a plugin. \n Plugin: {0}", typeof(RepositorySettings).FullName)));
            }

            if (repositorySettings.Client == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("Plugin is missing a client. Check initialize pipeline. \n Plugin: {0}; Property: {1}", typeof(RepositorySettings).FullName, "Client")));
            }

            var applicationEndpointSettings = endpoint.GetApplicationEndpointSettings();
            var applicationSettings         = (ApplicationSettings)applicationEndpointSettings?.Application?.RefreshPlugin.Invoke();

            if (applicationSettings == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("Endpoint is missing a value. \n Endpoint: {0} \n Property: {1}", endpoint.Name, Templates.RestfulEndpoint.FieldNames.Application)));
            }

            if (string.IsNullOrWhiteSpace(applicationSettings.BaseUrl))
            {
                return(TroubleshooterResult.FailResult(string.Format("Application is missing a value. \n Property: {0})", Templates.Application.FieldNames.BaseUrl)));
            }

            var resourceSettings = pipelineStep.GetResourceSettings();

            if (resourceSettings == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("Pipeline step is missing a value. \n Property: {0}", Templates.BaseResourceEndpointPipelineStep.FieldNames.Resource)));
            }

            if (string.IsNullOrWhiteSpace(resourceSettings.Url))
            {
                return(TroubleshooterResult.FailResult(string.Format("Resource is missing a value. \n Property: {0}", Templates.Resource.FieldNames.Url)));
            }

            if (string.IsNullOrWhiteSpace(resourceSettings.Method))
            {
                return(TroubleshooterResult.FailResult(string.Format("Resource is missing a value. \n Property: {0}", Templates.Resource.FieldNames.Method)));
            }

            var readDataSettings = pipelineStep.GetReadResourceDataSettings();

            if (readDataSettings == null || string.IsNullOrWhiteSpace(readDataSettings.PathExpression))
            {
                return(TroubleshooterResult.FailResult(string.Format("Pipeline step is missing a value. \n Property: {0}", Templates.ReadResourceDataPipelineStep.FieldNames.PathExpression)));
            }

            var response = await repositorySettings.Client.SendAsync(applicationSettings, resourceSettings);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(TroubleshooterResult.FailResult(string.Format("Status Code: {0} \n Reason: {1}", (int)response.StatusCode, response.ReasonPhrase)));
            }

            var content = await response.Content.ReadAsStringAsync();

            var jObject = JsonConvert.DeserializeObject <JObject>(content);

            if (jObject == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("No JSON data returned. \n Content: {0}", content)));
            }

            var jArray = (JArray)jObject.SelectToken(readDataSettings.PathExpression, false);

            if (jArray == null)
            {
                return(TroubleshooterResult.FailResult(string.Format("No data returned from path expression. \n Content: {0}", content)));
            }


            return(TroubleshooterResult.SuccessResult(string.Format("Connection was successfully established. \n {0} row(s) were returned.", jArray.Count)));
        }
        /// <summary>
        /// Check the connection, topic and subscriber. Create the topic / subscriber if needed (not exist yet).
        /// Subscriber name pattern: SC9_MC (market code).
        /// Note: we're checking the filter as well
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        protected override ITroubleshooterResult Troubleshoot(Endpoint endpoint, TroubleshooterContext context)
        {
            // data validation
            if (endpoint == null)
            {
                return(TroubleshooterResult.FailResult("The endpoint is null."));
            }

            var settings = endpoint.GetPlugin <ServiceBusSettings>();

            if (settings == null)
            {
                return(TroubleshooterResult.FailResult("The endpoint is missing the ServiceBusSettings plugin."));
            }

            var result = new TroubleshooterResult();

            bool wasWrong = false;

            if (string.IsNullOrWhiteSpace(settings.ConnectionString))
            {
                wasWrong = true;
                result.Data.Add("ConnectionString", "There is no Connection String in the endpoint configuration.");
            }
            if (string.IsNullOrWhiteSpace(settings.Topic))
            {
                wasWrong = true;
                result.Data.Add("Topic", "There is no Topic in the endpoint configuration.");
            }
            if (string.IsNullOrWhiteSpace(settings.Market))
            {
                wasWrong = true;
                result.Data.Add("Market", "There is no Market in the endpoint configuration.");
            }
            if (wasWrong)
            {
                result.Success = false;
                result.Message = "Wrong configuration.";
                return(result);
            }

            // filter preparation:
            try
            {
                var serviceBusService = new ServiceBusService();
                var subscriptionName  = serviceBusService.GetSubscriptionName(settings.Market, settings.Sender);
                var filter            = serviceBusService.GetFilter(settings.Market, settings.Sender);
                var ruleName          = serviceBusService.GetRuleName();

                // test the connection to selected bus
                var serviceBus = new ServiceBusProvider(settings.ConnectionString);

                // check the topic existence
                if (!serviceBus.TopicExist(settings.Topic))
                {
                    // try to create one
                    try
                    {
                        serviceBus.CreateTopic(settings.Topic);
                        result.Data.Add("Topic", $"The '{settings.Topic}' topic has been created.");
                    }
                    catch (Exception exception)
                    {
                        return(TroubleshooterResult.FailResult($"Unable to create the '{settings.Topic}' topic.",
                                                               exception));
                    }
                }
                else
                {
                    result.Data.Add("Topic", $"The '{settings.Topic}' topic already exist.");
                }

                // check the subscription existence
                if (!serviceBus.SubscribtionExist(settings.Topic, subscriptionName))
                {
                    // try to create one
                    try
                    {
                        serviceBus.CreateSubscribtion(settings.Topic, subscriptionName, ruleName, filter);
                        result.Data.Add("Subscription", $"The '{subscriptionName}' subscription has been created.");
                    }
                    catch (Exception exception)
                    {
                        return(TroubleshooterResult.FailResult(
                                   $"Unable to create the '{subscriptionName}' subscription.", exception));
                    }
                }
                else
                {
                    result.Data.Add("Subscription", $"The '{subscriptionName}' subscription already exist.");
                }

                // check the filter
                try
                {
                    serviceBus.CheckFilter(settings.Topic, subscriptionName, ruleName, filter);
                    result.Data.Add("Filter", "The filter has been checked successfully.");
                }
                catch (Exception exception)
                {
                    return(TroubleshooterResult.FailResult($"Unable to check filter for '{subscriptionName}' subscription: {exception.Message}", exception));
                }
            }
            catch (Exception e)
            {
                return(TroubleshooterResult.FailResult($"Something is wrong with ServiceBus: {e.Message}", e));
            }

            result.Success = true;
            result.Message = "The Service Bus endpoint is OK.";
            return(result);
        }