Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright Edward Hilgendorf, <[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: dnsimple_info
11
12 short_description: Pull basic info from DNSimple API
13
14 version_added: "4.2.0"
15
16 description: Retrieve existing records and domains from DNSimple API.
17
18 extends_documentation_fragment:
19 - community.general._attributes
20 - community.general._attributes.info_module
21
22 options:
23 name:
24 description:
25 - The domain name to retrieve info from.
26 - Returns all associated records for this domain if specified.
27 - If not specified, returns all domains associated with the account ID.
28 type: str
29
30 account_id:
31 description: The account ID to query.
32 required: true
33 type: str
34
35 api_key:
36 description: The API key to use.
37 required: true
38 type: str
39
40 record:
41 description:
42 - The record to find.
43 - If specified, only this record is returned instead of all records.
44 type: str
45
46 sandbox:
47 description: Whether or not to use sandbox environment.
48 default: false
49 type: bool
50
51 author:
52 - Edward Hilgendorf (@edhilgendorf)
53 """
54
55 EXAMPLES = r"""
56 - name: Get all domains from an account
57 community.general.dnsimple_info:
58 account_id: "1234"
59 api_key: "1234"
60
61 - name: Get all records from a domain
62 community.general.dnsimple_info:
63 name: "example.com"
64 account_id: "1234"
65 api_key: "1234"
66
67 - name: Get all info from a matching record
68 community.general.dnsimple_info:
69 name: "example.com"
70 record: "subdomain"
71 account_id: "1234"
72 api_key: "1234"
73 """
74
75 RETURN = r"""
76 dnsimple_domain_info:
77 description: Returns a list of dictionaries of all domains associated with the supplied account ID.
78 type: list
79 elements: dict
80 returned: success when O(name) is not specified
81 sample:
82 - account_id: 1234
83 created_at: '2021-10-16T21:25:42Z'
84 id: 123456
85 last_transferred_at:
86 name: example.com
87 reverse: false
88 secondary: false
89 updated_at: '2021-11-10T20:22:50Z'
90 contains:
91 account_id:
92 description: The account ID.
93 type: int
94 created_at:
95 description: When the domain entry was created.
96 type: str
97 id:
98 description: ID of the entry.
99 type: int
100 last_transferred_at:
101 description: Date the domain was transferred, or empty if not.
102 type: str
103 name:
104 description: Name of the record.
105 type: str
106 reverse:
107 description: Whether or not it is a reverse zone record.
108 type: bool
109 updated_at:
110 description: When the domain entry was updated.
111 type: str
112
113 dnsimple_records_info:
114 description: Returns a list of dictionaries with all records for the domain supplied.
115 type: list
116 elements: dict
117 returned: success when O(name) is specified, but O(record) is not
118 sample:
119 - content: ns1.dnsimple.com admin.dnsimple.com
120 created_at: '2021-10-16T19:07:34Z'
121 id: 12345
122 name: 'catheadbiscuit'
123 parent_id:
124 priority:
125 regions:
126 - global
127 system_record: true
128 ttl: 3600
129 type: SOA
130 updated_at: '2021-11-15T23:55:51Z'
131 zone_id: example.com
132 contains:
133 content:
134 description: Content of the returned record.
135 type: str
136 created_at:
137 description: When the domain entry was created.
138 type: str
139 id:
140 description: ID of the entry.
141 type: int
142 name:
143 description: Name of the record.
144 type: str
145 parent_id:
146 description: Parent record or null.
147 type: int
148 priority:
149 description: Priority setting of the record.
150 type: str
151 regions:
152 description: List of regions where the record is available.
153 type: list
154 system_record:
155 description: Whether or not it is a system record.
156 type: bool
157 ttl:
158 description: Record TTL.
159 type: int
160 type:
161 description: Record type.
162 type: str
163 updated_at:
164 description: When the domain entry was updated.
165 type: str
166 zone_id:
167 description: ID of the zone that the record is associated with.
168 type: str
169 dnsimple_record_info:
170 description: Returns a list of dictionaries that match the record supplied.
171 returned: success when O(name) and O(record) are specified
172 type: list
173 elements: dict
174 sample:
175 - content: 1.2.3.4
176 created_at: '2021-11-15T23:55:51Z'
177 id: 123456
178 name: catheadbiscuit
179 parent_id:
180 priority:
181 regions:
182 - global
183 system_record: false
184 ttl: 3600
185 type: A
186 updated_at: '2021-11-15T23:55:51Z'
187 zone_id: example.com
188 contains:
189 content:
190 description: Content of the returned record.
191 type: str
192 created_at:
193 description: When the domain entry was created.
194 type: str
195 id:
196 description: ID of the entry.
197 type: int
198 name:
199 description: Name of the record.
200 type: str
201 parent_id:
202 description: Parent record or null.
203 type: int
204 priority:
205 description: Priority setting of the record.
206 type: str
207 regions:
208 description: List of regions where the record is available.
209 type: list
210 system_record:
211 description: Whether or not it is a system record.
212 type: bool
213 ttl:
214 description: Record TTL.
215 type: int
216 type:
217 description: Record type.
218 type: str
219 updated_at:
220 description: When the domain entry was updated.
221 type: str
222 zone_id:
223 description: ID of the zone that the record is associated with.
224 type: str
225 """
226
227 from ansible.module_utils.basic import AnsibleModule
228
229 from ansible_collections.community.general.plugins.module_utils import _deps as deps
230
231 with deps.declare("requests"):
232 from requests import Request, Session
233
234
235 def build_url(account, key, is_sandbox):
236 headers = {"Accept": "application/json", "Authorization": f"Bearer {key}"}
237 sandbox = ".sandbox" if is_sandbox else ""
238 url = f"https://api{sandbox}.dnsimple.com/v2/{account}"
239 req = Request(url=url, headers=headers)
240 prepped_request = req.prepare()
241 return prepped_request
242
243
244 def iterate_data(module, request_object):
245 base_url = request_object.url
246 response = Session().send(request_object)
247 if "pagination" not in response.json():
248 module.fail_json("API Call failed, check ID, key and sandbox values")
249
250 data = response.json()["data"]
251 total_pages = response.json()["pagination"]["total_pages"]
252 page = 1
253
254 while page < total_pages:
255 page = page + 1
256 request_object.url = f"{base_url}&page={page}"
257 new_results = Session().send(request_object)
258 data = data + new_results.json()["data"]
259
260 return data
261
262
263 def record_info(dnsimple_mod, req_obj):
264 req_obj.url, req_obj.method = (
265 f"{req_obj.url}/zones/{dnsimple_mod.params['name']}/records?name={dnsimple_mod.params['record']}",
266 "GET",
267 )
268 return iterate_data(dnsimple_mod, req_obj)
269
270
271 def domain_info(dnsimple_mod, req_obj):
272 req_obj.url, req_obj.method = f"{req_obj.url}/zones/{dnsimple_mod.params['name']}/records?per_page=100", "GET"
273 return iterate_data(dnsimple_mod, req_obj)
274
275
276 def account_info(dnsimple_mod, req_obj):
277 req_obj.url, req_obj.method = f"{req_obj.url}/zones/?per_page=100", "GET"
278 return iterate_data(dnsimple_mod, req_obj)
279
280
281 def main():
282 result = {"changed": False}
283
284 module = AnsibleModule(
285 argument_spec=dict(
286 account_id=dict(required=True, type="str"),
287 api_key=dict(required=True, type="str", no_log=True),
288 name=dict(type="str"),
289 record=dict(type="str"),
290 sandbox=dict(type="bool", default=False),
291 ),
292 supports_check_mode=True,
293 )
294
295 params = module.params
296 req = build_url(params["account_id"], params["api_key"], params["sandbox"])
297
298 deps.validate(module)
299
300 # If we have a record return info on that record
301 if params["name"] and params["record"]:
302 result["dnsimple_record_info"] = record_info(module, req)
303 module.exit_json(**result)
304
305 # If we have the account only and domain, return records for the domain
306 elif params["name"]:
307 result["dnsimple_records_info"] = domain_info(module, req)
308 module.exit_json(**result)
309
310 # If we have the account only, return domains
311 else:
312 result["dnsimple_domain_info"] = account_info(module, req)
313 module.exit_json(**result)
314
315
316 if __name__ == "__main__":
317 main()