Skip to content

Commit 7e55c71

Browse files
authored
Merge pull request #54 from TheAntColony/master
Live tests, added missing features
2 parents 82e6034 + ef0db65 commit 7e55c71

38 files changed

+1915
-140
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __pycache__/
55

66
# C extensions
77
*.so
8+
.idea/
89

910
# Distribution / packaging
1011
.Python
@@ -23,6 +24,7 @@ var/
2324
*.egg-info/
2425
.installed.cfg
2526
*.egg
27+
enable/
2628

2729
# PyInstaller
2830
# Usually these files are written by a python script from a template

README.md

Lines changed: 180 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
Packet
2-
======
1+
# Packet
32

43
A Python client for the Packet API.
54

65
![travis build status badge](https://travis-ci.org/packethost/packet-python.svg?branch=master "Build Status")
76

8-
Installation
9-
------------
7+
## Table of Contents
8+
9+
* [Installation](#installation)
10+
* [Documentation](#documentation)
11+
* [Authentication](#authentication)
12+
* [Examples](#examples)
13+
* [List Projects](#list-projects)
14+
* [List Plans](#list-plans)
15+
* [Creating a Device](#creating-a-device)
16+
* [Checking the Status and Rebooting a Device](#checking-the-status-and-rebooting-a-device)
17+
* [Listing all Devices Limiting to 50 per Page](#listing-all-devices-limiting-to-50-per-page)
18+
* [Updating a Device](#updating-a-device)
19+
* [Deleting a Device](#deleting-a-device)
20+
* [Creating a Device Batch](#creating-a-device-batch)
21+
* [Creating a Volume](#creating-a-volume)
22+
* [Attaching and Detaching a Volume](#attaching-and-detaching-a-volume)
23+
* [Creating and Restoring a Volume Snapshot](#creating-and-restoring-a-volume-snapshot)
24+
* [Listing Project IP Addresses](#listing-project-ip-addresses)
25+
* [Creating a Project for an Organization](#creating-a-project-for-an-organization)
26+
* [Creating a VLAN](#creating-a-vlan)
27+
* [Contributing](#contributing)
28+
* [Copyright](#copyright)
29+
* [Changes](#changes)
30+
31+
## Installation
32+
1033
The packet python api library can be installed using pip:
1134

1235
pip install packet-python
@@ -15,14 +38,23 @@ Package information available here:
1538

1639
https://pypi.python.org/pypi/packet-python
1740

18-
Documentation
19-
-------------
41+
## Documentation
42+
2043
Full Packet API documenation is available here:
2144
[https://www.packet.net/developers/api/](https://www.packet.net/developers/api/)
2245

23-
Examples
24-
--------
25-
### List projects
46+
## Authentication
47+
48+
Provide your credentials when instantiating client:
49+
50+
```python
51+
import packet
52+
manager = packet.Manager(auth_token="yourapiauthtoken")
53+
```
54+
55+
## Examples
56+
57+
### List Projects
2658

2759
```python
2860
import packet
@@ -33,7 +65,7 @@ for project in projects:
3365
print(project)
3466
```
3567

36-
### List plans
68+
### List Plans
3769

3870
```python
3971
import packet
@@ -55,11 +87,11 @@ manager = packet.Manager(auth_token="yourapiauthtoken")
5587
device = manager.create_device(project_id='project-id',
5688
hostname='node-name-of-your-choice',
5789
plan='baremetal_1', facility='ewr1',
58-
operating_system='ubuntu_14_04')
90+
operating_system='ubuntu_18_04')
5991
print(device)
6092
```
6193

62-
### Checking the status and rebooting a Device
94+
### Checking the Status and Rebooting a Device
6395

6496
```python
6597
import packet
@@ -70,7 +102,7 @@ print(device.state)
70102
device.reboot()
71103
```
72104

73-
### Listing all devices, limiting to 50 per page
105+
### Listing all Devices Limiting to 50 per Page
74106

75107
_Packet API defaults to a limit of 10 per page_
76108

@@ -84,8 +116,138 @@ devices = manager.list_devices(project_id='project_id', params = params)
84116
print(devices)
85117
```
86118

87-
Contributing
88-
------------
119+
### Updating a Device
120+
121+
```python
122+
import packet
123+
manager = packet.Manager(auth_token="yourapiauthtoken")
124+
125+
device = manager.get_device('device-id')
126+
device.hostname = "test02"
127+
device.description = "new description"
128+
129+
device.update()
130+
```
131+
132+
### Deleting a Device
133+
134+
```python
135+
import packet
136+
manager = packet.Manager(auth_token="yourapiauthtoken")
137+
138+
device = manager.get_device('device-id')
139+
device.delete()
140+
```
141+
142+
### Creating a Device Batch
143+
144+
```python
145+
import packet
146+
manager = packet.Manager(auth_token="yourapiauthtoken")
147+
148+
batch01 = packet.DeviceBatch({
149+
"hostname": "batch01",
150+
"quantity": 2,
151+
"facility": "ams1",
152+
"operating_system": "centos_7",
153+
"plan": "baremetal_0",
154+
})
155+
156+
device_batch = manager.create_batch(project_id="project_id", params=[batch01])
157+
print(device_batch)
158+
```
159+
160+
### Creating a Volume
161+
162+
```python
163+
import packet
164+
manager = packet.Manager(auth_token="yourapiauthtoken")
165+
166+
volume = manager.create_volume(project_id="project-id",
167+
description="volume description",
168+
plan="storage_1",
169+
size="100",
170+
facility="ewr1",
171+
snapshot_count=7,
172+
snapshot_frequency="1day")
173+
print(volume)
174+
```
175+
176+
### Attaching and Detaching a Volume
177+
178+
```python
179+
import packet
180+
import time
181+
182+
manager = packet.Manager(auth_token="yourapiauthtoken")
183+
volume = manager.get_volume("volume_id")
184+
185+
volume.attach("device_id")
186+
187+
while True:
188+
if manager.get_device("device_id").state == "active":
189+
break
190+
time.sleep(2)
191+
192+
volume.detach()
193+
```
194+
195+
## Creating and Restoring a Volume Snapshot
196+
197+
```python
198+
import packet
199+
import time
200+
201+
manager = packet.Manager(auth_token="yourapiauthtoken")
202+
203+
volume = manager.get_volume("volume_id")
204+
volume.create_snapshot()
205+
206+
while True:
207+
if manager.get_volume(volume.id).state == "active":
208+
break
209+
time.sleep(2)
210+
211+
snapshots = manager.get_snapshots(volume.id)
212+
volume.restore(snapshots[0].timestamp)
213+
```
214+
215+
### Listing Project IP Addresses
216+
217+
```python
218+
import packet
219+
manager = packet.Manager(auth_token="yourapiauthtoken")
220+
221+
ips = manager.list_project_ips("project_id")
222+
for ip in ips:
223+
print(ip.address)
224+
```
225+
226+
### Creating a Project for an Organization
227+
228+
```python
229+
import packet
230+
manager = packet.Manager(auth_token="yourapiauthtoken")
231+
232+
project = manager.create_organization_project(
233+
org_id="organization_id",
234+
name="Integration Tests",
235+
customdata={"tag": "QA"}
236+
)
237+
print(project)
238+
```
239+
240+
### Creating a VLAN
241+
242+
```python
243+
import packet
244+
manager = packet.Manager(auth_token="yourapiauthtoken")
245+
246+
vlan = manager.create_vlan(project_id="project_id", facility="ewr1")
247+
print(vlan)
248+
```
249+
250+
## Contributing
89251

90252
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
91253
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
@@ -94,18 +256,15 @@ Contributing
94256
* Commit and push until you are happy with your contribution.
95257
* You can test your changes with the `test/tests.sh` script, which is what travis uses to check builds.
96258

97-
Credits
98-
-------
259+
## Credits
99260

100261
CargoCulted with much gratitude from:
101262
https://github.com/koalalorenzo/python-digitalocean
102263

103-
Copyright
104-
---------
264+
## Copyright
105265

106266
Copyright (c) 2017 Packet Host. See [License](LICENSE.txt) for further details.
107267

108-
Changes
109-
-------
268+
## Changes
110269

111270
See the [Changelog](CHANGELOG.md) for further details.

packet/BGPConfig.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# SPDX-License-Identifier: LGPL-3.0-only
3+
4+
5+
class BGPConfig:
6+
def __init__(self, data):
7+
self.id = data.get("id")
8+
self.status = data.get("status")
9+
self.deployment_type = data.get("deployment_type")
10+
self.asn = data.get("asn")
11+
self.md5 = data.get("md5")
12+
self.route_object = data.get("route_object")
13+
self.max_prefix = data.get("max_prefix")
14+
self.created_at = data.get("created_at")
15+
self.requested_at = data.get("requested_at")
16+
self.project = data.get("project")
17+
self.sessions = data.get("sessions")
18+
self.ranges = data.get("ranges")
19+
self.href = data.get("href")
20+
21+
def __str__(self):
22+
return "%s" % self.id
23+
24+
def __repr__(self):
25+
return "{}: {}".format(self.__class__.__name__, self.id)

packet/BGPSession.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
# SPDX-License-Identifier: LGPL-3.0-only
3+
4+
5+
class BGPSession:
6+
def __init__(self, data):
7+
self.id = data.get("id")
8+
self.status = data.get("status")
9+
self.learned_routes = data.get("learned_routes")
10+
self.switch_name = data.get("switch_name")
11+
self.default_route = data.get("default_route")
12+
self.created_at = data.get("created_at")
13+
self.updated_at = data.get("updated_at")
14+
self.device = data.get("device")
15+
self.address_family = data.get("address_family")
16+
self.href = data.get("href")
17+
18+
def __str__(self):
19+
return "%s" % self.id
20+
21+
def __repr__(self):
22+
return "{}: {}".format(self.__class__.__name__, self.id)

packet/Batch.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
# SPDX-License-Identifier: LGPL-3.0-only
3+
4+
5+
class Batch:
6+
def __init__(self, data):
7+
self.id = data.get("id")
8+
self.error_messages = data.get("error_messages")
9+
self.quantity = data.get("quantity")
10+
self.state = data.get("state")
11+
self.created_at = data.get("created_at")
12+
self.updated_at = data.get("updated_at")
13+
self.devices = data.get("devices")
14+
self.project = data.get("project")
15+
self.state = data.get("state")
16+
self.error_messages = data.get("error_messages")
17+
18+
def __str__(self):
19+
return "%s" % self.id
20+
21+
def __repr__(self):
22+
return "{}: {}".format(self.__class__.__name__, self.id)

0 commit comments

Comments
 (0)