summaryrefslogtreecommitdiffstats
path: root/docs/labs/lab08-resource-apis
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-05-11 09:04:53 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-05-13 09:34:21 +0000
commit47553c43d71b7b1144f912ab9679f5b60e858fa2 (patch)
tree08378beaeeea8f9bb2686d3037c7b6f5062bb948 /docs/labs/lab08-resource-apis
parentInitial commit. (diff)
downloadcvprac-47553c43d71b7b1144f912ab9679f5b60e858fa2.tar.xz
cvprac-47553c43d71b7b1144f912ab9679f5b60e858fa2.zip
Adding upstream version 1.3.1+dfsg.upstream/1.3.1+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/labs/lab08-resource-apis')
-rw-r--r--docs/labs/lab08-resource-apis/resource_cvprac.py187
-rw-r--r--docs/labs/lab08-resource-apis/topology_tag_assignment.py106
2 files changed, 293 insertions, 0 deletions
diff --git a/docs/labs/lab08-resource-apis/resource_cvprac.py b/docs/labs/lab08-resource-apis/resource_cvprac.py
new file mode 100644
index 0000000..e454fc9
--- /dev/null
+++ b/docs/labs/lab08-resource-apis/resource_cvprac.py
@@ -0,0 +1,187 @@
+# Copyright (c) 2021 Arista Networks, Inc.
+# Use of this source code is governed by the Apache License 2.0
+# that can be found in the COPYING file.
+
+from cvprac.cvp_client import CvpClient
+from pprint import pprint as pp
+import ssl
+ssl._create_default_https_context = ssl._create_unverified_context
+import requests.packages.urllib3
+requests.packages.urllib3.disable_warnings()
+
+# Reading the service account token from a file
+with open("token.tok") as f:
+ token = f.read().strip('\n')
+
+clnt = CvpClient()
+clnt.connect(nodes=['cvp1'], username='',password='',api_token=token)
+
+def get_events_all(client):
+ ''' Get All events '''
+ event_url = '/api/resources/event/v1/Event/all'
+ response = client.get(event_url)
+ return response['data']
+
+def get_event(client, key, ts):
+ event_url = '/api/resources/event/v1/Event?'
+ url = event_url + 'key.key=' + key + "&key.timestamp=" + ts
+ response = client.get(url)
+ return response
+
+def get_events_t1_t2(client, t1, t2):
+ event_url = '/api/resources/event/v1/Event/all?'
+ url = event_url + 'time.start=' + t1 + "&time.end=" + t2
+ response = client.get(url)
+ return response['data']
+
+def get_events_by_severity(client, severity):
+ payload = {"partialEqFilter": [{"severity": severity }]}
+ event_url = '/api/resources/event/v1/Event/all'
+ response = client.post(event_url, data=payload)
+ if 'data' in response.keys():
+ return response['data']
+ else:
+ return response
+
+def get_events_by_type(client, etype):
+ payload = {"partialEqFilter": [{"eventType": etype }]}
+ event_url = '/api/resources/event/v1/Event/all'
+ response = client.post(event_url, data=payload)
+ if 'data' in response.keys():
+ return response['data']
+ else:
+ return response
+
+def get_active_devices(client):
+ ''' Get active devices '''
+ dev_url = '/api/resources/inventory/v1/Device/all'
+ devices_data = client.get(dev_url)
+ devices = []
+ for device in devices_data['data']:
+ try:
+ if device['result']['value']['streamingStatus'] == "STREAMING_STATUS_ACTIVE":
+ devices.append(device['result']['value']['hostname'])
+ # pass on archived datasets
+ except KeyError as e:
+ continue
+ return devices
+
+def get_all_device_tags(client):
+ tag_url = '/api/resources/tag/v1/DeviceTag/all'
+ tag_data = client.get(tag_url)
+ tags = []
+ for tag in tag_data['data']:
+ tags.append({tag['result']['value']['key']['label']:tag['result']['value']['key']['value']})
+ return tags
+
+def get_all_interface_tags(client):
+ tag_url = '/api/resources/tag/v1/InterfaceTagAssignmentConfig/all'
+ tags = client.get(tag_url)
+ return tags['data']
+
+def filter_interface_tag(client, dId=None, ifId=None, label=None, value=None):
+ tag_url = '/api/resources/tag/v1/InterfaceTagAssignmentConfig/all'
+ payload = {
+ "partialEqFilter": [
+ {"key": {"deviceId": dId, "interfaceId": ifId, "label": label, "value": value}}
+ ]
+ }
+ response = client.post(tag_url, data=payload)
+ return response
+
+def create_itag(client, label, value):
+ tag_url = '/api/resources/tag/v1/InterfaceTagConfig'
+ payload = {"key":{"label":label,"value":value}}
+ response = client.post(tag_url, data=payload)
+ return response
+
+def assign_itag(client, dId, ifId, label, value):
+ tag_url = '/api/resources/tag/v1/InterfaceTagAssignmentConfig'
+ payload = {"key":{"label":label, "value":value, "deviceId": dId, "interfaceId": ifId}}
+ response = client.post(tag_url, data=payload)
+ return response
+
+def create_dtag(client, label, value):
+ tag_url = '/api/resources/tag/v1/DeviceTagConfig'
+ payload = {"key":{"label":label,"value":value}}
+ response = client.post(tag_url, data=payload)
+ return response
+
+def assign_dtag(client, dId, label, value):
+ tag_url = '/api/resources/tag/v1/DeviceTagAssignmentConfig'
+ payload = {"key":{"label":label, "value":value, "deviceId": dId}}
+ response = client.post(tag_url, data=payload)
+ return response
+
+### Uncomment the below functions/print statement to test
+
+# ### Get all active events
+# print ('=== All active events ===')
+# cvpevents = get_events_all(clnt)
+# for event in cvpevents:
+# print(event)
+
+# ### Get a specific event
+# key = "6098ae39e4c8a9d7"
+# ts ="2021-04-06T21:53:00Z"
+# get_event(clnt, key, ts)
+
+# ### Get events between two dates
+# t1 = "2021-04-06T09:00:00Z"
+# t2 = "2021-04-06T14:00:00Z"
+# events = get_events_t1_t2(clnt, t1, t2)
+# print(f"=== Events between {t1} and {t2} ===")
+# pp(events)
+
+# ### Get all INFO severity events ###
+# # EVENT_SEVERITY_UNSPECIFIED = 0
+# # EVENT_SEVERITY_INFO = 1
+# # EVENT_SEVERITY_WARNING = 2
+# # EVENT_SEVERITY_ERROR = 3
+# # EVENT_SEVERITY_CRITICAL = 4
+# ####################################
+
+# severity = 1 ## Severity INFO
+# info = get_events_by_severity(clnt, severity)
+# print('=== Get all INFO severity events ===')
+# pp(info)
+
+# ### Get specific event types
+
+# etype = "LOW_DEVICE_DISK_SPACE"
+# event = get_events_by_type(clnt, etype)
+# print('=== Get all Low Disk Space events ===')
+# pp(event)
+
+# ### Get the inventory
+# print ('=== Inventory ===')
+# print(get_active_devices(clnt))
+
+# ### Get all devie tags
+# print('=== Device Tags ===' )
+# for tag in get_all_device_tags(clnt):
+# print (tag)
+
+# ### Get all interface tag assignments
+# print(get_all_interface_tags(clnt))
+
+# ### Get all interfaces that have a tag with a specific value on a device
+# print(filter_interface_tag(clnt, dId="JPE14070534", value="speed40Gbps"))
+
+# ### Get all tags for an interface of a device
+# print(filter_interface_tag(clnt, dId="JPE14070534", ifId="Ethernet1"))
+
+# ### Get all interfaces that have a specific tag assigned
+# print(filter_interface_tag(clnt, dId="JPE14070534", label="lldp_hostname"))
+
+# ### Create an interface tag
+# create_itag(clnt, "lldp_chassis", "50:08:00:0d:00:48")
+
+# ### Assign an interface tag
+# assign_itag(clnt, "JPE14070534", "Ethernet4", "lldp_chassis", "50:08:00:0d:00:38")
+
+# ### Create a device tag
+# create_dtag(clnt, "topology_hint_pod", "ire-pod11")
+
+# ### Assign an interface tag
+# assign_dtag(clnt, "JPE14070534", "topology_hint_pod", "ire-pod11" )
diff --git a/docs/labs/lab08-resource-apis/topology_tag_assignment.py b/docs/labs/lab08-resource-apis/topology_tag_assignment.py
new file mode 100644
index 0000000..973e9f7
--- /dev/null
+++ b/docs/labs/lab08-resource-apis/topology_tag_assignment.py
@@ -0,0 +1,106 @@
+# Copyright (c) 2021 Arista Networks, Inc.
+# Use of this source code is governed by the Apache License 2.0
+# that can be found in the COPYING file.
+
+# In this example we are going to assign topology tags using the tags.v2 workspace aware API
+# More details on tag.v2 can be found at https://aristanetworks.github.io/cloudvision-apis/models/tag.v2/
+# NOTE: Tag.v2 can be used for assigning both device and interface tags (studios, topology, etc) and it's not
+# limited to topology tags only.
+# The following are some of the built-in tags that can be used to modify the Topology rendering:
+# topology_hint_type: < core | edge | endpoint | management | leaf | spine >
+# topology_hint_rack: < rack name as string >
+# topology_hint_pod: < pod name as string >
+# topology_hint_datacenter: < datacenter name as string >
+# topology_hint_building: < building name as string >
+# topology_hint_floor: < floor name as string >
+# topology_network_type: < datacenter | campus | cloud >
+
+from cvprac.cvp_client import CvpClient
+import uuid
+from datetime import datetime
+
+# Reading the service account token from a file
+with open("token.tok") as f:
+ token = f.read().strip('\n')
+
+# Create connection to CloudVision
+clnt = CvpClient()
+clnt.connect(nodes=['cvp1'], username='',password='',api_token=token)
+
+tags_common = [{"key": "topology_hint_pod", "value": "tp-avd-pod1"},
+ {"key": "topology_hint_datacenter", "value": "tp-avd-dc1"}]
+tags_leaf1 = [{"key": "topology_hint_rack", "value": "tp-avd-leafs1"},
+ {"key": "topology_hint_type", "value": "leaf"}]
+tags_leaf2 = [{"key": "topology_hint_rack", "value": "tp-avd-leafs2"},
+ {"key": "topology_hint_type", "value": "leaf"}]
+tags_spines = [{"key": "topology_hint_rack", "value": "tp-avd-spines"},
+ {"key": "topology_hint_type", "value": "spine"}]
+
+# Create workspace
+display_name = f"Change_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
+workspace_id = str(uuid.uuid4())
+clnt.api.workspace_config(workspace_id,display_name)
+
+### Create tags
+element_type = "ELEMENT_TYPE_DEVICE"
+
+for tag in tags_common+tags_leaf1+tags_leaf2+tags_spines:
+ tag_label = tag['key']
+ tag_value = tag['value']
+ clnt.api.tag_config(element_type, workspace_id, tag_label, tag_value)
+
+### Assign tags
+devices = {"leafs1":["BAD032986065E8DC14CBB6472EC314A6","0123F2E4462997EB155B7C50EC148767"],
+ "leafs2":["8520AF39790A4EC959550166DC5DEADE", "6323DA7D2B542B5D09630F87351BEA41"],
+ "spines":["CD0EADBEEA126915EA78E0FB4DC776CA", "2568DB4A33177968A78C4FD5A8232159"]}
+
+for tag in tags_common+tags_leaf1:
+ tag_label = tag['key']
+ tag_value = tag['value']
+ interface_id = ''
+ for leaf in devices['leafs1']:
+ device_id = leaf
+ clnt.api.tag_assignment_config(element_type, workspace_id, tag_label, tag_value, device_id, interface_id)
+for tag in tags_common+tags_leaf2:
+ tag_label = tag['key']
+ tag_value = tag['value']
+ interface_id = ''
+ for leaf in devices['leafs2']:
+ device_id = leaf
+ clnt.api.tag_assignment_config(element_type, workspace_id, tag_label, tag_value, device_id, interface_id)
+for tag in tags_common+tags_spines:
+ tag_label = tag['key']
+ tag_value = tag['value']
+ interface_id = ''
+ for spine in devices['spines']:
+ device_id = spine
+ clnt.api.tag_assignment_config(element_type, workspace_id, tag_label, tag_value, device_id, interface_id)
+
+### Start build
+request = 'REQUEST_START_BUILD'
+request_id = 'b1'
+description='testing cvprac build'
+clnt.api.workspace_config(workspace_id=workspace_id, display_name=display_name,
+ description=description, request=request, request_id=request_id)
+
+### Check workspace build status and proceed only after it finishes building
+b = 0
+while b == 0:
+ build_id = request_id
+ # Requesting for the build status too fast might fail if the build start didn't finish creating
+ # the build with the request_id/build_id
+ while True:
+ try:
+ request = clnt.api.workspace_build_status(workspace_id, build_id)
+ break
+ except Exception as e:
+ continue
+ if request['value']['state'] == 'BUILD_STATE_SUCCESS':
+ b = b+1
+ else:
+ continue
+
+### Submit workspace
+request = 'REQUEST_SUBMIT'
+request_id = 's1'
+clnt.api.workspace_config(workspace_id=workspace_id,display_name=display_name,description=description,request=request,request_id=request_id)