Skip to content

Commit f0c849a

Browse files
authored
Merge pull request #119 from AndrewTwydell/main
Added `fail_on_nodata` attribute to the cmci_get task
2 parents dfdcc7e + 1c77d40 commit f0c849a

21 files changed

+529
-98
lines changed

plugins/module_utils/cmci.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ def handle_response(self, response_dict): # type: (Dict) -> None
418418
feedback = errors_node[FEEDBACK]
419419
self.result[FEEDBACK] = read_error_node(feedback)
420420

421-
# Non-OK CPSM responses fail the module
422-
if cpsm_response_code != 1024:
421+
# If CPSM response code not in Valid List, fail the module
422+
if self.get_ok_cpsm_response_codes().count(cpsm_response_code) == 0:
423423
self._fail(
424424
'CMCI request failed with response "{0}" reason "{1}"'
425425
.format(
@@ -436,6 +436,9 @@ def handle_response(self, response_dict): # type: (Dict) -> None
436436
'Could not parse CMCI response: missing node "{0}"'
437437
.format(e.args[0])
438438
)
439+
440+
def get_ok_cpsm_response_codes(self):
441+
return [1024]
439442

440443
def init_url(self): # type: () -> str
441444
t = self._p.get(TYPE).lower()

plugins/modules/cmci_get.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
- The count value must be an integer; a value of zero is not permitted.
4141
type: int
4242
required: false
43+
fail_on_nodata:
44+
description:
45+
- Specifies whether the module should fail if no data is returned by the
46+
query. If set to true, the module will fail if no data is returned.
47+
- Default behaviour is for the module to fail if no data is returned. When
48+
set to false, the module will return OK, just with no records.
49+
type: bool
50+
required: false
51+
default: true
4352
'''
4453

4554

@@ -87,6 +96,23 @@
8796
value: MYGRP
8897
record_count: 1
8998
99+
- name: pass module even if bundle definition is not found
100+
cmci_get:
101+
cmci_host: 'winmvs2c.hursley.ibm.com'
102+
cmci_port: 10080
103+
cmci_cert: './sec/ansible.pem'
104+
cmci_key: './sec/ansible.key'
105+
context: 'iyk3z0r9'
106+
type: cicsdefinitionbundle
107+
resources:
108+
filter:
109+
name: MYBUNDLE
110+
get_parameters:
111+
- name: csdgroup
112+
value: MYGRP
113+
record_count: 1
114+
fail_on_nodata: "false"
115+
90116
- name: Using complex_filter to combine filter expressions and change operators
91117
cmci_get:
92118
cmci_host: 'winmvs2c.hursley.ibm.com'
@@ -496,6 +522,7 @@
496522

497523

498524
_RECORD_COUNT = 'record_count'
525+
_FAIL_ON_NODATA = 'fail_on_nodata'
499526

500527

501528
class AnsibleCMCIGetModule(AnsibleCMCIModule):
@@ -507,6 +534,10 @@ def init_argument_spec(self): # type: () -> Dict
507534
argument_spec.update({
508535
_RECORD_COUNT: {
509536
'type': 'int'
537+
},
538+
_FAIL_ON_NODATA: {
539+
'type': 'bool',
540+
'default': True
510541
}
511542
})
512543
argument_spec.update(RESOURCES_ARGUMENT)
@@ -522,6 +553,14 @@ def init_url(self): # type: () -> str
522553
url = url + '//' + str(self._p.get(_RECORD_COUNT))
523554

524555
return url
556+
557+
def get_ok_cpsm_response_codes(self):
558+
ok_codes = super(AnsibleCMCIGetModule, self).get_ok_cpsm_response_codes()
559+
560+
if not self._p.get(_FAIL_ON_NODATA):
561+
ok_codes.append(1027)
562+
563+
return ok_codes
525564

526565

527566
def main():

tests/integration/targets/cics_cmci/cmci-variables.yml.template

100644100755
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ cmci_secure_port: 28951
77
cmci_user: __user__
88
cmci_password: __password__
99
cmci_context: CICSEX56
10-
cmci_scope: IYCWEMW2
10+
cmci_scope_https: IYCWEMW2
11+
cmci_scope_http: IYCWEMW1
12+
cmci_scope_region_1: IYCWEML1
13+
cmci_scope_region_2: IYCWEMM1

tests/integration/targets/cics_cmci/playbooks/cics_cmci_http.yml

100644100755
Lines changed: 143 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
collections:
66
- ibm.ibm_zos_cics
77
hosts: 'localhost'
8-
gather_facts: 'false'
8+
gather_facts: false
99
vars:
1010
csdgroup: 'HTTPTEST'
1111
program: 'HTTPTEST'
12-
http_scope: IYCWEMW1
12+
program_2: 'HTTPTES2'
13+
program_filter: 'HTTPTES*'
1314

1415
module_defaults:
1516
ibm.ibm_zos_cics.cmci_get:
@@ -18,7 +19,7 @@
1819
cmci_user: '{{ cmci_user }}'
1920
cmci_password: '{{ cmci_password }}'
2021
context: '{{ cmci_context }}'
21-
scope: '{{ http_scope }}'
22+
scope: '{{ cmci_scope_http }}'
2223
insecure: true
2324
scheme: 'http'
2425

@@ -29,7 +30,7 @@
2930
cmci_user: '{{ cmci_user }}'
3031
cmci_password: '{{ cmci_password }}'
3132
context: '{{ cmci_context }}'
32-
scope: '{{ http_scope }}'
33+
scope: '{{ cmci_scope_http }}'
3334
insecure: true
3435
scheme: 'http'
3536

@@ -40,7 +41,7 @@
4041
cmci_user: '{{ cmci_user }}'
4142
cmci_password: '{{ cmci_password }}'
4243
context: '{{ cmci_context }}'
43-
scope: '{{ http_scope }}'
44+
scope: '{{ cmci_scope_http }}'
4445
insecure: true
4546
scheme: 'http'
4647

@@ -51,7 +52,7 @@
5152
cmci_user: '{{ cmci_user }}'
5253
cmci_password: '{{ cmci_password }}'
5354
context: '{{ cmci_context }}'
54-
scope: '{{ http_scope }}'
55+
scope: '{{ cmci_scope_http }}'
5556
insecure: true
5657
scheme: 'http'
5758

@@ -62,7 +63,7 @@
6263
cmci_user: '{{ cmci_user }}'
6364
cmci_password: '{{ cmci_password }}'
6465
context: '{{ cmci_context }}'
65-
scope: '{{ http_scope }}'
66+
scope: '{{ cmci_scope_http }}'
6667
insecure: true
6768
scheme: 'http'
6869

@@ -169,9 +170,6 @@
169170
- result.record_count == 1
170171
- result.records[0].program == program
171172

172-
- name: debug 1
173-
debug:
174-
msg: "{{ result }}"
175173

176174
- name: 'HTTP Disable program'
177175
delegate_to: 'localhost'
@@ -194,13 +192,8 @@
194192
- attribute: 'USECOUNT'
195193
operator: 'LT'
196194
value: '1'
197-
198195
register: result
199196

200-
- name: debug 2
201-
debug:
202-
msg: "{{ result }}"
203-
204197
- name: assert 5
205198
assert:
206199
that:
@@ -227,6 +220,68 @@
227220
- result.cpsm_response == 'OK'
228221
- result.record_count == 1
229222
- result.success_count == 1
223+
224+
225+
- name: 'HTTP Create progdef 2'
226+
delegate_to: 'localhost'
227+
ibm.ibm_zos_cics.cmci_create:
228+
type: 'CICSDefinitionProgram'
229+
attributes:
230+
name: '{{ program_2 }}'
231+
csdgroup: '{{ csdgroup }}'
232+
create_parameters:
233+
- name: 'CSD'
234+
register: result
235+
236+
- name: assert program_2 created
237+
assert:
238+
that:
239+
- result is changed
240+
- result.cpsm_response == 'OK'
241+
- result.record_count == 1
242+
- result.records[0].name == program_2
243+
244+
245+
- name: 'HTTP Check All Records Returned'
246+
delegate_to: 'localhost'
247+
ibm.ibm_zos_cics.cmci_get:
248+
type: 'CICSDefinitionProgram'
249+
resources:
250+
filter:
251+
name: '{{ program_filter }}'
252+
get_parameters:
253+
- name: 'CSDGROUP'
254+
value: '{{ csdgroup }}'
255+
register: result
256+
257+
- name: assert record_count is 2
258+
assert:
259+
that:
260+
- result is not changed
261+
- result.cpsm_response == 'OK'
262+
- result.record_count == 2
263+
264+
265+
- name: 'HTTP Check record count attribute'
266+
delegate_to: 'localhost'
267+
ibm.ibm_zos_cics.cmci_get:
268+
type: 'CICSDefinitionProgram'
269+
record_count: 1
270+
resources:
271+
filter:
272+
name: '{{ program_filter }}'
273+
get_parameters:
274+
- name: 'CSDGROUP'
275+
value: '{{ csdgroup }}'
276+
register: result
277+
278+
- name: assert record_count attribute
279+
assert:
280+
that:
281+
- result is not changed
282+
- result.cpsm_response == 'OK'
283+
- result.record_count == 2
284+
- result.records|length == 1
230285

231286

232287
- name: 'HTTP Delete progdef'
@@ -247,4 +302,76 @@
247302
- result is changed
248303
- result.cpsm_response == 'OK'
249304
- result.record_count == 1
250-
- result.success_count == 1
305+
- result.success_count == 1
306+
307+
308+
- name: 'HTTP Delete progdef2'
309+
delegate_to: 'localhost'
310+
ibm.ibm_zos_cics.cmci_delete:
311+
type: 'CICSDefinitionProgram'
312+
resources:
313+
filter:
314+
NAME: '{{ program_2 }}'
315+
get_parameters:
316+
- name: 'CSDGROUP'
317+
value: '{{ csdgroup }}'
318+
register: result
319+
320+
- name: assert program_2 deleted
321+
assert:
322+
that:
323+
- result is changed
324+
- result.cpsm_response == 'OK'
325+
- result.record_count == 1
326+
- result.success_count == 1
327+
328+
329+
- name: 'HTTP fail_on_nodata default'
330+
delegate_to: 'localhost'
331+
ibm.ibm_zos_cics.cmci_get:
332+
type: 'CICSDefinitionProgram'
333+
resources:
334+
filter:
335+
name: '{{ program_filter }}'
336+
get_parameters:
337+
- name: 'CSDGROUP'
338+
value: '{{ csdgroup }}'
339+
register: result
340+
failed_when: >
341+
'cpsm_response_code' not in result or result.cpsm_response_code not in [1024, 1027]
342+
343+
- name: assert fail_on_nodata attribute default
344+
assert:
345+
that:
346+
- result is not changed
347+
- result.failed == false
348+
- result.cpsm_response == 'NODATA'
349+
- result.cpsm_response_code == 1027
350+
- result.record_count == 0
351+
- result.msg is defined
352+
- result.failed_when_result is defined
353+
354+
355+
- name: 'HTTP fail_on_nodata'
356+
delegate_to: 'localhost'
357+
ibm.ibm_zos_cics.cmci_get:
358+
type: 'CICSDefinitionProgram'
359+
fail_on_nodata: False
360+
resources:
361+
filter:
362+
name: '{{ program_filter }}'
363+
get_parameters:
364+
- name: 'CSDGROUP'
365+
value: '{{ csdgroup }}'
366+
register: result
367+
368+
- name: assert fail_on_nodata attribute false
369+
assert:
370+
that:
371+
- result is not changed
372+
- result.failed == false
373+
- result.cpsm_response == 'NODATA'
374+
- result.cpsm_response_code == 1027
375+
- result.record_count == 0
376+
- result.msg is not defined
377+
- result.failed_when_result is not defined

0 commit comments

Comments
 (0)