utils

Constants

plugins.module_utils.utils.API_APPS_ENDPOINTS = {'circuits': ['circuits', 'circuit_types', 'circuit_terminations', 'providers'], 'dcim': ['cables', 'console_ports', 'console_port_templates', 'console_server_ports', 'console_server_port_templates', 'device_bays', 'device_bay_templates', 'devices', 'device_types', 'device_redundancy_groups', 'front_ports', 'front_port_templates', 'interfaces', 'interface_templates', 'inventory_items', 'locations', 'location_types', 'manufacturers', 'platforms', 'power_feeds', 'power_outlets', 'power_outlet_templates', 'power_panels', 'power_ports', 'power_port_templates', 'racks', 'rack_groups', 'rear_ports', 'rear_port_templates', 'virtual_chassis'], 'extras': ['custom_fields', 'custom_field_choices', 'relationship_associations', 'roles', 'statuses', 'tags'], 'ipam': ['ip_addresses', 'ip_address_to_interface', 'namespaces', 'prefixes', 'rirs', 'route_targets', 'services', 'vlans', 'vlan_groups', 'vrfs'], 'plugins': [], 'secrets': [], 'tenancy': ['tenants', 'tenant_groups'], 'virtualization': ['cluster_groups', 'cluster_types', 'clusters', 'virtual_machines']}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.QUERY_TYPES = {'circuit': 'cid', 'circuit_termination': 'circuit', 'circuit_type': 'name', 'cluster': 'name', 'cluster_group': 'name', 'cluster_type': 'name', 'device': 'name', 'device_type': 'model', 'export_targets': 'name', 'group': 'name', 'import_targets': 'name', 'installed_device': 'name', 'location': 'name', 'manufacturer': 'name', 'master': 'name', 'nat_inside': 'address', 'nat_outside': 'address', 'parent_location': 'name', 'parent_location_type': 'name', 'parent_rack_group': 'name', 'parent_tenant_group': 'name', 'platform': 'name', 'power_panel': 'name', 'power_port': 'name', 'power_port_template': 'name', 'primary_ip': 'address', 'primary_ip4': 'address', 'primary_ip6': 'address', 'provider': 'name', 'rack': 'name', 'rack_group': 'name', 'rear_port': 'name', 'rear_port_template': 'name', 'rir': 'name', 'role': 'name', 'route_targets': 'name', 'status': 'name', 'tenant': 'name', 'tenant_group': 'name', 'time_zone': 'timezone', 'virtual_chassis': 'name', 'virtual_machine': 'name', 'vlan': 'name', 'vlan_group': 'name', 'vrf': 'name'}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.CONVERT_TO_ID = {'circuit': 'circuits', 'circuit_termination': 'circuit_terminations', 'circuit_type': 'circuit_types', 'circuits.circuittermination': 'circuit_terminations', 'cluster': 'clusters', 'cluster_group': 'cluster_groups', 'cluster_type': 'cluster_types', 'dcim.consoleport': 'console_ports', 'dcim.consoleserverport': 'console_server_ports', 'dcim.frontport': 'front_ports', 'dcim.interface': 'interfaces', 'dcim.powerfeed': 'power_feeds', 'dcim.poweroutlet': 'power_outlets', 'dcim.powerport': 'power_ports', 'dcim.rearport': 'rear_ports', 'device': 'devices', 'device_type': 'device_types', 'export_targets': 'route_targets', 'group': 'tenant_groups', 'import_targets': 'route_targets', 'installed_device': 'devices', 'interface': 'interfaces', 'interface_template': 'interface_templates', 'ip_addresses': 'ip_addresses', 'ipaddresses': 'ip_addresses', 'lag': 'interfaces', 'location': 'locations', 'manufacturer': 'manufacturers', 'master': 'devices', 'namespace': 'namespaces', 'nat_inside': 'ip_addresses', 'nat_outside': 'ip_addresses', 'parent_location': 'locations', 'parent_location_type': 'location_types', 'parent_rack_group': 'rack_groups', 'parent_tenant_group': 'tenant_groups', 'platform': 'platforms', 'power_panel': 'power_panels', 'power_port': 'power_ports', 'power_port_template': 'power_port_templates', 'primary_ip': 'ip_addresses', 'primary_ip4': 'ip_addresses', 'primary_ip6': 'ip_addresses', 'provider': 'providers', 'rack': 'racks', 'rack_group': 'rack_groups', 'rear_port': 'rear_ports', 'rear_port_template': 'rear_port_templates', 'rir': 'rirs', 'role': 'roles', 'route_targets': 'route_targets', 'services': 'services', 'status': 'statuses', 'tagged_vlans': 'vlans', 'tags': 'tags', 'tenant': 'tenants', 'tenant_group': 'tenant_groups', 'termination_a': 'interfaces', 'termination_b': 'interfaces', 'untagged_vlan': 'vlans', 'virtual_chassis': 'virtual_chassis', 'virtual_machine': 'virtual_machines', 'vlan': 'vlans', 'vlan_group': 'vlan_groups', 'vm_interface': 'interfaces', 'vrf': 'vrfs'}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.ENDPOINT_NAME_MAPPING = {'cables': 'cable', 'circuit_terminations': 'circuit_termination', 'circuit_types': 'circuit_type', 'circuits': 'circuit', 'cluster_groups': 'cluster_group', 'cluster_types': 'cluster_type', 'clusters': 'cluster', 'console_port_templates': 'console_port_template', 'console_ports': 'console_port', 'console_server_port_templates': 'console_server_port_template', 'console_server_ports': 'console_server_port', 'custom_field_choices': 'custom_field_choice', 'custom_fields': 'custom_field', 'device_bay_templates': 'device_bay_template', 'device_bays': 'device_bay', 'device_redundancy_groups': 'device_redundancy_group', 'device_types': 'device_type', 'devices': 'device', 'front_port_templates': 'front_port_template', 'front_ports': 'front_port', 'interface_templates': 'interface_template', 'interfaces': 'interface', 'inventory_items': 'inventory_item', 'ip_address_to_interface': 'ip_address_to_interface', 'ip_addresses': 'ip_address', 'location_types': 'location_type', 'locations': 'location', 'manufacturers': 'manufacturer', 'namespaces': 'namespace', 'platforms': 'platform', 'power_feeds': 'power_feed', 'power_outlet_templates': 'power_outlet_template', 'power_outlets': 'power_outlet', 'power_panels': 'power_panel', 'power_port_templates': 'power_port_template', 'power_ports': 'power_port', 'prefixes': 'prefix', 'providers': 'provider', 'rack_groups': 'rack_group', 'racks': 'rack', 'rear_port_templates': 'rear_port_template', 'rear_ports': 'rear_port', 'relationship_associations': 'relationship_associations', 'rirs': 'rir', 'roles': 'role', 'route_targets': 'route_target', 'services': 'services', 'statuses': 'statuses', 'tags': 'tags', 'tenant_groups': 'tenant_group', 'tenants': 'tenant', 'virtual_chassis': 'virtual_chassis', 'virtual_machines': 'virtual_machine', 'vlan_groups': 'vlan_group', 'vlans': 'vlan', 'vrfs': 'vrf'}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.ALLOWED_QUERY_PARAMS = {'circuit': {'cid'}, 'circuit_termination': {'circuit', 'term_side'}, 'circuit_type': {'name'}, 'circuits.circuittermination': {'circuit', 'term_side'}, 'cluster': {'name', 'type'}, 'cluster_group': {'name'}, 'cluster_type': {'name'}, 'console_port': {'device', 'name'}, 'console_port_template': {'device_type', 'name'}, 'console_server_port': {'device', 'name'}, 'console_server_port_template': {'device_type', 'name'}, 'custom_field': {'label'}, 'custom_field_choice': {'custom_field', 'value'}, 'dcim.consoleport': {'device', 'name'}, 'dcim.consoleserverport': {'device', 'name'}, 'dcim.frontport': {'device', 'name', 'rear_port'}, 'dcim.interface': {'device', 'name', 'virtual_machine'}, 'dcim.powerfeed': {'name', 'power_panel'}, 'dcim.poweroutlet': {'device', 'name'}, 'dcim.powerport': {'device', 'name'}, 'dcim.rearport': {'device', 'name'}, 'device': {'name'}, 'device_bay': {'device', 'name'}, 'device_bay_template': {'device_type', 'name'}, 'device_redundancy_group': {'name'}, 'device_type': {'model'}, 'front_port': {'device', 'name', 'rear_port'}, 'front_port_template': {'device_type', 'name', 'rear_port_template'}, 'installed_device': {'name'}, 'interface': {'device', 'name', 'virtual_machine'}, 'interface_template': {'device_type', 'name'}, 'inventory_item': {'device', 'name'}, 'ip_address': {'address', 'device', 'interfaces', 'namespace', 'vm_interfaces'}, 'ip_address_to_interface': {'interface', 'ip_address', 'vm_interface'}, 'ip_addresses': {'address', 'device', 'interfaces', 'namespace', 'vm_interfaces'}, 'ipaddresses': {'address', 'device', 'interfaces', 'namespace', 'vm_interfaces'}, 'lag': {'name'}, 'location': {'id', 'name', 'parent'}, 'location_type': {'name'}, 'manufacturer': {'name'}, 'master': {'name'}, 'namespace': {'name'}, 'nat_inside': {'address', 'namespace'}, 'parent_rack_group': {'name'}, 'parent_tenant_group': {'name'}, 'platform': {'name'}, 'power_feed': {'name', 'power_panel'}, 'power_outlet': {'device', 'name'}, 'power_outlet_template': {'device_type', 'name'}, 'power_panel': {'location', 'name'}, 'power_port': {'device', 'name'}, 'power_port_template': {'device_type', 'name'}, 'prefix': {'namespace', 'prefix'}, 'primary_ip4': {'address', 'namespace'}, 'primary_ip6': {'address', 'namespace'}, 'provider': {'name'}, 'rack': {'location', 'name'}, 'rack_group': {'name'}, 'rear_port': {'device', 'name'}, 'rear_port_template': {'device_type', 'name'}, 'relationship_associations': {'destination_id', 'source_id'}, 'rir': {'name'}, 'role': {'name'}, 'route_target': {'name'}, 'services': {'device', 'name', 'port', 'protocol', 'virtual_machine'}, 'statuses': {'name'}, 'tagged_vlans': {'group', 'location', 'name', 'tenant', 'vid', 'vlan_group'}, 'tags': {'name'}, 'tenant': {'name'}, 'tenant_group': {'name'}, 'termination_a': {'device', 'name', 'virtual_machine'}, 'termination_b': {'device', 'name', 'virtual_machine'}, 'untagged_vlan': {'group', 'location', 'name', 'tenant', 'vid', 'vlan_group'}, 'virtual_chassis': {'device', 'name'}, 'virtual_machine': {'cluster', 'name'}, 'vlan': {'location', 'name', 'tenant', 'vid', 'vlan_group'}, 'vlan_group': {'location', 'name'}, 'vm_interface': {'name', 'virtual_machine'}, 'vrf': {'name', 'namespace', 'rd'}}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.QUERY_PARAMS_IDS = {'circuit', 'cluster', 'device', 'group', 'interface', 'rir', 'tenant', 'type', 'virtual_machine', 'vminterface', 'vrf'}

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.

