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

Add GetEnvironmentVariableValuesAsync method to ResourceExtensions #4530

Merged
merged 22 commits into from
Jul 22, 2024

Conversation

Alirexaa
Copy link
Contributor

@Alirexaa Alirexaa commented Jun 15, 2024

close: #4464

This change is based on this EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync method
with little change.

the difference between the issue Proposed API and this commit:

The issue Proposed API :

public static class ResourceExtensions
{
   public static Task<IReadOnlyDictionary<string, string?>> GetEnvironmentVariableValuesAsync(this IResourceWithEnvironment resource);
}

The commit Proposed API :

public static class ResourceExtensions
{
  public static async ValueTask<IReadOnlyDictionary<string, string>> GetEnvironmentVariableValuesAsync(this IResourceWithEnvironment resource,
    DistributedApplicationOperation applicationOperation = DistributedApplicationOperation.Run);
}
Microsoft Reviewers: Open in CodeFlow

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Jun 15, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 15, 2024
@Alirexaa Alirexaa changed the title Add to ResourceExtensions GetEnvironmentVariableValuesAsync #4464 Add GetEnvironmentVariableValuesAsync method to ResourceExtensions Jun 15, 2024
/// </summary>
/// <param name="resource">The resource to get the environment variables from.</param>
/// <param name="applicationOperation">The context in which the AppHost is being executed</param>
/// <returns>The environment variables retrieved from the resource.</returns>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some <remarks> added which cover off where this method is useful and considerations around the sync nature. As well as one or two <example> sections which cover off its usage.

I'm not sure this needs its own conceptual document, but the following could be expanded:

https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/testing

Could you create a docs issue to capture that?

@mitchdenny
Copy link
Member

@Alirexaa this is looking good. Just want to make sure we capture the doc requirement and make sure we expand the XML docs a bit for this change.

We need some covering tests as well.

@RussKie
Copy link
Member

RussKie commented Jun 18, 2024

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mitchdenny mitchdenny self-assigned this Jun 19, 2024
@mitchdenny
Copy link
Member

@Alirexaa just to be clear we want the XML doc comments in this PR :)

@Alirexaa
Copy link
Contributor Author

@Alirexaa just to be clear we want the XML doc comments in this PR :)

sure 😄

Could you create a docs issue to capture that?

@mitchdenny, what did you mean by that?

@DamianEdwards
Copy link
Member

@Alirexaa

@mitchdenny, what did you mean by that?

Just that can you please log an issue in dotnet/docs-aspire to ensure the new documentation requirement is captured.

Alirexaa and others added 3 commits June 27, 2024 23:44
Co-authored-by: Damian Edwards <damian@damianedwards.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
@dotnet-bot

This comment was marked as outdated.

@JamesNK
Copy link
Member

JamesNK commented Jul 3, 2024

@Alirexaa We have a deadline for 8.1 coming in a couple of weeks and we'd like to include this feature. Do you have time to look at the changes soon? If not, I can help out by addressing some feedback on your branch.

@Alirexaa
Copy link
Contributor Author

Alirexaa commented Jul 3, 2024

@Alirexaa We have a deadline for 8.1 coming in a couple of weeks and we'd like to include this feature. Do you have time to look at the changes soon? If not, I can help out by addressing some feedback on your branch.

Yes, I have time, but I need some examples to write tests for the following cases :
343936435-1fa07906-c814-4eff-a590-74da1552fcc9

env =>
{
Assert.Equal("{ElasticPassword.value}", env.Value);
Assert.False(string.IsNullOrEmpty(env.Value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should verify the Value is the correct parameter reference in publish mode, not just that it isn't null or empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reactivating. This hasn't been resolved yet.

.WithEnvironment("xpack.security.enabled", "true")
.WithEnvironment(context =>
{
context.EnvironmentVariables["ELASTIC_PASSWORD"] = "123456";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid credscan failure:

Suggested change
context.EnvironmentVariables["ELASTIC_PASSWORD"] = "123456";
context.EnvironmentVariables["ELASTIC_PASSWORD"] = "p@ssw0rd1";

@mitchdenny mitchdenny requested review from eerhardt and radical July 18, 2024 23:48
@mitchdenny
Copy link
Member

@joperezr we are going to want to get this one in for 8.1

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

.WithEnvironment("xpack.security.enabled", "true")
.WithEnvironment(context =>
{
context.EnvironmentVariables["ELASTIC_PASSWORD"] = "p@ssw0rd1";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are still going to get cred scan failures here, but we can try it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will see. @radical was saying that they were in a suppression list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 cred scan checks, and my understanding is that only one of them respects the suppression list.

@mitchdenny mitchdenny merged commit e3ea763 into dotnet:main Jul 22, 2024
9 checks passed
@mitchdenny
Copy link
Member

/backport to release/8.1

Copy link
Contributor

Started backporting to release/8.1: https://github.com/dotnet/aspire/actions/runs/10032596995

Copy link
Contributor

@mitchdenny backporting to release/8.1 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Add to ResourceExtensions ```GetEnvironmentVariableValuesAsync``` #4464
Using index info to reconstruct a base tree...
M	src/Aspire.Hosting/PublicAPI.Unshipped.txt
Falling back to patching base and 3-way merge...
Auto-merging src/Aspire.Hosting/PublicAPI.Unshipped.txt
CONFLICT (content): Merge conflict in src/Aspire.Hosting/PublicAPI.Unshipped.txt
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Patch failed at 0001 Add to ResourceExtensions ```GetEnvironmentVariableValuesAsync``` #4464
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

Copy link
Contributor

@mitchdenny an error occurred while backporting to release/8.1, please check the run log for details!

Error: git am failed, most likely due to a merge conflict.

mitchdenny added a commit that referenced this pull request Jul 22, 2024
…ons (#4530)

* Add to ResourceExtensions ```GetEnvironmentVariableValuesAsync``` #4464

* add example and remarks

* add test

* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs

Co-authored-by: Damian Edwards <damian@damianedwards.com>

* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* add more tests,add suggested code

* Tweak API based on Eric's feedback.

* Use a different example.

* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Remove password from example.

* Indentation fixes.

* Update tests/Aspire.Hosting.Tests/ResourceExtensionsTests.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update tests/Aspire.Hosting.Tests/ResourceExtensionsTests.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* PR feedback.

* Namespace fix.

---------

Co-authored-by: Mitch Denny <midenn@microsoft.com>
Co-authored-by: Damian Edwards <damian@damianedwards.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Co-authored-by: Mitch Denny <mitchdenny@outlook.com>
joperezr pushed a commit that referenced this pull request Jul 22, 2024
…ons (#4530) (#5005)

* Add to ResourceExtensions ```GetEnvironmentVariableValuesAsync``` #4464

* add example and remarks

* add test

* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs



* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs



* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs



* add more tests,add suggested code

* Tweak API based on Eric's feedback.

* Use a different example.

* Update src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs



* Remove password from example.

* Indentation fixes.

* Update tests/Aspire.Hosting.Tests/ResourceExtensionsTests.cs



* Update tests/Aspire.Hosting.Tests/ResourceExtensionsTests.cs



* PR feedback.

* Namespace fix.

---------

Co-authored-by: Alireza Baloochi <alireza.baloochi1380@gmail.com>
Co-authored-by: Damian Edwards <damian@damianedwards.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
@Alirexaa Alirexaa deleted the ResourceExtensions branch July 22, 2024 16:45
@github-actions github-actions bot locked and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an API to easily extract a resource's environment variables as a dictionary
8 participants