diff --git a/src/PortingAssistant.Client.Telemetry/TelemetryClient.cs b/src/PortingAssistant.Client.Telemetry/TelemetryClient.cs index e2d7cd2f..85e56f0c 100644 --- a/src/PortingAssistant.Client.Telemetry/TelemetryClient.cs +++ b/src/PortingAssistant.Client.Telemetry/TelemetryClient.cs @@ -52,38 +52,38 @@ public Task 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; + } } } - 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; } } diff --git a/tests/PortingAssistant.Client.UnitTests/TelemetryClientFactoryTest.cs b/tests/PortingAssistant.Client.UnitTests/TelemetryClientFactoryTest.cs index 18831095..634954a1 100644 --- a/tests/PortingAssistant.Client.UnitTests/TelemetryClientFactoryTest.cs +++ b/tests/PortingAssistant.Client.UnitTests/TelemetryClientFactoryTest.cs @@ -1,4 +1,5 @@ using System; +using Amazon.Runtime; using NUnit.Framework; using PortingAssistant.Client.Telemetry; using PortingAssistantExtensionTelemetry.Model; @@ -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( @@ -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); + } } } diff --git a/tests/PortingAssistant.Client.UnitTests/TelemetryClientTest.cs b/tests/PortingAssistant.Client.UnitTests/TelemetryClientTest.cs index 28aadc69..51c5d03a 100644 --- a/tests/PortingAssistant.Client.UnitTests/TelemetryClientTest.cs +++ b/tests/PortingAssistant.Client.UnitTests/TelemetryClientTest.cs @@ -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