plugins.module_utils.utils.REQUIRED_ID_FIND = {'cables': {'length_unit', 'termination_a_type', 'termination_b_type', 'type'}, 'console_port_templates': {'type'}, 'console_ports': {'type'}, 'console_server_port_templates': {'type'}, 'console_server_ports': {'type'}, 'device_types': {'subdevice_role'}, 'devices': {'face'}, 'front_port_templates': {'type'}, 'front_ports': {'type'}, 'interface_templates': {'type'}, 'interfaces': {'form_factor', 'mode', 'type'}, 'ip_addresses': {'type'}, 'power_feeds': {'phase', 'supply', 'type'}, 'power_outlet_templates': {'feed_leg', 'type'}, 'power_outlets': {'feed_leg', 'type'}, 'power_port_templates': {'type'}, 'power_ports': {'type'}, 'prefixes': {'type'}, 'racks': {'outer_unit', 'type'}, 'rear_port_templates': {'type'}, 'rear_ports': {'type'}, 'services': {'protocol'}, 'virtual_machines': {'face'}}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.CONVERT_KEYS = {'parent_location': 'parent', 'parent_location_type': 'parent', 'parent_rack_group': 'parent', 'parent_tenant_group': 'parent', 'rear_port_template_position': 'rear_port_position', 'termination_a': 'termination_a_id', 'termination_b': 'termination_b_id'}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

