mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-02 23:31:25 -07:00
virt_net: idempotency of create/stop actions (#53276)
Currently, if we try to stop or start a network two time in a row, the second call will fail. With this patch: - we don't recreate a network, if it exists - we only stop a network if it's active, and so we avoid an exception saying the network is not active * test: mock libvirt * add integration tests for virt_net * test: enable virt_net test on RedHat 7 and 8 * ci: use the unsupported alias * tests that require privileged mode are run in VM * virt_net/create raise unexpected libvirt exception * import mock from units.compat * virt_net: do not call create() on "active" network * virt_net func test: only clean up the libvirt packages * test: virt_net: don't use assert_called() * virt_net: add the destructive alias * move the test in virt_net dir * test/virt_net: clean up the network at the end
This commit is contained in:
parent
56418cc274
commit
fc3064471b
16 changed files with 268 additions and 29 deletions
|
@ -201,28 +201,16 @@ class LibvirtConnection(object):
|
|||
self.conn = conn
|
||||
|
||||
def find_entry(self, entryid):
|
||||
# entryid = -1 returns a list of everything
|
||||
if entryid == -1: # Get active entries
|
||||
names = self.conn.listNetworks() + self.conn.listDefinedNetworks()
|
||||
return [self.conn.networkLookupByName(n) for n in names]
|
||||
|
||||
results = []
|
||||
|
||||
# Get active entries
|
||||
for name in self.conn.listNetworks():
|
||||
entry = self.conn.networkLookupByName(name)
|
||||
results.append(entry)
|
||||
|
||||
# Get inactive entries
|
||||
for name in self.conn.listDefinedNetworks():
|
||||
entry = self.conn.networkLookupByName(name)
|
||||
results.append(entry)
|
||||
|
||||
if entryid == -1:
|
||||
return results
|
||||
|
||||
for entry in results:
|
||||
if entry.name() == entryid:
|
||||
return entry
|
||||
|
||||
raise EntryNotFound("network %s not found" % entryid)
|
||||
try:
|
||||
return self.conn.networkLookupByName(entryid)
|
||||
except libvirt.libvirtError as e:
|
||||
if e.get_error_code() == libvirt.VIR_ERR_NO_NETWORK:
|
||||
raise EntryNotFound("network %s not found" % entryid)
|
||||
raise
|
||||
|
||||
def create(self, entryid):
|
||||
if not self.module.check_mode:
|
||||
|
@ -285,11 +273,18 @@ class LibvirtConnection(object):
|
|||
return self.module.exit_json(changed=True)
|
||||
|
||||
def undefine(self, entryid):
|
||||
if not self.module.check_mode:
|
||||
entry = None
|
||||
try:
|
||||
entry = self.find_entry(entryid)
|
||||
found = True
|
||||
except EntryNotFound:
|
||||
found = False
|
||||
|
||||
if found:
|
||||
return self.find_entry(entryid).undefine()
|
||||
else:
|
||||
if not self.find_entry(entryid):
|
||||
return self.module.exit_json(changed=True)
|
||||
|
||||
if self.module.check_mode:
|
||||
return self.module.exit_json(changed=found)
|
||||
|
||||
def get_status2(self, entry):
|
||||
state = entry.isActive()
|
||||
|
@ -418,19 +413,27 @@ class VirtNetwork(object):
|
|||
return self.conn.set_autostart(entryid, state)
|
||||
|
||||
def create(self, entryid):
|
||||
return self.conn.create(entryid)
|
||||
if self.conn.get_status(entryid) == "active":
|
||||
return
|
||||
try:
|
||||
return self.conn.create(entryid)
|
||||
except libvirt.libvirtError as e:
|
||||
if e.get_error_code() == libvirt.VIR_ERR_NETWORK_EXIST:
|
||||
return None
|
||||
raise
|
||||
|
||||
def modify(self, entryid, xml):
|
||||
return self.conn.modify(entryid, xml)
|
||||
|
||||
def start(self, entryid):
|
||||
return self.conn.create(entryid)
|
||||
return self.create(entryid)
|
||||
|
||||
def stop(self, entryid):
|
||||
return self.conn.destroy(entryid)
|
||||
if self.conn.get_status(entryid) == "active":
|
||||
return self.conn.destroy(entryid)
|
||||
|
||||
def destroy(self, entryid):
|
||||
return self.conn.destroy(entryid)
|
||||
return self.stop(entryid)
|
||||
|
||||
def undefine(self, entryid):
|
||||
return self.conn.undefine(entryid)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue