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

Refactor Some Command-Related Methods in aws_ssm.py #2248

Conversation

beeankha
Copy link
Contributor

@beeankha beeankha commented Feb 17, 2025

SUMMARY

This work is part of the currently ongoing AWS SSM Connection Refactoring & Plugin Promotion effort and related to ansible-collections/amazon.aws#2394.

Fixes ACA-2095

ISSUE TYPE
  • Feature Pull Request
TASK LIST
  • The _exec_transport_commands method is refactored to accept a single structured object (a tuple containing lists of command dictionaries) that contains the necessary command data.
  • Create a custom dataclass to hold command output.
  • Create a custom dataclass TypedDict to hold command results.
  • The _generate_commands function returns a list of typed dictionaries with clear metadata (command strings, method, headers).
  • _exec_transport_commands accepts a list of typed dictionaries as an argument and returns the custom CommandResults object.
  • Python type hints are added to the method signature and the structured object to improve readability, facilitate static analysis, and clearly define the expected structure and data types.
  • Docstrings should be added to any refactored methods and the structured object (if custom), clarifying the purpose, structure, inputs, outputs, and any edge cases or special handling (if applicable).
  • Unit tests have been created to ensure that the refactored _generate_commands method outputs the expected structured command object.
  • Add a changelog file.

@beeankha beeankha added the WIP Work in progress label Feb 17, 2025
@beeankha beeankha self-assigned this Feb 17, 2025
Copy link
Contributor

Merge Failed.

This change or one of its cross-repo dependencies was unable to be automatically merged with the current state of its repository. Please rebase the change and upload a new patchset.
Warning:
Error merging github.com/ansible-collections/community.aws for 2248,f0da9b82ad297cf0279d614cc1031dcf568760e3

@beeankha beeankha force-pushed the refactor-exec-transport-command-method branch from b46d9dc to 17a29e9 Compare February 17, 2025 17:18
Copy link
Contributor

Build failed.
https://ansible.softwarefactory-project.io/zuul/buildset/c55abb9436424eafb32790c7d6b77d5a

ansible-galaxy-importer FAILURE in 4m 42s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 25s
✔️ ansible-test-splitter SUCCESS in 4m 02s
integration-community.aws-1 FAILURE in 58m 33s
integration-community.aws-2 FAILURE in 25m 09s
integration-community.aws-3 FAILURE in 25m 35s
integration-community.aws-4 FAILURE in 27m 15s
integration-community.aws-5 FAILURE in 26m 27s
integration-community.aws-6 FAILURE in 26m 48s
integration-community.aws-7 FAILURE in 24m 43s
integration-community.aws-8 FAILURE in 25m 47s
integration-community.aws-9 FAILURE in 24m 38s
✔️ integration-community.aws-10 SUCCESS in 6m 37s
integration-community.aws-11 FAILURE in 28m 50s
Skipped 11 jobs

Copy link
Contributor

Build failed.
https://ansible.softwarefactory-project.io/zuul/buildset/ca7cffedbcb04b7099bd326806e84762

ansible-galaxy-importer FAILURE in 4m 10s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 29s
✔️ ansible-test-splitter SUCCESS in 4m 17s
integration-community.aws-1 TIMED_OUT in 1h 00m 20s
integration-community.aws-2 FAILURE in 30m 54s
integration-community.aws-3 FAILURE in 24m 33s
integration-community.aws-4 FAILURE in 25m 26s
integration-community.aws-5 FAILURE in 25m 04s
integration-community.aws-6 FAILURE in 31m 00s
integration-community.aws-7 FAILURE in 27m 15s
integration-community.aws-8 FAILURE in 23m 38s
integration-community.aws-9 FAILURE in 30m 30s
✔️ integration-community.aws-10 SUCCESS in 6m 20s
integration-community.aws-11 FAILURE in 25m 16s
Skipped 11 jobs

@beeankha beeankha force-pushed the refactor-exec-transport-command-method branch 3 times, most recently from 6ad3c3d to 599cb1a Compare February 25, 2025 22:09
Copy link
Contributor

Build failed.
https://ansible.softwarefactory-project.io/zuul/buildset/ce79518076784bdebfdb69270b1914b2

