示例#1
0
    def __init__(self, client, data, complete=True):

        datacenter = data.get('datacenter')
        if datacenter is not None:
            data['datacenter'] = BoundDatacenter(client._client.datacenters, datacenter)

        volumes = data.get('volumes', [])
        if volumes:
            volumes = [BoundVolume(client._client.volumes, {"id": volume}, complete=False) for volume in volumes]
            data['volumes'] = volumes

        image = data.get("image", None)
        if image is not None:
            data['image'] = BoundImage(client._client.images, image)

        iso = data.get("iso", None)
        if iso is not None:
            data['iso'] = BoundIso(client._client.isos, iso)

        server_type = data.get("server_type")
        if server_type is not None:
            data['server_type'] = BoundServerType(client._client.server_types, server_type)

        public_net = data.get("public_net")
        if public_net:
            ipv4_address = IPv4Address(**public_net['ipv4'])
            ipv6_network = IPv6Network(**public_net['ipv6'])
            floating_ips = [BoundFloatingIP(client._client.floating_ips, {"id": floating_ip}, complete=False) for
                            floating_ip in public_net['floating_ips']]
            data['public_net'] = PublicNetwork(ipv4=ipv4_address, ipv6=ipv6_network, floating_ips=floating_ips)

        super(BoundServer, self).__init__(client, data, complete)
示例#2
0
    def test_bound_floating_ip_init(self, floating_ip_response):
        bound_floating_ip = BoundFloatingIP(
            client=mock.MagicMock(), data=floating_ip_response['floating_ip'])

        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.description == "Web Frontend"
        assert bound_floating_ip.ip == "131.232.99.1"
        assert bound_floating_ip.type == "ipv4"
        assert bound_floating_ip.protection == {"delete": False}
        assert bound_floating_ip.labels == {}
        assert bound_floating_ip.blocked is False

        assert isinstance(bound_floating_ip.server, BoundServer)
        assert bound_floating_ip.server.id == 42

        assert isinstance(bound_floating_ip.home_location, BoundLocation)
        assert bound_floating_ip.home_location.id == 1
        assert bound_floating_ip.home_location.name == "fsn1"
        assert bound_floating_ip.home_location.description == "Falkenstein DC Park 1"
        assert bound_floating_ip.home_location.country == "DE"
        assert bound_floating_ip.home_location.city == "Falkenstein"
        assert bound_floating_ip.home_location.latitude == 50.47612
        assert bound_floating_ip.home_location.longitude == 12.370071