plugins.module_utils.utils.NAUTOBOT_ARG_SPEC = {'api_version': {'required': False, 'type': 'str'}, 'query_params': {'elements': 'str', 'required': False, 'type': 'list'}, 'state': {'choices': ['present', 'absent'], 'default': 'present', 'required': False}, 'token': {'no_log': True, 'required': True, 'type': 'str'}, 'url': {'required': True, 'type': 'str'}, 'validate_certs': {'default': True, 'type': 'raw'}}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

Classes

class plugins.module_utils.utils.NautobotApiBase(**kwargs)
__init__(**kwargs)
class plugins.module_utils.utils.NautobotGraphQL(query_str, api=None, variables=None)
__init__(query_str, api=None, variables=None)
query()

Makes API call and checks response from GraphQL endpoint.

class plugins.module_utils.utils.NautobotModule(module, endpoint, client=None, remove_keys=None)

Initialize connection to Nautobot, sets AnsibleModule passed in to self.module to be used throughout the class :params module (obj): Ansible Module object :params endpoint (str): Used to tell class which endpoint the logic needs to follow :params nb_client (obj): pynautobot.api object passed in (not required)

__init__(module, endpoint, client=None, remove_keys=None)
_build_diff(before=None, after=None)

Builds diff of before and after changes

_build_payload(data, remove_keys)

