Skip to content

Commit

Permalink
Fix build warning
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed Jul 20, 2023
1 parent 892d3e5 commit f3bf8d7
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 36 deletions.
3 changes: 0 additions & 3 deletions EchoServiceApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@
logger.LogInformation("Environment: {environmentName}; DeveloperMode:{isDevelopment}", app.Environment.EnvironmentName, app.Environment.IsDevelopment());

app.UseHttpOverrides();

#pragma warning disable S3923 // All branches in a conditional structure should not have exactly the same implementation
if (app.Environment.IsDevelopment())
#pragma warning restore S3923 // All branches in a conditional structure should not have exactly the same implementation
{
// app.UseDeveloperExceptionPage()
}
Expand Down
4 changes: 3 additions & 1 deletion EchoServiceApi/Verifiers/BaseVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ protected BaseVerifier(IServiceProvider serviceProvider)
protected ProviderConnectionString GetConnection(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}

IConnectionStringManager connectionStringManager = new ConnectionStringManager(Configuration);
var connectionObj = connectionStringManager[name] ?? throw new Exception($"Connection string '{name}' not found");
return connectionObj;
}

protected IDisposable LoggerBeginScopeDiagnostic() => Logger.BeginScope(DiagnosticInfo.LoggingScopeState);
protected IDisposable LoggerBeginScopeDiagnostic() => Logger.BeginScope(DiagnosticInfo.LoggingScopeState) ?? throw new Exception("Logger.BeginScope null");
}
8 changes: 4 additions & 4 deletions EchoServiceApi/Verifiers/CosmosCacheVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public CosmosCacheVerifier(IServiceProvider serviceProvider) : base(serviceProvi
public async Task<VerifyResult> VerifyAsync(string name)
{
var connectionObj = GetConnection(name);
var cosmosCacheInfo = connectionObj.Get<CosmosCacheInfo>();
var cosmosCacheInfo = connectionObj.Get<CosmosCacheInfo>() ?? throw new Exception("CosmosCacheInfo is required");

var databaseName = cosmosCacheInfo.DatabaseName;
var containerName = cosmosCacheInfo.ContainerName;

using var cosmosclient = await CreateClientAsync(connectionObj, cosmosCacheInfo);
using var cosmosClient = await CreateClientAsync(connectionObj, cosmosCacheInfo);

using var scope = LoggerBeginScopeDiagnostic();

Expand All @@ -31,14 +31,14 @@ public async Task<VerifyResult> VerifyAsync(string name)

var cosmosCacheOptions = new CosmosCacheOptions
{
CosmosClient = cosmosclient,
CosmosClient = cosmosClient,
ContainerName = containerName,
DatabaseName = databaseName,
CreateIfNotExists = false,
};
var cache = new CosmosCache(Options.Create(cosmosCacheOptions));
_ = await cache.GetStringAsync("CosmosCacheVerifier");

return VerifyResult.Successed("CosmosCache", connectionObj);
return VerifyResult.Succeed("CosmosCache", connectionObj);
}
}
12 changes: 6 additions & 6 deletions EchoServiceApi/Verifiers/CosmosVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Azure.Cosmos;
using NetLah.Extensions.Configuration;

#pragma warning disable S4457 // Parameter validation in "async"/"await" methods should be wrapped
namespace EchoServiceApi.Verifiers;

public class CosmosVerifier : BaseCosmosVerifier
Expand All @@ -11,26 +10,27 @@ public CosmosVerifier(IServiceProvider serviceProvider) : base(serviceProvider)
public async Task<VerifyResult> VerifyAsync(string name, string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}

var connectionObj = GetConnection(name);
var cosmosInfo = connectionObj.Get<CosmosContainerInfo>();
var cosmosInfo = connectionObj.Get<CosmosContainerInfo>() ?? throw new Exception("CosmosContainerInfo is required");

var containerName = cosmosInfo.ContainerName;
var databaseName = cosmosInfo.DatabaseName;

using var cosmosclient = await CreateClientAsync(connectionObj, cosmosInfo);
using var cosmosClient = (await CreateClientAsync(connectionObj, cosmosInfo)) ?? throw new Exception("CosmosContainerInfo is required");

using var scope = LoggerBeginScopeDiagnostic();

Logger.LogInformation("CosmosVerifier db:{databaseName} container:{containerName} name={query_name} key={query_key}",
databaseName, containerName, name, key);

