This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
infoblox.py
executable file
·2233 lines (1990 loc) · 88.5 KB
/
infoblox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Copyright: Ansible Project
# 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: infoblox
author: "Joan Miquel Luque (@xoanmi)"
short_description: Manage Infoblox via Web API
description:
- Manage Infoblox IPAM and DNS via Web API
version_added: "2.5"
requirements:
- "requests <= 2.16.0"
options:
server:
description:
- Infoblox IP/URL
required: True
username:
description:
- Infoblox username
- The user must have API privileges
required: True
password:
description:
- Infoblox password
required: True
action:
description:
- Action to perform
required: True
choices: ["get_aliases", "get_cname", "get_a_record", "get_host", "get_network", "get_range", "get_next_available_ip", "get_fixedaddress",
"get_ipv6network", "get_ptr_record", "get_srv_record", "get_auth_zone", "get_forward_zone", "get_delegated_zone",
"add_alias", "add_cname", "add_host", "add_ipv6_host", "create_ptr_record", "get_txt_record", "get_network_container",
"create_a_record", "create_srv_record", "create_auth_zone", "create_forward_zone", "create_delegated_zone", "create_txt_record",
"create_network_container", "set_a_record", "set_name", "set_extattr", "update_a_record", "update_srv_record",
"update_ptr_record", "update_cname_record", "update_auth_zone", "update_forward_zone", "update_txt_record",
"update_network_container", "update_host_record", "delete_alias", "delete_cname", "delete_a_record", "delete_fixedaddress",
"delete_host", "delete_ptr_record", "delete_srv_record", "reserve_next_available_ip, search_a_record, search_host_record"]
host:
description:
- Hostname variable to search, add or delete host object
- The hostname must be in fqdn format
required: False
network:
description:
- Network address
- Must be indicated as a CDIR format or 192.168.1.0 format
required: False
default: False
start_addr:
description:
- Starting address for a range
- Must be indicated as 192.168.1.0 format
required: False
default: False
end_addr:
description:
- Ending address for a range
- Must be indicated as 192.168.1.0 format
required: False
default: False
address:
description:
- IP Address
required: False
default: False
addresses:
description:
- IP Addresses
required: False
default: False
attr_name:
description:
- Extra Attribute name
required: False
attr_value:
description:
- Extra Attribute value
required: False
comment:
description:
- Object comment
- This comment will be added when the module create any object
required: False
default: "Object managed by ansible-infoblox module"
api_version:
description:
- Infoblox Web API user to perfom actions
required: False
default: "1.7.1"
dns_view:
description:
- Infoblox DNS View
required: False
default: "Private"
net_view:
description:
- Infoblox Network View
required: False
default: "default"
ttl:
description:
- DNS time-to-live
required: False
default: None
'''
EXAMPLES = '''
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Add host
infoblox:
server: 192.168.1.1
username: admin
password: admin
action: add_host
network: 192.168.1.0/24
host: "{{ item }}"
with_items:
- test01.local
- test02.local
register: infoblox
'''
RETURN = '''
hostname:
description: Hostname of the object
returned: success
type: str
sample: test1.local
result:
description: result returned by the infoblox web API
returned: success
type: list
sample: [['...', '...'], ['...'], ['...']]
'''
from ansible.module_utils.basic import AnsibleModule
from copy import copy
_RETURN_FIELDS_PROPERTY = "_return_fields"
_COMMENT_PROPERTY = "comment"
_TTL_PROPERTY = "ttl"
_USE_TTL_PROPERTY = "use_ttl"
_NAME_PROPERTY = "name"
_VIEW_PROPERTY = "view"
_IPV4_ADDRESSES_PROPERTY = "ipv4addrs"
_IPV4_ADDRESS_PROPERTY = "ipv4addr"
_IPV6_ADDRESS_PROPERTY = "ipv6addr"
_ID_PROPERTY = "_ref"
_PTRDNAME_PROPERTY = "ptrdname"
_EXT_ATTR_PROPERTY = "extattrs"
_TXT_PROPERTY = "text"
_PORT_PROPERTY = "port"
_PRIORITY_PROPERTY = "priority"
_WEIGHT_PROPERTY = "weight"
_TARGET_PROPERTY = "target"
_MAC_PROPERTY = "mac"
_CANONICAL_PROPERTY = "canonical"
_FQDN_PROPERTY = "fqdn"
_FORWARD_TO_PROPERTY = "forward_to"
_NETWORK_PROPERTY = "network"
_NETWORK_CONTAINER_PROPERTY = "network_container"
_NETWORK_VIEW_PROPERTY = "network_view"
_DELEGATE_TO_PROPERTY = "delegate_to"
try:
import requests
requests.packages.urllib3.disable_warnings()
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
# ---------------------------------------------------------------------------
# Infoblox
# ---------------------------------------------------------------------------
class Infoblox(object):
"""
Class for manage all the REST API calls with the Infoblox appliances
"""
def __init__(self, module, server, username, password, api_version, dns_view, net_view):
self.module = module
self.dns_view = dns_view
self.net_view = net_view
self.auth = (username, password)
self.base_url = "https://{host}/wapi/v{version}/".format(
host=server, version=api_version)
self.model_list = [_COMMENT_PROPERTY, _TTL_PROPERTY, _USE_TTL_PROPERTY, _NAME_PROPERTY,
_VIEW_PROPERTY, _IPV4_ADDRESS_PROPERTY, _IPV6_ADDRESS_PROPERTY,
_ID_PROPERTY, _PTRDNAME_PROPERTY, _EXT_ATTR_PROPERTY, _TXT_PROPERTY,
_PORT_PROPERTY, _PRIORITY_PROPERTY, _WEIGHT_PROPERTY, _TARGET_PROPERTY,
_MAC_PROPERTY, _CANONICAL_PROPERTY, _FQDN_PROPERTY, _FORWARD_TO_PROPERTY,
_NETWORK_PROPERTY, _NETWORK_VIEW_PROPERTY, _DELEGATE_TO_PROPERTY,
_IPV4_ADDRESSES_PROPERTY]
def invoke(self, method, tail, ok_codes=(200,), **params):
"""
Perform the HTTPS request by using rest api
"""
request = getattr(requests, method)
response = request(self.base_url + tail,
auth=self.auth, verify=False, **params)
if response.status_code not in ok_codes:
raise Exception(response.json())
else:
payload = response.json()
if isinstance(payload, dict) and "text" in payload:
raise Exception(payload["text"])
else:
return payload
def _return_property(self, base, property_list=[]):
return_property = []
if base:
return_property.extend([_NAME_PROPERTY, _TTL_PROPERTY, _USE_TTL_PROPERTY,
_VIEW_PROPERTY, _COMMENT_PROPERTY, _EXT_ATTR_PROPERTY])
for current_property in property_list:
return_property.append(current_property)
return return_property
def _make_model(self, model_dict):
return_model = {}
for model in self.model_list:
if model_dict.get(model) or model_dict.get(model) is False:
return_model[model] = model_dict.get(model)
return return_model
# ---------------------------------------------------------------------------
# get_network()
# ---------------------------------------------------------------------------
def get_network(self, network=None, filters=None):
"""
Search network in infoblox by using rest api
Network format supported:
- 192.168.1.0
- 192.168.1.0/24
"""
if not network and not filters:
self.module.fail_json(
msg="You must specify the option 'network' or 'filters'.")
elif not network and not filters:
self.module.fail_json(
msg="Specify either 'network' or 'filter', but not both.")
elif network:
params = {_NETWORK_PROPERTY: network,
_NETWORK_VIEW_PROPERTY: self.net_view}
return self.invoke("get", "network", params=params)
elif filters:
list_of_filters = ['network?']
if not isinstance(filters, list):
self.module.fail_json(
msg="Specify a list of dicts with keys of 'filter' and 'value'")
for current_filter in filters:
if not isinstance(current_filter, dict):
self.module.fail_json(
msg="Please ensure each element is a dict with 'filter' and 'value' as keys")
elif not current_filter.get('filter') or not current_filter.get('value'):
self.module.fail_json(
msg="Please ensure each element is a dict with 'filter' and 'value' as keys")
list_of_filters.append(current_filter.get(
'filter') + '=' + current_filter.get('value'))
list_of_filters.append('&')
list_of_filters.pop()
out_filter = "".join(list_of_filters)
params = {_NETWORK_PROPERTY: network,
_NETWORK_VIEW_PROPERTY: self.net_view}
return self.invoke("get", out_filter, params=params)
else:
self.module.fail_json(msg="Unknown get_network issue.")
# ---------------------------------------------------------------------------
# get_range()
# ---------------------------------------------------------------------------
def get_range(self, start_addr, end_addr):
"""
Search range in infoblox by using rest api
Starting and ending address format supported:
- 192.168.1.0
"""
if not start_addr:
self.module.fail_json(
msg="You must specify the option 'start_addr.")
if not end_addr:
self.module.fail_json(msg="You must specify the option 'end_addr.")
params = {"start_addr": start_addr,
"end_addr": end_addr, _NETWORK_VIEW_PROPERTY: self.net_view}
return self.invoke("get", "range", params=params)
# ---------------------------------------------------------------------------
# get_ipv6network()
# ---------------------------------------------------------------------------
def get_ipv6network(self, network):
"""
Search ipv6 network in infoblox by using rest api
Network format supported:
- ipv6-cidr notation
"""
if not network:
self.module.fail_json(msg="You must specify the option 'network'.")
params = {_NETWORK_PROPERTY: network,
_NETWORK_VIEW_PROPERTY: self.net_view}
return self.invoke("get", "ipv6network", params=params)
# ---------------------------------------------------------------------------
# get_next_available_ip()
# ---------------------------------------------------------------------------
def get_next_available_ip(self, network_ref):
"""
Return next available ip in a network range
"""
if not network_ref:
self.module.exit_json(
msg="You must specify the option 'network_ref'.")
params = {"_function": "next_available_ip"}
return self.invoke("post", network_ref, ok_codes=(200,), params=params)
# ---------------------------------------------------------------------------
# get_next_available_network()
# ---------------------------------------------------------------------------
def get_next_available_network(self, network_container, cidr):
"""
Return next available ip in a network range
"""
network_containter_info = self.get_network_container(network_container)
if len(network_containter_info) < 1:
self.module.fail_json(msg="Network Container does not exist.")
network_ref = network_containter_info[0].get('_ref').split(':')[0]
if not network_ref:
self.module.exit_json(
msg="Container was not found.")
params = {"_function": "next_available_network",
"cidr": cidr, "num": 1}
# raise Exception([network_ref, params])
return self.invoke("post", network_ref, ok_codes=(200,), params=params)
# ---------------------------------------------------------------------------
# reserve_next_available_ip()
# ---------------------------------------------------------------------------
def reserve_next_available_ip(self, network, mac_addr="00:00:00:00:00:00",
comment=None, extattrs=None):
"""
Reserve ip address via fixedaddress in infoblox by using rest api
"""
if comment is None:
comment = "IP reserved via ansible infoblox self.module"
if extattrs is not None:
extattrs = self.add_attr(extattrs)
model = {_IPV4_ADDRESS_PROPERTY: "func:nextavailableip:" + network, _MAC_PROPERTY: mac_addr,
_COMMENT_PROPERTY: comment, _EXT_ATTR_PROPERTY: extattrs}
model = self._make_model(model)
return self.invoke("post", "fixedaddress?_return_fields=ipv4addr", ok_codes=(200, 201, 400), json=model)
# ---------------------------------------------------------------------------
# get_fixedaddress()
# ---------------------------------------------------------------------------
def get_fixedaddress(self, address):
"""
Search FIXEDADDRESS reserve by address in infoblox through the rest api
"""
params = {"ipv4addr": address}
return self.invoke("get", "fixedaddress", params=params)
# ---------------------------------------------------------------------------
# get_cname_object()
# ---------------------------------------------------------------------------
def get_cname_object(self, cname):
object_ref = None
cnames = self.get_cname(cname)
_ref = None
canonical = None
if isinstance(cnames, list) and len(cnames) > 0:
detail = cnames[0]
_ref = detail.get('_ref')
if _ref:
object_ref = _ref.split(':')[0] + ':' + _ref.split(':')[1]
canonical = cnames[0].get('canonical')
return object_ref, canonical
# ---------------------------------------------------------------------------
# get_cname()
# ---------------------------------------------------------------------------
def get_cname(self, cname):
"""
Search CNAME by FQDN in infoblox by using rest api
"""
if not cname:
self.module.fail_json(msg="You must specify the option 'cname'.")
params = {_NAME_PROPERTY: cname, _VIEW_PROPERTY: self.dns_view}
return self.invoke("get", "record:cname", params=params)
# ---------------------------------------------------------------------------
# create_cname()
# ---------------------------------------------------------------------------
def create_cname(self, cname, canonical, comment=None, ttl=None, extattrs=None):
"""
Add CNAME in infoblox by using rest api
"""
if cname is None or canonical is None:
self.module.exit_json(
msg="You must specify the option 'name' and 'canonical'.")
if extattrs is not None:
extattrs = self.add_attr(extattrs)
object_ref, current_canonical = self.get_cname_object(cname)
if object_ref:
if current_canonical != canonical:
msg = 'Canonical name is {} and {} is not the same ' \
'please use update_cname_record'.format(
current_canonical, canonical)
self.module.fail_json(msg=msg)
self.module.exit_json(msg='CNAME Exists')
model = {_NAME_PROPERTY: cname, _CANONICAL_PROPERTY: canonical,
_VIEW_PROPERTY: self.dns_view, _COMMENT_PROPERTY: comment,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("post", "record:cname", ok_codes=(200, 201, 400), json=model)
# ---------------------------------------------------------------------------
# update_cname_record()
# ---------------------------------------------------------------------------
def update_cname_record(self, desired_cname, desired_canonical, current, comment=None, ttl=None, extattrs=None):
"""
Update alias for a cname entry
"""
if not isinstance(current, dict):
self.module.fail_json(msg="The 'current' check is not a dict")
elif not current.get('cname'):
self.module.fail_json(
msg="The 'current' dict must contain a 'cname' key")
else:
current_cname = current.get('cname')
if extattrs is not None:
extattrs = self.add_attr(extattrs)
object_ref, current_canonical = self.get_cname_object(current_cname)
if object_ref is None:
self.module.fail_json(
msg="Name {} was not found.".format(current_cname))
if current_canonical == desired_canonical:
self.module.exit_json(msg='Canonical Exists')
model = {_NAME_PROPERTY: desired_cname, _CANONICAL_PROPERTY: desired_canonical,
_VIEW_PROPERTY: self.dns_view, _COMMENT_PROPERTY: comment,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("put", object_ref, json=model)
# ---------------------------------------------------------------------------
# delete_cname_record()
# ---------------------------------------------------------------------------
def delete_cname_record(self, cname):
"""
Delete cname object
"""
object_ref, _ = self.get_cname_object(cname)
return self.delete_object(object_ref)
# ---------------------------------------------------------------------------
# get_a_object()
# ---------------------------------------------------------------------------
def get_a_object(self, name, address):
object_ref = None
a_records = self.get_a_record(name)
for a_record in a_records:
if a_record.get(_IPV4_ADDRESS_PROPERTY) == address:
key_out = a_record.get('_ref')
object_ref = key_out.split(
':')[0] + ':' + key_out.split(':')[1]
break
return object_ref
# ---------------------------------------------------------------------------
# get_a_record()
# ---------------------------------------------------------------------------
def get_a_record(self, name):
"""
Retrieves information about the A record with the given name.
"""
if not name:
self.module.fail_json(msg="You must specify the option 'name'.")
property_list = [_IPV4_ADDRESS_PROPERTY]
my_property = self._return_property(True, property_list)
params = {_NAME_PROPERTY: name, _VIEW_PROPERTY: self.dns_view,
_RETURN_FIELDS_PROPERTY: my_property}
return self.invoke("get", "record:a", params=params)
# ---------------------------------------------------------------------------
# seach_a_record()
# ---------------------------------------------------------------------------
def search_a_record(self, name):
params = {_NAME_PROPERTY + "~": name }
return self.invoke("get", "record:a", params=params)
# ---------------------------------------------------------------------------
# seach_host_record()
# ---------------------------------------------------------------------------
def search_host_record(self, name):
params = {_NAME_PROPERTY + "~": name }
return self.invoke("get", "record:host", params=params)
# ---------------------------------------------------------------------------
# create_a_record()
# ---------------------------------------------------------------------------
def create_a_record(self, name, address, comment=None, ttl=None, extattrs=None, roundrobin=False):
"""
Creates an A record with the given name that points to the given IP address.
For documentation on how to use the related part of the InfoBlox WAPI, refer to:
https://ipam.illinois.edu/wapidoc/objects/record.a.html
"""
if not name or not address:
self.module.exit_json(
msg="You must specify the option 'name' and 'address'.")
if extattrs is not None:
extattrs = self.add_attr(extattrs)
if not roundrobin:
object_ref = self.get_a_object(name, address)
if object_ref:
self.module.exit_json(msg='A record Exists')
model = {_NAME_PROPERTY: name, _IPV4_ADDRESS_PROPERTY: address,
_VIEW_PROPERTY: self.dns_view, _COMMENT_PROPERTY: comment,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("post", "record:a", ok_codes=(200, 201, 400), json=model)
# ---------------------------------------------------------------------------
# update_a_record()
# ---------------------------------------------------------------------------
def update_a_record(self, desired_name, desired_address, current, comment=None, ttl=None, extattrs=None):
"""
Update an A record
"""
if not isinstance(current, dict):
self.module.fail_json(msg="The 'current' check is not a dict")
elif not current.get('name') or not current.get('address'):
self.module.fail_json(
msg="The 'current' dict must contain a 'name' and 'address' key")
else:
current_name = current.get('name')
current_address = current.get('address')
object_ref = self.get_a_object(desired_name, desired_address)
if object_ref:
self.module.exit_json(msg='A record Exists')
object_ref = None
a_records = self.get_a_record(current_name)
for a_record in a_records:
if a_record.get('name') == current_name:
key_out = a_record.get('_ref')
object_ref = key_out.split(
':')[0] + ':' + key_out.split(':')[1]
break
if not object_ref:
msg = "IP {} and ptrdname {} pair was not found.".format(
current_address, current_name)
self.module.fail_json(msg=msg)
if object_ref is None:
self.module.fail_json(
msg="Name {} was not found.".format(current_name))
if extattrs is not None:
extattrs = self.add_attr(extattrs)
model = {_NAME_PROPERTY: desired_name, _IPV4_ADDRESS_PROPERTY: desired_address,
_COMMENT_PROPERTY: comment, _EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("put", object_ref, json=model)
# ---------------------------------------------------------------------------
# delete_a_record()
# ---------------------------------------------------------------------------
def delete_a_record(self, name, address):
"""
Delete a record object
"""
object_ref = self.get_a_object(name, address)
if object_ref:
return self.delete_object(object_ref)
else:
self.module.exit_json(msg='Object deleted already')
# ---------------------------------------------------------------------------
# get_ptr_object()
# ---------------------------------------------------------------------------
def get_ptr_object(self, address, name):
object_ref = None
ptrs = self.get_ptr_record(address)
for ptr in ptrs:
if ptr.get('ptrdname') == name:
key_out = ptr.get('_ref')
object_ref = key_out.split(
':')[0] + ':' + key_out.split(':')[1]
break
return object_ref
# ---------------------------------------------------------------------------
# get_ptr_record()
# ---------------------------------------------------------------------------
def get_ptr_record(self, address):
"""
Retrieves information about the PTR record with the given address.
"""
if not address:
self.module.fail_json(msg="You must specify the option 'address'.")
property_list = [_IPV4_ADDRESS_PROPERTY, _PTRDNAME_PROPERTY]
my_property = self._return_property(True, property_list)
params = {_IPV4_ADDRESS_PROPERTY: address, _VIEW_PROPERTY: self.dns_view,
_RETURN_FIELDS_PROPERTY: my_property}
return self.invoke("get", "record:ptr", params=params)
# ---------------------------------------------------------------------------
# create_ptr_record()
# ---------------------------------------------------------------------------
def create_ptr_record(self, name, address, comment=None, ttl=None, extattrs=None):
"""
Creates an PTR record with the given name that points to the given IP address.
For documentation on how to use the related part of the InfoBlox WAPI, refer to:
https://ipam.illinois.edu/wapidoc/objects/record.ptr.html
"""
if not name or not address:
self.module.exit_json(
msg="You must specify the option 'name' and 'address'.")
if extattrs is not None:
extattrs = self.add_attr(extattrs)
object_ref = self.get_ptr_object(address, name)
if object_ref:
self.module.exit_json(msg='PTR Exists')
model = {_PTRDNAME_PROPERTY: name, _NAME_PROPERTY: name, _IPV4_ADDRESS_PROPERTY: address,
_VIEW_PROPERTY: self.dns_view, _COMMENT_PROPERTY: comment,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("post", "record:ptr", ok_codes=(200, 201, 400), json=model)
# ---------------------------------------------------------------------------
# update_ptr_record()
# ---------------------------------------------------------------------------
def update_ptr_record(self, desired_name, desired_address, current, comment=None, ttl=None, extattrs=None):
"""
Update alias for a ptr record
"""
if not isinstance(current, dict):
self.module.fail_json(
msg="The 'current' check is not a dict{}".format(current))
elif not current.get('name') or not current.get('address'):
self.module.fail_json(
msg="The 'current' dict must contain a 'address' and 'name' key")
else:
current_name = current.get('name')
current_address = current.get('address')
object_ref = self.get_ptr_object(desired_address, desired_name)
if object_ref:
self.module.exit_json(msg='PTR Exists')
object_ref = None
ptrs = self.get_ptr_record(current_address)
for current_ptr in ptrs:
if current_ptr.get('ptrdname') == current_name:
key_out = current_ptr.get('_ref')
object_ref = key_out.split(
':')[0] + ':' + key_out.split(':')[1]
break
if object_ref is None:
self.module.fail_json(msg="IP {} and ptrdname {} pair was not found.".format(
current_address, current_name))
if extattrs is not None:
extattrs = self.add_attr(extattrs)
model = {_PTRDNAME_PROPERTY: desired_name,
_IPV4_ADDRESS_PROPERTY: desired_address,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("put", object_ref, json=model)
# ---------------------------------------------------------------------------
# delete_ptr_record()
# ---------------------------------------------------------------------------
def delete_ptr_record(self, name, address):
"""
Delete cname object
"""
object_ref = self.get_ptr_object(address, name)
if object_ref:
return self.delete_object(object_ref)
else:
self.module.exit_json(msg='Object deleted already')
# ---------------------------------------------------------------------------
# get_srv_object()
# ---------------------------------------------------------------------------
def get_srv_object(self, name):
object_ref = None
srvs = self.get_srv_record(name)
if srvs:
key_out = srvs[0].get('_ref')
object_ref = key_out.split(':')[0] + ':' + key_out.split(':')[1]
return object_ref
# ---------------------------------------------------------------------------
# get_srv_record()
# ---------------------------------------------------------------------------
def get_srv_record(self, name):
"""
Retrieves information about the SRV record with the given name.
"""
if not name:
self.module.fail_json(msg="You must specify the option 'address'.")
property_list = [_VIEW_PROPERTY, _COMMENT_PROPERTY, _TTL_PROPERTY, _EXT_ATTR_PROPERTY,
_WEIGHT_PROPERTY, _PORT_PROPERTY, _PRIORITY_PROPERTY, _TARGET_PROPERTY]
my_property = self._return_property(True, property_list)
params = {_NAME_PROPERTY: name, _VIEW_PROPERTY: self.dns_view,
_RETURN_FIELDS_PROPERTY: my_property}
return self.invoke("get", "record:srv", params=params)
# ---------------------------------------------------------------------------
# create_srv_record()
# ---------------------------------------------------------------------------
def create_srv_record(self, name, srv_attr, comment=None, ttl=None, extattrs=None):
"""
Creates an SRV record with the name and .
For documentation on how to use the related part of the InfoBlox WAPI, refer to:
https://ipam.illinois.edu/wapidoc/objects/record.srv.html
"""
if not isinstance(srv_attr, dict):
self.module.fail_json(msg="The variable 'srv_attr' is not a dict")
for attr in ['port', 'priority', 'dns_target', 'weight']:
if not srv_attr.get(attr):
self.module.fail_json(
msg="The 'srv_attr' dict must contain a '{}' key".format(attr))
object_ref = self.get_srv_object(name)
if object_ref:
self.module.exit_json(msg='SRV Exists')
port = srv_attr.get('port')
priority = srv_attr.get('priority')
dns_target = srv_attr.get('dns_target')
weight = srv_attr.get('weight')
if extattrs is not None:
extattrs = self.add_attr(extattrs)
model = {_NAME_PROPERTY: name, _PORT_PROPERTY: port,
_PRIORITY_PROPERTY: priority, _TARGET_PROPERTY: dns_target,
_WEIGHT_PROPERTY: weight,
_VIEW_PROPERTY: self.dns_view, _COMMENT_PROPERTY: comment,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("post", "record:srv", ok_codes=(200, 201, 400), json=model)
# ---------------------------------------------------------------------------
# update_srv_record()
# ---------------------------------------------------------------------------
def update_srv_record(self, desired_name, srv_attr, current_name, comment=None, ttl=None, extattrs=None):
"""
Update svr record for a named entry
"""
if not isinstance(srv_attr, dict):
self.module.fail_json(msg="The variable 'srv_attr' is not a dict")
port = srv_attr.get('port')
priority = srv_attr.get('priority')
dns_target = srv_attr.get('dns_target')
weight = srv_attr.get('weight')
current_name = current_name.get('name')
object_ref = None
srvs = self.get_srv_record(current_name)
for srv in srvs:
if srv.get('name') == current_name:
key_out = srv.get('_ref')
object_ref = key_out.split(
':')[0] + ':' + key_out.split(':')[1]
break
if object_ref is None:
self.module.fail_json(
msg="Name {} was not found.".format(current_name))
if extattrs is not None:
extattrs = self.add_attr(extattrs)
model = {_PORT_PROPERTY: port,
_PRIORITY_PROPERTY: priority, _TARGET_PROPERTY: dns_target,
_WEIGHT_PROPERTY: weight, _COMMENT_PROPERTY: comment,
_EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("put", object_ref, json=model)
# ---------------------------------------------------------------------------
# delete_srv_record()
# ---------------------------------------------------------------------------
def delete_srv_record(self, name):
"""
Delete srv record object
"""
object_ref = self.get_srv_object(name)
if object_ref:
return self.delete_object(object_ref)
else:
self.module.exit_json(msg='Object deleted already')
# ---------------------------------------------------------------------------
# get_txt_record()
# ---------------------------------------------------------------------------
def get_txt_record(self, name):
"""
Retrieves information about the PTR record with the given name.
"""
if not name:
self.module.fail_json(msg="You must specify the option 'name'.")
property_list = [_NAME_PROPERTY, _COMMENT_PROPERTY, _TXT_PROPERTY, _TTL_PROPERTY,
_EXT_ATTR_PROPERTY, _COMMENT_PROPERTY]
my_property = self._return_property(True, property_list)
params = {_NAME_PROPERTY: name, _VIEW_PROPERTY: self.dns_view,
_RETURN_FIELDS_PROPERTY: my_property}
return self.invoke("get", "record:txt", params=params)
# ---------------------------------------------------------------------------
# create_txt_record()
# ---------------------------------------------------------------------------
def create_txt_record(self, name, txt, comment=None, ttl=None, extattrs=None):
"""
Creates an PTR record with the given name that points to the given IP address.
For documentation on how to use the related part of the InfoBlox WAPI, refer to:
https://ipam.illinois.edu/wapidoc/objects/record.txt.html
"""
if not name or not txt:
self.module.exit_json(
msg="You must specify the option 'name' and 'txt'.")
if extattrs is not None:
extattrs = self.add_attr(extattrs)
current_txts = self.get_txt_record(name)
if current_txts:
for current_txt in current_txts:
if current_txt.get(_TXT_PROPERTY) == txt:
self.module.exit_json(msg='TXT object exist')
model = {_NAME_PROPERTY: name, _TXT_PROPERTY: txt,
_VIEW_PROPERTY: self.dns_view,
_COMMENT_PROPERTY: comment, _EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("post", "record:txt", ok_codes=(200, 201, 400), json=model)
# ---------------------------------------------------------------------------
# update_txt_record()
# ---------------------------------------------------------------------------
def update_txt_record(self, desired_name, desired_txt, current, comment=None, ttl=None, extattrs=None):
"""
Update alias for a txt record
"""
if not isinstance(current, dict):
self.module.fail_json(msg="The 'current' check is not a dict")
elif not current.get('name'):
self.module.fail_json(
msg="The 'current' dict must contain a 'name' key")
else:
current_name = current.get('name')
current_txts = self.get_txt_record(desired_name)
if current_txts:
for current_txt in current_txts:
if current_txt.get(_TXT_PROPERTY) == desired_txt:
self.module.exit_json(msg='TXT object exist')
object_ref = None
current_txt = current.get('current_txt')
first_found = current.get('first_found')
if first_found is not None and current_txt is not None:
self.module.fail_json(
msg="Please Specify either current_txt or first_found, but not both.")
elif first_found is None and current_txt is None:
self.module.fail_json(
msg="The 'current' dict must have either 'current_txt' or 'first_found' as a key.")
txts = self.get_txt_record(current_name)
if current_txt:
for index, current_txt in entxts:
if current_txt.get('name') == current_name and current_txt.get('txt') == current_txt:
my_entry = index
else:
my_entry = 0
used_txt = txts[my_entry]
key_out = used_txt.get('_ref')
object_ref = key_out.split(':')[0] + ':' + key_out.split(':')[1]
if object_ref is None:
self.module.fail_json(
msg="Name {} was not found.".format(current_name))
self.module.exit_json(
msg="Name {} was not found.".format(current_name))
if extattrs is not None:
extattrs = self.add_attr(extattrs)
model = {_NAME_PROPERTY: desired_name, _TXT_PROPERTY: desired_txt,
_VIEW_PROPERTY: self.dns_view,
_COMMENT_PROPERTY: comment, _EXT_ATTR_PROPERTY: extattrs}
if(ttl is not None):
model.update({_USE_TTL_PROPERTY: True, _TTL_PROPERTY: int(ttl)})
model = self._make_model(model)
return self.invoke("put", object_ref, json=model)
# ---------------------------------------------------------------------------
# delete_txt_record()
# ---------------------------------------------------------------------------
def delete_txt_record(self, name):
"""
Delete txt record object
"""
object_ref = self.get_txt_object(name)
if object_ref:
return self.delete_object(object_ref)
else:
self.module.exit_json(msg='Object deleted already')
# ---------------------------------------------------------------------------
# get_aliases()
# ---------------------------------------------------------------------------
def get_aliases(self, host):
"""