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

New module - helix_configurable #57594

Open
wants to merge 2 commits into
base: devel
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

No changes.
@@ -0,0 +1,49 @@
"""
This module adds shared support for Perforce Helix modules
"""

import traceback

try:
from P4 import P4, P4Exception
HAS_P4 = True
except ImportError:
P4_IMP_ERR = traceback.format_exc()
HAS_P4 = False

from ansible.module_utils.basic import env_fallback, missing_required_lib


def helix_connect(module, script_name):
"""
Pass this function a user, p4port, password to connect to a Helix Core server
"""

if not HAS_P4:
module.fail_json(msg=missing_required_lib('p4python', url='https://pypi.org/project/p4python/'), exception=P4_IMP_ERR)

try:
p4 = P4()
p4.prog = script_name
p4.port = module.params['server']
p4.user = module.params['user']
p4.password = module.params['password']
p4.connect()
p4.run_login()
if p4.connected() is not True:
module.fail_json(msg="Unable to connect to Helix")
return p4
except Exception as e:
module.fail_json(msg="There was a problem connecting to Helix: {0}".format(e))


def helix_disconnect(module, connection):
"""
Pass this function a connection object to disconnect the Helix Core
session
"""

try:
connection.disconnect()
except Exception as e:
module.fail_json(msg="There was a problem disconnecting from Helix: {0}".format(e))
No changes.
@@ -0,0 +1,188 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2019, Asif Shaikh (@ripclawffb) <ripclaw_ffb@hotmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}

DOCUMENTATION = '''
---
module: helix_configurable
short_description: This module will allow you to set config options on Perforce Helix Core
version_added: "2.9"
description:
- "Configurables allow you to customize a Perforce service. Configurable settings might affect the server, the client, or a proxy."
- "This module supports check mode."
requirements:
- "P4Python pip module is required. Tested with 2018.2.1743033"
seealso:
- name: Helix Core Configurables
description: "List of supported configurables"
link: https://www.perforce.com/manuals/cmdref/Content/CmdRef/appendix.configurables.html
- name: P4Python Pip Module
description: "Python module to interact with Helix Core"
link: https://pypi.org/project/p4python/
options:
state:
choices:
- present
- absent
default: present
description:
- Determines if the configurable is set or removed
type: str
name:
description:
- The name of the configurable that needs to be set
required: true
type: str
value:
description:
- The value of named configurable
required: true
type: str
server:
description:
- The hostname/ip and port of the server (perforce:1666)
- Can also use 'P4PORT' environment variable
required: true
type: str
aliases:
- p4port
user:
description:
- A user with super user access
- Can also use 'P4USER' environment variable
required: true
type: str
aliases:
- p4user
password:
description:
- The super user password
- Can also use 'P4PASSWD' environment variable
required: true
type: str
aliases:
- p4passwd
serverid:
default: any
description:
- The server ID of the helix server
required: false
type: str
author:
- Asif Shaikh (@ripclawffb)
'''

EXAMPLES = '''
# Set auth.id configurable for any server
- name: Set auth.id
helix_configurable:
state: present
name: auth.id
value: master.1
server: '1666'
user: bruno
password: ''
# Unset auth.id configurable for specific server
- name: Unset auth.id
helix_configurable:
state: absent
name: auth.id
value: master.1
serverid: master.1
server: '1666'
user: bruno
password: ''
'''

RETURN = r''' # '''


from ansible.module_utils.basic import AnsibleModule, env_fallback
from ansible.module_utils.perforce.common import helix_connect, helix_disconnect


def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
state=dict(type='str', default='present', choices=['present', 'absent']),
name=dict(type='str', required=True),
value=dict(type='str', required=True),
server=dict(type='str', required=True, aliases=['p4port'], fallback=(env_fallback, ['P4PORT'])),
user=dict(type='str', required=True, aliases=['p4user'], fallback=(env_fallback, ['P4USER'])),
password=dict(type='str', required=True, aliases=['p4passwd'], fallback=(env_fallback, ['P4PASSWD']), no_log=True),
serverid=dict(type='str', default='any')
)

result = dict(
changed=False,
)

module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)