var container = cosmosclient.GetContainer(databaseName, containerName);
var container = cosmosClient.GetContainer(databaseName, containerName);
_ = await container.ReadContainerAsync().ConfigureAwait(false);

var itemResponse = await container.ReadItemAsync<Dictionary<string, object>>(key, new PartitionKey(key));
return VerifyResult.SuccessObject("Cosmos", connectionObj, itemResponse.Resource);
}
}
#pragma warning restore S4457 // Parameter validation in "async"/"await" methods should be wrapped
}
2 changes: 2 additions & 0 deletions EchoServiceApi/Verifiers/DirVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public DirVerifier(IServiceProvider serviceProvider) : base(serviceProvider) { }
public Task<VerifyResult> VerifyAsync(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("Path is required");
}

var isDir = false;
var isFile = File.Exists(path);
Expand Down
2 changes: 2 additions & 0 deletions EchoServiceApi/Verifiers/HttpVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public HttpVerifier(HttpClient httpClient, IServiceProvider serviceProvider)
public async Task<string> VerifyAsync(Uri url, string? host)
{
if (url == null)
{
throw new ArgumentException("Url is required");
}

if (!string.IsNullOrEmpty(host))
{
Expand Down
7 changes: 5 additions & 2 deletions EchoServiceApi/Verifiers/KeyVaultCertificateVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public async Task<VerifyResult> VerifyAsync(string name, bool privateKey, bool a
var certificateName = locationParts[2];
var versions = new List<string?>();
var version = locationParts.Length >= 4 ? locationParts[3] : null;
if (string.IsNullOrEmpty(version)) version = null;

if (string.IsNullOrEmpty(version))
{
version = null;
}

versions.Add(version);

if (privateKey)
Expand Down
4 changes: 3 additions & 1 deletion EchoServiceApi/Verifiers/KeyVaultKeyVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public async Task<VerifyResult> VerifyAsync(string name)
var keyName = locationParts[2];
var version = locationParts.Length >= 4 ? locationParts[3] : null;
if (string.IsNullOrEmpty(version))
{
version = null;
}

var keyClient = new KeyClient(keyIdentifier, tokenCredential);
var response = await keyClient.GetKeyAsync(keyName, version);
Expand All @@ -51,7 +53,7 @@ public async Task<VerifyResult> VerifyAsync(string name)

var detail = $"KeyType={keyVaultKey.KeyType}; Text={text}; Signature={signed}; Ciphertext={ciphertext}; Plaintext={decryptText}; ValidSignature={validSignature}";

return VerifyResult.Successed("KeyVaultKey", connectionObj, detail: detail);
return VerifyResult.Succeed("KeyVaultKey", connectionObj, detail: detail);
}

return new VerifyResult
Expand Down
10 changes: 5 additions & 5 deletions EchoServiceApi/Verifiers/PosgreSqlVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma warning disable S4457 // Parameter validation in "async"/"await" methods should be wrapped
namespace EchoServiceApi.Verifiers;
namespace EchoServiceApi.Verifiers;

public class PosgreSqlVerifier : BaseVerifier
{
Expand All @@ -8,7 +7,9 @@ public PosgreSqlVerifier(IServiceProvider serviceProvider) : base(serviceProvide
public async Task<VerifyResult> VerifyAsync(string name, string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentNullException(nameof(tableName));
}

var connectionObj = GetConnection(name);
var connectionString = connectionObj.Value;
Expand All @@ -22,7 +23,6 @@ public async Task<VerifyResult> VerifyAsync(string name, string tableName)
command.CommandType = System.Data.CommandType.Text;
command.ExecuteNonQuery();

return VerifyResult.Successed("PosgreSql", connectionObj, detail: query);
return VerifyResult.Succeed("PosgreSql", connectionObj, detail: query);
}
}
#pragma warning restore S4457 // Parameter validation in "async"/"await" methods should be wrapped
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static TConnectionCredentialInfo TryGet<TConnectionCredentialInfo>(this P
return new TConnectionCredentialInfo { Value = connectionString.Value };
}

return connectionString.Get<TConnectionCredentialInfo>();
return connectionString.Get<TConnectionCredentialInfo>() ?? throw new Exception("ConnectionCredentialValue is required");
}

public static ConnectionCredentialBag<TValue> Load<TValue>(this ProviderConnectionString connectionString)
{
var result = connectionString.Get<ConnectionCredentialBag<TValue>>();
var result = connectionString.Get<ConnectionCredentialBag<TValue>>() ?? throw new Exception("ConnectionCredentialBag is required");
result.Value = connectionString.Get<TValue>();
return result;
}
Expand Down
12 changes: 8 additions & 4 deletions EchoServiceApi/Verifiers/ServiceBusVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<VerifyResult> VerifyAsync(string name, bool send, bool receive
{
if (string.IsNullOrEmpty(queueName))
{
queueName = connectionObj.Get<ServiceBusVerifierQueueName>().QueueName ?? throw new Exception("QueueName is required");
queueName = connectionObj.Get<ServiceBusVerifierQueueName>()?.QueueName ?? throw new Exception("QueueName is required");
}
client = new ServiceBusClient(connectionString: connectionObj.Value);
}
Expand Down Expand Up @@ -57,29 +57,33 @@ public async Task<VerifyResult> VerifyAsync(string name, bool send, bool receive
await receiver.CompleteMessageAsync(message);
}
var detail1 = $"Received={isExist}; queueName={queueName}; fqns={receiver.FullyQualifiedNamespace}; messageId={message?.MessageId}; messageBody={message?.Body.ToString()}";
return VerifyResult.Successed("ServiceBus", connectionObj, detail1);
return VerifyResult.Succeed("ServiceBus", connectionObj, detail1);
}
else if (send)
{
sender = client.CreateSender(queueName);
await sender.SendMessageAsync(new ServiceBusMessage($"{queueName}-{DateTimeOffset.Now}"));

var detail1 = $"Status=Sent; queueName={queueName}; fqns={sender.FullyQualifiedNamespace};";
return VerifyResult.Successed("ServiceBus", connectionObj, detail1);
return VerifyResult.Succeed("ServiceBus", connectionObj, detail1);
}

sender = client.CreateSender(queueName);
receiver = client.CreateReceiver(queueName);
var detail = $"queueName={queueName}; fqns={sender.FullyQualifiedNamespace};";
return VerifyResult.Successed("ServiceBus", connectionObj, detail);
return VerifyResult.Succeed("ServiceBus", connectionObj, detail);
}
finally
{
if (receiver != null)
{
await receiver.DisposeAsync();
}

if (sender != null)
{
await sender.DisposeAsync();
}

await client.DisposeAsync();

Expand Down
7 changes: 2 additions & 5 deletions EchoServiceApi/Verifiers/TokenCredentialFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ private async Task<TokenCredential> GetDefaultTokenCredentialAsync()
return result;
}

