Skip to content

Commit

Permalink
feat(): file_scope namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Feser <[email protected]>
  • Loading branch information
joefeser committed Aug 10, 2024
1 parent 49add2d commit eee763e
Show file tree
Hide file tree
Showing 2,522 changed files with 183,554 additions and 186,095 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ csharp_style_prefer_readonly_struct_member = true
# Code-block preferences
csharp_prefer_braces = false:warning
csharp_prefer_simple_using_statement = true
# csharp_style_namespace_declarations = file_scoped #This allows for no { } for namespaces
csharp_style_namespace_declarations = file_scoped:error #This allows for no { } for namespaces
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_primary_constructors = false:warning
csharp_style_prefer_top_level_statements = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,35 @@
* under the License.
*/

namespace OpenSearch.OpenSearch.Ephemeral
namespace OpenSearch.OpenSearch.Ephemeral;

/// <summary>
/// Authentication credentials for the cluster
/// </summary>
public class ClusterAuthentication
{
/// <summary>
/// Authentication credentials for the cluster
/// Administrator credentials
/// </summary>
public class ClusterAuthentication
{
/// <summary>
/// Administrator credentials
/// </summary>
public static Credentials Admin => new Credentials { Username = "admin", Role = "admin" };
public static Credentials Admin => new Credentials { Username = "admin", Role = "admin" };

/// <summary>
/// User credentials
/// </summary>
public static Credentials User => new Credentials { Username = "admin", Role = "admin" };
/// <summary>
/// User credentials
/// </summary>
public static Credentials User => new Credentials { Username = "admin", Role = "admin" };

/// <summary>
/// Credentials for all users
/// </summary>
public static Credentials[] AllUsers { get; } = { Admin, User };
/// <summary>
/// Credentials for all users
/// </summary>
public static Credentials[] AllUsers { get; } = { Admin, User };

/// <summary>
/// Authentication credentials
/// </summary>
public class Credentials
{
public string Username { get; set; }
public string Role { get; set; }
public string Password => Username;
}
/// <summary>
/// Authentication credentials
/// </summary>
public class Credentials
{
public string Username { get; set; }
public string Role { get; set; }
public string Password => Username;
}
}
29 changes: 14 additions & 15 deletions abstractions/src/OpenSearch.OpenSearch.Ephemeral/ClusterFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,22 @@

using System;

namespace OpenSearch.OpenSearch.Ephemeral
namespace OpenSearch.OpenSearch.Ephemeral;

/// <summary>
/// Hints to <see cref="EphemeralClusterConfiguration" /> what features the cluster to be started should have.
/// It's up to the <see cref="EphemeralClusterComposer{TConfiguration}" /> to actually bootstrap these features.
/// </summary>
[Flags]
public enum ClusterFeatures
{
/// <summary>
/// Hints to <see cref="EphemeralClusterConfiguration" /> what features the cluster to be started should have.
/// It's up to the <see cref="EphemeralClusterComposer{TConfiguration}" /> to actually bootstrap these features.
/// No features
/// </summary>
[Flags]
public enum ClusterFeatures
{
/// <summary>
/// No features
/// </summary>
None = 1 << 0,
None = 1 << 0,

/// <summary>
/// SSL/TLS for HTTP and Transport layers
/// </summary>
SSL = 1 << 3,
}
/// <summary>
/// SSL/TLS for HTTP and Transport layers
/// </summary>
SSL = 1 << 3,
}
155 changes: 77 additions & 78 deletions abstractions/src/OpenSearch.OpenSearch.Ephemeral/EphemeralCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,105 +37,104 @@
using OpenSearch.OpenSearch.Managed.Configuration;
using OpenSearch.Stack.ArtifactsApi;

namespace OpenSearch.OpenSearch.Ephemeral
namespace OpenSearch.OpenSearch.Ephemeral;

public class EphemeralCluster : EphemeralCluster<EphemeralClusterConfiguration>
{
public class EphemeralCluster : EphemeralCluster<EphemeralClusterConfiguration>
public EphemeralCluster(OpenSearchVersion version, int numberOfNodes = 1)
: base(new EphemeralClusterConfiguration(version, ClusterFeatures.None, numberOfNodes: numberOfNodes))
{
public EphemeralCluster(OpenSearchVersion version, int numberOfNodes = 1)
: base(new EphemeralClusterConfiguration(version, ClusterFeatures.None, numberOfNodes: numberOfNodes))
{
}

public EphemeralCluster(EphemeralClusterConfiguration clusterConfiguration) : base(clusterConfiguration)
{
}
}

public abstract class EphemeralCluster<TConfiguration> : ClusterBase<TConfiguration>,
IEphemeralCluster<TConfiguration>
where TConfiguration : EphemeralClusterConfiguration
public EphemeralCluster(EphemeralClusterConfiguration clusterConfiguration) : base(clusterConfiguration)
{
protected EphemeralCluster(TConfiguration clusterConfiguration) : base(clusterConfiguration) =>
Composer = new EphemeralClusterComposer<TConfiguration>(this);
}
}

protected EphemeralClusterComposer<TConfiguration> Composer { get; }
public abstract class EphemeralCluster<TConfiguration> : ClusterBase<TConfiguration>,
IEphemeralCluster<TConfiguration>
where TConfiguration : EphemeralClusterConfiguration
{
protected EphemeralCluster(TConfiguration clusterConfiguration) : base(clusterConfiguration) =>
Composer = new EphemeralClusterComposer<TConfiguration>(this);

protected override void ModifyNodeConfiguration(NodeConfiguration nodeConfiguration, int port)
{
base.ModifyNodeConfiguration(nodeConfiguration, port);
protected EphemeralClusterComposer<TConfiguration> Composer { get; }

if (!ClusterConfiguration.EnableSsl) nodeConfiguration.Add("plugins.security.disabled", "true");
}
protected override void ModifyNodeConfiguration(NodeConfiguration nodeConfiguration, int port)
{
base.ModifyNodeConfiguration(nodeConfiguration, port);

public virtual ICollection<Uri> NodesUris(string hostName = null)
{
hostName = hostName ?? (ClusterConfiguration.HttpFiddlerAware && Process.GetProcessesByName("fiddler").Any()
? "ipv4.fiddler"
: "localhost");
var ssl = ClusterConfiguration.EnableSsl ? "s" : "";
return Nodes
.Select(n => $"http{ssl}://{hostName}:{n.Port ?? 9200}")
.Distinct()
.Select(n => new Uri(n))
.ToList();
}
if (!ClusterConfiguration.EnableSsl) nodeConfiguration.Add("plugins.security.disabled", "true");
}

public bool CachingAndCachedHomeExists()
{
if (!ClusterConfiguration.CacheOpenSearchHomeInstallation) return false;
var cachedOpenSearchHomeFolder = Path.Combine(FileSystem.LocalFolder, GetCacheFolderName());
return Directory.Exists(cachedOpenSearchHomeFolder);
}
public virtual ICollection<Uri> NodesUris(string hostName = null)
{
hostName = hostName ?? (ClusterConfiguration.HttpFiddlerAware && Process.GetProcessesByName("fiddler").Any()
? "ipv4.fiddler"
: "localhost");
var ssl = ClusterConfiguration.EnableSsl ? "s" : "";
return Nodes
.Select(n => $"http{ssl}://{hostName}:{n.Port ?? 9200}")
.Distinct()
.Select(n => new Uri(n))
.ToList();
}

public virtual string GetCacheFolderName()
{
var config = ClusterConfiguration;
public bool CachingAndCachedHomeExists()
{
if (!ClusterConfiguration.CacheOpenSearchHomeInstallation) return false;
var cachedOpenSearchHomeFolder = Path.Combine(FileSystem.LocalFolder, GetCacheFolderName());
return Directory.Exists(cachedOpenSearchHomeFolder);
}

public virtual string GetCacheFolderName()
{
var config = ClusterConfiguration;

var sb = new StringBuilder();
sb.Append(EphemeralClusterComposerBase.InstallationTasks.Count());
var sb = new StringBuilder();
sb.Append(EphemeralClusterComposerBase.InstallationTasks.Count());
sb.Append("-");
if (config.EnableSsl) sb.Append("ssl");
if (config.Plugins != null && config.Plugins.Count > 0)
{
sb.Append("-");
if (config.EnableSsl) sb.Append("ssl");
if (config.Plugins != null && config.Plugins.Count > 0)
{
sb.Append("-");
foreach (var p in config.Plugins.OrderBy(p => p.SubProductName))
sb.Append(p.SubProductName.ToLowerInvariant());
}
foreach (var p in config.Plugins.OrderBy(p => p.SubProductName))
sb.Append(p.SubProductName.ToLowerInvariant());
}

var name = sb.ToString();
var name = sb.ToString();

return CalculateSha1(name, Encoding.UTF8);
}
return CalculateSha1(name, Encoding.UTF8);
}

protected override void OnBeforeStart()
{
Composer.Install();
Composer.OnBeforeStart();
}
protected override void OnBeforeStart()
{
Composer.Install();
Composer.OnBeforeStart();
}

protected override void OnDispose() => Composer.OnStop();
protected override void OnDispose() => Composer.OnStop();

protected override void OnAfterStarted() => Composer.OnAfterStart();
protected override void OnAfterStarted() => Composer.OnAfterStart();

protected override string SeeLogsMessage(string message)
protected override string SeeLogsMessage(string message)
{
var log = Path.Combine(FileSystem.LogsPath, $"{ClusterConfiguration.ClusterName}.log");
if (!File.Exists(log) || ClusterConfiguration.ShowOpenSearchOutputAfterStarted) return message;
if (!Started) return message;
using (var fileStream = new FileStream(log, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var textReader = new StreamReader(fileStream))
{
var log = Path.Combine(FileSystem.LogsPath, $"{ClusterConfiguration.ClusterName}.log");
if (!File.Exists(log) || ClusterConfiguration.ShowOpenSearchOutputAfterStarted) return message;
if (!Started) return message;
using (var fileStream = new FileStream(log, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var textReader = new StreamReader(fileStream))
{
var logContents = textReader.ReadToEnd();
return message + $" contents of {log}:{Environment.NewLine}" + logContents;
}
var logContents = textReader.ReadToEnd();
return message + $" contents of {log}:{Environment.NewLine}" + logContents;
}
}

public static string CalculateSha1(string text, Encoding enc)
{
var buffer = enc.GetBytes(text);
var cryptoTransformSha1 = new SHA1CryptoServiceProvider();
return BitConverter.ToString(cryptoTransformSha1.ComputeHash(buffer))
.Replace("-", "").ToLowerInvariant().Substring(0, 12);
}
public static string CalculateSha1(string text, Encoding enc)
{
var buffer = enc.GetBytes(text);
var cryptoTransformSha1 = new SHA1CryptoServiceProvider();
return BitConverter.ToString(cryptoTransformSha1.ComputeHash(buffer))
.Replace("-", "").ToLowerInvariant().Substring(0, 12);
}
}
Loading

0 comments on commit eee763e

Please sign in to comment.