Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version comparison #335

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions plugins/module_utils/ah_api_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import json
import time

from ansible.module_utils.compat.version import LooseVersion as Version

from ansible.module_utils.basic import AnsibleModule, env_fallback
from ansible.module_utils._text import to_bytes, to_text

Expand Down Expand Up @@ -134,7 +136,7 @@ def __init__(self, argument_spec, direct_params=None, **kwargs):
self.plugin_path_prefix = "{galaxy_prefix}/v3/plugin".format(galaxy_prefix=self.galaxy_path_prefix)
self.authenticate()
self.server_version = self.get_server_version()
if self.server_version < "4.6":
if Version(self.server_version) < Version("4.6"):
self.pulp_path_prefix = "/pulp/api/v3"
else:
self.pulp_path_prefix = "{galaxy_prefix}/pulp/api/v3".format(galaxy_prefix=self.galaxy_path_prefix)
Expand Down Expand Up @@ -485,7 +487,7 @@ def exit_json(self, **kwargs):
def get_server_version(self):
"""Return the automation hub/galaxy server version.

:return: the server version ("4.2.5" for example) or an empty string if
:return: the server version ("4.2.5" for example) or an "0" string if
that information is not available.
:rtype: str
"""
Expand All @@ -501,4 +503,4 @@ def get_server_version(self):
else:
fail_msg = "Unable to get server version: {code}".format(code=response["status_code"])
self.fail_json(msg=fail_msg)
return response["json"]["server_version"].replace('dev', '') if "server_version" in response["json"] else ""
return response["json"]["server_version"].replace('dev', '') if "server_version" in response["json"] else "0"
12 changes: 7 additions & 5 deletions plugins/module_utils/ah_ui_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from __future__ import absolute_import, division, print_function
import time


from ansible.module_utils.compat.version import LooseVersion as Version
from .ah_api_module import AHAPIModuleError
from .ah_pulp_object import AHPulpTask

Expand Down Expand Up @@ -1070,7 +1072,7 @@ def get_object(self, name, vers, exit_on_error=True):
"""
query = {self.name_field: name, "limit": "1000"}
self.vers = vers
if vers < "4.7":
if Version(vers) < Version("4.7"):
url = self.api.build_ui_url(self.endpoint, query_params=query)
else:
url = self.api.build_plugin_url(self.endpoint, query_params=query)
Expand Down Expand Up @@ -1158,7 +1160,7 @@ def sync(self, wait, interval, timeout):
:rtype: bool
"""

if self.vers < "4.7":
if Version(self.vers) < Version("4.7"):
url = self.api.build_ui_url("{endpoint}/_content/sync".format(endpoint=self.id_endpoint))
else:
url = self.api.build_plugin_url("{endpoint}/_content/sync".format(endpoint=self.id_endpoint))
Expand Down Expand Up @@ -1230,7 +1232,7 @@ def get_readme(self):
if not self.exists:
return ""

if self.vers < "4.7":
if Version(self.vers) < Version("4.7"):
url = self.api.build_ui_url("{endpoint}/_content/readme".format(endpoint=self.id_endpoint))
else:
url = self.api.build_plugin_url("{endpoint}/_content/readme".format(endpoint=self.id_endpoint))
Expand Down Expand Up @@ -1289,7 +1291,7 @@ def update_readme(self, readme, auto_exit=True):
self.api.exit_json(**json_output)
return True

if self.vers < "4.7":
if Version(self.vers) < Version("4.7"):
url = self.api.build_ui_url("{endpoint}/_content/readme".format(endpoint=self.id_endpoint))
else:
url = self.api.build_plugin_url("{endpoint}/_content/readme".format(endpoint=self.id_endpoint))
Expand Down Expand Up @@ -1579,7 +1581,7 @@ def get_tag(self, name, tag, vers):
self.image_name = name
self.tag = tag
self.vers = vers
if vers < "4.7":
if Version(vers) < Version("4.7"):
url = self.api.build_ui_url(self.id_endpoint, query_params={"limit": 1000})
else:
url = self.api.build_plugin_url(self.id_endpoint, query_params={"limit": 1000})
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ah_ee_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

RETURN = r""" # """

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIEEImage
from ..module_utils.ah_pulp_object import AHPulpEERepository
Expand Down Expand Up @@ -121,7 +122,7 @@ def main():

# Only recent versions support execution environment
vers = module.get_server_version()
if vers < "4.3.2":
if Version(vers) < Version("4.3.2"):
module.fail_json(msg="This module requires private automation hub version 4.3.2 or later. Your version is {vers}".format(vers=vers))

