Skip to content

Commit

Permalink
Rewrite raise_if_not_configurable()
Browse files Browse the repository at this point in the history
Because:
- Error message should be more specific about which CNaaS NMS management
  criteria aren't met.
  • Loading branch information
lunkwill42 committed Mar 26, 2021
1 parent 8a0c403 commit f64068c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 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
41 changes: 33 additions & 8 deletions python/nav/portadmin/cnaas_nms/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
from nav.models import manage
from nav.portadmin.config import CONFIG
from nav.portadmin.cnaas_nms.lowlevel import get_api
from nav.portadmin.cnaas_nms.lowlevel import get_api, ClientError
from nav.portadmin.handlers import (
ManagementHandler,
DeviceNotConfigurableError,
Expand Down Expand Up @@ -58,14 +58,39 @@ def commit_configuration(self):
# TODO: Poll the job API for "status": "FINISHED"

def raise_if_not_configurable(self):
device = self._get_device_record()
if not (
device.get("state") == "MANAGED"
and device.get("device_type") == "ACCESS"
and device.get("synchronized")
):
"""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(
"CNaaS NMS will not permit configuration of this device"
"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."
)

def _get_device_record(self):
Expand Down

0 comments on commit f64068c

Please sign in to comment.