Commit 50ff268
feat: add new pre_hook_with_context method to Command (#35)
This feature adds a new lifecycle method to the Command base class:
def pre_hook_with_context(self, context):
pass
The value the context parameter comes from the new context parameter on AccountRunner.run:
def run(self, cmd, accounts, key=lambda x: x, context=None):
pass
This allows users of the awsrun library to pass a runtime context into commands.
As the primary user of the library, the awsrun CLI command uses this new feature to pass in references to the account loader and session provider plug-ins to a command. It allows command authors to lookup metadata associated with other accounts and obtaining sessions for other accounts while processing accounts. Here is an example:
"""Identify shared VPCs from our main account 999999999999 that have
been shared with other accounts owned by a different Business Unit."""
import io
from awsrun.cli import Context
from awsrun.runner import RegionalCommand
class CLICommand(RegionalCommand):
"""Display VPCs configured in accounts."""
def pre_hook_with_context(self, context: Context):
acct_id = "999999999999"
# Let's get the account object for our main account. Assume the
# account loader plug-in used annotates that object with one
# metadata value called "BU" representing the business unit.
acct = context.account_loader.accounts(acct_ids=[acct_id])[0]
# Let's get a session object for our main account so we can get
# a list of VPCs from it.
session = context.session_provider.session(acct_id)
vpc_ids = []
for region in self.regions:
ec2 = session.resource("ec2", region_name=region)
vpc_ids.extend(vpc.id for vpc in ec2.vpcs.all())
# Save these for use later in regional_execute()
self.important_acct = acct
self.important_vpc_ids = vpc_ids
def regional_execute(self, session, acct, region):
out = io.StringIO()
ec2 = session.resource("ec2", region_name=region)
# Check each VPC in this current account to see if it is in the
# list of VPCs from our main account. And, then check if the BU
# for this current account is the same as the BU for our main
# account. Report when they differ.
for vpc in ec2.vpcs.all():
if vpc.id in self.important_vpc_ids and acct.BU != self.important_acct.BU:
print(
"{acct}/{region}: Shared VPC {vpc.id} owned by other Business Unit!",
file=out,
)
return out.getvalue()
The above example demonstrates how you can use these plug-ins in your
commands to inspect metadata of other accounts and to obtain sessions for
any account. This can be useful if you need to access another account while
processing the current account. For example, perhaps you want to accept a
VPC peering request after initiating it.
---------
Co-authored-by: Kazmier, Peter <peter.kazmier@fmr.com>1 parent 415a351 commit 50ff268
3 files changed
+157
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1078 | 1078 | | |
1079 | 1079 | | |
1080 | 1080 | | |
1081 | | - | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
1082 | 1087 | | |
1083 | 1088 | | |
1084 | 1089 | | |
| |||
1179 | 1184 | | |
1180 | 1185 | | |
1181 | 1186 | | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
1182 | 1201 | | |
1183 | 1202 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
487 | 487 | | |
488 | 488 | | |
489 | 489 | | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
490 | 556 | | |
491 | 557 | | |
492 | 558 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
100 | 130 | | |
101 | 131 | | |
102 | 132 | | |
| |||
173 | 203 | | |
174 | 204 | | |
175 | 205 | | |
| 206 | + | |
176 | 207 | | |
177 | 208 | | |
178 | 209 | | |
| |||
307 | 338 | | |
308 | 339 | | |
309 | 340 | | |
310 | | - | |
311 | | - | |
| 341 | + | |
| 342 | + | |
312 | 343 | | |
313 | 344 | | |
314 | | - | |
315 | | - | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
316 | 364 | | |
317 | 365 | | |
318 | 366 | | |
| |||
844 | 892 | | |
845 | 893 | | |
846 | 894 | | |
847 | | - | |
| 895 | + | |
848 | 896 | | |
849 | 897 | | |
850 | 898 | | |
851 | 899 | | |
852 | 900 | | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
859 | 912 | | |
860 | 913 | | |
861 | 914 | | |
| |||
887 | 940 | | |
888 | 941 | | |
889 | 942 | | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
890 | 949 | | |
891 | 950 | | |
892 | 951 | | |
| |||
899 | 958 | | |
900 | 959 | | |
901 | 960 | | |
902 | | - | |
| 961 | + | |
903 | 962 | | |
904 | 963 | | |
905 | 964 | | |
| |||
0 commit comments