示例#3
0
class TestFloatingIPsClient(object):
    @pytest.fixture()
    def floating_ips_client(self):
        return FloatingIPsClient(client=mock.MagicMock())

    def test_get_by_id(self, floating_ips_client, floating_ip_response):
        floating_ips_client._client.request.return_value = floating_ip_response
        bound_floating_ip = floating_ips_client.get_by_id(1)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/1", method="GET")
        assert bound_floating_ip._client is floating_ips_client
        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.description == "Web Frontend"

    def test_get_by_name(self, floating_ips_client, one_floating_ips_response):
        floating_ips_client._client.request.return_value = one_floating_ips_response
        bound_floating_ip = floating_ips_client.get_by_name("Web Frontend")
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips", method="GET", params={"name": "Web Frontend"})
        assert bound_floating_ip._client is floating_ips_client
        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.name == "Web Frontend"
        assert bound_floating_ip.description == "Web Frontend"

    @pytest.mark.parametrize("params", [{
        'label_selector': "label1",
        'page': 1,
        'per_page': 10
    }, {
        'name': ""
    }, {}])
    def test_get_list(self, floating_ips_client, two_floating_ips_response,
                      params):
        floating_ips_client._client.request.return_value = two_floating_ips_response
        result = floating_ips_client.get_list(**params)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips", method="GET", params=params)

        bound_floating_ips = result.floating_ips
        assert result.meta is None

        assert len(bound_floating_ips) == 2

        bound_floating_ip1 = bound_floating_ips[0]
        bound_floating_ip2 = bound_floating_ips[1]

        assert bound_floating_ip1._client is floating_ips_client
        assert bound_floating_ip1.id == 4711
        assert bound_floating_ip1.description == "Web Frontend"

        assert bound_floating_ip2._client is floating_ips_client
        assert bound_floating_ip2.id == 4712
        assert bound_floating_ip2.description == "Web Backend"

    @pytest.mark.parametrize("params", [{'label_selector': "label1"}, {}])
    def test_get_all(self, floating_ips_client, two_floating_ips_response,
                     params):
        floating_ips_client._client.request.return_value = two_floating_ips_response
        bound_floating_ips = floating_ips_client.get_all(**params)

        params.update({'page': 1, 'per_page': 50})

        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips", method="GET", params=params)

        assert len(bound_floating_ips) == 2

        bound_floating_ip1 = bound_floating_ips[0]
        bound_floating_ip2 = bound_floating_ips[1]

        assert bound_floating_ip1._client is floating_ips_client
        assert bound_floating_ip1.id == 4711
        assert bound_floating_ip1.description == "Web Frontend"

        assert bound_floating_ip2._client is floating_ips_client
        assert bound_floating_ip2.id == 4712
        assert bound_floating_ip2.description == "Web Backend"

    def test_create_with_location(self, floating_ips_client,
                                  floating_ip_response):
        floating_ips_client._client.request.return_value = floating_ip_response
        response = floating_ips_client.create(
            "ipv6",
            "Web Frontend",
            home_location=Location(name="location"),
        )
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips",
            method="POST",
            json={
                'description': "Web Frontend",
                'type': "ipv6",
                'home_location': "location"
            })

        bound_floating_ip = response.floating_ip
        action = response.action

        assert bound_floating_ip._client is floating_ips_client
        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.description == "Web Frontend"
        assert action is None

    @pytest.mark.parametrize(
        "server",
        [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
    def test_create_with_server(self, floating_ips_client, server,
                                floating_ip_create_response):
        floating_ips_client._client.request.return_value = floating_ip_create_response
        response = floating_ips_client.create(type="ipv6",
                                              description="Web Frontend",
                                              server=server)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips",
            method="POST",
            json={
                'description': "Web Frontend",
                'type': "ipv6",
                'server': 1
            })
        bound_floating_ip = response.floating_ip
        action = response.action

        assert bound_floating_ip._client is floating_ips_client
        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.description == "Web Frontend"
        assert action.id == 13

    @pytest.mark.parametrize(
        "server",
        [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
    def test_create_with_name(self, floating_ips_client, server,
                              floating_ip_create_response):
        floating_ips_client._client.request.return_value = floating_ip_create_response
        response = floating_ips_client.create(type="ipv6",
                                              description="Web Frontend",
                                              name="Web Frontend")
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips",
            method="POST",
            json={
                'description': "Web Frontend",
                'type': "ipv6",
                'name': "Web Frontend"
            })
        bound_floating_ip = response.floating_ip
        action = response.action

        assert bound_floating_ip._client is floating_ips_client
        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.description == "Web Frontend"
        assert bound_floating_ip.name == "Web Frontend"
        assert action.id == 13

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_get_actions(self, floating_ips_client, floating_ip,
                         response_get_actions):
        floating_ips_client._client.request.return_value = response_get_actions
        actions = floating_ips_client.get_actions(floating_ip)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/1/actions",
            method="GET",
            params={
                'page': 1,
                'per_page': 50
            })

        assert len(actions) == 1
        assert isinstance(actions[0], BoundAction)

        assert actions[0]._client == floating_ips_client._client.actions
        assert actions[0].id == 13
        assert actions[0].command == "assign_floating_ip"

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_update(self, floating_ips_client, floating_ip,
                    response_update_floating_ip):
        floating_ips_client._client.request.return_value = response_update_floating_ip
        floating_ip = floating_ips_client.update(floating_ip,
                                                 description="New description",
                                                 name="New name")
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/1",
            method="PUT",
            json={
                "description": "New description",
                "name": "New name"
            })

        assert floating_ip.id == 4711
        assert floating_ip.description == "New description"
        assert floating_ip.name == "New name"

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_change_protection(self, floating_ips_client, floating_ip,
                               generic_action):
        floating_ips_client._client.request.return_value = generic_action
        action = floating_ips_client.change_protection(floating_ip, True)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/1/actions/change_protection",
            method="POST",
            json={"delete": True})

        assert action.id == 1
        assert action.progress == 0

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_delete(self, floating_ips_client, floating_ip, generic_action):
        floating_ips_client._client.request.return_value = generic_action
        delete_success = floating_ips_client.delete(floating_ip)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/1", method="DELETE")

        assert delete_success is True

    @pytest.mark.parametrize("server,floating_ip",
                             [(Server(id=1), FloatingIP(id=12)),
                              (BoundServer(mock.MagicMock(), dict(id=1)),
                               BoundFloatingIP(mock.MagicMock(), dict(id=12)))]
                             )
    def test_assign(self, floating_ips_client, server, floating_ip,
                    generic_action):
        floating_ips_client._client.request.return_value = generic_action
        action = floating_ips_client.assign(floating_ip, server)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/12/actions/assign",
            method="POST",
            json={"server": 1})
        assert action.id == 1
        assert action.progress == 0

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=12),
         BoundFloatingIP(mock.MagicMock(), dict(id=12))])
    def test_unassign(self, floating_ips_client, floating_ip, generic_action):
        floating_ips_client._client.request.return_value = generic_action
        action = floating_ips_client.unassign(floating_ip)
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/12/actions/unassign", method="POST")
        assert action.id == 1
        assert action.progress == 0

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=12),
         BoundFloatingIP(mock.MagicMock(), dict(id=12))])
    def test_change_dns_ptr(self, floating_ips_client, floating_ip,
                            generic_action):
        floating_ips_client._client.request.return_value = generic_action
        action = floating_ips_client.change_dns_ptr(floating_ip, "1.2.3.4",
                                                    "server02.example.com")
        floating_ips_client._client.request.assert_called_with(
            url="/floating_ips/12/actions/change_dns_ptr",
            method="POST",
            json={
                "ip": "1.2.3.4",
                "dns_ptr": "server02.example.com"
            })
        assert action.id == 1
        assert action.progress == 0