# connect to helix
p4 = helix_connect(module, 'ansible')

try:
# get existing config values
p4_current_configs = p4.run('configure', 'show', 'allservers')

p4_current_values = []

# search for all config values specific to this server id and add to list
for config in p4_current_configs:
if config["ServerName"] == module.params['serverid']:
p4_current_values.append(config)

# get the current value of our specific configurable
p4_current_value = next((item for item in p4_current_values if item["Name"] == module.params['name']), None)

if module.params['state'] == 'present':
if p4_current_value is None or module.params['value'] != p4_current_value['Value']:
if not module.check_mode:
p4.run('configure', 'set', "{0}#{1}={2}".format(
module.params['serverid'], module.params['name'], module.params['value'])
)
result['changed'] = True
elif module.params['state'] == 'absent':
if p4_current_value is not None:
if not module.check_mode:
p4.run('configure', 'unset', "{0}#{1}".format(
module.params['serverid'], module.params['name'])
)
result['changed'] = True
except Exception as e:
module.fail_json(msg="Error: {0}".format(e), **result)

helix_disconnect(module, p4)

module.exit_json(**result)


def main():
run_module()


if __name__ == '__main__':
main()
@@ -0,0 +1 @@
unsupported
@@ -0,0 +1,151 @@
# Test code for the Helix configurable module
# -*- coding: utf-8 -*-

# Copyright: (c) 2019, Asif Shaikh (@ripclawffb) <ripclaw_ffb@hotmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
- name: Install required lib
pip:
name: p4python

- name: Set auth.id for any server id
helix_configurable:
state: present
name: auth.id
value: master.1
p4port: '1666'
p4user: bruno
p4passwd: ''
register: auth_set_configurable

- debug:
var: auth_set_configurable

- assert:
that:
- auth_set_configurable is changed

- name: Set auth.id for any server id (idempotent)
helix_configurable:
state: present
name: auth.id
value: master.1
p4port: '1666'
p4user: bruno
p4passwd: ''
register: auth_set_configurable_idempotent

- debug:
var: auth_set_configurable_idempotent

- assert:
that:
- auth_set_configurable_idempotent is not changed

- name: Unset auth.id for any server id
helix_configurable:
state: absent
name: auth.id
value: master.1
p4port: '1666'
p4user: bruno
p4passwd: ''
register: auth_unset_configurable

- debug:
var: auth_set_configurable

- assert:
that:
- auth_set_configurable is changed

- name: Set auth.id for specific server (check mode)
helix_configurable:
state: present
name: auth.id
value: master.1
serverid: master
p4port: '1666'
p4user: bruno
p4passwd: ''
register: auth_set_serverid_configurable
check_mode: yes

- debug:
var: auth_set_serverid_configurable

- assert:
that:
- auth_set_serverid_configurable is changed

- name: Set auth.id for specific server
helix_configurable:
state: present
name: auth.id
value: master.1
serverid: master
p4port: '1666'
p4user: bruno
p4passwd: ''
register: auth_set_serverid_configurable

- debug:
var: auth_set_serverid_configurable

- assert:
that:
- auth_set_serverid_configurable is changed

- name: Unset auth.id for specific server id
helix_configurable:
state: absent
name: auth.id
value: master.1
serverid: master
p4port: '1666'
p4user: bruno
p4passwd: ''
register: auth_unset_serverid_configurable

- debug:
var: auth_unset_serverid_configurable

- assert:
that:
- auth_unset_serverid_configurable is changed

- name: Fail when using invalid login
helix_configurable:
state: present
name: auth.id
value: master.1
p4port: '1666'
p4user: bruno1
p4passwd: ''
register: auth_set_invalid_login
ignore_errors: true

- debug:
var: auth_set_invalid_login

- assert:
that:
- auth_set_invalid_login.failed | bool

- name: Fail when using invalid port
helix_configurable:
state: present
name: auth.id
value: master.1
p4port: '1999'
p4user: bruno1
p4passwd: ''
register: auth_set_invalid_port
ignore_errors: true

- debug:
var: auth_set_invalid_port

- assert:
that:
- auth_set_invalid_port.failed | bool
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.