|
| 1 | +import xml.etree.ElementTree as ET |
| 2 | +from xml.etree.ElementTree import ElementTree |
| 3 | + |
| 4 | + |
| 5 | +def get_element_tree_from_str(xml_str: str) -> ElementTree: |
| 6 | + """ |
| 7 | + https://stackoverflow.com/a/18281386 |
| 8 | + """ |
| 9 | + return ET.ElementTree(ET.fromstring(xml_str)) |
| 10 | + |
| 11 | + |
| 12 | +def get_element_tree_from_file_path(file_path: str) -> ElementTree: |
| 13 | + return ET.parse(file_path) |
| 14 | + |
| 15 | + |
| 16 | +def edit_vlan_id_in_tree(element_tree: ElementTree, target_service_model: str, new_vlan_id: str) -> ElementTree: |
| 17 | + """ |
| 18 | + Iterate over services, find ServiceName that matches service model |
| 19 | + Iterate over the attributes, update the VLAN ID attribute |
| 20 | + write to tree |
| 21 | + """ |
| 22 | + root_element = element_tree.getroot() |
| 23 | + for curr_service in root_element.iter("Service"): |
| 24 | + service_data: dict = curr_service.attrib |
| 25 | + if service_data["ServiceName"] != target_service_model: |
| 26 | + continue |
| 27 | + for curr_attr in curr_service.iter("Attribute"): |
| 28 | + attr_data: dict = curr_attr.attrib |
| 29 | + if attr_data["Name"] == "VLAN ID": |
| 30 | + attr_data["Value"] = new_vlan_id |
| 31 | + return element_tree |
| 32 | + raise ValueError("Target Service not found in Element Tree") |
| 33 | + |
| 34 | + |
| 35 | +def update_xml_tree(xml_path: str, target_service_model: str, new_vlan_id: str): |
| 36 | + element_tree = get_element_tree_from_file_path(xml_path) |
| 37 | + element_tree = edit_vlan_id_in_tree(element_tree, target_service_model, new_vlan_id) |
| 38 | + element_tree.write(xml_path) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + TEST_TOPOLOGY_XML = """ |
| 43 | + <TopologyInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 44 | + <Details Name="vlan dev" Alias="vlan dev" Driver="Python Setup & Teardown" SetupDuration="10" TeardownDuration="10" Public="false" DefaultDuration="120" EnableSandboxSave="true" AbstractOnSavePolicy="Default" IsPersistentSandbox="false" TopologyId="f36c4723-8496-4d5b-9b61-8374d01c0fe2" BaseTopologyId=""> |
| 45 | + <Description>Blueprint with preconfigured setup & teardown processes.Deploys Apps and resolves connections on Setup, and deletes App VMs on Teardown</Description> |
| 46 | + <Categories /> |
| 47 | + <Scripts> |
| 48 | + <Script Name="Default Sandbox Setup 4.0" /> |
| 49 | + </Scripts> |
| 50 | + <Diagram Zoom="1" NodeSize="Medium" /> |
| 51 | + </Details> |
| 52 | + <Services> |
| 53 | + <Service PositionX="1034.09091186523" PositionY="169.01136779785202" Alias="VLAN Manual" ServiceName="VLAN Manual"> |
| 54 | + <Attributes> |
| 55 | + <Attribute Name="VLAN ID" Value="169" /> |
| 56 | + <Attribute Name="Dummy Attr" Value="Hi" /> |
| 57 | + </Attributes> |
| 58 | + </Service> |
| 59 | + <Service PositionX="100" PositionY="100" Alias="Dummy Service" ServiceName="DummyService"> |
| 60 | + <Attributes> |
| 61 | + <Attribute Name="Dummy Attr 5" Value="179" /> |
| 62 | + <Attribute Name="Dummy Attr" Value="Hi" /> |
| 63 | + </Attributes> |
| 64 | + </Service> |
| 65 | + </Services> |
| 66 | + <Apps /> |
| 67 | + </TopologyInfo> |
| 68 | + """ |
| 69 | + element_tree = get_element_tree_from_str(TEST_TOPOLOGY_XML) |
| 70 | + element_tree = edit_vlan_id_in_tree(element_tree, "VLAN Manual", "66") |
| 71 | + root = element_tree.getroot() |
| 72 | + ET.indent(root) |
| 73 | + print(ET.tostring(root, encoding="unicode")) |
0 commit comments