cvprac_abstraction package¶
-
cvprac_abstraction.
config_read
(config_file='actions.json')[source]¶ Read JSON configuration.
Load information from JSON file defined in
config_file
First, method check if file exists or not and then try to load content usingjson.load()
If file is not a JSON or if file does not exist, method return NoneData structure to read:
[ { "name": "Change CVX to EVPN", "type": "container", "action": "attach-device", "container": "CVX", "apply": true, "devices": [ "leaf1", "leaf2", "cvx01" ] }, ... ]
Parameters: config_file (str) – Path to the configuration file Returns: Json structure with all actions to execute Return type: json
-
cvprac_abstraction.
connect_to_cvp
(parameters, log_level='WARNING')[source]¶ Create a CVP connection.
- parameters option should at least contain following elements:
- username
- password
- cvp (server IP or DNS hostname)
Parameters: Returns: cvp client object
Return type: cvprac.CvpClient()
-
cvprac_abstraction.
load_constant
(key_name, default='UNSET', verbose=False)[source]¶ Set up constant value from OS Environment.
Help to define CONSTANT from OS Environment. If it is not defined, then, fallback to default value provided within parameters
:Example:
>>> USERNAME = load_constant(key_name='USERNAME_1', default='myUser') >>> print USERNAME >>> myUsername
Parameters: Returns: Value to use to configure variable
Return type:
Submodules¶
cvprac_abstraction.cvpChangeControl module¶
-
class
cvprac_abstraction.cvpChangeControl.
CvpChangeControl
(cvp_server, name='Automated_Change_Control')[source]¶ Bases:
object
Change-control class to provide generic method for CVP CC mechanism.
- Change Control structure is based on:
- A name to identify change
- A list of tasks already created on CVP and on pending state
- An optional scheduling. If no schedule is defined,
- then task will be run 3 minutes after creatio of CC
List of public available methods:
Example
>>> from cvprac_abstraction import CVP >>> from cvprac_abstraction import connect_to_cvp >>> from cvprac_abstraction.cvpConfiglet import CvpChangeControl >>> >>> parameters['cvp'] = '127.0.0.1' >>> parameters['username'] = 'arista' >>> parameters['password'] = 'arista' >>> >>> client = connect_to_cvp(parameters) >>> >>> change_control = CvpChangeControl(cvp_server=client, name='MyChanegControl') >>> result = change_control.create(tz=timezone, country='FR', schedule=True, schedule_at='2019-03-01-12h00', snap_template="snapshotTemplate_9_4694793526491", change_type='Custom', stop_on_error="true") >>>
Warning
- Change Control execution is not running snapshot before and after with cvprac 1.0.1
-
__init__
(cvp_server, name='Automated_Change_Control')[source]¶ Class Constructor.
- Build class content with followinactivities:
- save cvp_server information
- save name for CC
- instanciate list for tasks
- Collect tasks available from CVP
Parameters: - cvp_server (CvpClient) – CVP Server information
- name (str) – Optional - Name of the Change Control.
Default is
Automated_Change_Control
-
_build_change_dictionnary
(order_mode='linear')[source]¶ Build ordered list to schedule changes.
CVP Change Control expect a list with an order to run tasks. By default, all tasks are executed at the same time. But using order_mode set to incremental every task will be scheduled sequentially in this change-control
Parameters: order_mode (str) – Optional - Method to build task list. Shall be linear
orincremental
.Note
Only linear has been tested.
-
_retrieve_tasks
()[source]¶ Extract tasks from CVP Server.
Connect to CVP server and collect tasks in pending state These tasks are saved in self._available structure dedicated to pending tasks.
-
add_task
(task)[source] Add a tasks to available list.
This task attach this new tasks to the pending tasks list.
Parameters: task (str) – TaskID from CVP server
-
create
(mode='linear', country='France', tz='Europe/Paris', schedule=False, schedule_at='', snap_template='1708dd89-ff4b-4d1e-b09e-ee490b3e27f0', change_type='Custom', stop_on_error='true')[source] Create a change-control.
Parameters: - mode (str) – Optional - method to order tasks (default : linear)
- country (str) – Optional - Country requested by CVP API (default:France)
- tz (str) – Optional - Timezone required by CVP (default: Europe/Paris)
- schedule (bool) – Optional - Enable CC scheduling (default: False)
- schedule_at (str) – Optional - Time to execute CC if scheduled
- snap_template (str) – Optional - Snapshot template ID to run before / after tasks
- change_type (str) – Optional - CVP definition for CC Might be Custom or Rollback. (default: Custom)
- stop_on_error (str) – Optional - boolean string to stop CVP on errors
Returns: CVP creation result (None if error occurs)
Return type:
-
get_list_changes
(mode='linear')[source] Return list of tasks and their execution order.
Parameters: mode (str) – Information about tasks scheduling. Shall be linear
orincremental
.Note
Only linear has been tested.
Returns: List of changes and their order Return type: list
-
get_tasks
(refresh=False)[source] Provide list of all available tasks.
Return list of all tasks getting from CVP and/or attached with add_task method.
Parameters: refresh (bool) – Optional - Make a call to CVP to get latest list of tasks Returns: List of available tasks found in this CC Return type: list
cvprac_abstraction.cvpConfiglet module¶
-
class
cvprac_abstraction.cvpConfiglet.
CvpConfiglet
(cvp_server, configlet_file=None, configlet_name=None)[source]¶ Bases:
object
Configlet class to provide generic method to manage CVP configlet.
Data Structure
Configlet structure is a name based dictionnary with following keys:
name
: Name of configlet. This name is built from filenamefile
: Complete path of the local configlet filecontent
: Local Configlet content read fromconfiglet['file']
key
: Key ID defined by CVP to identify configlet.- it is found by our instance during update, addition or deletion
devices
: List of devices structure compliant- with
CvpApi.get_device_by_name()
It can be found by usingCvpInventory
object.
List of attributes:
-
_cvp_server
¶ cvprac.CvpClient() object to manage CVP connection
-
_devices_configlet
¶ List of devices attached to configlet
-
_configlet
¶ Dictionary with all configlet information:
name
,file
,content
,key
,devices
-
_cvp_found
¶ Boolean to get status of configlet on CVP: True if configlet is on server, False other cases
List of public available methods:
-
update_configlet
()[source]¶ Start update process for that configlet. Do not deploy content to devices
Example
>>> from cvprac_abstraction import CVP >>> from cvprac_abstraction import connect_to_cvp >>> from cvprac_abstraction.cvpConfiglet import CvpConfiglet >>> >>> parameters['cvp'] = '127.0.0.1' >>> parameters['username'] = 'arista' >>> parameters['password'] = 'arista' >>> >>> client = connect_to_cvp(parameters) >>> >>> my_configlet = CvpConfiglet(cvp_server=client,configlet_file='/path/to/configlet') >>> >>> my_configlet.update_configlet() >>> >>> my_configlet.deploy_bulk()
Note
This class use calls to
cvprac
to get and push data to CVP server.-
__init__
(cvp_server, configlet_file=None, configlet_name=None)[source]¶ Class Constructor.
Parameters: - cvp_server (CvpClient) – CvpClient object from cvprack. Gives methods to manage CVP API
- configlet_file (str) – Path to configlet file.
-
_configlet_lookup
()[source]¶ Check if a configlet is already present on CVP.
Check if CVP has already a configlet configured with the same name. If yes return True and report key under self._configlet[‘key’] If no, return False
Returns: Return True
orFalse
if configlet name is already configured on CVPReturn type: bool
-
_retireve_devices
()[source]¶ Get list of devices attached to the configlet.
If configlet exists, then, retrieve a complete list of devices attached to it.
Returns: List of devices from CVP Return type: list
-
_wait_task
(task_id, timeout=10)[source]¶ Wait for Task execution.
As API call is asynchronous, task will run avec after receiving a status. This function implement a wait_for to get final status of a task As we have to protect against application timeout or task issue, a basic timeout has been implemented
Parameters:
-
add_device
(device_hostnames)[source]¶ Remove device(s) from a configlet.
Remove device from configlet and create a task on CVP to remove configuration generated by configlet from device. For every hostname defined in devices_hostnames, a lookup is done to get a complete data set for that device and a call to remove device is sent.
Warning
This function never send a call to execute task. it is managed by logic out of that object
- Arguments:
- devices_hostnames {list} – List of devices hostname to remove from
- the configlet.
-
delete_configlet
()[source] Delete a configlet from CVP.
To protect, function first check if configlet exists, if not, we stop and return to next action out of this function. Remove configlet from all devices where it is configured Then if configlet exist, remove configlet from CVP DB
Returns: True
if able to remove configlet / False otherwiseReturn type: bool
-
deploy
(device, schedule_at=None, task_timeout=10)[source] Deploy One configlet to One device.
This function manage a deployment this configlet to a given device already attached to the configlet.
Parameters: Warning
schedule_at
option is not yet implemented and shall not be usedReturns: message from server Return type: dict
-
deploy_bulk
(device_list=None, schedule_at=None, task_timeout=10)[source] Run configlet deployment against all devices.
Run configlet deployment over all devices attached to this configlet. Every single deployment are managed by function self.deploy()
Parameters: Warning
schedule_at
option is not yet implemented and shall not be usedReturns: A list of tasks executed for the deployment Return type: list
-
deploy_configlet
(device_hostnames)[source] Create configlet on CVP with content from object.
Create a new configlet on CVP server and attached it to all devices you provide in your JSON file. Device attachement is managed with a CvpInventory call to get all information from CVP. It means you just have to provide existing hostname in your JSON
Each time a device is attached to configlet on CVP, it is also added in CvpConfiglet object for futur use
Parameters: devices_hostname (list) – List of hostname to attached to configlet
-
get_configlet_info
()[source]¶ To share configlet information.
Returns: dictionnary with configlet information Return type: dict
-
get_devices
(refresh=False)[source] To share list of devices attached to the configlet.
If list is empty or if refresh trigger is active, function will get a new list of device from self._retireve_devices() Otherwise, just send back list to the caller
Parameters: refresh (bool) – Update device list from CVP (Optional) Returns: List of devices from CVP Return type: list
-
name
()[source]¶ Expose name of the configlet.
Returns: Name of configlet built by __init__
Return type: str
-
on_cvp
()[source] Expose flag about configlet configured on CVP.
Return True if configlet is configured on CVP and can be updated. If configlet is not present, then, False
Returns: True if configlet already configured on CVP, False otherwise Return type: bool
-
remove_device
(devices_hostnames)[source]¶ Remove device(s) from a configlet.
Remove device from configlet and create a task on CVP to remove configuration generated by configlet from device. For every hostname defined in devices_hostnames, a lookup is done to get a complete data set for that device and a call to remove device is sent.
Warning
This function never send a call to execute task. it is managed by logic out of that object
- Arguments:
- devices_hostnames {list} – List of devices hostname to remove from
- the configlet.
-
update_configlet
()[source] Update configlet on CVP with content from object.
Check if configlet is configured on CVP server before pushing an update. If configlet is not there, then, stop method execution.
Returns: str Return type: message from server with result
cvprac_abstraction.cvpContainer module¶
-
class
cvprac_abstraction.cvpContainer.
CvpContainer
(name, cvp_server)[source]¶ Bases:
object
Class to manage Container on CVP.
Centralize a abstraction layer of CVPRAC to manage actions related to container.
List of public available methods:
Example
>>> from cvprac_abstraction import CVP >>> from cvprac_abstraction import connect_to_cvp >>> from cvprac_abstraction.cvpConfiglet import CvpContainer >>> >>> parameters['cvp'] = '127.0.0.1' >>> parameters['username'] = 'arista' >>> parameters['password'] = 'arista' >>> >>> client = connect_to_cvp(parameters) >>> >>> container = CvpContainer(name='My New Container', cvp_server=client) >>> >>> container.create(parent_name='My Root Container')
Note
This class use calls to
cvprac
to get and push data to CVP server.-
__init__
(name, cvp_server)[source]¶ Class Constructor.
Parameters: - name (str) – container’s name to look for on CloudVision server
- cvp_server (cvprack.CvpClient()) – Object in charge of sending API calls to CVP server.
Returns: Return type:
-
_container_id
(name=None)[source]¶ Get Container ID based on its name.
Parameters: name (str, optional) – Container name to get ID, by default None Returns: container ID configured on CVP Return type: str
-
_container_info
(name=None)[source]¶ Pull CVP to get container information.
Execute a call against CVP to get a dict of information.
Structure is:
{ u'dateTimeInLongFormat': 1513002053415, u'key': u'container_8_2864853689536', u'mode': u'expand', u'name': u'CVX', u'root': False, u'undefined': False, u'userId': u'arista' }
Parameters: name ([type], optional) – Name of container to pull. If not set, name of container used for this instance is configured, by default None Returns: Structure sent back by CVP Return type: dict
-
_get_devices
()[source]¶ Get list of devices attached to container.
Extract information from CVP to get complete list of devices attached to this container on CVP. Result is saved part of this object.
-
_wait_task
(task_id, timeout=10)[source]¶ Wait for Task execution.
As API call is asynchronous, task will run avec after receiving a status. This function implement a wait_for to get final status of a task As we have to protect against application timeout or task issue, a basic timeout has been implemented
Parameters: Returns: Last status message collected from the server
Return type:
-
attach_device
(hostname, deploy=False)[source] Move device to container
Move device within container represented in this object. This method create a task to be executed later by user or by the script itself.
Parameters: Returns: Task ID created by the change on CVP.
Return type:
-
attach_device_bulk
(hostname_list, deploy=False)[source] Attach a list of device to existing container.
Get a list of hostname to move to current container. For every hostname, a call to CVP is sent to get device’s information and build structure to move it to appropriate container.
Parameters:
-
create
(parent_name='Tenant')[source] Create a container on CVP.
Implement workflow to create a container on CVP. Manage following actions: - Collect Parent container information - Create container.
Parameters: parent_name (str, optional) – Name of parent container to use to attach container, by default ‘Tenant’
-
destroy
(parent_name='Tenant')[source] Remove a container from CVP topology.
Parameters: parent_name (str, optional) – Name of the parent container, by default “Tenant” Returns: Return Nothing Return type: None
-
get_info
()[source] Return container’s information.
Return container’s information pulling from CloudVision server.
Structure is:
{ u'dateTimeInLongFormat': 1513002053415, u'key': u'container_8_2864853689536', u'mode': u'expand', u'name': u'CVX', u'root': False, u'undefined': False, u'userId': u'arista' }
Returns: Container information from CVP Return type: dict
-
is_device_attached
(hostname)[source] Test wether or not a device is part of container.
Test if device hostname is already attached to this container. it is based on list provided by self._get_devices()
Parameters: hostname (str) – Hostname to search in container. Returns: True if device is part of container, False if not found. Return type: boolean
-
run_pending
(task_timeout=10)[source] Execute pending tasks related to container
Run tasks created when you change container. It does not manage tasks from other objects.
>>> status = my_container.run_pending() >>> print status [{id:200, status: completed}, {id:201, status: completed}]
Parameters: task_timeout (int, optional) – timer to wait for task completion, by default 10 Returns: A list of dictionary where every entry is result of a task: Return type: list()
-
cvprac_abstraction.cvpInventory module¶
-
class
cvprac_abstraction.cvpInventory.
CvpInventory
(cvp_server=<cvprac.cvp_client.CvpClient object>)[source]¶ Bases:
object
CVP Inventory Class.
Get complete inventory from CVP and expose some functions to get data. It is RO only and nothing is pushed to CVP with this object.
-
__init__
(cvp_server=<cvprac.cvp_client.CvpClient object>)[source]¶ Class Constructor.
Instantiate an Inventory with a REST call to get device list
Parameters: cvp_server (cvprack.CvpClient()) – Your CVP Rack server
-