def main():
    module = AnsibleModule(
        argument_spec=dict(
            url=dict(type="str", required=True),
            port=dict(type="int", default=3080),
            user=dict(type="str", default=None),
            password=dict(type="str", default=None, no_log=True),
            state=dict(
                type="str",
                required=True,
                choices=["opened", "closed", "present", "absent"],
            ),
            project_name=dict(type="str", default=None),
            project_id=dict(type="str", default=None),
            nodes_state=dict(type="str", choices=["started", "stopped"]),
            nodes_strategy=dict(type="str",
                                choices=["all", "one_by_one"],
                                default="all"),
            nodes_delay=dict(type="int", default=10),
            poll_wait_time=dict(type="int", default=5),
            nodes_spec=dict(type="list"),
            links_spec=dict(type="list"),
        ),
        supports_check_mode=True,
        required_one_of=[["project_name", "project_id"]],
        required_if=[["nodes_strategy", "one_by_one", ["nodes_delay"]]],
    )
    result = dict(changed=False)
    if not HAS_GNS3FY:
        module.fail_json(msg=missing_required_lib("gns3fy"),
                         exception=GNS3FY_IMP_ERR)
    if module.check_mode:
        module.exit_json(**result)

    server_url = module.params["url"]
    server_port = module.params["port"]
    server_user = module.params["user"]
    server_password = module.params["password"]
    state = module.params["state"]
    project_name = module.params["project_name"]
    project_id = module.params["project_id"]
    nodes_state = module.params["nodes_state"]
    nodes_strategy = module.params["nodes_strategy"]
    nodes_delay = module.params["nodes_delay"]
    poll_wait_time = module.params["poll_wait_time"]
    nodes_spec = module.params["nodes_spec"]
    links_spec = module.params["links_spec"]

    try:
        # Create server session
        server = Gns3Connector(url=f"{server_url}:{server_port}",
                               user=server_user,
                               cred=server_password)
        # Define the project
        if project_name is not None:
            project = Project(name=project_name, connector=server)
        elif project_id is not None:
            project = Project(project_id=project_id, connector=server)
    except Exception as err:
        module.fail_json(msg=str(err), **result)

    #  Retrieve project information
    try:
        project.get()
        pr_exists = True
    except Exception as err:
        pr_exists = False
        reason = str(err)

    if state == "opened":
        if pr_exists:
            if project.status != "opened":
                # Open project
                project.open()

                # Now verify nodes
                if nodes_state is not None:

                    # Change flag based on the nodes state
                    result["changed"] = nodes_state_verification(
                        expected_nodes_state=nodes_state,
                        nodes_strategy=nodes_strategy,
                        nodes_delay=nodes_delay,
                        poll_wait_time=poll_wait_time,
                        project=project,
                    )
                else:
                    # Means that nodes are not taken into account for idempotency
                    result["changed"] = True
            # Even if the project is open if nodes_state has been set, check it
            else:
                if nodes_state is not None:
                    result["changed"] = nodes_state_verification(
                        expected_nodes_state=nodes_state,
                        nodes_strategy=nodes_strategy,
                        nodes_delay=nodes_delay,
                        poll_wait_time=poll_wait_time,
                        project=project,
                    )

        else:
            module.fail_json(msg=reason, **result)

    elif state == "closed":
        if pr_exists:
            if project.status != "closed":
                # Close project
                project.close()
                result["changed"] = True
        else:
            module.fail_json(msg=reason, **result)

    elif state == "present":
        if pr_exists:
            if nodes_spec is not None:
                # Need to verify if nodes exist
                _nodes_already_created = [node.name for node in project.nodes]
                for node_spec in nodes_spec:
                    if node_spec["name"] not in _nodes_already_created:
                        # Open the project in case it was closed
                        project.open()
                        create_node(node_spec, project, module)
                        result["changed"] = True
            if links_spec is not None:
                for link_spec in links_spec:
                    project.open()
                    # Trigger another get to refresh nodes attributes
                    project.get()
                    # Link verification is already built in the library
                    created = create_link(link_spec, project, module)
                    if created:
                        result["changed"] = True
        else:
            # Create project
            project.create()
            # Nodes section
            if nodes_spec is not None:
                for node_spec in nodes_spec:
                    create_node(node_spec, project, module)
            # Links section
            if links_spec is not None:
                for link_spec in links_spec:
                    create_link(link_spec, project, module)
            result["changed"] = True
    elif state == "absent":
        if pr_exists:
            # Stop nodes and close project to perform delete gracefully
            if project.status != "opened":
                # Project needs to be opened in order to be deleted...
                project.open()
            project.stop_nodes(poll_wait_time=0)
            project.delete()
            result["changed"] = True
        else:
            module.exit_json(**result)

    # Return the project data
    result["project"] = return_project_data(project)
    module.exit_json(**result)
예제 #2
0
def OpenProject(project_id, project_name, gns3_server):
	project = Project(project_id=project_id, name=project_name, connector=gns3_server)
	project.get()
	project.open()
예제 #3
0
from gns3fy import Gns3Connector, Project

server = Gns3Connector("http://localhost:3080")

project = Project(name="topology_container", connector=server)

project.get()
project.open()
print(project)
예제 #4
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            url=dict(type="str", required=True),
            port=dict(type="int", default=3080),
            user=dict(type="str", default=None),
            password=dict(type="str", default=None, no_log=True),
            state=dict(
                type="str",
                required=True,
                choices=["started", "stopped", "suspended", "reload"],
            ),
            project_name=dict(type="str", default=None),
            project_id=dict(type="str", default=None),
            node_name=dict(type="str", default=None),
            node_id=dict(type="str", default=None),
            retry=dict(type="bool", default=False),
            poll_wait_time=dict(type="int", default=5),
            force_project_open=dict(type="bool", default=False),
        ),
        required_one_of=[["project_name", "project_id"],
                         ["node_name", "node_id"]],
    )
    result = dict(changed=False)
    if not HAS_GNS3FY:
        module.fail_json(msg=missing_required_lib("gns3fy"),
                         exception=GNS3FY_IMP_ERR)

    server_url = module.params["url"]
    server_port = module.params["port"]
    server_user = module.params["user"]
    server_password = module.params["password"]
    state = module.params["state"]
    project_name = module.params["project_name"]
    project_id = module.params["project_id"]
    node_name = module.params["node_name"]
    node_id = module.params["node_id"]
    retry = module.params["retry"]
    poll_wait_time = module.params["poll_wait_time"]
    force_project_open = module.params["force_project_open"]

    try:
        # Create server session
        server = Gns3Connector(url=f"{server_url}:{server_port}",
                               user=server_user,
                               cred=server_password)
        # Define the project
        if project_name is not None:
            project = Project(name=project_name, connector=server)
        elif project_id is not None:
            project = Project(project_id=project_id, connector=server)
        if project is None:
            module.fail_json(msg="Could not retrieve project. Check name",
                             **result)

        project.get()
        if project.status != "opened" and force_project_open:
            project.open()

        # Retrieve node
        if node_name is not None:
            node = project.get_node(name=node_name)
        elif node_id is not None:
            node = project.get_node(node_id=node_id)
        if node is None:
            module.fail_json(msg="Could not retrieve node. Check name",
                             **result)
    except Exception as err:
        module.fail_json(msg=str(err), **result)

    # Apply state change
    result["changed"] = state_verification(expected_state=state,
                                           node=node,
                                           retry=retry,
                                           poll_wait_time=poll_wait_time)

    # Return the node data
    result["node"] = return_node_data(node)
    module.exit_json(**result)