Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support credentials parameter when generate telemetry client #2183

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions src/PortingAssistant.Client.Telemetry/TelemetryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,38 @@ public Task<AmazonWebServiceResponse> SendAsync(TelemetryRequest request)

public static class TelemetryClientFactory
{
public static bool TryGetClient(string profile, TelemetryConfiguration config, out ITelemetryClient client, bool enabledDefaultCredentials = false)
{
public static bool TryGetClient(string profile, TelemetryConfiguration config, out ITelemetryClient client,
bool enabledDefaultCredentials = false, AWSCredentials? awsCredentials = null)
{
client = null;
if (string.IsNullOrEmpty(profile)) { return false; }
var chain = new CredentialProfileStoreChain();
AWSCredentials awsCredentials;
if (enabledDefaultCredentials)
if (awsCredentials == null)
{
awsCredentials = FallbackCredentialsFactory.GetCredentials();
if (awsCredentials == null)
{
if (string.IsNullOrEmpty(profile) && !enabledDefaultCredentials)
return false;
var chain = new CredentialProfileStoreChain();
if (enabledDefaultCredentials)
{
awsCredentials = FallbackCredentialsFactory.GetCredentials();
if (awsCredentials == null)
{
return false;
}
}
}
else
{
if (!chain.TryGetAWSCredentials(profile, out awsCredentials))
else
{
return false;
if (!chain.TryGetAWSCredentials(profile, out awsCredentials))
{
return false;
}
}
cslong marked this conversation as resolved.
Show resolved Hide resolved
}
if (awsCredentials != null)
client = new TelemetryClient(awsCredentials, new TelemetryClientConfig(config.InvokeUrl)
{
client = new TelemetryClient(awsCredentials, new TelemetryClientConfig(config.InvokeUrl)
{
RegionEndpoint = RegionEndpoint.GetBySystemName(config.Region),
MaxErrorRetry = 2,
ServiceURL = config.InvokeUrl,
});
return true;
}
return false;
RegionEndpoint = RegionEndpoint.GetBySystemName(config.Region),
MaxErrorRetry = 2,
ServiceURL = config.InvokeUrl,
});
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Amazon.Runtime;
using NUnit.Framework;
using PortingAssistant.Client.Telemetry;
using PortingAssistantExtensionTelemetry.Model;
Expand All @@ -14,7 +15,7 @@ public void EnablingDefaultCredentials_CreatesTelemetryClient()
var enabledDefaultCredentials = true;
var telemetryConfig = new TelemetryConfiguration()
{
InvokeUrl = @"https://8cvsix1u33.execute-api.us-east-1.amazonaws.com/gamma",
InvokeUrl = @"https://dummy.amazonaws.com/gamma",
Region = "us-east-1",
};
var isClientCreated = TelemetryClientFactory.TryGetClient(
Expand All @@ -25,5 +26,41 @@ public void EnablingDefaultCredentials_CreatesTelemetryClient()

Assert.IsTrue(isClientCreated);
}

[Test]
public void CreatesTelemetryClient_WithCredentials()
{
string profile = null;
var telemetryConfig = new TelemetryConfiguration()
{
InvokeUrl = @"https://dummy.amazonaws.com/gamma",
Region = "us-east-1",
};
AWSCredentials credentials = new BasicAWSCredentials("accessKey", "secretKey");
ITelemetryClient client;

bool result = TelemetryClientFactory.TryGetClient(profile, telemetryConfig, out client, awsCredentials: credentials);

Assert.IsTrue(result);
Assert.IsNotNull(client);
}

[Test]
public void CreatesTelemetryClient_WithoutProfileOrCredentials_Failed()
{
string profile = null;
var telemetryConfig = new TelemetryConfiguration()
{
InvokeUrl = @"https://dummy.amazonaws.com/gamma",
Region = "us-east-1",
};
AWSCredentials credentials = null;
ITelemetryClient client;

bool result = TelemetryClientFactory.TryGetClient(profile, telemetryConfig, out client, awsCredentials: credentials);

Assert.IsFalse(result);
Assert.IsNull(client);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TelemetryClientTest
[Test]
public void EnablingDefaultCredentials_CreatesTelemetryClient()
{
var url = "https://8cvsix1u33.execute-api.us-east-1.amazonaws.com/gamma";
var url = "https://dummy.amazonaws.com/gamma";
var telemetryClientConfig = new TelemetryClientConfig(url)
{
ServiceURL = url
Expand Down
Loading