Skip to content

Commit

Permalink
ovirt: Add support to pass hostname (#40610)
Browse files Browse the repository at this point in the history
Previously we supported only to pass API URL, now we support also
hostname.
  • Loading branch information
machacekondra authored and ryansb committed May 23, 2018
1 parent bc7ff83 commit 46fbfd5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/ansible/module_utils/ovirt.py
Expand Up @@ -135,8 +135,12 @@ def create_connection(auth):
:return: Python SDK connection
"""

url = auth.get('url')
if url is None and auth.get('hostname') is not None:
url = 'https://{0}/ovirt-engine/api'.format(auth.get('hostname'))

return sdk.Connection(
url=auth.get('url'),
url=url,
username=auth.get('username'),
password=auth.get('password'),
ca_file=auth.get('ca_file', None),
Expand Down Expand Up @@ -338,13 +342,16 @@ def wait(

def __get_auth_dict():
OVIRT_URL = os.environ.get('OVIRT_URL')
OVIRT_HOSTNAME = os.environ.get('OVIRT_HOSTNAME')
OVIRT_USERNAME = os.environ.get('OVIRT_USERNAME')
OVIRT_PASSWORD = os.environ.get('OVIRT_PASSWORD')
OVIRT_TOKEN = os.environ.get('OVIRT_TOKEN')
OVIRT_CAFILE = os.environ.get('OVIRT_CAFILE')
OVIRT_INSECURE = OVIRT_CAFILE is None

env_vars = None
if OVIRT_URL is None and OVIRT_HOSTNAME is not None:
OVIRT_URL = 'https://{0}/ovirt-engine/api'.format(OVIRT_HOSTNAME)
if OVIRT_URL and ((OVIRT_USERNAME and OVIRT_PASSWORD) or OVIRT_TOKEN):
env_vars = {
'url': OVIRT_URL,
Expand Down
21 changes: 19 additions & 2 deletions lib/ansible/modules/cloud/ovirt/ovirt_auth.py
Expand Up @@ -59,9 +59,18 @@
url:
required: False
description:
- "A string containing the base URL of the server.
- "A string containing the API URL of the server.
For example: I(https://server.example.com/ovirt-engine/api).
Default value is set by I(OVIRT_URL) environment variable."
- "Either C(url) or C(hostname) is required."
hostname:
required: False
description:
- "A string containing the hostname of the server.
For example: I(server.example.com).
Default value is set by I(OVIRT_HOSTNAME) environment variable."
- "Either C(url) or C(hostname) is required."
version_added: "2.6"
insecure:
required: False
description:
Expand Down Expand Up @@ -217,6 +226,7 @@ def main():
module = AnsibleModule(
argument_spec=dict(
url=dict(default=None),
hostname=dict(default=None),
username=dict(default=None),
password=dict(default=None, no_log=True),
ca_file=dict(default=None, type='path'),
Expand Down Expand Up @@ -249,7 +259,14 @@ def get_required_parameter(param, env_var, required=False):

return var

url = get_required_parameter('url', 'OVIRT_URL', required=True)
url = get_required_parameter('url', 'OVIRT_URL', required=False)
hostname = get_required_parameter('hostname', 'OVIRT_HOSTNAME', required=False)
if url is None and hostname is None:
module.fail_json(msg="You must specify either 'url' or 'hostname'.")

if url is None and hostname is not None:
url = 'https://{0}/ovirt-engine/api'.format(hostname)

username = get_required_parameter('username', 'OVIRT_USERNAME', required=True)
password = get_required_parameter('password', 'OVIRT_PASSWORD', required=True)
token = get_required_parameter('token', 'OVIRT_TOKEN')
Expand Down
8 changes: 6 additions & 2 deletions lib/ansible/utils/module_docs_fragments/ovirt.py
Expand Up @@ -45,8 +45,12 @@ class ModuleDocFragment(object):
- C(username)[I(required)] - The name of the user, something like I(admin@internal).
Default value is set by I(OVIRT_USERNAME) environment variable.
- "C(password)[I(required)] - The password of the user. Default value is set by I(OVIRT_PASSWORD) environment variable."
- "C(url)[I(required)] - A string containing the base URL of the server, usually
something like `I(https://server.example.com/ovirt-engine/api)`. Default value is set by I(OVIRT_URL) environment variable."
- "C(url) - A string containing the API URL of the server, usually
something like `I(https://server.example.com/ovirt-engine/api)`. Default value is set by I(OVIRT_URL) environment variable.
Either C(url) or C(hostname) is required."
- "C(hostname) - A string containing the hostname of the server, usually
something like `I(server.example.com)`. Default value is set by I(OVIRT_HOSTNAME) environment variable.
Either C(url) or C(hostname) is required."
- "C(token) - Token to be used instead of login with username/password. Default value is set by I(OVIRT_TOKEN) environment variable."
- "C(insecure) - A boolean flag that indicates if the server TLS
certificate and host name should be checked."
Expand Down
8 changes: 6 additions & 2 deletions lib/ansible/utils/module_docs_fragments/ovirt_facts.py
Expand Up @@ -42,8 +42,12 @@ class ModuleDocFragment(object):
- C(username)[I(required)] - The name of the user, something like I(admin@internal).
Default value is set by I(OVIRT_USERNAME) environment variable.
- "C(password)[I(required)] - The password of the user. Default value is set by I(OVIRT_PASSWORD) environment variable."
- "C(url)[I(required)] - A string containing the base URL of the server, usually
something like `I(https://server.example.com/ovirt-engine/api)`. Default value is set by I(OVIRT_URL) environment variable."
- "C(url)- A string containing the API URL of the server, usually
something like `I(https://server.example.com/ovirt-engine/api)`. Default value is set by I(OVIRT_URL) environment variable.
Either C(url) or C(hostname) is required."
- "C(hostname) - A string containing the hostname of the server, usually
something like `I(server.example.com)`. Default value is set by I(OVIRT_HOSTNAME) environment variable.
Either C(url) or C(hostname) is required."
- "C(token) - Token to be used instead of login with username/password. Default value is set by I(OVIRT_TOKEN) environment variable."
- "C(insecure) - A boolean flag that indicates if the server TLS
certificate and host name should be checked."
Expand Down

0 comments on commit 46fbfd5

Please sign in to comment.