Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/modules/nios_dtc_topology.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright (c) 2018-2019 Red Hat, Inc.
3 # Copyright (c) 2020 Infoblox, Inc.
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = '''
10 ---
11 module: nios_dtc_topology
12 author: "Joachim Buyse (@jbisabel)"
13 short_description: Configure Infoblox NIOS DTC Topology
14 version_added: "1.6.0"
15 description:
16 - Adds and/or removes instances of DTC Topology objects from
17 Infoblox NIOS topologies. This module manages NIOS C(dtc:topology) objects
18 using the Infoblox WAPI interface over REST.
19 requirements:
20 - infoblox-client
21 extends_documentation_fragment: infoblox.nios_modules.nios
22 notes:
23 - This module supports C(check_mode).
24 options:
25 name:
26 description:
27 - Specifies the DTC Topology display name.
28 required: true
29 type: str
30 rules:
31 description:
32 - Configures the topology rules
33 type: list
34 elements: dict
35 suboptions:
36 dest_type:
37 description:
38 - Configures the type of the destination for this DTC Topology Rule.
39 type: str
40 choices:
41 - POOL
42 - SERVER
43 required: true
44 destination_link:
45 description:
46 - Configures the name of the destination DTC pool or DTC server.
47 type: str
48 return_type:
49 description:
50 - Configures the type of the DNS response for the rule.
51 type: str
52 choices:
53 - NOERR
54 - NXDOMAIN
55 - REGULAR
56 default: REGULAR
57 sources:
58 description:
59 - Configures the conditions for matching sources. Should be empty to
60 set the rule as default destination.
61 type: list
62 elements: dict
63 suboptions:
64 source_op:
65 description:
66 - Configures the operation used to match the value.
67 type: str
68 choices:
69 - IS
70 - IS_NOT
71 source_type:
72 description:
73 - Configures the source type.
74 type: str
75 choices:
76 - CITY
77 - CONTINENT
78 - COUNTRY
79 - EA0
80 - EA1
81 - EA2
82 - EA3
83 - SUBDIVISION
84 - SUBNET
85 required: true
86 source_value:
87 description:
88 - Configures the source value.
89 type: str
90 required: true
91 extattrs:
92 description:
93 - Allows for the configuration of Extensible Attributes on the
94 instance of the object. This argument accepts a set of key / value
95 pairs for configuration.
96 type: dict
97 comment:
98 description:
99 - Configures a text string comment to be associated with the instance
100 of this object. The provided text string will be configured on the
101 object instance.
102 type: str
103 state:
104 description:
105 - Configures the intended state of the instance of the object on
106 the NIOS topology. When this value is set to C(present), the object
107 is configured on the device and when this value is set to C(absent)
108 the value is removed (if necessary) from the device.
109 default: present
110 choices:
111 - present
112 - absent
113 type: str
114 '''
115
116 EXAMPLES = '''
117 - name: Configure a DTC Topology
118 infoblox.nios_modules.nios_dtc_topology:
119 name: a_topology
120 rules:
121 - dest_type: POOL
122 destination_link: web_pool1
123 return_type: REGULAR
124 sources:
125 - source_op: IS
126 source_type: EA0
127 source_value: DC1
128 - dest_type: POOL
129 destination_link: web_pool2
130 return_type: REGULAR
131 sources:
132 - source_op: IS
133 source_type: EA0
134 source_value: DC2
135 state: present
136 provider:
137 host: "{{ inventory_hostname_short }}"
138 username: admin
139 password: admin
140 connection: local
141
142 - name: Add a comment to a DTC topology
143 infoblox.nios_modules.nios_dtc_topology:
144 name: a_topology
145 comment: this is a test comment
146 state: present
147 provider:
148 host: "{{ inventory_hostname_short }}"
149 username: admin
150 password: admin
151 connection: local
152
153 - name: Remove a DTC Topology from the system
154 infoblox.nios_modules.nios_dtc_topology:
155 name: a_topology
156 state: absent
157 provider:
158 host: "{{ inventory_hostname_short }}"
159 username: admin
160 password: admin
161 connection: local
162 '''
163
164 RETURN = ''' # '''
165
166 from ansible.module_utils.basic import AnsibleModule
167 from ..module_utils.api import WapiModule
168 from ..module_utils.api import NIOS_DTC_TOPOLOGY
169 from ..module_utils.api import normalize_ib_spec
170
171
172 def main():
173 ''' Main entry point for module execution
174 '''
175
176 def sources_transform(sources, module):
177 source_list = list()
178 for source in sources:
179 src = dict([(k, v) for k, v in source.items() if v is not None])
180 if 'source_type' not in src or 'source_value' not in src:
181 module.fail_json(msg='source_type and source_value are required for source')
182 source_list.append(src)
183 return source_list
184
185 def rules_transform(module):
186 rule_list = list()
187 dest_obj = None
188
189 if not module.params['rules']:
190 return rule_list
191
192 for rule in module.params['rules']:
193 if rule['dest_type'] == 'POOL':
194 dest_obj = wapi.get_object('dtc:pool', {'name': rule['destination_link']})
195 else:
196 dest_obj = wapi.get_object('dtc:server', {'name': rule['destination_link']})
197 if not dest_obj and rule['return_type'] == 'REGULAR':
198 module.fail_json(msg='destination_link %s does not exist' % rule['destination_link'])
199
200 tf_rule = dict(
201 dest_type=rule['dest_type'],
202 destination_link=dest_obj[0]['_ref'] if dest_obj else None,
203 return_type=rule['return_type']
204 )
205
206 if rule['sources']:
207 tf_rule['sources'] = sources_transform(rule['sources'], module)
208
209 rule_list.append(tf_rule)
210 return rule_list
211
212 source_spec = dict(
213 source_op=dict(choices=['IS', 'IS_NOT']),
214 source_type=dict(required=True, choices=['CITY', 'CONTINENT', 'COUNTRY', 'EA0', 'EA1', 'EA2', 'EA3', 'SUBDIVISION', 'SUBNET']),
215 source_value=dict(required=True, type='str')
216 )
217
218 rule_spec = dict(
219 dest_type=dict(required=True, choices=['POOL', 'SERVER']),
220 destination_link=dict(type='str'),
221 return_type=dict(default='REGULAR', choices=['NOERR', 'NXDOMAIN', 'REGULAR']),
222 sources=dict(type='list', elements='dict', options=source_spec)
223 )
224
225 ib_spec = dict(
226 name=dict(required=True, ib_req=True),
227
228 rules=dict(type='list', elements='dict', options=rule_spec, transform=rules_transform),
229
230 extattrs=dict(type='dict'),
231 comment=dict(),
232 )
233
234 argument_spec = dict(
235 provider=dict(required=True),
236 state=dict(default='present', choices=['present', 'absent'])
237 )
238
239 argument_spec.update(normalize_ib_spec(ib_spec))
240 argument_spec.update(WapiModule.provider_spec)
241
242 module = AnsibleModule(argument_spec=argument_spec,
243 supports_check_mode=True)
244
245 wapi = WapiModule(module)
246 result = wapi.run(NIOS_DTC_TOPOLOGY, ib_spec)
247
248 module.exit_json(**result)
249
250
251 if __name__ == '__main__':
252 main()