Skip to content

Commit 6154e19

Browse files
authored
Merge pull request #334 from koalalorenzo/develop
Adding Digital Ocean's Project Support (#300)
2 parents ba4d997 + 3dd9e54 commit 6154e19

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,37 @@ python-digitalocean support all the features provided via digitalocean.com APIs,
8484

8585
**[⬆ back to top](#table-of-contents)**
8686

87-
## Examples
87+
## Examples
88+
89+
### Listing the Projects
90+
91+
This example shows how to list all the projects:
92+
93+
```python
94+
import digitalocean
95+
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
96+
my_projects = manager.get_all_projects()
97+
print(my_projects)
98+
```
99+
100+
### Assign a resource for specific project
101+
102+
```python
103+
import digitalocean
104+
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
105+
my_projects = manager.get_all_projects()
106+
my_projects[0].assign_resource(["do:droplet:<Droplet Number>"])
107+
```
108+
109+
### List all the resources of a project
110+
```python
111+
import digitalocean
112+
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
113+
my_projects = manager.get_all_projects()
114+
resources = my_projects[0].get_all_resources()
115+
print(resources)
116+
```
117+
88118
### Listing the droplets
89119

90120
This example shows how to list all the active droplets:

digitalocean/Manager.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from .VPC import VPC
2626
from .Project import Project
2727

28-
2928
class Manager(BaseAPI):
3029
def __init__(self, *args, **kwargs):
3130
super(Manager, self).__init__(*args, **kwargs)
@@ -445,5 +444,35 @@ def get_all_vpcs(self):
445444

446445
return vpcs
447446

447+
def get_all_projects(self):
448+
"""
449+
All the projects of the account
450+
"""
451+
data = self.get_data("projects")
452+
projects = list()
453+
for jsoned in data['projects']:
454+
project = Project(**jsoned)
455+
project.token = self.token
456+
projects.append(project)
457+
return projects
458+
459+
def get_project(self, project_id):
460+
"""
461+
Return a Project by its ID.
462+
"""
463+
return Project.get_object(
464+
api_token=self.token,
465+
project_id=project_id,
466+
)
467+
468+
def get_default_project(self):
469+
"""
470+
Return default project of the account
471+
"""
472+
return Project.get_object(
473+
api_token=self.token,
474+
project_id="default",
475+
)
476+
448477
def __str__(self):
449478
return "<Manager>"

digitalocean/baseapi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def get_data(self, url, type=GET, params=None):
222222

223223
try:
224224
data = req.json()
225+
225226
except ValueError as e:
226227
raise JSONReadError(
227228
'Read failed from DigitalOcean: %s' % str(e)

digitalocean/tests/test_domain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_create_new_caa_record_zero_flags(self):
154154

155155
@responses.activate
156156
def test_create(self):
157-
data = self.load_from_file('domains/create.json')
157+
data = self.load_from_file( 'domains/create.json')
158158

159159
url = self.base_url + "domains"
160160
responses.add(responses.POST,

0 commit comments

Comments
 (0)