示例#4
0
    def __init__(self, client, data, complete=True):

        datacenter = data.get('datacenter')
        if datacenter is not None:
            data['datacenter'] = BoundDatacenter(client._client.datacenters,
                                                 datacenter)

        volumes = data.get('volumes', [])
        if volumes:
            volumes = [
                BoundVolume(client._client.volumes, {"id": volume},
                            complete=False) for volume in volumes
            ]
            data['volumes'] = volumes

        image = data.get("image", None)
        if image is not None:
            data['image'] = BoundImage(client._client.images, image)

        iso = data.get("iso", None)
        if iso is not None:
            data['iso'] = BoundIso(client._client.isos, iso)

        server_type = data.get("server_type")
        if server_type is not None:
            data['server_type'] = BoundServerType(client._client.server_types,
                                                  server_type)

        public_net = data.get("public_net")
        if public_net:
            ipv4_address = IPv4Address(**public_net['ipv4'])
            ipv6_network = IPv6Network(**public_net['ipv6'])
            floating_ips = [
                BoundFloatingIP(client._client.floating_ips,
                                {"id": floating_ip},
                                complete=False)
                for floating_ip in public_net['floating_ips']
            ]
            firewalls = [
                PublicNetworkFirewall(BoundFirewall(client._client.firewalls,
                                                    {"id": firewall["id"]},
                                                    complete=False),
                                      status=firewall["status"])
                for firewall in public_net.get("firewalls", [])
            ]
            data['public_net'] = PublicNetwork(ipv4=ipv4_address,
                                               ipv6=ipv6_network,
                                               floating_ips=floating_ips,
                                               firewalls=firewalls)

        private_nets = data.get("private_net")
        if private_nets:
            private_nets = [
                PrivateNet(network=BoundNetwork(client._client.networks,
                                                {"id": private_net['network']},
                                                complete=False),
                           ip=private_net['ip'],
                           alias_ips=private_net['alias_ips'],
                           mac_address=private_net['mac_address'])
                for private_net in private_nets
            ]
            data['private_net'] = private_nets

        super(BoundServer, self).__init__(client, data, complete)
