Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/modules/nios_dtc_pool.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_pool
12 author: "Mauricio Teixeira (@badnetmask)"
13 version_added: "1.1.0"
14 short_description: Configure Infoblox NIOS DTC Pool
15 description:
16 - Adds and/or removes instances of DTC Pool objects from
17 Infoblox NIOS servers. This module manages NIOS C(dtc:pool) objects
18 using the Infoblox WAPI interface over REST. A DTC pool is a collection
19 of IDNS resources (virtual servers).
20 requirements:
21 - infoblox-client
22 extends_documentation_fragment: infoblox.nios_modules.nios
23 notes:
24 - This module supports C(check_mode).
25 options:
26 name:
27 description:
28 - Specifies the DTC Pool display name
29 required: true
30 type: str
31 lb_preferred_method:
32 description:
33 - Configures the preferred load balancing method.
34 - Use this to select a method type from the pool.
35 choices:
36 - ALL_AVAILABLE
37 - DYNAMIC_RATIO
38 - GLOBAL_AVAILABILITY
39 - RATIO
40 - ROUND_ROBIN
41 - TOPOLOGY
42 required: true
43 type: str
44 lb_preferred_topology:
45 description:
46 - Configures the topology rules for the C(TOPOLOGY) load balancing method.
47 - Required only when I(lb_preferred_method) is set to C(TOPOLOGY).
48 required: false
49 type: str
50 servers:
51 description:
52 - Configure the DTC Servers related to the pool
53 required: false
54 type: list
55 elements: dict
56 suboptions:
57 server:
58 description:
59 - Provide the name of the DTC Server
60 required: true
61 type: str
62 ratio:
63 description:
64 - Provide the weight of the server
65 default: 1
66 required: false
67 type: int
68 monitors:
69 description:
70 - Specifies the health monitors related to pool.
71 - The format of this parameter is required due to an API
72 limitation.
73 - This option only works if you set the C(wapi_version)
74 variable on your C(provider) variable to a number higher
75 than "2.6".
76 required: false
77 type: list
78 elements: dict
79 suboptions:
80 name:
81 description:
82 - Provide the name of the health monitor.
83 required: true
84 type: str
85 type:
86 description:
87 - Provide the type of health monitor.
88 choices:
89 - http
90 - icmp
91 - tcp
92 - pdp
93 - sip
94 - snmp
95 required: true
96 type: str
97 extattrs:
98 description:
99 - Allows for the configuration of Extensible Attributes on the
100 instance of the object. This argument accepts a set of key / value
101 pairs for configuration.
102 type: dict
103 comment:
104 description:
105 - Configures a text string comment to be associated with the instance
106 of this object. The provided text string will be configured on the
107 object instance.
108 type: str
109 state:
110 description:
111 - Configures the intended state of the instance of the object on
112 the NIOS server. When this value is set to C(present), the object
113 is configured on the device and when this value is set to C(absent)
114 the value is removed (if necessary) from the device.
115 default: present
116 choices:
117 - present
118 - absent
119 type: str
120 '''
121
122 EXAMPLES = '''
123 - name: Configure a DTC Pool
124 infoblox.nios_modules.nios_dtc_pool:
125 name: web_pool
126 lb_preferred_method: ROUND_ROBIN
127 servers:
128 - server: a.ansible.com
129 - server: b.ansible.com
130 state: present
131 provider:
132 host: "{{ inventory_hostname_short }}"
133 username: admin
134 password: admin
135 connection: local
136
137 - name: Add a comment to a DTC Pool
138 infoblox.nios_modules.nios_dtc_pool:
139 name: web_pool
140 lb_preferred_method: ROUND_ROBIN
141 comment: this is a test comment
142 state: present
143 provider:
144 host: "{{ inventory_hostname_short }}"
145 username: admin
146 password: admin
147 connection: local
148
149 - name: Remove a DTC Pool from the system
150 infoblox.nios_modules.nios_dtc_pool:
151 name: web_pool
152 lb_preferred_method: ROUND_ROBIN
153 state: absent
154 provider:
155 host: "{{ inventory_hostname_short }}"
156 username: admin
157 password: admin
158 connection: local
159 '''
160
161 RETURN = ''' # '''
162
163 from ..module_utils.api import NIOS_DTC_POOL
164 from ..module_utils.api import WapiModule
165 from ..module_utils.api import normalize_ib_spec
166 from ansible.module_utils.basic import AnsibleModule
167
168
169 def main():
170 ''' Main entry point for module execution
171 '''
172
173 def servers_transform(module):
174 server_list = list()
175 if module.params['servers']:
176 for server in module.params['servers']:
177 server_obj = wapi.get_object('dtc:server',
178 {'name': server['server']})
179 if server_obj:
180 server_list.append({'server': server_obj[0]['_ref'],
181 'ratio': server['ratio']})
182 else:
183 module.fail_json(msg='Server %s cannot be found.' % server)
184 return server_list
185
186 def monitors_transform(module):
187 monitor_list = list()
188 if module.params['monitors']:
189 for monitor in module.params['monitors']:
190 monitor_obj = wapi.get_object('dtc:monitor:' + monitor['type'],
191 {'name': monitor['name']})
192 if monitor_obj:
193 monitor_list.append(monitor_obj[0]['_ref'])
194 else:
195 module.fail_json(
196 msg='monitor %s cannot be found.' % monitor)
197 return monitor_list
198
199 def topology_transform(module):
200 topology = module.params['lb_preferred_topology']
201 if topology:
202 topo_obj = wapi.get_object('dtc:topology', {'name': topology})
203 if topo_obj:
204 return topo_obj[0]['_ref']
205 else:
206 module.fail_json(
207 msg='topology %s cannot be found.' % topology)
208
209 servers_spec = dict(
210 server=dict(required=True),
211 ratio=dict(type='int', default=1)
212 )
213
214 monitors_spec = dict(
215 name=dict(required=True),
216 type=dict(required=True, choices=['http', 'icmp', 'tcp', 'pdp', 'sip', 'snmp'])
217 )
218
219 ib_spec = dict(
220 name=dict(required=True, ib_req=True),
221 lb_preferred_method=dict(required=True, choices=['ALL_AVAILABLE',
222 'DYNAMIC_RATIO',
223 'GLOBAL_AVAILABILITY',
224 'RATIO',
225 'ROUND_ROBIN',
226 'TOPOLOGY']),
227 lb_preferred_topology=dict(type='str', transform=topology_transform),
228
229 servers=dict(type='list', elements='dict', options=servers_spec,
230 transform=servers_transform),
231 monitors=dict(type='list', elements='dict', options=monitors_spec,
232 transform=monitors_transform),
233
234 extattrs=dict(type='dict'),
235 comment=dict(),
236 )
237
238 argument_spec = dict(
239 provider=dict(required=True),
240 state=dict(default='present', choices=['present', 'absent'])
241 )
242
243 argument_spec.update(normalize_ib_spec(ib_spec))
244 argument_spec.update(WapiModule.provider_spec)
245
246 module = AnsibleModule(argument_spec=argument_spec,
247 supports_check_mode=True)
248
249 # Validate lb_preferred_topology is only used with TOPOLOGY method
250 lb_method = module.params.get('lb_preferred_method')
251 lb_topology = module.params.get('lb_preferred_topology')
252
253 if lb_topology and lb_method != 'TOPOLOGY':
254 module.fail_json(
255 msg="lb_preferred_topology can only be specified when lb_preferred_method is 'TOPOLOGY', "
256 "but lb_preferred_method is set to '%s'" % lb_method
257 )
258
259 if lb_method == 'TOPOLOGY' and not lb_topology:
260 module.fail_json(
261 msg="lb_preferred_topology is required when lb_preferred_method is set to 'TOPOLOGY'"
262 )
263
264 wapi = WapiModule(module)
265 result = wapi.run(NIOS_DTC_POOL, ib_spec)
266
267 module.exit_json(**result)
268
269
270 if __name__ == '__main__':
271 main()