ansible-galaxy-importer FAILURE in 3m 54s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 15s
✔️ ansible-test-splitter SUCCESS in 4m 12s
integration-community.aws-1 FAILURE in 7m 52s
integration-community.aws-2 FAILURE in 29m 52s
integration-community.aws-3 FAILURE in 19m 08s
integration-community.aws-4 FAILURE in 6m 39s
integration-community.aws-5 FAILURE in 25m 23s
integration-community.aws-6 FAILURE in 20m 26s
integration-community.aws-7 FAILURE in 25m 44s
integration-community.aws-8 FAILURE in 24m 22s
integration-community.aws-9 FAILURE in 25m 14s
✔️ integration-community.aws-10 SUCCESS in 4m 40s
integration-community.aws-11 FAILURE in 27m 58s
Skipped 11 jobs

Copy link
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/fc9bb8d8029d4fc3b58d557f8dc5f3e6

✔️ ansible-galaxy-importer SUCCESS in 5m 50s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 55s
✔️ ansible-test-splitter SUCCESS in 4m 03s
✔️ integration-community.aws-1 SUCCESS in 23m 09s
✔️ integration-community.aws-2 SUCCESS in 13m 37s
✔️ integration-community.aws-3 SUCCESS in 15m 25s
✔️ integration-community.aws-4 SUCCESS in 15m 21s
✔️ integration-community.aws-5 SUCCESS in 25m 13s
✔️ integration-community.aws-6 SUCCESS in 18m 20s
✔️ integration-community.aws-7 SUCCESS in 14m 00s
✔️ integration-community.aws-8 SUCCESS in 13m 58s
✔️ integration-community.aws-9 SUCCESS in 17m 21s
✔️ integration-community.aws-10 SUCCESS in 5m 45s
✔️ integration-community.aws-11 SUCCESS in 13m 34s
Skipped 11 jobs

@beeankha beeankha removed the WIP Work in progress label Feb 26, 2025
@beeankha beeankha changed the title [WIP] Refactor Some Command-Related Methods in aws_ssm.py Refactor Some Command-Related Methods in aws_ssm.py Feb 26, 2025
Copy link
Contributor

@abikouo abikouo left a comment

Choose a reason for hiding this comment

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

There is no need to add a test skipped depending on the platform because the is_windows param is computed from the EC2 instance, not the source computer.
You can combine both tests into a single one.

Copy link
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/7fd64c5298f54185aa74dc74b89e4fa6

ansible-galaxy-importer FAILURE in 4m 36s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 13s
✔️ ansible-test-splitter SUCCESS in 3m 54s
✔️ integration-community.aws-1 SUCCESS in 23m 12s
✔️ integration-community.aws-2 SUCCESS in 17m 01s
✔️ integration-community.aws-3 SUCCESS in 15m 19s
✔️ integration-community.aws-4 SUCCESS in 16m 39s
✔️ integration-community.aws-5 SUCCESS in 13m 27s
✔️ integration-community.aws-6 SUCCESS in 14m 41s
✔️ integration-community.aws-7 SUCCESS in 14m 09s
✔️ integration-community.aws-8 SUCCESS in 14m 04s
✔️ integration-community.aws-9 SUCCESS in 13m 07s
✔️ integration-community.aws-10 SUCCESS in 4m 45s
✔️ integration-community.aws-11 SUCCESS in 14m 30s
Skipped 11 jobs

@beeankha beeankha force-pushed the refactor-exec-transport-command-method branch from c862d6d to 843b36b Compare February 27, 2025 01:53
Copy link
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/a4c125157ce14021bac1a5375030befe

✔️ ansible-galaxy-importer SUCCESS in 3m 16s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 21s
✔️ ansible-test-splitter SUCCESS in 3m 59s
✔️ integration-community.aws-1 SUCCESS in 22m 35s
✔️ integration-community.aws-2 SUCCESS in 13m 51s
✔️ integration-community.aws-3 SUCCESS in 13m 52s
✔️ integration-community.aws-4 SUCCESS in 15m 04s
✔️ integration-community.aws-5 SUCCESS in 13m 04s
✔️ integration-community.aws-6 SUCCESS in 15m 38s
✔️ integration-community.aws-7 SUCCESS in 14m 36s
✔️ integration-community.aws-8 SUCCESS in 15m 25s
✔️ integration-community.aws-9 SUCCESS in 13m 23s
✔️ integration-community.aws-10 SUCCESS in 4m 57s
✔️ integration-community.aws-11 SUCCESS in 15m 47s
Skipped 11 jobs

Copy link
Contributor

@abikouo abikouo left a comment

Choose a reason for hiding this comment

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

LGTM!!

Copy link

@brahmanim brahmanim left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -433,6 +436,17 @@ def filter_ansi(line: str, is_windows: bool) -> str:
return line


