Skip to content

Commit

Permalink
change factory pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
gcbeattyAWS committed Oct 31, 2024
1 parent 6d7bd85 commit de6dc79
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 352 deletions.
25 changes: 12 additions & 13 deletions src/AWS.Deploy.CLI/AWSUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,35 @@ public class AWSUtilities : IAWSUtilities
private readonly IDirectoryManager _directoryManager;
private readonly IOptionSettingHandler _optionSettingHandler;
private readonly IServiceProvider _serviceProvider;
private readonly Func<ICredentialProfileStoreChain> _credentialChainFactory;
private readonly Func<ISharedCredentialsFile> _sharedCredentialsFileFactory;
private readonly IFallbackCredentialsFactory _fallbackCredentialsFactory;

private readonly ICredentialProfileStoreChainFactory _credentialChainFactory;
private readonly ISharedCredentialsFileFactory _sharedCredentialsFileFactory;
private readonly IFallbackCredentialsFactoryFactory _fallbackCredentialsFactory;

public AWSUtilities(
IServiceProvider serviceProvider,
IToolInteractiveService toolInteractiveService,
IConsoleUtilities consoleUtilities,
IDirectoryManager directoryManager,
IOptionSettingHandler optionSettingHandler,
Func<ICredentialProfileStoreChain>? credentialChainFactory = null,
Func<ISharedCredentialsFile>? sharedCredentialsFileFactory = null,
IFallbackCredentialsFactory? fallbackCredentialsFactory = null)
ICredentialProfileStoreChainFactory credentialChainFactory,
ISharedCredentialsFileFactory sharedCredentialsFileFactory,
IFallbackCredentialsFactoryFactory fallbackCredentialsFactory)

Check warning on line 42 in src/AWS.Deploy.CLI/AWSUtilities.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/AWSUtilities.cs#L39-L42

Added lines #L39 - L42 were not covered by tests
{
_serviceProvider = serviceProvider;
_toolInteractiveService = toolInteractiveService;
_consoleUtilities = consoleUtilities;
_directoryManager = directoryManager;
_optionSettingHandler = optionSettingHandler;
_credentialChainFactory = credentialChainFactory ?? (() => new CredentialProfileStoreChainWrapper());
_sharedCredentialsFileFactory = sharedCredentialsFileFactory ?? (() => new SharedCredentialsFileWrapper());
_fallbackCredentialsFactory = fallbackCredentialsFactory ?? new FallbackCredentialsFactoryWrapper();
_credentialChainFactory = credentialChainFactory;
_sharedCredentialsFileFactory = sharedCredentialsFileFactory;
_fallbackCredentialsFactory = fallbackCredentialsFactory;

Check warning on line 51 in src/AWS.Deploy.CLI/AWSUtilities.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/AWSUtilities.cs#L49-L51

Added lines #L49 - L51 were not covered by tests
}