Remove any key/value pairs that aren’t relevant for interacting with Nautobot.

Parameters:
  • data ([type]) – [description]

  • remove_keys ([type]) – [description]

Returns:

[description]

Return type:

[type]

_build_query_params(parent, module_data, user_query_params=None, child=None)
Returns dict(query_dict):

Returns a query dictionary built using mappings to dynamically

build available query params for Nautobot endpoints :params parent(str): This is either a key from _find_ids or a string passed in to determine which keys in the data that we need to use to construct query_dict :params module_data(dict): Uses the data provided to the Nautobot module :params child(dict): This is used within _find_ids and passes the inner dictionary to build the appropriate query_dict for the parent

_change_choices_id(endpoint, data)

Used to change data that is static and under _choices for the application. ex. DEVICE_STATUS :returns data (dict): Returns the user defined data back with updated fields for _choices :params endpoint (str): The endpoint that will be used for mapping to required _choices :params data (dict): User defined data passed into the module

_connect_api(url, token, ssl_verify, api_version)
_convert_identical_keys(data)

Used to change non-clashing keys for each module into identical keys that are required to be passed to pynautobot ex. rack_role back into role to pass to Nautobot Returns data :params data (dict): Data dictionary after _find_ids method ran

_create_object(nb_endpoint, data)

Create a Nautobot object. :returns tuple(serialized_nb_obj, diff): tuple of the serialized created Nautobot object and the Ansible diff.

_delete_object()

Delete a Nautobot object. :returns diff (dict): Ansible diff

_ensure_object_absent(endpoint_name, name)

Used when state is absent to make sure object does not exist :params endpoint_name (str): Endpoint name that was created/updated. ex. device :params name (str): Name of the object

_ensure_object_exists(nb_endpoint, endpoint_name, name, data)

Used when state is present to make sure object exists or if the object exists that it is updated :params nb_endpoint (pynautobot endpoint object): This is the nb endpoint to be used to create or update the object :params endpoint_name (str): Endpoint name that was created/updated. ex. device :params name (str): Name of the object :params data (dict): User defined data passed into the module

_fetch_choice_value(search, endpoint)
_find_app(endpoint)

Dynamically finds application of endpoint passed in using the API_APPS_ENDPOINTS for mapping :returns nb_app (str): The application the endpoint lives under :params endpoint (str): The endpoint requiring resolution to application

_find_ids(data, user_query_params)

Will find the IDs of all user specified data if resolvable :returns data (dict): Returns the updated dict with the IDs of user specified data :params data (dict): User defined data passed into the module

_get_query_param_id(match, data)

Used to find IDs of necessary searches when required under _build_query_params :returns id (int) or data (dict): Either returns the ID or original data passed in :params match (str): The key within the user defined data that is required to have an ID :params data (dict): User defined data passed into the module

_handle_errors(msg)

Returns message and changed = False :params msg (str): Message indicating why there is no change

_nb_endpoint_get(nb_endpoint, query_params, search_item)
_normalize_data(data)
Returns data (dict):

Normalized module data to formats accepted by Nautobot searches

Params data (dict):

Original data from Nautobot module

_remove_arg_spec_default(data)

Used to remove any data keys that were not provided by user, but has the arg spec default values

_update_object(data)

Update a Nautobot object. :returns tuple(serialized_nb_obj, diff): tuple of the serialized updated Nautobot object and the Ansible diff.

_validate_query_params(query_params)

Validate query_params that are passed in by users to make sure they’re valid and return error if they’re not valid.

_version_check_greater(greater, lesser, greater_or_equal=False)

Determine if first argument is greater than second argument.

Parameters:
  • greater (str) – decimal string

  • lesser (str) – decimal string

is_valid_uuid(match)

Determine if the match is already UUID.

run()

Must be implemented in subclasses