| 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 |
# In this example we are going to assign topology tags using the tags.v2 workspace aware API |
| 6 |
# More details on tag.v2 can be found at https://aristanetworks.github.io/cloudvision-apis/models/tag.v2/ |
| 7 |
# NOTE: Tag.v2 can be used for assigning both device and interface tags (studios, topology, etc) and it's not |
| 8 |
# limited to topology tags only. |
| 9 |
# The following are some of the built-in tags that can be used to modify the Topology rendering: |
| 10 |
# topology_hint_type: < core | edge | endpoint | management | leaf | spine > |
| 11 |
# topology_hint_rack: < rack name as string > |
| 12 |
# topology_hint_pod: < pod name as string > |
| 13 |
# topology_hint_datacenter: < datacenter name as string > |
| 14 |
# topology_hint_building: < building name as string > |
| 15 |
# topology_hint_floor: < floor name as string > |
| 16 |
# topology_network_type: < datacenter | campus | cloud > |
| 17 |
|
| 18 |
from cvprac.cvp_client import CvpClient |
| 19 |
import uuid |
| 20 |
from datetime import datetime |
| 21 |
|
| 22 |
# Reading the service account token from a file |
| 23 |
with open("token.tok") as f: |
| 24 |
token = f.read().strip('\n') |
| 25 |
|
| 26 |
# Create connection to CloudVision |
| 27 |
clnt = CvpClient() |
| 28 |
clnt.connect(nodes=['cvp1'], username='',password='',api_token=token) |
| 29 |
|
| 30 |
tags_common = [{"key": "topology_hint_pod", "value": "tp-avd-pod1"}, |
| 31 |
{"key": "topology_hint_datacenter", "value": "tp-avd-dc1"}] |
| 32 |
tags_leaf1 = [{"key": "topology_hint_rack", "value": "tp-avd-leafs1"}, |
| 33 |
{"key": "topology_hint_type", "value": "leaf"}] |
| 34 |
tags_leaf2 = [{"key": "topology_hint_rack", "value": "tp-avd-leafs2"}, |
| 35 |
{"key": "topology_hint_type", "value": "leaf"}] |
| 36 |
tags_spines = [{"key": "topology_hint_rack", "value": "tp-avd-spines"}, |
| 37 |
{"key": "topology_hint_type", "value": "spine"}] |
| 38 |
|
| 39 |
# Create workspace |
| 40 |
display_name = f"Change_{datetime.now().strftime('%Y%m%d_%H%M%S')}" |
| 41 |
workspace_id = str(uuid.uuid4()) |
| 42 |
clnt.api.workspace_config(workspace_id,display_name) |
| 43 |
|
| 44 |
### Create tags |
| 45 |
element_type = "ELEMENT_TYPE_DEVICE" |
| 46 |
|
| 47 |
for tag in tags_common+tags_leaf1+tags_leaf2+tags_spines: |
| 48 |
tag_label = tag['key'] |
| 49 |
tag_value = tag['value'] |
| 50 |
clnt.api.tag_config(element_type, workspace_id, tag_label, tag_value) |
| 51 |
|
| 52 |
### Assign tags |
| 53 |
devices = {"leafs1":["BAD032986065E8DC14CBB6472EC314A6","0123F2E4462997EB155B7C50EC148767"], |
| 54 |
"leafs2":["8520AF39790A4EC959550166DC5DEADE", "6323DA7D2B542B5D09630F87351BEA41"], |
| 55 |
"spines":["CD0EADBEEA126915EA78E0FB4DC776CA", "2568DB4A33177968A78C4FD5A8232159"]} |
| 56 |
|
| 57 |
for tag in tags_common+tags_leaf1: |
| 58 |
tag_label = tag['key'] |
| 59 |
tag_value = tag['value'] |
| 60 |
interface_id = '' |
| 61 |
for leaf in devices['leafs1']: |
| 62 |
device_id = leaf |
| 63 |
clnt.api.tag_assignment_config(element_type, workspace_id, tag_label, tag_value, device_id, interface_id) |
| 64 |
for tag in tags_common+tags_leaf2: |
| 65 |
tag_label = tag['key'] |
| 66 |
tag_value = tag['value'] |
| 67 |
interface_id = '' |
| 68 |
for leaf in devices['leafs2']: |
| 69 |
device_id = leaf |
| 70 |
clnt.api.tag_assignment_config(element_type, workspace_id, tag_label, tag_value, device_id, interface_id) |
| 71 |
for tag in tags_common+tags_spines: |
| 72 |
tag_label = tag['key'] |
| 73 |
tag_value = tag['value'] |
| 74 |
interface_id = '' |
| 75 |
for spine in devices['spines']: |
| 76 |
device_id = spine |
| 77 |
clnt.api.tag_assignment_config(element_type, workspace_id, tag_label, tag_value, device_id, interface_id) |
| 78 |
|
| 79 |
### Start build |
| 80 |
request = 'REQUEST_START_BUILD' |
| 81 |
request_id = 'b1' |
| 82 |
description='testing cvprac build' |
| 83 |
clnt.api.workspace_config(workspace_id=workspace_id, display_name=display_name, |
| 84 |
description=description, request=request, request_id=request_id) |
| 85 |
|
| 86 |
### Check workspace build status and proceed only after it finishes building |
| 87 |
b = 0 |
| 88 |
while b == 0: |
| 89 |
build_id = request_id |
| 90 |
# Requesting for the build status too fast might fail if the build start didn't finish creating |
| 91 |
# the build with the request_id/build_id |
| 92 |
while True: |
| 93 |
try: |
| 94 |
request = clnt.api.workspace_build_status(workspace_id, build_id) |
| 95 |
break |
| 96 |
except Exception as e: |
| 97 |
continue |
| 98 |
if request['value']['state'] == 'BUILD_STATE_SUCCESS': |
| 99 |
b = b+1 |
| 100 |
else: |
| 101 |
continue |
| 102 |
|
| 103 |
### Submit workspace |
| 104 |
request = 'REQUEST_SUBMIT' |
| 105 |
request_id = 's1' |
| 106 |
clnt.api.workspace_config(workspace_id=workspace_id,display_name=display_name,description=description,request=request,request_id=request_id) |