Skip to content
Snippets Groups Projects
Commit f21a006f authored by Erik Reid's avatar Erik Reid
Browse files

added validation to docs

parent 3c474fa9
No related branches found
No related tags found
No related merge requests found
...@@ -4,3 +4,5 @@ CLI Tools ...@@ -4,3 +4,5 @@ CLI Tools
========================= =========================
.. automodule:: resource_management.cli .. automodule:: resource_management.cli
.. automodule:: resource_management.hardware.router
...@@ -11,7 +11,7 @@ Documentation for the NAT Resource Management tools ...@@ -11,7 +11,7 @@ Documentation for the NAT Resource Management tools
------------------ ------------------
Configuration file format Configuration file format
--------------------- ---------------------------------
The following example indicates the schema for the configuration file required to call the Resource Management client:: The following example indicates the schema for the configuration file required to call the Resource Management client::
......
...@@ -29,7 +29,7 @@ to this schema: ...@@ -29,7 +29,7 @@ to this schema:
import json import json
import click import click
import logging import logging
from jsonschema.exceptions import ValidationError from jsonschema import validate, ValidationError
import environment import environment
import config import config
...@@ -38,7 +38,7 @@ from db import mysql_dsn ...@@ -38,7 +38,7 @@ from db import mysql_dsn
from db import init_db_model from db import init_db_model
from db import model from db import model
from db import session_scope from db import session_scope
from hardware.router import junos_router from hardware.router import load_line_cards, LINE_CARDS_LIST_SCHEMA
environment.setup_logging() environment.setup_logging()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -81,14 +81,12 @@ def create(config, fqdn): ...@@ -81,14 +81,12 @@ def create(config, fqdn):
""" """
router_config = config["router"] router_config = config["router"]
jr = junos_router(fqdn, router_config) line_cards = list(load_line_cards(fqdn, router_config))
router = jr.load_router() # logger.info("Obtained the following hardware info about router: {0}"
jr.close_device() # .format(router))
logger.info("Obtained the following hardware info about router: {0}"
.format(router))
try: try:
junos_router_schema.validate(router) validate(line_cards, LINE_CARDS_LIST_SCHEMA)
except ValidationError as e: except ValidationError as e:
raise click.BadParameter(e.message) raise click.BadParameter(e.message)
......
"""
router utilities
===================
.. contents:: :local:
.. autofunction:: resource_management.hardware.router.load_line_cards
"""
import logging import logging
from jnpr.junos import Device from jnpr.junos import Device
...@@ -7,6 +17,50 @@ from jnpr.junos.op.phyport import PhyPortTable ...@@ -7,6 +17,50 @@ from jnpr.junos.op.phyport import PhyPortTable
import contextlib import contextlib
LINE_CARDS_LIST_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'node': {
'type': 'string'
},
'line-card': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'model': {'type': 'string'},
'position': {'type': 'integer', 'minimum': 0}
},
'required': ['model', 'position'],
'additionalProperties': False
}
},
'port': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'name': {'type': 'string', 'pattern': '^[0-9]{1,2}\\/[0-9]{1,3}'},
'speed': {'type': 'integer', 'minimum': 0},
'_line_card_position': {'type': 'integer', 'minimum': 0}
},
'required': ['name', 'speed', '_line_card_position'],
'additionalProperties': False
}
}
},
'type': 'object',
'properties': {
'node': {'$ref': '#/definitions/node'},
'line_cards': {'$ref': '#/definitions/line-card'},
'ports': {'$ref': '#/definitions/port'}
},
'required': ['node', 'line_cards', 'ports'],
'additionalProperties': False
}
@contextlib.contextmanager @contextlib.contextmanager
def device(hostname, username, key_filename, port=830): def device(hostname, username, key_filename, port=830):
dev = Device( dev = Device(
...@@ -22,6 +76,21 @@ def device(hostname, username, key_filename, port=830): ...@@ -22,6 +76,21 @@ def device(hostname, username, key_filename, port=830):
def load_line_cards(hostname, username, key_filename, port=830): def load_line_cards(hostname, username, key_filename, port=830):
"""
Loads all line cards from the given hostname
The response will be formatted according to the following schema:
.. asjson::
resource_management.hardware.router.load_line_cards.LINE_CARDS_LIST_SCHEMA
:param hostname: router hostname
:param username: router username
:param key_filename: ssh private key filename
:param port: netconf port
:return: yields elements of the LINE_CARDS_LIST_SCHEMA
"""
with device( with device(
hostname=hostname, hostname=hostname,
port=port, port=port,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment