Skip to content
Snippets Groups Projects

Fix: Output formatting

Merged Petar Hristov requested to merge dev into main
2 files
+ 52
32
Compare changes
  • Side-by-side
  • Inline
Files
2
using Amazon.S3;
using Amazon.S3;
using Coscine.Database.DataModel;
using Coscine.Database.Models;
using Coscine.ECSManager;
using Coscine.ResourceTypes;
using Coscine.ResourceTypes.Base;
using Coscine.ResourceTypes.Rds;
using Coscine.ResourceTypes.RdsS3;
using Coscine.ResourceTypes.RdsWorm;
using ResourceTypes.Rds;
using Coscine.ResourceTypes.RdsS3Worm;
namespace IPConnectivityChecker.Implementations
{
public class ECSChecker
{
private const string format = " {0,-18} [{1}]\t{2}";
public async Task<bool> CheckECS()
{
var resourceTypeModel = new ResourceTypeModel();
@@ -23,16 +25,15 @@ namespace IPConnectivityChecker.Implementations
{
if (resourceType.Type.Contains("rds"))
{
Console.WriteLine("Checking resourcetype with type '" + resourceType.Type + "' and specific type '" + resourceType.SpecificType + "'!");
Console.WriteLine($"\n{new string('=', 70)}\n Resource Type: {resourceType.Type.ToUpper()} -- {resourceType.SpecificType}\n{new string('-', 70)}");
BaseResourceType resourceTypeDefinition;
try
{
resourceTypeDefinition = ResourceTypeFactory.Instance.GetResourceType(resourceType.Type, resourceType.SpecificType);
}
}
catch (Exception e)
{
Console.WriteLine("Error on resourcetype with type '" + resourceType.Type + "' and specific type '" + resourceType.SpecificType + "'!");
Console.WriteLine(e);
Console.Error.WriteLine($"!! Error inside Resource Type Factory: {e.Message}");
working = false;
continue;
}
@@ -41,62 +42,71 @@ namespace IPConnectivityChecker.Implementations
var rdsResourceTypeDefinition = (RdsResourceType)resourceTypeDefinition;
var rdsResourceTypeConfiguration = rdsResourceTypeDefinition.RdsResourceTypeConfiguration;
working = working && await S3ClientCheck(rdsResourceTypeConfiguration.Endpoint, rdsResourceTypeConfiguration.AccessKey, rdsResourceTypeConfiguration.SecretKey, resourceType);
var s3Check = await S3ClientCheck(rdsResourceTypeConfiguration.Endpoint, rdsResourceTypeConfiguration.AccessKey, rdsResourceTypeConfiguration.SecretKey, resourceType);
working = working && s3Check;
var ecsManager = new EcsManager
{
EcsManagerConfiguration = rdsResourceTypeConfiguration.EcsManagerConfiguration
};
working = working && await LoginToECS(ecsManager.EcsManagerConfiguration, resourceType);
var ecsLogin = await LoginToECS(ecsManager.EcsManagerConfiguration, resourceType);
working = working && ecsLogin;
}
else if (resourceType.Type == "rdss3")
{
var rdss3ResourceTypeDefinition = (RdsS3ResourceType)resourceTypeDefinition;
var rdsS3ResourceTypeConfiguration = rdss3ResourceTypeDefinition.RdsS3ResourceTypeConfiguration;
working = working && await S3ClientCheck(rdsS3ResourceTypeConfiguration.Endpoint, rdsS3ResourceTypeConfiguration.AccessKey, rdsS3ResourceTypeConfiguration.SecretKey, resourceType);
var s3Check = await S3ClientCheck(rdsS3ResourceTypeConfiguration.Endpoint, rdsS3ResourceTypeConfiguration.AccessKey, rdsS3ResourceTypeConfiguration.SecretKey, resourceType);
working = working && s3Check;
var rdsS3EcsManager = new EcsManager
{
EcsManagerConfiguration = rdsS3ResourceTypeConfiguration.RdsS3EcsManagerConfiguration
};
working = working && await LoginToECS(rdsS3EcsManager.EcsManagerConfiguration, resourceType);
var ecsLogin = await LoginToECS(rdsS3EcsManager.EcsManagerConfiguration, resourceType);
working = working && ecsLogin;
var userEcsManager = new EcsManager
{
EcsManagerConfiguration = rdsS3ResourceTypeConfiguration.UserEcsManagerConfiguration
};
working = working && await LoginToECS(userEcsManager.EcsManagerConfiguration, resourceType);
var ecsUserLogin = await LoginToECS(userEcsManager.EcsManagerConfiguration, resourceType, " user ");
working = working && ecsUserLogin;
}
else if (resourceType.Type == "rdss3worm")
{
var rdss3wormResourceTypeDefinition = (RdsS3WormResourceType)resourceTypeDefinition;
var rdsS3WormResourceTypeConfiguration = rdss3wormResourceTypeDefinition.RdsS3WormResourceTypeConfiguration;
working = working && await S3ClientCheck(rdsS3WormResourceTypeConfiguration.Endpoint, rdsS3WormResourceTypeConfiguration.AccessKey, rdsS3WormResourceTypeConfiguration.SecretKey, resourceType);
var s3Check = await S3ClientCheck(rdsS3WormResourceTypeConfiguration.Endpoint, rdsS3WormResourceTypeConfiguration.AccessKey, rdsS3WormResourceTypeConfiguration.SecretKey, resourceType);
working = working && s3Check;
var rdsS3EcsManager = new EcsManager
{
EcsManagerConfiguration = rdsS3WormResourceTypeConfiguration.RdsS3EcsManagerConfiguration
};
working = working && await LoginToECS(rdsS3EcsManager.EcsManagerConfiguration, resourceType);
var ecsLogin = await LoginToECS(rdsS3EcsManager.EcsManagerConfiguration, resourceType);
working = working && ecsLogin;
var userEcsManager = new EcsManager
{
EcsManagerConfiguration = rdsS3WormResourceTypeConfiguration.UserEcsManagerConfiguration
};
working = working && await LoginToECS(userEcsManager.EcsManagerConfiguration, resourceType);
var ecsUserLogin = await LoginToECS(userEcsManager.EcsManagerConfiguration, resourceType, " user ");
working = working && ecsUserLogin;
}
else
{
Console.WriteLine($"!! Could not correctly identify the provided {resourceType.Type}!");
}
Console.WriteLine();
}
}
return working;
}
private async Task<bool> S3ClientCheck(string endpoint, string accessKey, string secretKey, ResourceType resourceType)
private async Task<bool> S3ClientCheck(string? endpoint, string? accessKey, string? secretKey, ResourceType resourceType)
{
try
{
@@ -106,33 +116,43 @@ namespace IPConnectivityChecker.Implementations
ForcePathStyle = true
};
using var _s3client = new AmazonS3Client(accessKey, secretKey, amazonS3Config);
await _s3client.ListBucketsAsync();
var output = await _s3client.ListBucketsAsync();
Console.WriteLine(string.Format(format, "S3 Client Check", output.HttpStatusCode, $"Found {output.Buckets.Count} entries"));
}
catch (Exception e)
{
Console.WriteLine("S3 Client Check: FAILED for resourcetype with type '" + resourceType.Type + "' and specific type '" + resourceType.SpecificType + "'!");
Console.WriteLine(e);
Console.WriteLine(string.Format(format, "S3 Client Check", "FAILED", $"For type {resourceType.Type} with specific type {resourceType.SpecificType}!"));
Console.WriteLine($"\tException: {e.Message}");
if (e.InnerException is not null)
{
Console.WriteLine($"\tInner Exception: {e.InnerException.Message}");
}
return false;
}
return true;
}
private async Task<bool> LoginToECS(EcsManagerConfiguration EcsManagerConfiguration, ResourceType resourceType)
private async Task<bool> LoginToECS(EcsManagerConfiguration? EcsManagerConfiguration, ResourceType resourceType, string loginType = " ")
{
try
{
// create new management client USING NAMESPACE ADMINISTRATOR to obtain token
using var client = new CoscineECSManagementClient(EcsManagerConfiguration.NamespaceAdminName, EcsManagerConfiguration.NamespaceAdminPassword, EcsManagerConfiguration.ManagerApiEndpoint);
using var client = new CoscineECSManagementClient(EcsManagerConfiguration!.NamespaceAdminName, EcsManagerConfiguration!.NamespaceAdminPassword, EcsManagerConfiguration!.ManagerApiEndpoint);
// authenticate
await client.Authenticate();
// log out
await client.LogOut();
}
Console.WriteLine(string.Format(format, "Login Check", "OK", $"ECS{loginType}login & logout successful"));
}
catch (Exception e)
{
Console.WriteLine("Login Check: FAILED for resourcetype with type '" + resourceType.Type + "' and specific type '" + resourceType.SpecificType + "'!");
Console.WriteLine(e);
return false;
Console.WriteLine(string.Format(format, "Login Check", "FAILED", $"ECS{loginType}login & logout failed for type {resourceType.Type} with specific type {resourceType.SpecificType}!"));
Console.WriteLine($"\tException: {e.Message}");
if (e.InnerException is not null)
{
Console.WriteLine($"\tInner Exception: {e.InnerException.Message}");
}
}
return true;
}
Loading