veritas.sot.selection

Module Contents

Classes

Selection

class to select data from nautobot

class veritas.sot.selection.Selection(sot: Selection.__init__.sot, select: tuple[list | str])

Bases: object

class to select data from nautobot

This class is used to query nautobot. The syntax is comparable to SQL, where you have the three components:

  • SELECT

  • FROM

  • WHERE

In addition, graphql queries can also be made with this class.

Parameters:
  • sot (sot) – the sot object

  • values (string | list) – the values to select

Examples

>>> # get id and hostname of a device
>>> sot.select('id, hostname') \
>>>    .using('nb.devices') \
>>>    .where('name=lab.local')
>>> # get all hosts of two locations
>>> sot.select('hostname') \
>>>    .using('nb.devices') \
>>>    .where('location=default-site or location=site_1')
query_cache
using(schema: str) None

configures which data source to use

Parameters:

schema (str) – which schema to use

Returns:

obj – Selection

Return type:

Selection

Notes

  • We implement a fluent syntax. This methods returns self

join(schema: str) None

join two schemas

See the ‘where’ method for an example.

Parameters:

schema (str) – name of the two graphql schemas

Returns:

obj – Selection

Return type:

Selection

Notes

  • We implement a fluent syntax. This methods returns self

on(column: str) None

on

See the ‘where’ method for an example

Parameters:

column (str) – name of the columns separeted by ‘=’

Returns:

obj – Selection

Return type:

Selection

Notes

  • We implement a fluent syntax. This methods returns self

mode(mode: str) None

mode

Parameters:

mode (str) – which mode (sql or gql)

Returns:

obj – Selection

Return type:

Selection

Notes

  • We implement a fluent syntax. This methods returns self

transform(transform: list | str) None

transform

If ‘transform’ is set, the data received is transformed to a different format

Parameters:

transform (list | str) – str or list of strings how to transform the data

Returns:

obj – Selection

Return type:

Selection

Notes

  • We implement a fluent syntax. This methods returns self

where(*unnamed, **named) dict

where

Parameters:
  • *unnamed – unnamed parameter that are used as ‘where’ clause

  • **named – named parameter that are used as ‘where’ clause

Returns:

result – the result of the query

Return type:

dict

Notes

by calling this method the query is executed and the result returned

Examples

>>> devices = sot.select('id, hostname') \
...              .using('nb.devices') \
...              .where('name=lab.local')
_parse_gql_query(expression: str, select: list, using: str) dict

parse GraphQL mode query

_parse_sql_query(expression: str, select: list, using: str) dict

parse query and return result as dict

Parameters:
  • expression (str) – boolean expression as string

  • select (list) – value to get from the query

  • using (str) – name of table to use

Returns:

result of the query

Return type:

dict

_simple_sql_query(select: list, using: str, expression: tuple[list | dict | str]) dict

return data of simple SQL queries This is a query that runs independently, so no additional data is required.

_build_logical_tree(res: dict) anytree.AnyNode

parse logical expression and build tree

_convert_expression(expression: dict) dict

convert boolean parse condition to dict

_condense_tree(root: anytree.AnyNode) None

condense tree - single run

_refresh_cf_types() None
_merge_equal_properties(root: anytree.AnyNode) bool

merge query values

Sometimes we have a logical expression like ‘name=lab-01.local or name=lab-02.local’ Such a query can be merged to ‘name=[‘lab-01.local’, ‘lab-02.local’]’

Parameters:

root (AnyNode) – the logical tree

Returns:

something_condensed – some values were merged together

Return type:

bool

Raises:

KeyError – if a key is unknown eg. an unknown custom field

_merge_dicts(dict1: dict, dict2: dict) dict

merge two dicts together

Parameters:
  • dict1 (dict) – first dict

  • dict2 (dict) – second dict

Returns:

merged dict

Return type:

dict

_query_logical_tree(logical_tree: anytree.AnyNode, select: list, using: str) None

query each leaf and merge data (depending on or and and)

_get_items_with_equal_id(all_items: list) list

returns a list of items with equal id

_get_items(all_items: list) list

returns all values without duplicates

_join_results(left: dict, right: dict, join_on: str, left_select: str, right_select: str) list

join left and right table