Skip to content

Commit 1e935ba

Browse files
whites2lampwins
authored andcommitted
POST Request Support and new actions for netbox 2.1.0 (#4)
* Added action to get available IPs within a prefix * Added action for available IP POST request * Bumped version number to 0.2.1 * changed version number as per request * minor documentation fixes * minor fixes for circleci * removed extraneous whitespace * removed more extraneous whitespace * removed all of the extraneous whitespace * 'fixed' email address * removed email address
1 parent ccb13e5 commit 1e935ba

File tree

7 files changed

+115
-7
lines changed

7 files changed

+115
-7
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Change Log
2+
## 0.2.0
3+
- Added action to get available IPs from a prefix
4+
- Refactored action.py to allow for POST requests as well as GET
5+
- Added POST version of get_available_ips
6+
27
## 0.1.1
38
- Added `limit` and `offset` parameters to all actions
49

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ is loaded.
3434
- **ipam\_get\_vlans**: Get VLAN(s) via optional parameters
3535
- **ipam\_get\_vrfs**: Get VRF(s) via optional parameters
3636
- **ipam\_get\_prefixes**: Get Prefix(es) via optional parameters
37+
- **ipam\_get\_available_ips**: Get available IP Address(es) within a prefix
38+
- **ipam\_post\_available_ips**: POST request to create an object assigned to the first available IP address within a given prefix

actions/base_post_action.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
from lib.action import NetboxBaseAction
3+
4+
5+
class NetboxBasePostAction(NetboxBaseAction):
6+
"""Base get action"""
7+
8+
def run(self, endpoint_uri, **kwargs):
9+
"""Base get action
10+
endpoint_uri is pased from metadata file
11+
"""
12+
return self.post(endpoint_uri, **kwargs)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: ipam_get_available_ips
3+
pack: netbox
4+
runner_type: "python-script"
5+
description: Get available IP addresses within a prefix from NetBox
6+
enabled: true
7+
entry_point: base_get_action.py
8+
parameters:
9+
id:
10+
type: integer
11+
description: ID of the prefix to get.
12+
endpoint_uri:
13+
immutable: true
14+
default: "/api/ipam/prefixes/{{ id }}/available-ips/"
15+
limit:
16+
type: integer
17+
default: 50
18+
description: Max limit of objects to return from the request.
19+
offset:
20+
type: integer
21+
default: 0
22+
description: Offset result set by X objects. Used for pagination.
23+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
name: ipam_post_available_ips
3+
pack: netbox
4+
runner_type: "python-script"
5+
description: Creates new object with first available IP address in the given prefix
6+
enabled: true
7+
entry_point: base_post_action.py
8+
parameters:
9+
id:
10+
type: integer
11+
description: ID of the prefix to get.
12+
endpoint_uri:
13+
immutable: true
14+
default: "/api/ipam/prefixes/{{ id }}/available-ips/"
15+
limit:
16+
type: integer
17+
default: 50
18+
description: Max limit of objects to return from the request.
19+
offset:
20+
type: integer
21+
default: 0
22+
description: Offset result set by X objects. Used for pagination.
23+
status:
24+
type: string
25+
description: Status of the newly assigned IP address
26+
description:
27+
type: string
28+
description: Description of the newly assigned IP address
29+
is_pool:
30+
type: boolean
31+
description: IP address is a pool
32+
vlan:
33+
type: integer
34+
description: VLAN that this IP address belongs to
35+
site:
36+
type: integer
37+
description: Site that this IP address belongs to
38+
prefix:
39+
type: string
40+
description: Prefix that this IP address belongs to
41+
role:
42+
type: integer
43+
description: IP address role
44+
vrf:
45+
type: integer
46+
description: VRF the IP address is a member of
47+
tenant:
48+
type: integer
49+
description: Tenant the IP address belongs to
50+

actions/lib/action.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ class NetboxBaseAction(Action):
1515
def __init__(self, config):
1616
super(NetboxBaseAction, self).__init__(config)
1717

18-
def get(self, endpoint_uri, **kwargs):
19-
"""Make a get request to the API URI passed in
18+
def _make_request(self, endpoint_uri, http_action, **kwargs):
19+
"""Logic to make all types of requests
2020
"""
2121

22-
self.logger.debug("Calling base get with kwargs: {}".format(kwargs))
23-
2422
if self.config['use_https']:
2523
url = 'https://'
2624
else:
@@ -38,6 +36,24 @@ def get(self, endpoint_uri, **kwargs):
3836
kwargs['id__in'] = ','.join(kwargs['id__in'])
3937
self.logger.debug('id__in transformed to {}'.format(kwargs['id__in']))
4038

41-
r = requests.get(url, verify=self.config['ssl_verify'], headers=headers, params=kwargs)
39+
if http_action == "GET":
40+
self.logger.debug("Calling base get with kwargs: {}".format(kwargs))
41+
r = requests.get(url, verify=self.config['ssl_verify'], headers=headers, params=kwargs)
42+
43+
elif http_action == "POST":
44+
self.logger.debug("Calling base post with kwargs: {}".format(kwargs))
45+
r = requests.post(url, verify=self.config['ssl_verify'], headers=headers, data=kwargs)
4246

4347
return {'raw': r.json()}
48+
49+
def get(self, endpoint_uri, **kwargs):
50+
"""Make a get request to the API URI passed in
51+
"""
52+
53+
return self._make_request(endpoint_uri, "GET", **kwargs)
54+
55+
def post(self, endpoint_uri, **kwargs):
56+
"""Make a post request to the API URI passed in
57+
"""
58+
59+
return self._make_request(endpoint_uri, "POST", **kwargs)

pack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ keywords:
66
- networking
77
- ipam
88
- dcim
9-
version: 0.1.1
10-
author: John Anderson
9+
version: 0.2.0
10+
author: John Anderson, Jefferson White
1111

0 commit comments

Comments
 (0)