-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate_connections.py
More file actions
63 lines (50 loc) · 2.23 KB
/
migrate_connections.py
File metadata and controls
63 lines (50 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
migrate connections from one resource to a second
script assumes the target resource has same structure as source
"""
from typing import List
from cloudshell.api.cloudshell_api import CloudShellAPISession, ResourceInfo
from dataclasses import dataclass
@dataclass
class ConnectionData:
source_resource: str
target_resource: str
def _recursive_get_connections(children_resources: List[ResourceInfo], result: List=None):
"""
recursively get connections. No child resources is the base case
:param list[ResourceInfo] children_resources:
:param connections_list:
:return:
"""
result = result if result else []
for resource in children_resources:
connections = resource.Connections
children = resource.ChildResources
if not children:
if connections:
for connection in connections:
result.append(ConnectionData(resource.Name, connection.FullPath))
else:
_recursive_get_connections(children, result)
return result
def get_connection_list(api: CloudShellAPISession, root_resource_name: str):
root_details = api.GetResourceDetails(root_resource_name)
root_children = root_details.ChildResources
return _recursive_get_connections(root_children)
def migrate_connections(api: CloudShellAPISession, source_resource_name: str, target_resource_name: str) -> List[ConnectionData]:
connections = get_connection_list(api, source_resource_name)
for connection in connections:
source_split = connection.source_resource.split("/")
source_split[0] = target_resource_name
new_source = "/".join(source_split)
api.UpdatePhysicalConnection(resourceAFullPath=new_source,
resourceBFullPath=connection.target_resource,
overrideExistingConnections=True)
new_connections = get_connection_list(api, target_resource_name)
return new_connections
if __name__ == "__main__":
SOURCE_RESOURCE = "DUT Mock 2"
TARGET_RESOURCE = "mock_3"
api = CloudShellAPISession(host="localhost", username="admin", password="admin", domain="Global")
new_connections = migrate_connections(api, SOURCE_RESOURCE, TARGET_RESOURCE)
pass