示例#1
0
def test_create_connections__p2p_by_name(runner, login_mock):
    with mock.patch.object(
        ctl.sdk.AgentsApi,
        "v1_network_agents_get",
        autospec=True,
        return_value={
            "data": [
                {"agent_id": 1, "agent_name": "a"},
                {"agent_id": 2, "agent_name": "b"},
            ]
        },
    ) as index_mock:
        with mock.patch.object(
            ctl.sdk.ConnectionsApi,
            "v1_network_connections_create_p2_p",
            autospec=True,
            return_value={"data": []},
        ) as the_mock:
            runner.invoke(
                ctl.create_connections,
                ["--use-names", "a", "b"],
            )
            index_mock.assert_called_once()
            the_mock.assert_called_once_with(
                mock.ANY,
                body=models.V1NetworkConnectionsCreateP2PRequest(
                    agent_pairs=[
                        models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                            agent_1_id=1,
                            agent_2_id=2,
                        ),
                    ],
                ),
                _preload_content=False,
            )
示例#2
0
def test_create_connections__p2p__fail(runner, login_mock):
    with mock.patch.object(
        ctl.sdk.ConnectionsApi,
        "v1_network_connections_create_p2_p",
        autospec=True,
        return_value={"errors": [{"message": "some error"}]},
    ) as the_mock:
        result = runner.invoke(ctl.create_connections, ["1", "2", "3", "4"])
        the_mock.assert_called_once_with(
            mock.ANY,
            body=models.V1NetworkConnectionsCreateP2PRequest(
                agent_pairs=[
                    models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                        agent_1_id=1,
                        agent_2_id=2,
                    ),
                    models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                        agent_1_id=3,
                        agent_2_id=4,
                    ),
                ],
            ),
            _preload_content=False,
        )
        assert "some error" in result.output
示例#3
0
def create_connections(agents, use_names, json, api):
    """Create connections between endpoints. Number of endpoints must be even.

    \b
    Arguments:
        agents - a list of endpoint ids or names separated by spaces.

    In order to use endpoint names instead of ids provide --use-names option.

    Example:

        syntropyctl create-connections 1 2 3 4 5 6 7 8

        This command will create 4 connections from Endpoint 1 to Endpoint 2 like this:

        \b
        Endpoint 1 ID | Endpoint 2 ID
        1             | 2
        3             | 4
        5             | 6
        7             | 8
    """

    if use_names:
        all_agents = WithPagination(sdk.AgentsApi(api).v1_network_agents_get)(
            _preload_content=False)["data"]
        agents = find_by_name(all_agents, agents, "agent")
        if any(i is None for i in agents):
            raise SystemExit(1)
    else:
        try:
            agents = [int(i) for i in agents]
        except ValueError:
            click.secho("Invalid agent id", err=True, fg="red")
            raise SystemExit(1)

    if len(agents) == 0 or len(agents) % 2 != 0:
        click.secho("Number of agents must be even.", err=True, fg="red")
        raise SystemExit(1)
    agents = list(zip(agents[:-1:2], agents[1::2]))

    body = models.V1NetworkConnectionsCreateP2PRequest(agent_pairs=[
        models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
            agent_1_id=a,
            agent_2_id=b,
        ) for a, b in agents
    ], )
    result = sdk.ConnectionsApi(api).v1_network_connections_create_p2_p(
        body=body, _preload_content=False)

    if result and "errors" in result:
        for error in result["errors"]:
            click.secho(f"Error: {error.get('message')}", err=True, fg="red")
def test_update_network__mesh(
    api_agents_search, api_agents_get, api_connections, with_pagination, config_mock
):
    config = {
        "topology": "mesh",
        "state": "present",
        "connections": {
            "agent1": {
                "state": "present",
            },
            "agent2": {
                "state": "absent",
            },
            "agent3": {
                "state": "present",
            },
            "agent5": {
                "state": "present",
            },
        },
    }
    assert (
        configure.configure_network_update(mock.Mock(spec=sdk.ApiClient), config, False)
        == True
    )
    assert sdk.ConnectionsApi.v1_network_connections_get.call_count == 2
    assert sdk.ConnectionsApi.v1_network_connections_remove.call_args_list == [
        mock.call(
            mock.ANY,
            body=models.V1NetworkConnectionsRemoveRequest(
                agent_connection_group_ids=[1, 2],
            ),
        ),
    ]
    assert sdk.ConnectionsApi.v1_network_connections_create_p2_p.call_args_list == [
        mock.call(
            mock.ANY,
            body=models.V1NetworkConnectionsCreateP2PRequest(
                agent_pairs=[
                    models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                        agent_1_id=1,
                        agent_2_id=5,
                    ),
                    models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                        agent_1_id=3,
                        agent_2_id=5,
                    ),
                ],
            ),
            _preload_content=False,
        )
    ]
def test_create_connections(api_connections, with_pagination, created_connections):
    sdk.ConnectionsApi.v1_network_connections_get.side_effect = (
        lambda *args, **kwargs: models.V1NetworkConnectionsGetResponse(
            data=created_connections
        )
    )

    result = configure.create_connections(
        mock.Mock(spec=sdk.ApiClient), [(13, 11), (14, 13)], True
    )
    assert sdk.ConnectionsApi.v1_network_connections_create_p2_p.call_args_list == [
        mock.call(
            mock.ANY,
            body=models.V1NetworkConnectionsCreateP2PRequest(
                agent_pairs=[
                    models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                        agent_1_id=13,
                        agent_2_id=11,
                    ),
                    models.V1NetworkConnectionsCreateP2PRequestAgentPairs(
                        agent_1_id=14,
                        agent_2_id=13,
                    ),
                ],
            ),
            _preload_content=False,
        ),
    ]
    assert result == [
        {
            "agent_1": {"agent_id": 13, "agent_name": "iot_mqtt"},
            "agent_2": {"agent_id": 11, "agent_name": "iot_device2"},
            "agent_connection_group_id": 7,
        },
        {
            "agent_1": {"agent_id": 13, "agent_name": "iot_mqtt"},
            "agent_2": {"agent_id": 14, "agent_name": "iot_device2"},
            "agent_connection_group_id": 8,
        },
    ]