@dataclass
Copy link
Contributor

Choose a reason for hiding this comment

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

I would have used a typed dict instead of a dataclass, but it's ok.

Copy link
Contributor

Choose a reason for hiding this comment

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

We've seen both starting to appear, is this something we want to be opinionated about?

Copy link
Contributor Author

@beeankha beeankha Feb 27, 2025

Choose a reason for hiding this comment

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

I would have used a typed dict instead of a dataclass, but it's ok.

@alinabuzachis when we discussed some details prior to me making this PR, I totally misunderstood that particular point, so this was a miss on my part; I'm happy to update this to use TypedDict instead. I can update CommandResult to the following:

from typing import TypedDict

class CommandResult(TypedDict):
    """
    A custom dictionary containing the executed command results.
    """

    returncode: int
    stdout_combined: str
    stderr_combined: str

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/c69d21b090654bdb8d4af92977a12e3e

ansible-galaxy-importer FAILURE in 4m 54s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 20s
✔️ ansible-test-splitter SUCCESS in 3m 54s
✔️ integration-community.aws-1 SUCCESS in 25m 04s
✔️ integration-community.aws-2 SUCCESS in 14m 43s
✔️ integration-community.aws-3 SUCCESS in 15m 43s
✔️ integration-community.aws-4 SUCCESS in 15m 27s
✔️ integration-community.aws-5 SUCCESS in 14m 19s
✔️ integration-community.aws-6 SUCCESS in 17m 03s
✔️ integration-community.aws-7 SUCCESS in 15m 29s
✔️ integration-community.aws-8 SUCCESS in 17m 05s
✔️ integration-community.aws-9 SUCCESS in 14m 17s
✔️ integration-community.aws-10 SUCCESS in 8m 19s
✔️ integration-community.aws-11 SUCCESS in 16m 10s
Skipped 11 jobs

Comment on lines 294 to 307
if is_windows:
assert "command" in test_command_generation[0][0]
assert "method" in test_command_generation[0][1]
assert "headers" in test_command_generation[0][1]
assert "Invoke-WebRequest" in test_command_generation[0][1]["command"]
assert test_command_generation[0][1]["method"] == "put"
# Two command dictionaries are generated for Windows
assert len(test_command_generation[0]) == 2
else:
assert "method" in test_command_generation[0][2]
assert "headers" in test_command_generation[0][2]
assert "curl --request PUT -H" in test_command_generation[0][2]["command"]
assert test_command_generation[0][2]["method"] == "put"
# Three command dictionaries are generated on non-Windows systems
assert len(test_command_generation[0]) == 3
Copy link
Member

Choose a reason for hiding this comment

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

Not a blocking suggestion, but it seems like we ought to be able to do a more complete test against the contents of test_command_generation. Since you're mocking generate_presigned_url() I think the return value for _generate_commands() shouldn't vary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might be misunderstanding, but the command dictionaries that get generated contain different commands depending on whether the instances are Windows vs non-Windows, so I wanted to make sure that each variation of the commands were output as expected. The details like the URL and S3 path are the same; I do see some assertions I could pull out of the if/else block, though, so I'll update the test to reflect that.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, sorry, let me clarify. The tests currently test the structure of what's returned from _generate_command() and partially test the content. What I'm suggesting is that I think we should be able to test the full command that gets returned, rather than just testing that some portion of it contains a specific string. I would argue that if something were to change the command, it should break the test.

If I'm reading the code wrong and it's going to be difficult to test the full command, I think it's fine as is.

Copy link
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/87788c2f3dea42769ccc83020dbb90d7

ansible-galaxy-importer FAILURE in 4m 42s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 58s
✔️ ansible-test-splitter SUCCESS in 4m 49s
✔️ integration-community.aws-1 SUCCESS in 24m 12s
✔️ integration-community.aws-2 SUCCESS in 14m 33s
✔️ integration-community.aws-3 SUCCESS in 14m 12s
✔️ integration-community.aws-4 SUCCESS in 16m 07s
✔️ integration-community.aws-5 SUCCESS in 15m 18s
✔️ integration-community.aws-6 SUCCESS in 16m 08s
✔️ integration-community.aws-7 SUCCESS in 13m 41s
✔️ integration-community.aws-8 SUCCESS in 14m 31s
✔️ integration-community.aws-9 SUCCESS in 20m 28s
✔️ integration-community.aws-10 SUCCESS in 4m 55s
✔️ integration-community.aws-11 SUCCESS in 15m 58s
Skipped 11 jobs