class TestFloatingIPsClient(object):
    def test_get_by_id(self, hetzner_client):
        bound_floating_ip = hetzner_client.floating_ips.get_by_id(4711)
        assert bound_floating_ip.id == 4711
        assert bound_floating_ip.description == "Web Frontend"
        assert bound_floating_ip.type == "ipv4"

    def test_get_list(self, hetzner_client):
        result = hetzner_client.floating_ips.get_list()
        bound_floating_ips = result.floating_ips
        assert bound_floating_ips[0].id == 4711
        assert bound_floating_ips[0].description == "Web Frontend"
        assert bound_floating_ips[0].type == "ipv4"

    @pytest.mark.parametrize(
        "server",
        [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
    def test_create(self, hetzner_client, server):
        response = hetzner_client.floating_ips.create(
            type="ipv4",
            description="Web Frontend",
            # home_location=Location(description="fsn1"),
            server=server)

        floating_ip = response.floating_ip
        action = response.action

        assert floating_ip.id == 4711
        assert floating_ip.description == "Web Frontend"
        assert floating_ip.type == "ipv4"

        assert action.id == 13
        assert action.command == "assign_floating_ip"

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_get_actions(self, hetzner_client, floating_ip):
        actions = hetzner_client.floating_ips.get_actions(floating_ip)

        assert len(actions) == 1
        assert actions[0].id == 13
        assert actions[0].command == "assign_floating_ip"

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_update(self, hetzner_client, floating_ip):
        floating_ip = hetzner_client.floating_ips.update(
            floating_ip, description="New description", labels={})

        assert floating_ip.id == 4711
        assert floating_ip.description == "New description"

    @pytest.mark.parametrize(
        "floating_ip",
        [FloatingIP(id=1),
         BoundFloatingIP(mock.MagicMock(), dict(id=1))])
    def test_delete(self, hetzner_client, floating_ip):
        delete_success = hetzner_client.floating_ips.delete(floating_ip)

        assert delete_success is True

    @pytest.mark.parametrize(
        "server,floating_ip",
        [(Server(id=43), FloatingIP(id=4711)),
         (BoundServer(mock.MagicMock(), dict(id=43)),
          BoundFloatingIP(mock.MagicMock(), dict(id=4711)))])
    def test_assign(self, hetzner_client, server, floating_ip):
        action = hetzner_client.floating_ips.assign(floating_ip, server)
        assert action.id == 13
        assert action.progress == 0
        assert action.command == "assign_floating_ip"

    @pytest.mark.parametrize("floating_ip", [
        FloatingIP(id=4711),
        BoundFloatingIP(mock.MagicMock(), dict(id=4711))
    ])
    def test_unassign(self, hetzner_client, floating_ip):
        action = hetzner_client.floating_ips.unassign(floating_ip)
        assert action.id == 13
        assert action.progress == 0
        assert action.command == "unassign_floating_ip"

    @pytest.mark.parametrize("floating_ip", [
        FloatingIP(id=4711),
        BoundFloatingIP(mock.MagicMock(), dict(id=4711))
    ])
    def test_change_dns_ptr(self, hetzner_client, floating_ip):
        action = hetzner_client.floating_ips.change_dns_ptr(
            floating_ip, "1.2.3.4", "server02.example.com")
        assert action.id == 13
        assert action.progress == 0
        assert action.command == "change_dns_ptr"