Skip to content

Commit f1452f9

Browse files
committed
Enhance DigitalOcean droplet function to support power on/off actions and update schemas accordingly
1 parent 6ef4042 commit f1452f9

4 files changed

Lines changed: 82 additions & 9 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ref: https://docs.digitalocean.com/products/genai-platform/concepts/function-instructions/
22

3-
Call this function when the user asks about their DigitalOcean droplets, virtual machines, instances, or servers. Use this function to retrieve information about one or more droplets in a DigitalOcean account.
3+
Call this function when the user asks about their DigitalOcean droplets, virtual machines, instances, or servers. Use this function to retrieve information about one or more droplets in a DigitalOcean account. You can also use this function to power on or power off a specific droplet when the user asks to start, turn on, boot, stop, turn off, or shut down a droplet.

genai-examples/input_schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
"droplet_id": {
88
"type": "string",
99
"required": false,
10-
"description": "Specific droplet ID to retrieve information for. If not provided, returns a list of droplets"
10+
"description": "Specific droplet ID to retrieve information for or to perform an action on. If not provided, returns a list of droplets"
1111
},
1212
"tag": {
1313
"type": "string",
1414
"required": false,
1515
"description": "Filter droplets by tag"
16+
},
17+
"action": {
18+
"type": "string",
19+
"required": false,
20+
"description": "Action to perform on a droplet. Supported values: 'power_on', 'power_off'. Requires droplet_id to be specified"
1621
}
1722
}

genai-examples/output_schema.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@
1010
},
1111
"droplets": {
1212
"type": "string",
13-
"description": "JSON string containing list of droplet information"
13+
"description": "JSON string containing list of droplet information",
14+
"required": false
1415
},
1516
"count": {
1617
"type": "number",
17-
"description": "Total number of droplets returned"
18+
"description": "Total number of droplets returned",
19+
"required": false
20+
},
21+
"action": {
22+
"type": "object",
23+
"description": "Response from the droplet action API (when power_on or power_off is performed)",
24+
"required": false
25+
},
26+
"message": {
27+
"type": "string",
28+
"description": "Status message for action operations",
29+
"required": false
1830
}
1931
}

packages/do-api/api/__main__.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44

55
def main(args):
66
"""
7-
Retrieve droplet information from the DigitalOcean API.
7+
Retrieve or control droplet information from the DigitalOcean API.
88
99
Args:
1010
args (dict): Input parameters
1111
- api_token: DigitalOcean API token
12-
- droplet_id: Optional specific droplet ID to retrieve
12+
- droplet_id: Optional specific droplet ID to retrieve or control
1313
- tag: Optional tag to filter droplets
1414
- limit: Optional limit for the number of droplets to return (default: 10)
15+
- action: Optional action to perform on droplet (power_on, power_off)
1516
1617
Returns:
17-
dict: API response containing droplet information in the 'body' key
18+
dict: API response containing droplet information or action status in the 'body' key
1819
"""
1920
# Extract parameters
2021
api_token = os.environ.get('DO_API_TOKEN') # Get token from environment variable
2122
droplet_id = args.get('droplet_id')
2223
tag = args.get('tag')
2324
limit = args.get('limit', 10)
25+
action = args.get('action')
2426

2527
# Validate environment variable
2628
if not api_token:
@@ -35,8 +37,41 @@ def main(args):
3537
# Initialize DigitalOcean client
3638
client = Client(token=api_token)
3739

40+
# Check if we need to perform an action on a droplet
41+
if droplet_id and action:
42+
droplet_id = int(droplet_id) # Convert to integer as the API expects
43+
44+
if action == 'power_on':
45+
# Call the post method with the action type
46+
response = client.droplet_actions.post(droplet_id=droplet_id, body={"type": "power_on"})
47+
return {
48+
'body': {
49+
'action': response,
50+
'status': 'success',
51+
'message': f'Power on initiated for droplet {droplet_id}'
52+
}
53+
}
54+
elif action == 'power_off':
55+
# Call the post method with the action type
56+
response = client.droplet_actions.post(droplet_id=droplet_id, body={"type": "power_off"})
57+
return {
58+
'body': {
59+
'action': response,
60+
'status': 'success',
61+
'message': f'Power off initiated for droplet {droplet_id}'
62+
}
63+
}
64+
else:
65+
return {
66+
'body': {
67+
'error': f'Unsupported action: {action}. Supported actions: power_on, power_off',
68+
'status': 'error'
69+
}
70+
}
71+
3872
# Get specific droplet or list of droplets
3973
if droplet_id:
74+
droplet_id = int(droplet_id) # Convert to integer as the API expects
4075
response = client.droplets.get(droplet_id=droplet_id)
4176
droplets = [response['droplet']] if 'droplet' in response else []
4277
else:
@@ -90,9 +125,30 @@ def main(args):
90125

91126
# For local testing
92127
if __name__ == "__main__":
93-
test_args = {
94-
'api_token': 'your_test_token',
128+
import sys
129+
130+
# Example 1: List droplets (default behavior)
131+
test_args_list = {
95132
'limit': 5
96133
}
134+
135+
# Example 2: Power on a specific droplet
136+
test_args_power_on = {
137+
'droplet_id': 12345, # Replace with actual droplet ID
138+
'action': 'power_on'
139+
}
140+
141+
# Example 3: Power off a specific droplet
142+
test_args_power_off = {
143+
'droplet_id': 12345, # Replace with actual droplet ID
144+
'action': 'power_off'
145+
}
146+
147+
# Choose which example to run
148+
# Uncomment the test_args you want to use
149+
test_args = test_args_list
150+
# test_args = test_args_power_on
151+
# test_args = test_args_power_off
152+
97153
result = main(test_args)
98154
print(json.dumps(result, indent=2))

0 commit comments

Comments
 (0)