-
Notifications
You must be signed in to change notification settings - Fork 1
feat(resources): add workspace resources and workspace run tasks func… #56
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
Open
KshitijaChoudhari
wants to merge
10
commits into
next-0.1.1
Choose a base branch
from
kshitija/WorkspaceRunTasksAndWorkspaceResources
base: next-0.1.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0255bf0
feat(resources): add workspace resources and workspace run tasks func…
KshitijaChoudhari 34f0fb1
Fix code formatting issues
KshitijaChoudhari af3b5f8
Fix Stage enum values and remove unnecessary conversions
KshitijaChoudhari 5de6bc7
Refactored workspace run tasks and resources
KshitijaChoudhari 8bee515
Merge branch 'next-0.1.1' into kshitija/WorkspaceRunTasksAndWorkspace…
KshitijaChoudhari b8ee49f
Pr review changes - workspace run tasks and resources
KshitijaChoudhari e145ff7
Apply ruff formatting and linting fixes
KshitijaChoudhari d764a83
Replace dictionary with RunTaskRelationship model in WorkspaceRunTask…
KshitijaChoudhari bde1775
Replace RunTaskRelationship wrapper with direct RunTask object reference
KshitijaChoudhari 1190a02
Remove duplicate examples from workspace_run_tasks.py epilog
KshitijaChoudhari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,136 @@ | ||
| #!/usr/bin/env python3 | ||
| """Example script for working with workspace resources in Terraform Enterprise. | ||
| This script demonstrates how to list resources within a workspace. | ||
| """ | ||
|
|
||
| import argparse | ||
| import sys | ||
|
|
||
| from pytfe import TFEClient | ||
| from pytfe.models.workspace_resource import WorkspaceResourceListOptions | ||
KshitijaChoudhari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def list_workspace_resources( | ||
| client: TFEClient, | ||
| workspace_id: str, | ||
| page_number: int | None = None, | ||
| page_size: int | None = None, | ||
| ) -> None: | ||
| """List all resources in a workspace.""" | ||
| try: | ||
| print(f"Listing resources for workspace: {workspace_id}") | ||
|
|
||
| # Prepare list options | ||
| options = None | ||
| if page_number or page_size: | ||
| options = WorkspaceResourceListOptions() | ||
| if page_number: | ||
| options.page_number = page_number | ||
| if page_size: | ||
| options.page_size = page_size | ||
|
|
||
| # List workspace resources | ||
| resources_list = client.workspace_resources.list(workspace_id, options) | ||
|
|
||
| if not resources_list.data: | ||
| print("No resources found in this workspace.") | ||
| return | ||
|
|
||
| print(f"\nFound {len(resources_list.data)} resource(s):") | ||
| print("-" * 80) | ||
|
|
||
| for resource in resources_list.data: | ||
| print(f"ID: {resource.id}") | ||
| print(f"Address: {resource.address}") | ||
| print(f"Name: {resource.name}") | ||
| print(f"Module: {resource.module}") | ||
| print(f"Provider: {resource.provider}") | ||
| print(f"Provider Type: {resource.provider_type}") | ||
| print(f"Created At: {resource.created_at}") | ||
| print(f"Updated At: {resource.updated_at}") | ||
| print(f"Modified By State Version: {resource.modified_by_state_version_id}") | ||
| if resource.name_index: | ||
| print(f"Name Index: {resource.name_index}") | ||
| print("-" * 80) | ||
|
|
||
| # Show pagination info if available | ||
| if resources_list.pagination: | ||
| print("\nPagination Info:") | ||
| print(f" Current Page: {resources_list.pagination.current_page}") | ||
| print(f" Total Pages: {resources_list.pagination.total_pages}") | ||
| print(f" Total Count: {resources_list.pagination.total_count}") | ||
| if ( | ||
| hasattr(resources_list.pagination, "page_size") | ||
| and resources_list.pagination.page_size | ||
| ): | ||
| print(f" Page Size: {resources_list.pagination.page_size}") | ||
| if resources_list.pagination.next_page: | ||
| print(f" Next Page: {resources_list.pagination.next_page}") | ||
| if resources_list.pagination.previous_page: | ||
| print(f" Previous Page: {resources_list.pagination.previous_page}") | ||
|
|
||
| except Exception as e: | ||
| print(f"Error listing workspace resources: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def main(): | ||
| """Main function to handle command line arguments and execute operations.""" | ||
| parser = argparse.ArgumentParser( | ||
| description="Manage workspace resources in Terraform Enterprise", | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| epilog=""" | ||
| Examples: | ||
| # List all resources in a workspace | ||
| python workspace_resources.py list ws-abc123 | ||
| # List with pagination | ||
| python workspace_resources.py list ws-abc123 --page-number 2 --page-size 50 | ||
| Environment variables: | ||
| TFE_TOKEN: Your Terraform Enterprise API token | ||
| TFE_URL: Your Terraform Enterprise URL (default: https://app.terraform.io) | ||
| TFE_ORG: Your Terraform Enterprise organization name | ||
| """, | ||
| ) | ||
|
|
||
| subparsers = parser.add_subparsers(dest="command", help="Available commands") | ||
|
|
||
| # List command | ||
| list_parser = subparsers.add_parser("list", help="List workspace resources") | ||
| list_parser.add_argument("workspace_id", help="ID of the workspace") | ||
| list_parser.add_argument( | ||
| "--page-number", type=int, help="Page number for pagination" | ||
| ) | ||
| list_parser.add_argument("--page-size", type=int, help="Page size for pagination") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not args.command: | ||
| parser.print_help() | ||
| sys.exit(1) | ||
|
|
||
| # Initialize TFE client | ||
| try: | ||
| client = TFEClient() | ||
| except Exception as e: | ||
| print(f"Error initializing TFE client: {e}", file=sys.stderr) | ||
| print( | ||
| "Make sure TFE_TOKEN and TFE_URL environment variables are set.", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| # Execute the requested command | ||
| if args.command == "list": | ||
| list_workspace_resources( | ||
| client, | ||
| args.workspace_id, | ||
| args.page_number, | ||
| args.page_size, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.