-
Notifications
You must be signed in to change notification settings - Fork 1k
[conn graph] add support for multiple graph files #2194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4c4b5e5
16a2b02
fbc5e2a
de2c7a6
00db93d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <LabConnectionGraph> | ||
| <PhysicalNetworkGraphDeclaration> | ||
| <Devices/> | ||
| <DeviceInterfaceLinks/> | ||
| </PhysicalNetworkGraphDeclaration> | ||
| <DataPlaneGraph/> | ||
| </LabConnectionGraph> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| # Public graph files | ||
| - lab_connection_graph.xml | ||
| - example_ixia_connection_graph.xml | ||
|
|
||
|
|
||
| # Private graph files |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,9 +68,6 @@ | |
|
|
||
| ''' | ||
|
|
||
| LAB_CONNECTION_GRAPH_FILE = 'lab_connection_graph.xml' | ||
| LAB_GRAPHFILE_PATH = 'files/' | ||
|
|
||
| class Parse_Lab_Graph(): | ||
| """ | ||
| Parse the generated lab physical connection graph and insert Ansible fact of the graph | ||
|
|
@@ -231,28 +228,63 @@ def get_host_connections(self, hostname): | |
| else: | ||
| return self.links | ||
|
|
||
| def contains_hosts(self, hostnames): | ||
| return set(hostnames) <= set(self.devices) | ||
|
|
||
|
|
||
| LAB_CONNECTION_GRAPH_FILE = 'graph_files.yml' | ||
| EMPTY_GRAPH_FILE = 'empty_graph.xml' | ||
| LAB_GRAPHFILE_PATH = 'files/' | ||
|
|
||
| """ | ||
| Find a graph file contains all devices in testbed. | ||
| """ | ||
| def find_graph(hostnames, anchor): | ||
| filename = LAB_GRAPHFILE_PATH + LAB_CONNECTION_GRAPH_FILE | ||
| with open(filename) as fd: | ||
| file_list = yaml.load(fd) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think pyyaml will complain about this when we flip to python3, did you try using
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't try. I prefer to get this merged as-is since it is tested. Then improve. |
||
|
|
||
| for fn in file_list: | ||
| filename = LAB_GRAPHFILE_PATH + fn | ||
| lab_graph = Parse_Lab_Graph(filename) | ||
| lab_graph.parse_graph() | ||
| if hostnames and lab_graph.contains_hosts(hostnames): | ||
| return lab_graph | ||
| if anchor and lab_graph.contains_hosts(anchor): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is explained somewhere but what is
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explained in the PR comments.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming is still a little confusing to me, but this can be iterated on. |
||
| return lab_graph | ||
|
|
||
| # Fallback to return an empty connection graph, this is | ||
| # needed to bridge the kvm test needs. The KVM test needs | ||
| # A graph file, which used to be whatever hardcoded file. | ||
| # Here we provide one empty file for the purpose. | ||
| lab_graph = Parse_Lab_Graph(LAB_GRAPHFILE_PATH + EMPTY_GRAPH_FILE) | ||
| lab_graph.parse_graph() | ||
| return lab_graph | ||
|
|
||
| def main(): | ||
| module = AnsibleModule( | ||
| argument_spec=dict( | ||
| host=dict(required=False), | ||
| hosts=dict(required=False, type='list'), | ||
| filename=dict(required=False), | ||
| anchor=dict(required=False, type='list'), | ||
| ), | ||
| mutually_exclusive=[['host', 'hosts']], | ||
| supports_check_mode=True | ||
| ) | ||
| m_args = module.params | ||
|
|
||
| hostnames = m_args['hosts'] | ||
| anchor = m_args['anchor'] | ||
| if not hostnames: | ||
| hostnames = [m_args['host']] | ||
| try: | ||
| if m_args['filename']: | ||
| filename = m_args['filename'] | ||
| lab_graph = Parse_Lab_Graph(filename) | ||
| lab_graph.parse_graph() | ||
| else: | ||
| filename = LAB_GRAPHFILE_PATH + LAB_CONNECTION_GRAPH_FILE | ||
| lab_graph = Parse_Lab_Graph(filename) | ||
| lab_graph.parse_graph() | ||
| lab_graph = find_graph(hostnames, anchor) | ||
|
|
||
| device_info = [] | ||
| device_conn = [] | ||
|
|
@@ -280,7 +312,7 @@ def main(): | |
|
|
||
| module.exit_json(ansible_facts=results) | ||
| except (IOError, OSError): | ||
| module.fail_json(msg="Can not find lab graph file "+LAB_CONNECTION_GRAPH_FILE) | ||
| module.fail_json(msg="Can not find lab graph file") | ||
| except Exception as e: | ||
| module.fail_json(msg=traceback.format_exc()) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
os.path.join?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. But improve with another PR?