public async Task<Tuple<AWSCredentials, string?>> ResolveAWSCredentials(string? profileName)
{
async Task<Tuple<AWSCredentials, string?>> Resolve()
{
var chain = _credentialChainFactory();
var chain = _credentialChainFactory.Create();

Check warning on line 58 in src/AWS.Deploy.CLI/AWSUtilities.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/AWSUtilities.cs#L58

Added line #L58 was not covered by tests

if (!string.IsNullOrEmpty(profileName))
{
Expand All @@ -77,7 +76,7 @@ public AWSUtilities(

try
{
var fallbackCredentials = _fallbackCredentialsFactory.GetCredentials();
var fallbackCredentials = _fallbackCredentialsFactory.Create();

Check warning on line 79 in src/AWS.Deploy.CLI/AWSUtilities.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/AWSUtilities.cs#L79

Added line #L79 was not covered by tests

if (await CanLoadCredentials(fallbackCredentials))
{
Expand All @@ -92,7 +91,7 @@ public AWSUtilities(
_toolInteractiveService.WriteDebugLine(ex.PrettyPrint());
}

var sharedCredentials = _sharedCredentialsFileFactory();
var sharedCredentials = _sharedCredentialsFileFactory.Create();

Check warning on line 94 in src/AWS.Deploy.CLI/AWSUtilities.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/AWSUtilities.cs#L94

Added line #L94 was not covered by tests
if (sharedCredentials.ListProfileNames().Count == 0)
{
throw new NoAWSCredentialsFoundException(DeployToolErrorCode.UnableToResolveAWSCredentials, "Unable to resolve AWS credentials to access AWS.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

namespace AWS.Deploy.CLI
{
public interface ICredentialProfileStoreChain
public class CredentialProfileStoreChainFactory : ICredentialProfileStoreChainFactory
{
bool TryGetAWSCredentials(string profileName, out AWSCredentials credentials);
bool TryGetProfile(string profileName, out CredentialProfile profile);
public CredentialProfileStoreChain Create()
{
return new CredentialProfileStoreChain();

Check warning on line 16 in src/AWS.Deploy.CLI/CredentialProfileStoreChainFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/CredentialProfileStoreChainFactory.cs#L16

Added line #L16 was not covered by tests
}
}
}
19 changes: 0 additions & 19 deletions src/AWS.Deploy.CLI/CredentialProfileStoreChainWrapper.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public static void AddCustomServices(this IServiceCollection serviceCollection,
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IEnvironmentVariableManager), typeof(EnvironmentVariableManager), lifetime));
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IDeployToolWorkspaceMetadata), typeof(DeployToolWorkspaceMetadata), lifetime));
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IDeploymentSettingsHandler), typeof(DeploymentSettingsHandler), lifetime));
serviceCollection.TryAdd(new ServiceDescriptor(typeof(ICredentialProfileStoreChainFactory), typeof(CredentialProfileStoreChainFactory), lifetime));
serviceCollection.TryAdd(new ServiceDescriptor(typeof(ISharedCredentialsFileFactory), typeof(SharedCredentialsFileFactory), lifetime));
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IFallbackCredentialsFactoryFactory), typeof(FallbackCredentialsFactoryFactory), lifetime));

Check warning on line 76 in src/AWS.Deploy.CLI/Extensions/CustomServiceCollectionExtension.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/Extensions/CustomServiceCollectionExtension.cs#L74-L76

Added lines #L74 - L76 were not covered by tests



var packageJsonTemplate = typeof(PackageJsonGenerator).Assembly.ReadEmbeddedFile(PackageJsonGenerator.TemplateIdentifier);
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IPackageJsonGenerator), (serviceProvider) => new PackageJsonGenerator(packageJsonTemplate), lifetime));
Expand Down
15 changes: 15 additions & 0 deletions src/AWS.Deploy.CLI/FallbackCredentialsFactoryFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using Amazon.Runtime;

namespace AWS.Deploy.CLI
{
public class FallbackCredentialsFactoryFactory : IFallbackCredentialsFactoryFactory
{
public AWSCredentials Create()
{
return FallbackCredentialsFactory.GetCredentials();
}

Check warning on line 13 in src/AWS.Deploy.CLI/FallbackCredentialsFactoryFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/AWS.Deploy.CLI/FallbackCredentialsFactoryFactory.cs#L12-L13

Added lines #L12 - L13 were not covered by tests
}
}
12 changes: 0 additions & 12 deletions src/AWS.Deploy.CLI/FallbackCredentialsFactoryWrapper.cs

This file was deleted.

16 changes: 16 additions & 0 deletions src/AWS.Deploy.CLI/ICredentialProfileStoreChainFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using Amazon.Runtime.CredentialManagement;
using Amazon.Runtime;

namespace AWS.Deploy.CLI
{
public interface ICredentialProfileStoreChainFactory
{
CredentialProfileStoreChain Create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace AWS.Deploy.CLI
{
public interface IFallbackCredentialsFactory
public interface IFallbackCredentialsFactoryFactory
{
AWSCredentials GetCredentials();
AWSCredentials Create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using Amazon.Runtime.CredentialManagement;

namespace AWS.Deploy.CLI
{
public interface ISharedCredentialsFile
public interface ISharedCredentialsFileFactory
{
List<string> ListProfileNames();
SharedCredentialsFile Create();
}
}
16 changes: 16 additions & 0 deletions src/AWS.Deploy.CLI/SharedCredentialsFileFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using Amazon.Runtime.CredentialManagement;

namespace AWS.Deploy.CLI
{
public class SharedCredentialsFileFactory : ISharedCredentialsFileFactory
{
public SharedCredentialsFile Create()
{
return new SharedCredentialsFile();
}
}
}
17 changes: 0 additions & 17 deletions src/AWS.Deploy.CLI/SharedCredentialsFileWrapper.cs

This file was deleted.

Loading

0 comments on commit de6dc79

Please sign in to comment.