Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Integrations config and hooks refactoring #4
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Sep 1, 2016
1 parent c1fc188 commit e65b3dc
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 415 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private IEnumerable<IWatcherHookSpawnConfiguration> ResolveGlobalWatcherHooks(st

foreach (var hookConfig in hooksConfigurations)
{
SetWatcherHookConfiguration(wardenName, hookConfig, integrations, hookType: "globalWatcherHooks");
SetWatcherHookConfiguration(wardenName, hookConfig, integrations, type: "globalWatcherHooks");

yield return hookConfig;
}
Expand All @@ -128,7 +128,7 @@ private IEnumerable<IWatcherHookSpawnConfiguration> ResolveAggregatedWatcherHook

foreach (var hookConfig in hooksConfigurations)
{
SetWatcherHookConfiguration(wardenName, hookConfig, integrations, hookType: "aggregatedWatcherHooks");
SetWatcherHookConfiguration(wardenName, hookConfig, integrations, type: "aggregatedWatcherHooks");

yield return hookConfig;
}
Expand All @@ -138,7 +138,7 @@ private void SetWatcherHookConfiguration(string wardenName,
IWatcherHookSpawnConfiguration hookConfig,
IEnumerable<ISpawnIntegration> integrations,
string watcher = "",
string hookType = "")
string type = "")
{
var integrationName = hookConfig.Use.ToLowerInvariant();
var integration = integrations.FirstOrDefault(x =>
Expand All @@ -149,12 +149,12 @@ private void SetWatcherHookConfiguration(string wardenName,
var integrationNamespace = integration.GetType().Namespace;
var watcherHooksConfigurationName = integration.GetType().Name + "HooksConfiguration";
var watcherHooksConfigurationType =
Type.GetType($"{@integrationNamespace}.{watcherHooksConfigurationName},{integrationNamespace}");
Type.GetType($"{integrationNamespace}.{watcherHooksConfigurationName},{integrationNamespace}");
var cfg = JsonConvert.SerializeObject(hookConfig.Configuration);
var hookName = hookConfig.Type.ToString();
hookConfig.Configuration = JsonConvert.DeserializeObject(cfg, watcherHooksConfigurationType);
_credentialsConfigurator.SetConfiguration(wardenName, hookConfig.Configuration,
integration: integrationName, watcher: watcher, hook: hookName);
integration: integrationName, watcher: watcher, hook: hookName, type: type);
}

private IEnumerable<ISpawnIntegration> ResolveIntegrations(string wardenName, dynamic integrations)
Expand Down Expand Up @@ -210,7 +210,7 @@ private IEnumerable<IWardenHookSpawnConfiguration> ResolveHooks(string wardenNam
var hookName = hookConfig.Type.ToString();
hookConfig.Configuration = JsonConvert.DeserializeObject(cfg, wardenHooksConfigurationType);
_credentialsConfigurator.SetConfiguration(wardenName, hookConfig.Configuration,
integration: integrationName, hook: hookName);
integration: integrationName, hook: hookName, type: "wardenHooks");

yield return hookConfig;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Threading.Tasks;

namespace Warden.Spawn.Integrations.Console
{
public class ConsoleService : IConsoleService
{
private readonly ConsoleSpawnIntegrationConfiguration _integrationConfiguration;

public ConsoleService(ConsoleSpawnIntegrationConfiguration integrationConfiguration)
{
_integrationConfiguration = integrationConfiguration;
}

public async Task PrintAsync(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;

await Task.Factory.StartNew(() => System.Console.WriteLine(text));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ namespace Warden.Spawn.Integrations.Console
{
public class ConsoleSpawnIntegration : ISpawnIntegration
{
private IConsoleService _service;
private readonly ConsoleSpawnIntegrationConfiguration _configuration;
public string Name => "Console";
public IIntegration Integration { get; set; }

public IWatcherHooksResolver WatcherHooksResolver
=> new ConsoleSpawnIntegrationWatcherHooksResolver(_configuration);
=> new ConsoleSpawnIntegrationWatcherHooksResolver(Service);

public IWardenHooksResolver WardenHooksResolver
=> new ConsoleSpawnIntegrationWardenHooksResolver(_configuration);
=> new ConsoleSpawnIntegrationWardenHooksResolver(Service);

public IAggregatedWatcherHooksResolver AggregatedWatcherHooksResolver
=> new ConsoleSpawnIntegrationAggregatedWatcherHooksResolver(_configuration);
=> new ConsoleSpawnIntegrationAggregatedWatcherHooksResolver(Service);

public ISpawnIntegrationConfiguration Configuration => _configuration;

public ConsoleSpawnIntegration(ConsoleSpawnIntegrationConfiguration configuration)
{
_configuration = configuration;
}

private IConsoleService Service
=> _service ?? (_service = new ConsoleService(_configuration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,93 +9,53 @@ namespace Warden.Spawn.Integrations.Console
{
public class ConsoleSpawnIntegrationAggregatedWatcherHooksResolver : IAggregatedWatcherHooksResolver
{
private readonly ConsoleSpawnIntegrationConfiguration _integrationConfiguration;
private readonly IConsoleService _service;

public ConsoleSpawnIntegrationAggregatedWatcherHooksResolver(ConsoleSpawnIntegrationConfiguration integrationConfiguration)
public ConsoleSpawnIntegrationAggregatedWatcherHooksResolver(IConsoleService service)
{
_integrationConfiguration = integrationConfiguration;
_service = service;
}

public Expression<Action<IEnumerable<IWatcherCheck>>> OnStart()
{
throw new NotImplementedException();
}
public Expression<Action<IEnumerable<IWatcherCheck>>> OnStart(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<IWatcherCheck>, Task>> OnStartAsync()
{
throw new NotImplementedException();
}
public Expression<Func<IEnumerable<IWatcherCheck>, Task>> OnStartAsync(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnSuccess()
{
throw new NotImplementedException();
}
public Expression<Action<IEnumerable<IWardenCheckResult>>> OnSuccess(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnSuccessAsync()
{
throw new NotImplementedException();
}
public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnSuccessAsync(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFirstSuccess()
{
throw new NotImplementedException();
}
public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFirstSuccess(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFirstSuccessAsync()
{
throw new NotImplementedException();
}
public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFirstSuccessAsync(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFailure()
{
throw new NotImplementedException();
}
public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFailure(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFailureAsync()
{
throw new NotImplementedException();
}
public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFailureAsync(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnCompleted(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;

return x => System.Console.WriteLine(text);
}
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnCompletedAsync(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;

return x => Task.Factory.StartNew(() => System.Console.WriteLine(text));
}
=> x => _service.PrintAsync(configuration);

public Expression<Action<IEnumerable<Exception>>> OnError()
{
throw new NotImplementedException();
}
public Expression<Action<IEnumerable<Exception>>> OnError(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<Exception>, Task>> OnErrorAsync()
{
throw new NotImplementedException();
}
public Expression<Func<IEnumerable<Exception>, Task>> OnErrorAsync(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Action<IEnumerable<Exception>>> OnFirstError()
{
throw new NotImplementedException();
}
public Expression<Action<IEnumerable<Exception>>> OnFirstError(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IEnumerable<Exception>, Task>> OnFirstErrorAsync()
{
throw new NotImplementedException();
}
public Expression<Func<IEnumerable<Exception>, Task>> OnFirstErrorAsync(object configuration)
=> x => _service.PrintAsync(configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,47 @@ namespace Warden.Spawn.Integrations.Console
{
public class ConsoleSpawnIntegrationWardenHooksResolver : IWardenHooksResolver
{
private readonly ConsoleSpawnIntegrationConfiguration _integrationConfiguration;
private readonly IConsoleService _service;

public ConsoleSpawnIntegrationWardenHooksResolver(ConsoleSpawnIntegrationConfiguration integrationConfiguration)
public ConsoleSpawnIntegrationWardenHooksResolver(IConsoleService service)
{
_integrationConfiguration = integrationConfiguration;
_service = service;
}

public Expression<Action<IWardenIteration>> OnIterationCompleted(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();
public Expression<Action<Exception>> OnError(object configuration)
=> x => _service.PrintAsync(configuration);

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;
public Expression<Func<Exception, Task>> OnErrorAsync(object configuration)
=> x => _service.PrintAsync(configuration);

return x => System.Console.WriteLine(text);
}
public Expression<Action<IWardenIteration>> OnIterationCompleted(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Func<IWardenIteration, Task>> OnIterationCompletedAsync(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();
=> x => _service.PrintAsync(configuration);

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;
public Expression<Action<long>> OnIterationStart(object configuration)
=> x => _service.PrintAsync(configuration);

return x => Task.Factory.StartNew(() => System.Console.WriteLine(text));
}
public Expression<Func<long, Task>> OnIterationStartAsync(object configuration)
=> x => _service.PrintAsync(configuration);

public Expression<Action> OnPause(object configuration)
=> () => _service.PrintAsync(configuration);

public Expression<Func<Task>> OnPauseAsync(object configuration)
=> () => _service.PrintAsync(configuration);

public Expression<Action> OnStart(object configuration)
=> () => _service.PrintAsync(configuration);

public Expression<Func<Task>> OnStartAsync(object configuration)
=> () => _service.PrintAsync(configuration);

public Expression<Action> OnStop(object configuration)
=> () => _service.PrintAsync(configuration);

public Expression<Func<Task>> OnStopAsync(object configuration)
=> () => _service.PrintAsync(configuration);
}
}
Loading

0 comments on commit e65b3dc

Please sign in to comment.