public async Task<TokenCredential?> GetTokenCredentialAsync(AzureCredentialInfo? options) =>
#pragma warning disable S3358 // Ternary operators should not be nested
options != null && options.ClientId is { } clientId
? options.TenantId is { } tenantId
public async Task<TokenCredential?> GetTokenCredentialAsync(AzureCredentialInfo? options) => options != null && options.ClientId is { } clientId
? options.TenantId is { } tenantId
&& options.ClientSecret is { } clientSecret
? await GetClientSecretCredentialAsync(tenantId, clientId, clientSecret)
: await GetManagedIdentityClientIdAsync(clientId)
: null;
#pragma warning restore S3358 // Ternary operators should not be nested

public string? Redact(string? secret)
{
Expand Down
6 changes: 3 additions & 3 deletions EchoServiceApi/Verifiers/VerifyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static VerifyResult Failed(Exception ex)
{
Success = false,
Error = $"{ex.GetType().FullName}: {cosmosException.Message}",
Disagnostics = cosmosException.Diagnostics?.ToString(),
Diagnostics = cosmosException.Diagnostics?.ToString(),
StackTrace = cosmosException.StackTrace,
DetailError = cosmosException.ToString(),
}
Expand All @@ -31,7 +31,7 @@ public static VerifyResult Failed(Exception ex)
};
}

public static VerifyResult Successed(string serviceName, ProviderConnectionString connectionObj, string? detail = null)
public static VerifyResult Succeed(string serviceName, ProviderConnectionString connectionObj, string? detail = null)
{
return new VerifySuccessMessage
{
Expand All @@ -57,7 +57,7 @@ public class VerifyFailed : VerifyResult
public string? Error { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? Disagnostics { get; set; }
public object? Diagnostics { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? StackTrace { get; internal set; }
Expand Down

0 comments on commit f3bf8d7

Please sign in to comment.