Skip to content

Rename functionalities does not actually rename but create a new object instead #544

@MOBergeron

Description

@MOBergeron

The title says it. Here is an example of the rename method of the Network class:

    def rename(self, new_name):
        """
        Rename a network.

        :param new_name: new name of the network
        :type new_name: str
        :return: Renamed network instance
        :rtype: :class:`Network`
        """
        self.client.assert_has_api_extension("network")
        self.client.api.networks.post(json={"name": new_name})
        return Network.get(self.client, new_name)

It creates a new network instead of renaming it because it does not get the network first. A fix would be the following:

    def rename(self, new_name):
        """
        Rename a network.

        :param new_name: new name of the network
        :type new_name: str
        :return: Renamed network instance
        :rtype: :class:`Network`
        """
        self.client.assert_has_api_extension("network")
        self.client.api.networks[self.name].post(json={"name": new_name})
        return Network.get(self.client, new_name)

Notice the [self.name] in self.client.api.networks[self.name].post(json={"name": new_name}).

I haven't look further if other models are like this but I can confirm that Network and Instance are.

Demo:

>>> c.networks.create(name='test',type='bridge')
Network(config={"ipv4.address": "10.19.224.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:dc60:e2ee:ae00::1/64", "ipv6.nat": "true"}, description="", name="test", type="bridge")
>>> c.networks.get('test').rename('test2')
Network(config={"ipv4.address": "10.156.78.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:483f:d506:c1ff::1/64", "ipv6.nat": "true"}, description="", name="test2", type="bridge")
>>> c.networks.get('test')
Network(config={"ipv4.address": "10.19.224.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:dc60:e2ee:ae00::1/64", "ipv6.nat": "true"}, description="", name="test", type="bridge")
>>> c.networks.get('test2')
Network(config={"ipv4.address": "10.156.78.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:483f:d506:c1ff::1/64", "ipv6.nat": "true"}, description="", name="test2", type="bridge")
$ lxc network list local:
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+
|      NAME       |   TYPE   | MANAGED |      IPV4       |           IPV6            | DESCRIPTION | USED BY |  STATE  |
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+
| test            | bridge   | YES     | 10.19.224.1/24  | fd42:dc60:e2ee:ae00::1/64 |             | 0       | CREATED |
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+
| test2           | bridge   | YES     | 10.156.78.1/24  | fd42:483f:d506:c1ff::1/64 |             | 0       | CREATED |
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+

With the fix:

>>> c.networks.create(name='test',type='bridge')
Network(config={"ipv4.address": "10.103.75.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:ede6:29ab:4797::1/64", "ipv6.nat": "true"}, description="", name="test", type="bridge")
>>> c.networks.get('test').rename('test2')
Network(config={"ipv4.address": "10.103.75.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:ede6:29ab:4797::1/64", "ipv6.nat": "true"}, description="", name="test2", type="bridge")
>>> c.networks.get('test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mbergeron/Documents/gitlocal/pylxd/pylxd/models/network.py", line 114, in get
    response = client.api.networks[name].get()
  File "/home/mbergeron/Documents/gitlocal/pylxd/pylxd/client.py", line 210, in get
    self._assert_response(
  File "/home/mbergeron/Documents/gitlocal/pylxd/pylxd/client.py", line 159, in _assert_response
    raise exceptions.NotFound(response)
pylxd.exceptions.NotFound: Network not found
>>> c.networks.get('test2')
Network(config={"ipv4.address": "10.103.75.1/24", "ipv4.nat": "true", "ipv6.address": "fd42:ede6:29ab:4797::1/64", "ipv6.nat": "true"}, description="", name="test2", type="bridge")
$ lxc network list local:
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+
|      NAME       |   TYPE   | MANAGED |      IPV4       |           IPV6            | DESCRIPTION | USED BY |  STATE  |
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+
| test2           | bridge   | YES     | 10.103.75.1/24  | fd42:ede6:29ab:4797::1/64 |             | 0       | CREATED |
+-----------------+----------+---------+-----------------+---------------------------+-------------+---------+---------+

I will make a pull request eventually when I find the time for it :).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions