Skip to content

Commit 97d9173

Browse files
authored
Feature/controllers (#15)
* Implement controller method To determine which controllers will be supported by the api we implement a method which returns all controller names. To test the module we also add some simple tests that can be run against an existing phpIPAM installation. Here you only need to create a `server.yml` in `tests/vars`. Simply copy the `server.yml.exampl` and fill out with your server data. To secure your secrets we add `server.yml` to `.gitignore` * release v0.0.1
1 parent 8a64e61 commit 97d9173

File tree

8 files changed

+100
-6
lines changed

8 files changed

+100
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# project specific ignores
132+
tests/vars/server.yml

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
# phpypam
1+
# phpypam: Python API client library for phpIPAM installation
22

3-
Python API client library for [phpIPAM](https://phpipam.net/) installations.
3+
phpypam is intended to be a complete library for speaking with phpIPAM API.
44

5-
## Installation
5+
Using `phpypam` is as easy as using the UI.
66

7-
tbd
7+
```python
8+
import phpypam
9+
10+
pi = phpypam.api(
11+
url='https://ipam.example.com',
12+
app_id='ansible',
13+
username='apiuser',
14+
password='apiP455wd',
15+
ssl_verify=True
16+
)
17+
pi.get_entity(controller='sections')
18+
```

phpypam/core/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,10 @@ def update_entity(self, controller, data, **kwargs):
129129
_path = '{}/{}'.format(_path, _controller_path)
130130

131131
return self._query(token=self._api_token, method=PATCH, path=_path, data=data)
132+
133+
def controllers(self):
134+
result = self._query(token=self._api_token, method=OPTIONS, path='/')
135+
136+
controllers = ({v for ctrl in result['controllers'] for (k, v) in ctrl.items() if k == 'rel'})
137+
138+
return controllers

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ requests
55
inflection
66
twine
77
setuptools
8+
PyYAML

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="phpypam",
8-
version="0.0.1",
8+
version="0.0.2",
99
author="Christian Meißner",
1010
author_email="Christian Meißner <[email protected]>",
1111
description="Python API client library for phpIPAM installation",
@@ -18,8 +18,9 @@
1818
classifiers=[
1919
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
2020
"Programming Language :: Python :: 3",
21-
"Operating System :: OS Independent",
21+
"Intended Audience :: Developers",
2222
],
23+
keywords='api phpipam',
2324
python_requires='>=3.6',
2425
install_requires=[
2526
'requests (>=2.21,<3.0)',

tests/api_connection.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
import phpypam
4+
import json
5+
import yaml
6+
7+
with open('tests/vars/server.yml') as c:
8+
server = yaml.safe_load(c)
9+
10+
from phpypam import PHPyPAMEntityNotFoundException
11+
12+
13+
if __name__ == '__main__':
14+
pi = phpypam.api(
15+
url=server['url'],
16+
app_id=server['app_id'],
17+
username=server['username'],
18+
password=server['password'],
19+
ssl_verify=True
20+
)
21+
22+
my_section = dict(
23+
name='foobar',
24+
description='new section',
25+
permissions='{"3":"1","2":"2"}'
26+
)
27+
try:
28+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
29+
except PHPyPAMEntityNotFoundException:
30+
print('create entity')
31+
entity = pi.create_entity(controller='sections', data=my_section)
32+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
33+
34+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
35+
print(json.dumps(entity, indent=4, sort_keys=True))
36+
37+
my_section['description'] = 'new description'
38+
39+
print('update entity')
40+
pi.update_entity(controller='sections', controller_path=entity['id'], data=my_section)
41+
42+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
43+
print(json.dumps(entity, indent=4, sort_keys=True))
44+
45+
print('delete entity')
46+
pi.delete_entity(controller='sections', controller_path=entity['id'])

tests/controllers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
import phpypam
4+
import json
5+
import yaml
6+
7+
with open('tests/vars/server.yml') as c:
8+
server = yaml.safe_load(c)
9+
10+
if __name__ == '__main__':
11+
pi = phpypam.api(
12+
url=server['url'],
13+
app_id=server['app_id'],
14+
username=server['username'],
15+
password=server['password'],
16+
ssl_verify=True
17+
)
18+
19+
controllers = pi.controllers()
20+
print(controllers)

tests/vars/server.yml.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Parameter for all tests
2+
url: 'https://ipam.example.com'
3+
app_id: my_app
4+
username: user
5+
password: p455w0rd

0 commit comments

Comments
 (0)