ovirt_storage_connection: Fix issue in detaching the connection (#50005)

Currently deleting the storage connection just tries to delete the
connection but it doesn't detach the connection from storage domain.
The patch first tries to detach the connection from storage domain
before attempting to delete the same if the storage domain parameter
is provided.
This commit is contained in:
Nijin Ashok 2019-01-08 17:23:20 +05:30 committed by ansibot
parent 7845373f96
commit 449fdf44b9

View file

@ -147,19 +147,22 @@ class StorageConnectionModule(BaseModule):
vfs_type=self.param('vfs_type'),
)
def _get_storage_domain_service(self):
sds_service = self._connection.system_service().storage_domains_service()
sd = search_by_name(sds_service, self.param('storage'))
if sd is None:
raise Exception(
"Storage '%s' was not found." % self.param('storage')
)
return sd, sds_service.storage_domain_service(sd.id)
def post_present(self, entity_id):
if self.param('storage'):
sds_service = self._connection.system_service().storage_domains_service()
sd = search_by_name(sds_service, self.param('storage'))
if sd is None:
raise Exception(
"Storage '%s' was not found." % self.param('storage')
)
sd, sd_service = self._get_storage_domain_service()
if entity_id not in [
sd_conn.id for sd_conn in self._connection.follow_link(sd.storage_connections)
]:
scs_service = sds_service.storage_domain_service(sd.id).storage_connections_service()
scs_service = sd_service.storage_connections_service()
if not self._module.check_mode:
scs_service.add(
connection=otypes.StorageConnection(
@ -168,6 +171,18 @@ class StorageConnectionModule(BaseModule):
)
self.changed = True
def pre_remove(self, entity_id):
if self.param('storage'):
sd, sd_service = self._get_storage_domain_service()
if entity_id in [
sd_conn.id for sd_conn in self._connection.follow_link(sd.storage_connections)
]:
scs_service = sd_service.storage_connections_service()
sc_service = scs_service.connection_service(entity_id)
if not self._module.check_mode:
sc_service.remove()
self.changed = True
def update_check(self, entity):
return (
equal(self.param('address'), entity.address) and
@ -254,6 +269,7 @@ def main():
)
storage_connection_module.post_present(ret['id'])
elif state == 'absent':
storage_connection_module.pre_remove(module.params['id'])
ret = storage_connection_module.remove(entity=entity)
module.exit_json(**ret)