Skip to content

Commit

Permalink
Implement raise_if_not_configurable()
Browse files Browse the repository at this point in the history
Because:
- CNaaSNMSMixin needs a CNaaS-NMS-specific implementation of this method
  to indicate whether a device is configurable through CNaaS-NMS at all.
  • Loading branch information
lunkwill42 committed Jul 10, 2020
1 parent 78babca commit 7e67d5f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/nav/portadmin/cnaas_nms/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""Low-level CNaaS-NMS REST API access using simple_rest_client"""
from simple_rest_client.api import API
from simple_rest_client.resource import Resource
from simple_rest_client.exceptions import ClientError


def get_api(url, token):
Expand Down
40 changes: 38 additions & 2 deletions python/nav/portadmin/cnaas_nms/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#
from nav.models import manage
from nav.portadmin.config import CONFIG
from nav.portadmin.cnaas_nms.lowlevel import get_api
from nav.portadmin.handlers import ManagementHandler
from nav.portadmin.cnaas_nms.lowlevel import get_api, ClientError
from nav.portadmin.handlers import ManagementHandler, DeviceNotConfigurableError


class CNaaSNMSMixIn(ManagementHandler):
Expand Down Expand Up @@ -84,6 +84,42 @@ def _interface_name_from_index(self, if_index):
ifc = self.netbox.interface_set.only("ifdescr").get(ifindex=if_index)
return ifc.ifdescr

def raise_if_not_configurable(self):
"""Raises an exception if this device cannot be configured by CNaaS-NMS for
some reason.
"""
try:
device = self._get_device_record()
self.raise_on_unmatched_criteria(device)
except CNaaSNMSApiError as error:
raise DeviceNotConfigurableError(str(error)) from error
except ClientError as error:
raise DeviceNotConfigurableError(
"Unexpected error talking to the CNaaS-NMS backend: " + str(error)
) from error

def raise_on_unmatched_criteria(self, device_record):
"""Raises an exception if the device's CNaaS-NMS attributes do not match the
preset criteria for being managed via the API.
"""
state = device_record.get("state")
device_type = device_record.get("device_type")
synchronized = device_record.get("synchronized")

if state != "MANAGED":
raise DeviceNotConfigurableError(
"CNaaS-NMS does not list this device as managed ({})".format(state)
)
if device_type != "ACCESS":
raise DeviceNotConfigurableError(
"Cannot use CNaaS-NMS to configure {} type devices".format(device_type)
)
if not synchronized:
raise DeviceNotConfigurableError(
"Device configuration is not synchronized with CNaaS-NMS, cannot make "
"changes at the moment. Please try againt later."
)


class CNaaSNMSApiError(Exception):
"""An exception raised whenever there is a problem with the responses from the
Expand Down

0 comments on commit 7e67d5f

Please sign in to comment.