Debian Codegraph @

ansible_collections/community/general/plugins/modules/rundeck_job_executions_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2021, Phillipe Smith <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: rundeck_job_executions_info
11 short_description: Query executions for a Rundeck job
12 description:
13 - This module gets the list of executions for a specified Rundeck job.
14 author: "Phillipe Smith (@phsmith)"
15 version_added: 3.8.0
16 options:
17 job_id:
18 type: str
19 description:
20 - The job unique ID.
21 required: true
22 status:
23 type: str
24 description:
25 - The job status to filter.
26 choices: [succeeded, failed, aborted, running]
27 max:
28 type: int
29 description:
30 - Max results to return.
31 default: 20
32 offset:
33 type: int
34 description:
35 - The start point to return the results.
36 default: 0
37 extends_documentation_fragment:
38 - community.general._rundeck
39 - ansible.builtin.url
40 - community.general._attributes
41 - community.general._attributes.info_module
42 """
43
44 EXAMPLES = r"""
45 - name: Get Rundeck job executions info
46 community.general.rundeck_job_executions_info:
47 url: "https://rundeck.example.org"
48 api_version: 39
49 api_token: "mytoken"
50 job_id: "xxxxxxxxxxxxxxxxx"
51 register: rundeck_job_executions_info
52
53 - name: Show Rundeck job executions info
54 ansible.builtin.debug:
55 var: rundeck_job_executions_info.executions
56 """
57
58 RETURN = r"""
59 paging:
60 description: Results pagination info.
61 returned: success
62 type: dict
63 contains:
64 count:
65 description: Number of results in the response.
66 type: int
67 returned: success
68 total:
69 description: Total number of results.
70 type: int
71 returned: success
72 offset:
73 description: Offset from first of all results.
74 type: int
75 returned: success
76 max:
77 description: Maximum number of results per page.
78 type: int
79 returned: success
80 sample:
81 {
82 "count": 20,
83 "total": 100,
84 "offset": 0,
85 "max": 20
86 }
87 executions:
88 description: Job executions list.
89 returned: always
90 type: list
91 elements: dict
92 sample:
93 [
94 {
95 "id": 1,
96 "href": "https://rundeck.example.org/api/39/execution/1",
97 "permalink": "https://rundeck.example.org/project/myproject/execution/show/1",
98 "status": "succeeded",
99 "project": "myproject",
100 "executionType": "user",
101 "user": "admin",
102 "date-started": {
103 "unixtime": 1633525515026,
104 "date": "2021-10-06T13:05:15Z"
105 },
106 "date-ended": {
107 "unixtime": 1633525518386,
108 "date": "2021-10-06T13:05:18Z"
109 },
110 "job": {
111 "id": "697af0c4-72d3-4c15-86a3-b5bfe3c6cb6a",
112 "averageDuration": 6381,
113 "name": "Test",
114 "group": "",
115 "project": "myproject",
116 "description": "",
117 "options": {
118 "exit_code": "0"
119 },
120 "href": "https://rundeck.example.org/api/39/job/697af0c4-72d3-4c15-86a3-b5bfe3c6cb6a",
121 "permalink": "https://rundeck.example.org/project/myproject/job/show/697af0c4-72d3-4c15-86a3-b5bfe3c6cb6a"
122 },
123 "description": "Plugin[com.batix.rundeck.plugins.AnsiblePlaybookInlineWorkflowStep, nodeStep: false]",
124 "argstring": "-exit_code 0",
125 "serverUUID": "5b9a1438-fa3a-457e-b254-8f3d70338068"
126 }
127 ]
128 """
129
130 from urllib.parse import quote
131
132 from ansible.module_utils.basic import AnsibleModule
133
134 from ansible_collections.community.general.plugins.module_utils._rundeck import api_argument_spec, api_request
135
136
137 class RundeckJobExecutionsInfo:
138 def __init__(self, module):
139 self.module = module
140 self.url = self.module.params["url"]
141 self.api_version = self.module.params["api_version"]
142 self.job_id = self.module.params["job_id"]
143 self.offset = self.module.params["offset"]
144 self.max = self.module.params["max"]
145 self.status = self.module.params["status"] or ""
146
147 def job_executions(self):
148 response, info = api_request(
149 module=self.module,
150 endpoint=f"job/{quote(self.job_id)}/executions?offset={self.offset}&max={self.max}&status={self.status}",
151 method="GET",
152 )
153
154 if info["status"] != 200:
155 self.module.fail_json(msg=info["msg"], executions=response)
156
157 self.module.exit_json(msg="Executions info result", **response)
158
159
160 def main():
161 argument_spec = api_argument_spec()
162 argument_spec.update(
163 dict(
164 job_id=dict(required=True, type="str"),
165 offset=dict(type="int", default=0),
166 max=dict(type="int", default=20),
167 status=dict(type="str", choices=["succeeded", "failed", "aborted", "running"]),
168 )
169 )
170
171 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
172
173 if module.params["api_version"] < 14:
174 module.fail_json(msg="API version should be at least 14")
175
176 rundeck = RundeckJobExecutionsInfo(module)
177 rundeck.job_executions()
178
179
180 if __name__ == "__main__":
181 main()