-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
130 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """ | ||
| AWS utility functions for resource management. | ||
| """ | ||
| import boto3 | ||
| import config | ||
| from logging_utils import setup_logging | ||
|
|
||
| logger = setup_logging() | ||
|
|
||
| def create_boto3_client(service, credentials, region): | ||
| """ | ||
| Create a boto3 client for a specific service using the provided credentials. | ||
| Args: | ||
| service (str): AWS service name (e.g., 'ec2', 'rds') | ||
| credentials (dict): AWS credentials dict with access key, secret key, and token | ||
| region (str): AWS region name | ||
| Returns: | ||
| boto3.client: Configured boto3 client | ||
| """ | ||
| return boto3.client( | ||
| service, | ||
| aws_access_key_id=credentials['AccessKeyId'], | ||
| aws_secret_access_key=credentials['SecretAccessKey'], | ||
| aws_session_token=credentials['SessionToken'], | ||
| region_name=region | ||
| ) | ||
|
|
||
| def get_available_regions(): | ||
| """ | ||
| Get a list of all available AWS regions. | ||
| Returns: | ||
| list: List of region names | ||
| """ | ||
| try: | ||
| ec2_client = boto3.client('ec2') | ||
| response = ec2_client.describe_regions() | ||
| return [region['RegionName'] for region in response['Regions']] | ||
| except Exception as e: | ||
| logger.error(f"Error getting available regions: {e}") | ||
| # Return a default list of common regions as fallback | ||
| return ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2'] | ||
|
|
||
| def assume_role(account_id): | ||
| """ | ||
| Assume the OrganizationAccountAccessRole in the specified account. | ||
| Args: | ||
| account_id (str): AWS account ID to assume role in | ||
| Returns: | ||
| dict: Credentials dictionary or None if failed | ||
| """ | ||
| try: | ||
| role_arn = f"arn:aws:iam::{account_id}:role/{config.ASSUME_ROLE_NAME}" | ||
| sts_client = boto3.client('sts') | ||
|
|
||
| response = sts_client.assume_role( | ||
| RoleArn=role_arn, | ||
| RoleSessionName="ResourceManagementSession" | ||
| ) | ||
|
|
||
| return response['Credentials'] | ||
| except Exception as e: | ||
| logger.error(f"Error assuming role in account {account_id}: {e}") | ||
| return None | ||
|
|
||
| def get_organization_accounts(exclude_accounts=None): | ||
| """ | ||
| Get a list of accounts in the AWS organization. | ||
| Args: | ||
| exclude_accounts (list): List of account IDs to exclude | ||
| Returns: | ||
| list: List of account dicts with id and name | ||
| """ | ||
| if exclude_accounts is None: | ||
| exclude_accounts = [] | ||
|
|
||
| try: | ||
| org_client = boto3.client('organizations') | ||
| accounts = [] | ||
|
|
||
| # Get all accounts in the organization | ||
| paginator = org_client.get_paginator('list_accounts') | ||
| for page in paginator.paginate(): | ||
| for account in page['Accounts']: | ||
| # Skip suspended accounts and accounts in exclude list | ||
| if account['Status'] == 'ACTIVE' and account['Id'] not in exclude_accounts: | ||
| accounts.append({ | ||
| 'id': account['Id'], | ||
| 'name': account['Name'] | ||
| }) | ||
|
|
||
| return accounts | ||
| except Exception as e: | ||
| logger.error(f"Error getting organization accounts: {e}") | ||
| return [] |
6 changes: 3 additions & 3 deletions
6
...l-resource-actions/discovery/discovery.py → ...n-tools/gfl-resource-actions/discovery.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+557 Bytes
local-app/python-tools/gfl-resource-actions/managers/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+2.85 KB
local-app/python-tools/gfl-resource-actions/managers/__pycache__/base.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+5.09 KB
local-app/python-tools/gfl-resource-actions/managers/__pycache__/ec2.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+9.34 KB
local-app/python-tools/gfl-resource-actions/managers/__pycache__/eks.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+5.26 KB
local-app/python-tools/gfl-resource-actions/managers/__pycache__/emr.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+3.8 KB
local-app/python-tools/gfl-resource-actions/managers/__pycache__/rds.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters