Skip to content

Commit

Permalink
added add_device
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-lopez8 committed Dec 20, 2023
1 parent 3a61561 commit 70c4210
Showing 1 changed file with 77 additions and 5 deletions.
82 changes: 77 additions & 5 deletions pylibrenms/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def _post(self, route, data=None):
POST call to be used by endpoints.
"""
endpoint = self.url + route
print(endpoint)
if data is None:
data = {}
try:
Expand All @@ -55,6 +54,20 @@ def _post(self, route, data=None):
# TODO: implement exception here
raise Exception(f"Error occurred, {e}")

def _delete(self, route, data=None):
"""
POST call to be used by endpoints.
"""
endpoint = self.url + route
if data is None:
data = {}
try:
response = requests.delete(endpoint, headers=self._headers, json=data)
return response.json()
except requests.exceptions.RequestException as e:
# TODO: implement exception here
raise Exception(f"Error occurred, {e}")

def get_all_ports(self, columns=None):
"""
Get all ports on all devices.
Expand Down Expand Up @@ -106,8 +119,15 @@ def get_port_ip_info(self, port_id):

return self._get("ports/" + str(port_id) + "/ip")

def del_device(self, ):
...
def del_device(self, hostname):
"""
Delete a given device.
Parameters:
- hostname: either the device hostname or ID
"""

return self._delete("devices/" + str(hostname))

def get_device(self, hostname):
"""
Expand Down Expand Up @@ -170,7 +190,7 @@ def get_components(self, hostname, component_type=None, component_id=None, compo
- component_disabled: Filter the result by disabled (Equals).
- component_ignore: Filter the result by ignore (Equals).
"""

params = {
"type": component_type,
"id": component_id,
Expand All @@ -191,4 +211,56 @@ def add_components(self, hostname, component_type):
"""

return self._post("devices/" + str(hostname) + "/components/" + str(component_type))
return self._post("devices/" + str(hostname) + "/components/" + str(component_type))

def edit_components(self, ):
...

def delete_components(self, ):
...

def add_device(self, hostname, device_type="snmpv2c", **kwargs):
"""
Add a device.
Parameters:
- device_type: Monitoring method for a device. Can be icmp, snmpv1, snmpv2c, snmpv3
- hostname: device hostname or IP
- community: Required for snmpv2c. Community string to use.
"""

# define parameter dictionaries
required_params = {
"icmp" : [],
"snmpv1": ["community"],
"snmpv2c": ["community"],
"snmpv3": ["authlevel", "authname", "authpass", "authalgo", "cryptopass", "cryptoalgo"]
}
optional_params = {
"icmp": ["os", "sys_name", "hardware", "force_add", "location", "poller_group", "display"],
"snmpv1": ["port", "transport", "port_association_mode", "location", "force_add", "display"],
"snmpv2c": ["port", "transport", "port_association_mode", "location", "force_add", "display"],
"snmpv3": ["port", "transport", "port_association_mode", "location", "force_add", "display"],
}
data = {'hostname': hostname}
# validation
if device_type not in required_params.keys():
raise Exception(f"Unknown device type {device_type}.")
for param in required_params[device_type]:
if param not in kwargs.keys():
raise Exception(f"Missing parameter {param} for device type {device_type}.")
else:
data[param] = kwargs[param]
for param in optional_params[device_type]:
# ignore other parameters
if param in kwargs.keys():
data[param] = kwargs[param]
if device_type == "icmp":
data["snmp_disable"] = "true"
elif device_type == "snmpv1":
data['snmpver'] = "v1"
elif device_type == "snmpv2c":
data["snmpver"] = "v2c"
else:
data['snmpver'] = "v3"
return self._post("devices", data=data)

0 comments on commit 70c4210

Please sign in to comment.