feat: add workspace resources functionality #58
Merged
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.
Add comprehensive workspace resources service with list operations and complete data models. This implementation provides read-only access to workspace resources through the Terraform Enterprise API, enabling users to inspect infrastructure resources managed by their workspaces.
Key Features:
Complete workspace resources service with list operations
Robust data models with Pydantic validation
Pagination support with comprehensive filtering options
Working example application with CLI interface
11 comprehensive unit tests with 100% coverage
Full backward compatibility maintained
Description:
Change 1: Workspace Resources Implementation
Added complete WorkspaceResourcesService with list operations
Implemented proper URL encoding and parameter handling for workspace IDs
Added pagination support (page_number, page_size) following existing patterns
Created robust data models with Pydantic validation and proper field handling
Change 2: Data Models
WorkspaceResource: Complete model representing a resource in workspace state
Fields: id, address, name, module, provider, provider_type, created_at, updated_at, modified_by_state_version_id, name_index
Full Pydantic validation with proper datetime parsing
WorkspaceResourceListOptions: Pagination options for listing resources
Supports page_number and page_size parameters
Change 3: Client Integration
Updated TFEClient to include workspace_resources service
Added proper service initialization in client constructor
Maintained consistent API patterns with existing services
Change 4: Comprehensive Testing Suite
Added 11 new unit tests covering all functionality:
List operations with and without pagination
Empty response handling
Invalid workspace ID error handling
URL encoding validation
Malformed response handling
API error scenarios
Model creation and validation
Maintained 100% test coverage for new functionality
Zero regressions in existing 366 tests (377 total passing)
Change 5: Example Application
Created workspace_resources.py CLI tool with flag-based interface
Demonstrates real-world usage with comprehensive examples
Features:
List all resources in a workspace
Pagination support
Detailed resource metadata display
Environment variable configuration (TFE_TOKEN, TFE_URL, TFE_ORG)
Implementation Details
Architecture Changes
Service Layer: Added WorkspaceResourcesService following existing patterns in resources
Model Layer: Created comprehensive Pydantic models in workspace_resource.py
HTTP Transport: Enhanced URL construction for workspace-specific endpoints (/workspaces/:workspace_id/resources)
Type Safety: Full type hints and mypy compliance
Key Files Changed
workspace_resources.py - New service implementation
workspace_resource.py - New data models
init.py - Added model exports
client.py - Added service integration
workspace_resources.py - New example CLI tool
test_workspace_resources.py - New comprehensive test suite
Testing Plan
Test Results
377 total tests passing (100% success rate)
11 new tests added for workspace resources
0.59 seconds execution time - excellent performance
Zero regressions - all existing functionality maintained
Integration Testing
Real API Testing: Successfully tested against live HCP Terraform instance
Data Handling: Verified proper parsing of real API responses with 4 resources
Error Scenarios: Comprehensive error handling validation
Code Quality
Formatting: All files properly formatted (Ruff)
Linting: Zero linting issues (Ruff + MyPy)
Type Safety: All 85 source files pass type checking
Standards: Full compliance with project coding standards
Example Application Results
Successfully lists and displays 4 real resources from test workspace:
External Links
API Documentation:
https://developer.hashicorp.com/terraform/cloud-docs/api-docs/workspaces
Output from Tests:
Unit Test Results:
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_success PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_with_options PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_empty PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_invalid_workspace_id PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_url_encoding PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_malformed_response PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourcesService::test_list_workspace_resources_api_error PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourceModel::test_workspace_resource_creation PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourceModel::test_workspace_resource_optional_fields PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourceListOptions::test_workspace_resource_list_options_creation PASSED
tests/units/test_workspace_resources.py::TestWorkspaceResourceListOptions::test_workspace_resource_list_options_defaults PASSED
Example Application Output:
Listing resources for workspace: ws-xA6BjDL5YQMza8aU
Found 4 resource(s):
ID: wsr-vBwCqF9URDcLMxUY
Address: random_integer.demo_number
Name: demo_number
Module:
Provider: hashicorp/random
Provider Type: random_integer
Created At: 2024-12-10T19:40:26.827000+00:00
Updated At: 2024-12-10T19:40:26.827000+00:00
Modified By State Version: sv-C5SkdHa9Y6vbw4AC
ID: wsr-xxxxx
Address: random_pet.demo_pet
Name: demo_pet
Module:
Provider: hashicorp/random
Provider Type: random_pet
Created At:
Updated At:
Modified By State Version: sv-xxxxxxxx
ID: wsr-xxxxx
Address: random_string.demo1
Name: demo1
Module:
Provider: hashicorp/random
Provider Type: random_string
Created At:
Updated At:
Modified By State Version: sv-xxxxx
ID: wsr-xxxxx
Address: random_string.demo2
Name: demo2
Module:
Provider: hashicorp/random
Provider Type: random_string
Created At:
Updated At:
Modified By State Version: sv-xxxxx
Rollback Plan:
If a change needs to be reverted, we will roll out an update to the code within 7 days.
-->
Changes to Security Controls:
None
PCI review checklist
I have documented a clear reason for, and description of, the change I am making.
If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.
If applicable, I've documented the impact of any changes to security controls.
Examples of changes to security controls include using new access control methods, adding or removing logging pipelines, etc.
If you have any questions, please contact your direct supervisor, GRC (#team-grc), or the PCI working group (#proj-pci-reboot). You can also find more information at PCI Compliance.