From 61eb57ced5b6d2e327046e3bcb6692b6daeca080 Mon Sep 17 00:00:00 2001 From: Spencer Owen Date: Wed, 21 May 2025 14:14:27 -0600 Subject: [PATCH 1/2] Add support for custom attributes --- docs/QUEUE_MANAGER.md | 112 +++++++++++++++++++------------ plugins/modules/queue_manager.py | 47 ++++++++++++- tests/unit/test_queue_manager.py | 91 +++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 45 deletions(-) diff --git a/docs/QUEUE_MANAGER.md b/docs/QUEUE_MANAGER.md index bdc824d..ddad863 100644 --- a/docs/QUEUE_MANAGER.md +++ b/docs/QUEUE_MANAGER.md @@ -1,76 +1,85 @@ -# `queue_manager.py` - +# `queue_manager.py` Module to create, start, delete a queue manager and run MQSC files. ## Parameters - - `qmname` : IBM MQ queue manager name. - `state` : Desired state of the queue manager (`present`, `absent`, `running`). - `description` : IBM MQ queue manager description - *optional*. -- `unit_test`: flag used for unit tests of modules. +- `unit_test`: Flag used for unit tests of modules. - `mqsc_file` : Specified MQSC command file to run - *optional*. +- `data_dir` : Data directory path for queue manager (`-md` flag) - *optional*. +- `log_dir` : Log directory path for queue manager (`-ld` flag) - *optional*. +- `log_file_size` : Log file size in kilobytes (`-lf` flag) - *optional*. +- `log_primary` : Number of primary log files (`-lp` flag) - *optional*. +- `log_secondary` : Number of secondary log files (`-ls` flag) - *optional*. ## Return values - -- `msg` : message of the performed task. -- `rc` : return code. -- `state` +- `msg` : Message of the performed task. +- `rc` : Return code. +- `state` : State of the queue manager after the operation. ## Examples for playbooks #### Creating a Queue Manager task - -``` +```yaml - name: Create queue manager - queue_manager: - qmname: 'queue_manager_name' - state: present + queue_manager: + qmname: 'queue_manager_name' + state: present ``` -#### Starting a Queue Manager task - +#### Creating a Queue Manager with custom directory paths and log settings +```yaml +- name: Create queue manager with custom paths and log settings + queue_manager: + qmname: 'queue_manager_name' + state: present + data_dir: '/path/to/data/directory' + log_dir: '/path/to/log/directory' + log_file_size: 8192 + log_primary: 200 + log_secondary: 200 + description: 'My custom queue manager' ``` + +#### Starting a Queue Manager task +```yaml - name: Start queue manager - queue_manager: - qmname: 'queue_manager_name' - state: running + queue_manager: + qmname: 'queue_manager_name' + state: running ``` #### Deleting a Queue Manager task - -``` -- name: Start queue manager - queue_manager: - qmname: 'queue_manager_name' - state: absent +```yaml +- name: Delete queue manager + queue_manager: + qmname: 'queue_manager_name' + state: absent ``` #### Run MQSC command file task - -``` -- name: Run MQSC command file - queue_manager: - qmname: 'queue_manager_name' - state: running - mqsc_file: 'commfile.in' +```yaml +- name: Run MQSC command file + queue_manager: + qmname: 'queue_manager_name' + state: running + mqsc_file: 'commfile.in' ``` #### Use of ALL_QMGRS value - This may be used to refer to all queue managers currently defined to a system, e.g to start/stop both QM1 and QM2 defined in mq-setup.yml. - -``` -- name: Run MQSC command file - queue_manager: - qmname: 'ALL_QMGRS' - state: running +```yaml +- name: Start all queue managers + queue_manager: + qmname: 'ALL_QMGRS' + state: running ``` -## Example of unit testing of a module +## Example of unit testing of a module +Note: Exception classes `AnsibleExitJson` and `AnsibleFailJson` should be set. See [`test_queue_manager.py`](ansible_collections/ibm/ibmmq/tests/unit/test_queue_manager.py) for reference. -Note: Exeption classes `AnsibleExitJson` and `AnsibleFailJson` should be set. See [`test_queue_manager.py`](ansible_collections/ibm/ibmmq/tests/unit/test_queue_manager.py) for reference. - -``` +```python def test_delete_qm(self): set_module_args({ 'qmname': 'qm1', @@ -82,3 +91,22 @@ def test_delete_qm(self): queue_manager.main() self.assertEquals(result.exception.args[0]['state'], 'absent') ``` + +## Example of creating a queue manager with custom paths +```python +def test_create_qm_with_paths(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'present', + 'description': 'Queue manager with custom paths', + 'data_dir': '/var/mqm/qmgrs/qm1/data', + 'log_dir': '/var/mqm/qmgrs/qm1/logs', + 'log_file_size': 8192, + 'log_primary': 10, + 'log_secondary': 20, + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'present') +``` \ No newline at end of file diff --git a/plugins/modules/queue_manager.py b/plugins/modules/queue_manager.py index f22b153..9fe9307 100644 --- a/plugins/modules/queue_manager.py +++ b/plugins/modules/queue_manager.py @@ -35,10 +35,45 @@ def check_status_queue_managers(qmname, module): return False +def build_crtmqm_command(qmname, module): + """Build the crtmqm command with all supported flags""" + cmd = ['crtmqm'] + + # Add data directory if specified + if module.params.get('data_dir'): + cmd.extend(['-md', module.params['data_dir']]) + + # Add log directory if specified + if module.params.get('log_dir'): + cmd.extend(['-ld', module.params['log_dir']]) + + # Add log file size if specified + if module.params.get('log_file_size'): + cmd.extend(['-lf', str(module.params['log_file_size'])]) + + # Add log primary files if specified + if module.params.get('log_primary'): + cmd.extend(['-lp', str(module.params['log_primary'])]) + + # Add log secondary files if specified + if module.params.get('log_secondary'): + cmd.extend(['-ls', str(module.params['log_secondary'])]) + + # Add queue manager description if specified + if module.params.get('description'): + cmd.extend(['-q', module.params['description']]) + + # Add queue manager name at the end + cmd.append(qmname) + + return cmd + def state_present(qmname, module): if module.params['unit_test'] is False: - rc, stdout, stderr = module.run_command(['crtmqm', qmname]) + # Use the function to build the command with all parameters + cmd = build_crtmqm_command(qmname, module) + rc, stdout, stderr = module.run_command(cmd) result['rc'] = rc if module.params['mqsc_file'] is not None: @@ -93,7 +128,8 @@ def state_running(qmname, module): if rc == 72: # QMGR does not exist Create then set running - rc, stdout, stderr = module.run_command(['crtmqm', qmname]) + cmd = build_crtmqm_command(qmname, module) + rc, stdout, stderr = module.run_command(cmd) if rc > 0: # Critical Error module.fail_json(**result) @@ -185,7 +221,12 @@ def main(): state=dict(type='str', required=True), description=dict(type='str', required=False), unit_test=dict(type='bool', default=False, required=False), - mqsc_file=dict(type='str', required=False) + mqsc_file=dict(type='str', required=False), + data_dir=dict(type='str', required=False), + log_dir=dict(type='str', required=False), + log_file_size=dict(type='int', required=False), + log_primary=dict(type='int', required=False), + log_secondary=dict(type='int', required=False) ) module = AnsibleModule( diff --git a/tests/unit/test_queue_manager.py b/tests/unit/test_queue_manager.py index 4779edd..db36fc2 100644 --- a/tests/unit/test_queue_manager.py +++ b/tests/unit/test_queue_manager.py @@ -84,6 +84,60 @@ def test_create_qm(self): queue_manager.main() self.assertEquals(result.exception.args[0]['state'], 'present') + def test_create_qm_with_data_dir(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'present', + 'description': 'testing', + 'data_dir': '/mnt/mqm/qmgrs/qm1/data', + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'present') + + def test_create_qm_with_log_dir(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'present', + 'description': 'testing', + 'log_dir': '/mnt/mqm/qmgrs/qm1/logs', + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'present') + + def test_create_qm_with_log_parameters(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'present', + 'description': 'testing', + 'log_file_size': 8192, + 'log_primary': 200, + 'log_secondary': 200, + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'present') + + def test_create_qm_with_all_parameters(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'present', + 'description': 'testing', + 'data_dir': '/mnt/mqm/qmgrs/qm1/data', + 'log_dir': '/mnt/mqm/qmgrs/qm1/logs', + 'log_file_size': 8192, + 'log_primary': 200, + 'log_secondary': 200, + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'present') + def test_delete_qm(self): set_module_args({ 'qmname': 'qm1', @@ -109,6 +163,24 @@ def test_start_qmgr(self): self.assertEquals(result.exception.args[0]['msg'], 'IBM MQ queue manager \'qm1\' started') self.assertEquals(result.exception.args[0]['rc'], 0) + def test_start_qmgr_with_all_parameters(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'running', + 'description': 'testing', + 'data_dir': '/mnt/mqm/qmgrs/qm1/data', + 'log_dir': '/mnt/mqm/qmgrs/qm1/logs', + 'log_file_size': 8192, + 'log_primary': 200, + 'log_secondary': 200, + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'running') + self.assertEquals(result.exception.args[0]['msg'], 'IBM MQ queue manager \'qm1\' started') + self.assertEquals(result.exception.args[0]['rc'], 0) + def test_runmqsc_on_qmgr(self): set_module_args({ 'qmname': 'qm1', @@ -122,3 +194,22 @@ def test_runmqsc_on_qmgr(self): self.assertEquals(result.exception.args[0]['state'], 'present') self.assertEquals(result.exception.args[0]['msg'], 'runmqsc command ran successfully') self.assertEquals(result.exception.args[0]['rc'], 0) + + def test_runmqsc_on_qmgr_with_all_parameters(self): + set_module_args({ + 'qmname': 'qm1', + 'state': 'present', + 'description': 'testing', + 'mqsc_file': 'testing_file', + 'data_dir': '/mnt/mqm/qmgrs/qm1/data', + 'log_dir': '/mnt/mqm/qmgrs/qm1/logs', + 'log_file_size': 8192, + 'log_primary': 200, + 'log_secondary': 200, + 'unit_test': True + }) + with self.assertRaises(AnsibleExitJson) as result: + queue_manager.main() + self.assertEquals(result.exception.args[0]['state'], 'present') + self.assertEquals(result.exception.args[0]['msg'], 'runmqsc command ran successfully') + self.assertEquals(result.exception.args[0]['rc'], 0) \ No newline at end of file From fdb76d4d5548ff690466038d6e180d654732b716 Mon Sep 17 00:00:00 2001 From: Spencer Owen Date: Thu, 7 Aug 2025 14:09:24 -0600 Subject: [PATCH 2/2] Fix description --- plugins/modules/queue_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/queue_manager.py b/plugins/modules/queue_manager.py index 9fe9307..3ea8f83 100644 --- a/plugins/modules/queue_manager.py +++ b/plugins/modules/queue_manager.py @@ -61,7 +61,7 @@ def build_crtmqm_command(qmname, module): # Add queue manager description if specified if module.params.get('description'): - cmd.extend(['-q', module.params['description']]) + cmd.extend(['-c', module.params['description']]) # Add queue manager name at the end cmd.append(qmname)