@beeankha beeankha force-pushed the refactor-exec-transport-command-method branch from 183e819 to b4643a0 Compare February 27, 2025 21:22
Copy link
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/23291cf9a7dc412ebcd93db446a09663

✔️ ansible-galaxy-importer SUCCESS in 3m 17s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 10s
✔️ ansible-test-splitter SUCCESS in 4m 01s
✔️ integration-community.aws-1 SUCCESS in 23m 06s
✔️ integration-community.aws-2 SUCCESS in 15m 09s
✔️ integration-community.aws-3 SUCCESS in 14m 35s
✔️ integration-community.aws-4 SUCCESS in 14m 18s
✔️ integration-community.aws-5 SUCCESS in 15m 44s
✔️ integration-community.aws-6 SUCCESS in 16m 13s
✔️ integration-community.aws-7 SUCCESS in 15m 12s
✔️ integration-community.aws-8 SUCCESS in 16m 18s
✔️ integration-community.aws-9 SUCCESS in 15m 12s
✔️ integration-community.aws-10 SUCCESS in 5m 40s
✔️ integration-community.aws-11 SUCCESS in 19m 18s
Skipped 11 jobs

@beeankha beeankha added the mergeit Merge the PR (SoftwareFactory) label Mar 4, 2025
Copy link
Contributor

Build succeeded (gate pipeline).
https://ansible.softwarefactory-project.io/zuul/buildset/9bcbfd68dc2c41dfbcb06003d45e1a7a

ansible-galaxy-importer FAILURE in 4m 34s (non-voting)
✔️ build-ansible-collection SUCCESS in 10m 37s
✔️ ansible-test-splitter SUCCESS in 3m 58s
✔️ integration-community.aws-1 SUCCESS in 22m 32s
✔️ integration-community.aws-2 SUCCESS in 15m 44s
✔️ integration-community.aws-3 SUCCESS in 16m 00s
✔️ integration-community.aws-4 SUCCESS in 15m 36s
✔️ integration-community.aws-5 SUCCESS in 14m 40s
✔️ integration-community.aws-6 SUCCESS in 14m 40s
✔️ integration-community.aws-7 SUCCESS in 14m 50s
✔️ integration-community.aws-8 SUCCESS in 15m 09s
✔️ integration-community.aws-9 SUCCESS in 13m 17s
✔️ integration-community.aws-10 SUCCESS in 4m 13s
✔️ integration-community.aws-11 SUCCESS in 15m 32s
Skipped 11 jobs

@softwarefactory-project-zuul softwarefactory-project-zuul bot merged commit 3111215 into ansible-collections:main Mar 4, 2025
86 of 87 checks passed
Copy link

patchback bot commented Mar 4, 2025

Backport to stable-9: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-9/311121522ef21f2126e7f0b42121df0092db090c/pr-2248

Backported as #2261

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Mar 4, 2025
SUMMARY

This work is part of the currently ongoing AWS SSM Connection Refactoring & Plugin Promotion effort and related to ansible-collections/amazon.aws#2394.
Fixes ACA-2095

ISSUE TYPE

Feature Pull Request

TASK LIST

 The _exec_transport_commands method is refactored to accept a single structured object (a tuple containing lists of command dictionaries) that contains the necessary command data.
 Create a custom dataclass to hold command output.
 Create a custom dataclass TypedDict to hold command results.
 The _generate_commands function returns a list of typed dictionaries with clear metadata (command strings, method, headers).
 _exec_transport_commands accepts a list of typed dictionaries as an argument and returns the custom CommandResults object.
 Python type hints are added to the method signature and the structured object to improve readability, facilitate static analysis, and clearly define the expected structure and data types.
 Docstrings should be added to any refactored methods and the structured object (if custom), clarifying the purpose, structure, inputs, outputs, and any edge cases or special handling (if applicable).
 Unit tests have been created to ensure that the refactored _generate_commands method outputs the expected structured command object.
 Add a changelog file.

Reviewed-by: Bikouo Aubin
Reviewed-by: Rahmanim Benny <[email protected]>
Reviewed-by: Alina Buzachis
Reviewed-by: GomathiselviS <[email protected]>
Reviewed-by: Mark Chappell
Reviewed-by: Bianca Henderson <[email protected]>
Reviewed-by: Mike Graves <[email protected]>
(cherry picked from commit 3111215)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-9 mergeit Merge the PR (SoftwareFactory)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants