Debian Codegraph @

docs/labs/lab05-device-management/add_image_wo_tempaction.py

raw
1 # Copyright (c) 2021 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the COPYING file.
4
5 from cvprac.cvp_client import CvpClient
6 import json
7 import ssl
8 ssl._create_default_https_context = ssl._create_unverified_context
9 import requests.packages.urllib3
10 requests.packages.urllib3.disable_warnings()
11
12 # Create connection to CloudVision
13 clnt = CvpClient()
14 clnt.connect(['cvp1'],'username', 'password')
15
16 image_name = "vEOS-4.26.0.1F"
17 image = clnt.api.get_image_bundle_by_name(image_name)
18
19 device_name = "tp-avd-leaf2"
20 device = clnt.api.get_device_by_name(device_name)
21
22 def apply_image_to_element_no_temp(image, element, name, id_type, create_task=True):
23 ''' Apply an image bundle to a device or container
24 A copy of the appl_image_to_element() function without creating a tempAction.
25 Useful in situations where we need to call saveTopology on a per tempAction basis,
26 which is only possible if the addTempAction function is not used and the data
27 that we would've passed in the addTempAction call is passed in the
28 saveTopology call.
29 Args:
30 image (dict): The image info.
31 element (dict): Info about the element to apply an image to. Dict
32 can contain device info or container info.
33 name (str): Name of the element the image is being applied to.
34 id_type (str): - Id type of the element the image is being applied to
35 - can be 'netelement' or 'container'
36 create_task (bool): Determines whether or not to execute a save
37 and create the tasks (if any)
38 Returns:
39 response (list): A list that contains the tempAction data
40 Ex: [{'NetworkRollbackTask': False,
41 'taskJson': '[{
42 "info": "Apply image: vEOS-4.26.0.1F to netelement tp-avd-leaf2",
43 "infoPreview": "Apply image: vEOS-4.26.0.1F to netelement tp-avd-leaf2",
44 "note": "",
45 "action": "associate", "nodeType":
46 "imagebundle",
47 "nodeId": "imagebundle_1622072231719691917",
48 "toId": "50:08:00:b1:5b:0b",
49 "toIdType": "netelement",
50 "fromId": "",
51 "nodeName": "vEOS-4.26.0.1F",
52 "fromName": "", "
53 toName": "tp-avd-leaf2",
54 "childTasks": [],
55 "parentTask": ""}]'}]
56 '''
57
58 print('Attempt to apply %s to %s %s' % (image['name'],
59 id_type, name))
60 info = 'Apply image: %s to %s %s' % (image['name'], id_type, name)
61 node_id = ''
62 if 'imageBundleKeys' in image:
63 if image['imageBundleKeys']:
64 node_id = image['imageBundleKeys'][0]
65 print('Provided image is an image object.'
66 ' Using first value from imageBundleKeys - %s'
67 % node_id)
68 if 'id' in image:
69 node_id = image['id']
70 print('Provided image is an image bundle object.'
71 ' Found v1 API id field - %s' % node_id)
72 elif 'key' in image:
73 node_id = image['key']
74 print('Provided image is an image bundle object.'
75 ' Found v2 API key field - %s' % node_id)
76 data = [
77 {
78 "NetworkRollbackTask": False,
79 "taskJson": json.dumps([{'info': info,
80 'infoPreview': info,
81 'note': '',
82 'action': 'associate',
83 'nodeType': 'imagebundle',
84 'nodeId': node_id,
85 'toId': element['key'],
86 'toIdType': id_type,
87 'fromId': '',
88 'nodeName': image['name'],
89 'fromName': '',
90 'toName': name,
91 'childTasks': [],
92 'parentTask': ''}])
93 }
94 ]
95 return data
96
97 create_task = False
98 tempAction = apply_image_to_element_no_temp(image, device, device['fqdn'], 'netelement', create_task)
99
100 clnt.api._save_topology_v2(tempAction)