# Process the object from the Pulp API (delete or create)
Expand Down
5 changes: 3 additions & 2 deletions plugins/modules/ah_ee_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@

RETURN = r""" # """

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule, AHAPIModuleError
from ..module_utils.ah_ui_object import AHUIGroup, AHUIEENamespace
from ..module_utils.ah_pulp_object import AHPulpEENamespace, AHPulpEERepository
Expand Down Expand Up @@ -234,9 +235,9 @@ def main():

# Only recent versions support execution environment
vers = module.get_server_version()
if vers > "4.6.3":
if Version(vers) > Version("4.6.3"):
module.fail_json(msg="This module requires private automation hub version 4.6.2 or earlier. Your version is {vers}".format(vers=vers))
elif vers < "4.3.2":
elif Version(vers) < Version("4.3.2"):
module.fail_json(msg="This module requires private automation hub version 4.3.2 or later. Your version is {vers}".format(vers=vers))

# Process the object from the Pulp API (delete or create)
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ah_ee_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@

RETURN = r""" # """

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIEERegistry

Expand Down Expand Up @@ -189,7 +190,7 @@ def main():
module.authenticate()
vers = module.get_server_version()
registry = AHUIEERegistry(module)
if vers > "4.7.0":
if Version(vers) > Version("4.7.0"):
registry.id_field = "id"
# Removing the registry
if state == "absent":
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ah_ee_registry_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
timeout: 300
"""

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIEERegistry

Expand All @@ -91,7 +92,7 @@ def main():
module.authenticate()
vers = module.get_server_version()
registry = AHUIEERegistry(module)
if vers > "4.7.0":
if Version(vers) > Version("4.7.0"):
registry.id_field = "id"
registry.get_object(name, vers)

Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ah_ee_registry_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
timeout: 300
"""

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIEERegistry

Expand All @@ -90,7 +91,7 @@ def main():
vers = module.get_server_version()
registry = AHUIEERegistry(module)
registry.get_object(name, vers)
if vers > "4.7.0":
if Version(vers) > Version("4.7.0"):
registry.id_field = "id"

if not registry.exists:
Expand Down
5 changes: 3 additions & 2 deletions plugins/modules/ah_ee_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
import os
import os.path

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIEERepository, AHUIEERegistry, AHUIEERemote
from ..module_utils.ah_pulp_object import AHPulpEERepository, AHPulpEENamespace
Expand Down Expand Up @@ -187,9 +188,9 @@ def main():

# Only recent versions support execution environment
vers = module.get_server_version()
if vers < "4.3.2":
if Version(vers) < Version("4.3.2"):
module.fail_json(msg="This module requires private automation hub version 4.3.2 or later. Your version is {vers}".format(vers=vers))
elif vers < "4.4.0" and registry:
elif Version(vers) < Version("4.4.0") and registry:
module.fail_json(
msg="This module requires private automation hub version 4.4.0 or later to create remote repositories. Your version is {vers}".format(vers=vers)
)
Expand Down
5 changes: 3 additions & 2 deletions plugins/modules/ah_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

RETURN = r""" # """

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIGroup
from ..module_utils.ah_pulp_object import AHPulpGroups
Expand All @@ -82,10 +83,10 @@ def main():
state = module.params.get("state")
# Authenticate
module.authenticate()
vers = module.get_server_version()
vers = Version(module.get_server_version())

# Use Pulp with newer versions
if vers > "4.7.0":
if vers > Version("4.7.0"):
group = AHPulpGroups(module)
group.get_object(name)
else:
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ah_group_perm.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@

RETURN = r""" # """

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_ui_object import AHUIGroup

Expand Down Expand Up @@ -188,7 +189,7 @@ def main():
module.authenticate()

vers = module.get_server_version()
if vers < "4.3.2":
if Version(vers) < Version("4.3.2"):
del FRIENDLY_PERM_NAMES["change_containernamespace_perms"]
del FRIENDLY_PERM_NAMES["change_container"]
del FRIENDLY_PERM_NAMES["change_image_tag"]
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ah_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@

RETURN = r""" # """

from ansible.module_utils.compat.version import LooseVersion as Version
from ..module_utils.ah_api_module import AHAPIModule
from ..module_utils.ah_pulp_object import AHPulpRolePerm

Expand Down Expand Up @@ -194,7 +195,7 @@ def main():

# Only recent versions support execution environment
vers = module.get_server_version()
if vers < "4.6":
if Version(vers) < Version("4.6"):
module.fail_json(msg="This module requires private automation hub version 4.6 or later. Your version is {vers}".format(vers=vers))

# Process the object from the Pulp API (delete or create)
Expand Down
Loading