feat(helpers): add droplet actions helpers with unit tests #542
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.
Pull Request: Droplet Actions Helpers
📋 Description
This PR introduces a new
helpersmodule to thepydopackage that provides intuitive, well-documented wrapper functions for common Droplet Actions operations. These helpers abstract away the complexity of manually constructing request bodies and navigating nested response structures, making it significantly easier for users to perform common droplet operations.🎯 Motivation & Context
Problem
Currently, users interacting with droplet actions need to:
actionobject from response wrappersExample of current approach:
Solution
This PR provides convenience helpers that:
Example with helpers:
🆕 What's New
Helper Functions (11 Total)
All helpers are exposed at the package root for convenient importing:
1.
perform_action(client, droplet_id, action_type, **extra)Generic action executor - Handles any droplet action type
Features:
actionfrom response)2-11. Convenience Wrappers
power_on(client, droplet_id)power_off(client, droplet_id)reboot(client, droplet_id)shutdown(client, droplet_id)power_cycle(client, droplet_id)snapshot(client, droplet_id, name=None)name: Optional snapshot nameresize(client, droplet_id, size, disk=False)size: Size slug (required)disk: Permanent disk resizerename(client, droplet_id, name)name: New droplet namerebuild(client, droplet_id, image)image: Image ID or slugpassword_reset(client, droplet_id)Key Features
🎁 Response Normalization
Responses are automatically unwrapped to return the
actiondict directly:🛡️ Enhanced Error Handling
All exceptions are wrapped with contextual information:
📦 Package Root Exports
Import directly from
pydowithout navigating submodules:📝 Comprehensive Documentation
Every function includes:
🔧 Type Safety
Full type annotations for better IDE autocompletion and type checking:
📁 Files Changed
New Files (3)
src/pydo/helpers/__init__.py(29 lines)src/pydo/helpers/droplet_actions.py(286 lines)perform_actionfunctiontests/mocked/test_helpers_droplet_actions.py(323 lines)FakeClientandFakeDropletActionsmocksModified Files (1)
src/pydo/__init__.py__all__for package root accessTotal Changes: 4 files changed, 664 insertions(+)
✅ Testing
Unit Test Coverage
19 test cases covering all functionality:
Test Results
No Credentials Required
All tests use mocked clients - no
DIGITALOCEAN_TOKENneeded for local testing:Running Tests Locally
🎨 Code Quality
Formatting & Linting ✅
black --checkpasses)Code Style
📚 Usage Examples
Basic Operations
Operations with Parameters
Error Handling
Using the Generic Function
🔄 Backward Compatibility
✅ No Breaking Changes
This PR is purely additive. All existing functionality remains unchanged:
client.droplet_actions.post()still worksMigration Path
Users can adopt the helpers gradually:
🚀 Benefits
For End Users
For the Project
📋 Checklist
🔮 Future Enhancements
This PR establishes a pattern that can be extended to other resources:
pydo.helpers.volume_actions- Helpers for volume operationspydo.helpers.image_actions- Helpers for image operationspydo.helpers.kubernetes_actions- Helpers for Kubernetes cluster operationspydo.helpers.load_balancer_helpers- Helpers for load balancer management📝 Additional Notes
Design Decisions
Why separate helpers module?
Why normalize responses?
Why expose at package root?
dir(pydo))Integration with CI/CD
🙏 Review Focus Areas
Ready for review! This PR adds significant value to the pydo SDK by making common operations more accessible and developer-friendly. 🚀