| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_iapp_service |
| 13 |
short_description: Manages TCL iApp services on a BIG-IP |
| 14 |
description: |
| 15 |
- Manages TCL iApp services on a BIG-IP. |
| 16 |
- The API the system uses to communicate with on the BIG-IP is C(/mgmt/tm/sys/application/service/). |
| 17 |
version_added: "1.0.0" |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the iApp service you want to deploy. |
| 22 |
type: str |
| 23 |
required: True |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The iApp template from which to instantiate a new service. This |
| 27 |
template must exist on your BIG-IP before you can successfully |
| 28 |
create a service. |
| 29 |
- When creating a new service, this parameter is required. |
| 30 |
type: str |
| 31 |
parameters: |
| 32 |
description: |
| 33 |
- A hash of all the required template variables for the iApp template. |
| 34 |
If your parameters are stored in a file (the more common scenario) |
| 35 |
we recommend you use either the C(file) or C(template) lookups |
| 36 |
to supply the expected parameters. |
| 37 |
- These parameters typically consist of the C(lists), C(tables), and |
| 38 |
C(variables) fields. |
| 39 |
type: dict |
| 40 |
force: |
| 41 |
description: |
| 42 |
- Forces the updating of an iApp service, even if the parameters to the |
| 43 |
service have not changed. This option is of particular importance if |
| 44 |
the iApp template that underlies the service has been updated in-place. |
| 45 |
This option is equivalent to re-configuring the iApp if that template |
| 46 |
has changed. |
| 47 |
type: bool |
| 48 |
default: false |
| 49 |
state: |
| 50 |
description: |
| 51 |
- When C(present), ensures the iApp service is created and running. |
| 52 |
When C(absent), ensures the iApp service has been removed. |
| 53 |
type: str |
| 54 |
choices: |
| 55 |
- present |
| 56 |
- absent |
| 57 |
default: present |
| 58 |
partition: |
| 59 |
description: |
| 60 |
- Device partition to manage resources on. |
| 61 |
type: str |
| 62 |
default: Common |
| 63 |
strict_updates: |
| 64 |
description: |
| 65 |
- Indicates whether the application service is tied to the template, |
| 66 |
so when the template is updated, the application service changes to |
| 67 |
reflect the updates. |
| 68 |
- When C(true), disallows any updates to the resources that the iApp |
| 69 |
service has created, if they are not updated directly through the |
| 70 |
iApp. |
| 71 |
- When C(false), allows updates outside of the iApp. |
| 72 |
- If this option is specified in the Ansible task, it takes precedence |
| 73 |
over any similar setting in the iApp Service payload that you provide in |
| 74 |
the C(parameters) field. |
| 75 |
type: bool |
| 76 |
traffic_group: |
| 77 |
description: |
| 78 |
- The traffic group for the iApp service. When creating a new service, if |
| 79 |
this value is not specified, the default of C(/Common/traffic-group-1) |
| 80 |
is used. |
| 81 |
- If this option is specified in the Ansible task, it takes precedence |
| 82 |
over any similar setting in the iApp Service payload that you provide in |
| 83 |
the C(parameters) field. |
| 84 |
type: str |
| 85 |
metadata: |
| 86 |
description: |
| 87 |
- Metadata associated with the iApp service. |
| 88 |
- If this option is specified in the Ansible task, it takes precedence |
| 89 |
over any similar setting in the iApp Service payload that you provide in |
| 90 |
the C(parameters) field. |
| 91 |
type: list |
| 92 |
elements: raw |
| 93 |
description: |
| 94 |
description: |
| 95 |
- Description of the iApp service. |
| 96 |
- If this option is specified in the Ansible task, it takes precedence |
| 97 |
over any similar setting in the iApp Service payload that you provide in |
| 98 |
the C(parameters) field. |
| 99 |
type: str |
| 100 |
device_group: |
| 101 |
description: |
| 102 |
- The device group for the iApp service. |
| 103 |
- If this option is specified in the Ansible task, it takes precedence |
| 104 |
over any similar setting in the iApp Service payload that you provide in |
| 105 |
the C(parameters) field. |
| 106 |
type: str |
| 107 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 108 |
author: |
| 109 |
- Tim Rupp (@caphrim007) |
| 110 |
- Wojciech Wypior (@wojtek0806) |
| 111 |
''' |
| 112 |
|
| 113 |
EXAMPLES = r''' |
| 114 |
- name: Create HTTP iApp service from iApp template |
| 115 |
bigip_iapp_service: |
| 116 |
name: foo-service |
| 117 |
template: f5.http |
| 118 |
parameters: "{{ lookup('file', 'f5.http.parameters.json') }}" |
| 119 |
state: present |
| 120 |
provider: |
| 121 |
user: admin |
| 122 |
password: secret |
| 123 |
server: lb.mydomain.com |
| 124 |
delegate_to: localhost |
| 125 |
|
| 126 |
- name: Upgrade foo-service to v1.2.0rc4 of the f5.http template |
| 127 |
bigip_iapp_service: |
| 128 |
name: foo-service |
| 129 |
template: f5.http.v1.2.0rc4 |
| 130 |
state: present |
| 131 |
provider: |
| 132 |
user: admin |
| 133 |
password: secret |
| 134 |
server: lb.mydomain.com |
| 135 |
delegate_to: localhost |
| 136 |
|
| 137 |
- name: Configure a service using parameters in YAML |
| 138 |
bigip_iapp_service: |
| 139 |
name: tests |
| 140 |
template: web_frontends |
| 141 |
state: present |
| 142 |
parameters: |
| 143 |
variables: |
| 144 |
- name: var__vs_address |
| 145 |
value: 1.1.1.1 |
| 146 |
- name: pm__apache_servers_for_http |
| 147 |
value: 2.2.2.1:80 |
| 148 |
- name: pm__apache_servers_for_https |
| 149 |
value: 2.2.2.2:80 |
| 150 |
provider: |
| 151 |
user: admin |
| 152 |
password: secret |
| 153 |
server: lb.mydomain.com |
| 154 |
delegate_to: localhost |
| 155 |
|
| 156 |
- name: Re-configure a service whose underlying iApp was updated in place |
| 157 |
bigip_iapp_service: |
| 158 |
name: tests |
| 159 |
template: web_frontends |
| 160 |
force: true |
| 161 |
state: present |
| 162 |
parameters: |
| 163 |
variables: |
| 164 |
- name: var__vs_address |
| 165 |
value: 1.1.1.1 |
| 166 |
- name: pm__apache_servers_for_http |
| 167 |
value: 2.2.2.1:80 |
| 168 |
- name: pm__apache_servers_for_https |
| 169 |
value: 2.2.2.2:80 |
| 170 |
provider: |
| 171 |
user: admin |
| 172 |
password: secret |
| 173 |
server: lb.mydomain.com |
| 174 |
delegate_to: localhost |
| 175 |
|
| 176 |
- name: Try to remove the iApp template before the associated Service is removed |
| 177 |
bigip_iapp_template: |
| 178 |
name: web_frontends |
| 179 |
state: absent |
| 180 |
provider: |
| 181 |
user: admin |
| 182 |
password: secret |
| 183 |
server: lb.mydomain.com |
| 184 |
register: result |
| 185 |
failed_when: |
| 186 |
- result is not success |
| 187 |
- "'referenced by one or more applications' not in result.msg" |
| 188 |
|
| 189 |
- name: Configure a service using more complicated parameters |
| 190 |
bigip_iapp_service: |
| 191 |
name: tests |
| 192 |
template: web_frontends |
| 193 |
state: present |
| 194 |
provider: |
| 195 |
user: admin |
| 196 |
password: secret |
| 197 |
server: lb.mydomain.com |
| 198 |
parameters: |
| 199 |
variables: |
| 200 |
- name: var__vs_address |
| 201 |
value: 1.1.1.1 |
| 202 |
- name: pm__apache_servers_for_http |
| 203 |
value: 2.2.2.1:80 |
| 204 |
- name: pm__apache_servers_for_https |
| 205 |
value: 2.2.2.2:80 |
| 206 |
lists: |
| 207 |
- name: irules__irules |
| 208 |
value: |
| 209 |
- foo |
| 210 |
- bar |
| 211 |
tables: |
| 212 |
- name: basic__snatpool_members |
| 213 |
- name: net__snatpool_members |
| 214 |
- name: optimizations__hosts |
| 215 |
- name: pool__hosts |
| 216 |
columnNames: |
| 217 |
- name |
| 218 |
rows: |
| 219 |
- row: |
| 220 |
- internal.company.bar |
| 221 |
- name: pool__members |
| 222 |
columnNames: |
| 223 |
- addr |
| 224 |
- port |
| 225 |
- connection_limit |
| 226 |
rows: |
| 227 |
- row: |
| 228 |
- "none" |
| 229 |
- 80 |
| 230 |
- 0 |
| 231 |
- name: server_pools__servers |
| 232 |
delegate_to: localhost |
| 233 |
|
| 234 |
- name: Override metadata that may or may not exist in parameters |
| 235 |
bigip_iapp_service: |
| 236 |
name: foo-service |
| 237 |
template: f5.http |
| 238 |
parameters: "{{ lookup('file', 'f5.http.parameters.json') }}" |
| 239 |
metadata: |
| 240 |
- persist: true |
| 241 |
name: data 1 |
| 242 |
- persist: true |
| 243 |
name: data 2 |
| 244 |
state: present |
| 245 |
provider: |
| 246 |
user: admin |
| 247 |
password: secret |
| 248 |
server: lb.mydomain.com |
| 249 |
delegate_to: localhost |
| 250 |
''' |
| 251 |
|
| 252 |
RETURN = r''' |
| 253 |
# only common fields returned |
| 254 |
''' |
| 255 |
from datetime import datetime |
| 256 |
|
| 257 |
from ansible.module_utils.basic import ( |
| 258 |
AnsibleModule, env_fallback |
| 259 |
) |
| 260 |
from ansible.module_utils.six import iteritems |
| 261 |
|
| 262 |
from ..module_utils.bigip import F5RestClient |
| 263 |
from ..module_utils.common import ( |
| 264 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 265 |
) |
| 266 |
from ..module_utils.icontrol import tmos_version |
| 267 |
from ..module_utils.urls import build_service_uri |
| 268 |
from ..module_utils.teem import send_teem |
| 269 |
|
| 270 |
|
| 271 |
class Parameters(AnsibleF5Parameters): |
| 272 |
api_map = { |
| 273 |
'strictUpdates': 'strict_updates', |
| 274 |
'trafficGroup': 'traffic_group', |
| 275 |
'deviceGroup': 'device_group', |
| 276 |
} |
| 277 |
|
| 278 |
returnables = [ |
| 279 |
'tables', |
| 280 |
'variables', |
| 281 |
'lists', |
| 282 |
'strict_updates', |
| 283 |
'traffic_group', |
| 284 |
'device_group', |
| 285 |
'metadata', |
| 286 |
'template', |
| 287 |
'description', |
| 288 |
] |
| 289 |
|
| 290 |
api_attributes = [ |
| 291 |
'tables', |
| 292 |
'variables', |
| 293 |
'template', |
| 294 |
'lists', |
| 295 |
'deviceGroup', |
| 296 |
'inheritedDevicegroup', |
| 297 |
'inheritedTrafficGroup', |
| 298 |
'trafficGroup', |
| 299 |
'strictUpdates', |
| 300 |
|
| 301 |
'description', |
| 302 |
] |
| 303 |
|
| 304 |
updatables = [ |
| 305 |
'tables', |
| 306 |
'variables', |
| 307 |
'lists', |
| 308 |
'strict_updates', |
| 309 |
'device_group', |
| 310 |
'traffic_group', |
| 311 |
'metadata', |
| 312 |
'description', |
| 313 |
] |
| 314 |
|
| 315 |
def to_return(self): |
| 316 |
result = {} |
| 317 |
try: |
| 318 |
for returnable in self.returnables: |
| 319 |
result[returnable] = getattr(self, returnable) |
| 320 |
result = self._filter_params(result) |
| 321 |
except Exception: |
| 322 |
raise |
| 323 |
return result |
| 324 |
|
| 325 |
def normalize_tables(self, tables): |
| 326 |
result = [] |
| 327 |
for table in tables: |
| 328 |
tmp = dict() |
| 329 |
name = table.get('name', None) |
| 330 |
if name is None: |
| 331 |
raise F5ModuleError( |
| 332 |
"One of the provided tables does not have a name" |
| 333 |
) |
| 334 |
tmp['name'] = str(name) |
| 335 |
columns = table.get('columnNames', None) |
| 336 |
if columns: |
| 337 |
tmp['columnNames'] = [str(x) for x in columns] |
| 338 |
|
| 339 |
rows = table.get('rows', None) |
| 340 |
if rows: |
| 341 |
tmp['rows'] = [] |
| 342 |
for row in rows: |
| 343 |
tmp['rows'].append(dict(row=[str(x) for x in row['row']])) |
| 344 |
result.append(tmp) |
| 345 |
result = sorted(result, key=lambda k: k['name']) |
| 346 |
return result |
| 347 |
|
| 348 |
def normalize_variables(self, variables): |
| 349 |
result = [] |
| 350 |
for variable in variables: |
| 351 |
tmp = dict((str(k), str(v)) for k, v in iteritems(variable)) |
| 352 |
if 'encrypted' not in tmp: |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
tmp['encrypted'] = 'no' |
| 357 |
if 'value' not in tmp: |
| 358 |
tmp['value'] = '' |
| 359 |
|
| 360 |
|
| 361 |
elif tmp['value'] == 'none': |
| 362 |
tmp['value'] = '' |
| 363 |
elif tmp['value'] == 'True': |
| 364 |
tmp['value'] = 'yes' |
| 365 |
elif tmp['value'] == 'False': |
| 366 |
tmp['value'] = 'no' |
| 367 |
elif isinstance(tmp['value'], bool): |
| 368 |
if tmp['value'] is True: |
| 369 |
tmp['value'] = 'yes' |
| 370 |
else: |
| 371 |
tmp['value'] = 'no' |
| 372 |
|
| 373 |
if tmp['encrypted'] == 'True': |
| 374 |
tmp['encrypted'] = 'yes' |
| 375 |
elif tmp['encrypted'] == 'False': |
| 376 |
tmp['encrypted'] = 'no' |
| 377 |
elif isinstance(tmp['encrypted'], bool): |
| 378 |
if tmp['encrypted'] is True: |
| 379 |
tmp['encrypted'] = 'yes' |
| 380 |
else: |
| 381 |
tmp['encrypted'] = 'no' |
| 382 |
|
| 383 |
result.append(tmp) |
| 384 |
result = sorted(result, key=lambda k: k['name']) |
| 385 |
return result |
| 386 |
|
| 387 |
def normalize_list(self, lists): |
| 388 |
result = [] |
| 389 |
for list in lists: |
| 390 |
tmp = dict((str(k), str(v)) for k, v in iteritems(list) if k != 'value') |
| 391 |
if 'encrypted' not in list: |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
tmp['encrypted'] = 'no' |
| 396 |
if 'value' in list: |
| 397 |
if len(list['value']) > 0: |
| 398 |
|
| 399 |
|
| 400 |
tmp['value'] = [str(x) for x in list['value']] |
| 401 |
|
| 402 |
if tmp['encrypted'] == 'True': |
| 403 |
tmp['encrypted'] = 'yes' |
| 404 |
elif tmp['encrypted'] == 'False': |
| 405 |
tmp['encrypted'] = 'no' |
| 406 |
elif isinstance(tmp['encrypted'], bool): |
| 407 |
if tmp['encrypted'] is True: |
| 408 |
tmp['encrypted'] = 'yes' |
| 409 |
else: |
| 410 |
tmp['encrypted'] = 'no' |
| 411 |
|
| 412 |
result.append(tmp) |
| 413 |
result = sorted(result, key=lambda k: k['name']) |
| 414 |
return result |
| 415 |
|
| 416 |
def normalize_metadata(self, metadata): |
| 417 |
result = [] |
| 418 |
for item in metadata: |
| 419 |
name = item.get('name', None) |
| 420 |
persist = flatten_boolean(item.get('persist', "no")) |
| 421 |
if persist == "yes": |
| 422 |
persist = "true" |
| 423 |
else: |
| 424 |
persist = "false" |
| 425 |
result.append({ |
| 426 |
"name": name, |
| 427 |
"persist": persist |
| 428 |
}) |
| 429 |
return result |
| 430 |
|
| 431 |
|
| 432 |
class ApiParameters(Parameters): |
| 433 |
@property |
| 434 |
def metadata(self): |
| 435 |
if self._values['metadata'] is None: |
| 436 |
return None |
| 437 |
return self._values['metadata'] |
| 438 |
|
| 439 |
@property |
| 440 |
def tables(self): |
| 441 |
if self._values['tables'] is None: |
| 442 |
return None |
| 443 |
return self.normalize_tables(self._values['tables']) |
| 444 |
|
| 445 |
@property |
| 446 |
def lists(self): |
| 447 |
if self._values['lists'] is None: |
| 448 |
return None |
| 449 |
return self.normalize_list(self._values['lists']) |
| 450 |
|
| 451 |
@property |
| 452 |
def variables(self): |
| 453 |
if self._values['variables'] is None: |
| 454 |
return None |
| 455 |
return self.normalize_variables(self._values['variables']) |
| 456 |
|
| 457 |
@property |
| 458 |
def device_group(self): |
| 459 |
if self._values['device_group'] in [None, 'none']: |
| 460 |
return None |
| 461 |
return self._values['device_group'] |
| 462 |
|
| 463 |
|
| 464 |
class ModuleParameters(Parameters): |
| 465 |
@property |
| 466 |
def param_lists(self): |
| 467 |
if self._values['parameters'] is None: |
| 468 |
return None |
| 469 |
result = self._values['parameters'].get('lists', None) |
| 470 |
return result |
| 471 |
|
| 472 |
@property |
| 473 |
def param_tables(self): |
| 474 |
if self._values['parameters'] is None: |
| 475 |
return None |
| 476 |
result = self._values['parameters'].get('tables', None) |
| 477 |
return result |
| 478 |
|
| 479 |
@property |
| 480 |
def param_variables(self): |
| 481 |
if self._values['parameters'] is None: |
| 482 |
return None |
| 483 |
result = self._values['parameters'].get('variables', None) |
| 484 |
return result |
| 485 |
|
| 486 |
@property |
| 487 |
def param_metadata(self): |
| 488 |
if self._values['parameters'] is None: |
| 489 |
return None |
| 490 |
result = self._values['parameters'].get('metadata', None) |
| 491 |
return result |
| 492 |
|
| 493 |
@property |
| 494 |
def param_description(self): |
| 495 |
if self._values['parameters'] is None: |
| 496 |
return None |
| 497 |
result = self._values['parameters'].get('description', None) |
| 498 |
return result |
| 499 |
|
| 500 |
@property |
| 501 |
def param_traffic_group(self): |
| 502 |
if self._values['parameters'] is None: |
| 503 |
return None |
| 504 |
result = self._values['parameters'].get('trafficGroup', None) |
| 505 |
if not result: |
| 506 |
return result |
| 507 |
return fq_name(self.partition, result) |
| 508 |
|
| 509 |
@property |
| 510 |
def param_device_group(self): |
| 511 |
if self._values['parameters'] is None: |
| 512 |
return None |
| 513 |
result = self._values['parameters'].get('deviceGroup', None) |
| 514 |
if not result: |
| 515 |
return result |
| 516 |
return fq_name(self.partition, result) |
| 517 |
|
| 518 |
@property |
| 519 |
def param_strict_updates(self): |
| 520 |
if self._values['parameters'] is None: |
| 521 |
return None |
| 522 |
result = self._values['parameters'].get('strictUpdates', None) |
| 523 |
return flatten_boolean(result) |
| 524 |
|
| 525 |
@property |
| 526 |
def tables(self): |
| 527 |
if self._values['tables']: |
| 528 |
return self.normalize_tables(self._values['tables']) |
| 529 |
elif self.param_tables: |
| 530 |
return self.normalize_tables(self.param_tables) |
| 531 |
return None |
| 532 |
|
| 533 |
@property |
| 534 |
def lists(self): |
| 535 |
if self._values['lists']: |
| 536 |
return self.normalize_list(self._values['lists']) |
| 537 |
elif self.param_lists: |
| 538 |
return self.normalize_list(self.param_lists) |
| 539 |
return None |
| 540 |
|
| 541 |
@property |
| 542 |
def variables(self): |
| 543 |
if self._values['variables']: |
| 544 |
return self.normalize_variables(self._values['variables']) |
| 545 |
elif self.param_variables: |
| 546 |
return self.normalize_variables(self.param_variables) |
| 547 |
return None |
| 548 |
|
| 549 |
@property |
| 550 |
def metadata(self): |
| 551 |
if self._values['metadata']: |
| 552 |
result = self.normalize_metadata(self._values['metadata']) |
| 553 |
elif self.param_metadata: |
| 554 |
result = self.normalize_metadata(self.param_metadata) |
| 555 |
else: |
| 556 |
return None |
| 557 |
return result |
| 558 |
|
| 559 |
@property |
| 560 |
def template(self): |
| 561 |
if self._values['template'] is None: |
| 562 |
return None |
| 563 |
return fq_name(self.partition, self._values['template']) |
| 564 |
|
| 565 |
@property |
| 566 |
def device_group(self): |
| 567 |
if self._values['device_group'] not in [None, 'none']: |
| 568 |
result = fq_name(self.partition, self._values['device_group']) |
| 569 |
elif self.param_device_group not in [None, 'none']: |
| 570 |
result = self.param_device_group |
| 571 |
else: |
| 572 |
return None |
| 573 |
if not result.startswith('/Common/'): |
| 574 |
raise F5ModuleError( |
| 575 |
"Device groups can only exist in /Common" |
| 576 |
) |
| 577 |
return result |
| 578 |
|
| 579 |
@property |
| 580 |
def traffic_group(self): |
| 581 |
if self._values['traffic_group']: |
| 582 |
result = fq_name(self.partition, self._values['traffic_group']) |
| 583 |
elif self.param_traffic_group: |
| 584 |
result = self.param_traffic_group |
| 585 |
else: |
| 586 |
return None |
| 587 |
if not result.startswith('/Common/'): |
| 588 |
raise F5ModuleError( |
| 589 |
"Traffic groups can only exist in /Common" |
| 590 |
) |
| 591 |
return result |
| 592 |
|
| 593 |
@property |
| 594 |
def strict_updates(self): |
| 595 |
if self._values['strict_updates'] is not None: |
| 596 |
result = flatten_boolean(self._values['strict_updates']) |
| 597 |
elif self.param_strict_updates is not None: |
| 598 |
result = flatten_boolean(self.param_strict_updates) |
| 599 |
else: |
| 600 |
return None |
| 601 |
if result == 'yes': |
| 602 |
return 'enabled' |
| 603 |
return 'disabled' |
| 604 |
|
| 605 |
@property |
| 606 |
def description(self): |
| 607 |
if self._values['description']: |
| 608 |
return self._values['description'] |
| 609 |
elif self.param_description: |
| 610 |
return self.param_description |
| 611 |
return None |
| 612 |
|
| 613 |
|
| 614 |
class Changes(Parameters): |
| 615 |
pass |
| 616 |
|
| 617 |
|
| 618 |
class UsableChanges(Changes): |
| 619 |
pass |
| 620 |
|
| 621 |
|
| 622 |
class ReportableChanges(Changes): |
| 623 |
pass |
| 624 |
|
| 625 |
|
| 626 |
class Difference(object): |
| 627 |
def __init__(self, want, have=None): |
| 628 |
self.want = want |
| 629 |
self.have = have |
| 630 |
|
| 631 |
def compare(self, param): |
| 632 |
try: |
| 633 |
result = getattr(self, param) |
| 634 |
return result |
| 635 |
except AttributeError: |
| 636 |
return self.__default(param) |
| 637 |
|
| 638 |
def __default(self, param): |
| 639 |
attr1 = getattr(self.want, param) |
| 640 |
try: |
| 641 |
attr2 = getattr(self.have, param) |
| 642 |
if attr1 != attr2: |
| 643 |
return attr1 |
| 644 |
except AttributeError: |
| 645 |
return attr1 |
| 646 |
|
| 647 |
@property |
| 648 |
def metadata(self): |
| 649 |
if self.want.metadata is None: |
| 650 |
return None |
| 651 |
if self.have.metadata is None: |
| 652 |
return self.want.metadata |
| 653 |
want = [(k, v) for d in self.want.metadata for k, v in iteritems(d)] |
| 654 |
have = [(k, v) for d in self.have.metadata for k, v in iteritems(d)] |
| 655 |
if set(want) != set(have): |
| 656 |
return dict( |
| 657 |
metadata=self.want.metadata |
| 658 |
) |
| 659 |
|
| 660 |
|
| 661 |
class ModuleManager(object): |
| 662 |
def __init__(self, *args, **kwargs): |
| 663 |
self.module = kwargs.get('module', None) |
| 664 |
self.client = F5RestClient(**self.module.params) |
| 665 |
self.have = None |
| 666 |
self.want = ModuleParameters(params=self.module.params) |
| 667 |
self.changes = UsableChanges() |
| 668 |
|
| 669 |
def _set_changed_options(self): |
| 670 |
changed = {} |
| 671 |
for key in Parameters.returnables: |
| 672 |
if getattr(self.want, key) is not None: |
| 673 |
changed[key] = getattr(self.want, key) |
| 674 |
if changed: |
| 675 |
self.changes = UsableChanges(params=changed) |
| 676 |
|
| 677 |
def _update_changed_options(self): |
| 678 |
diff = Difference(self.want, self.have) |
| 679 |
updatables = Parameters.updatables |
| 680 |
changed = dict() |
| 681 |
for k in updatables: |
| 682 |
change = diff.compare(k) |
| 683 |
if change is None: |
| 684 |
continue |
| 685 |
else: |
| 686 |
if isinstance(change, dict): |
| 687 |
changed.update(change) |
| 688 |
else: |
| 689 |
changed[k] = change |
| 690 |
if changed: |
| 691 |
self.changes = UsableChanges(params=changed) |
| 692 |
return True |
| 693 |
return False |
| 694 |
|
| 695 |
def exec_module(self): |
| 696 |
start = datetime.now().isoformat() |
| 697 |
version = tmos_version(self.client) |
| 698 |
changed = False |
| 699 |
result = dict() |
| 700 |
state = self.want.state |
| 701 |
|
| 702 |
if state == "present": |
| 703 |
changed = self.present() |
| 704 |
elif state == "absent": |
| 705 |
changed = self.absent() |
| 706 |
|
| 707 |
changes = self.changes.to_return() |
| 708 |
result.update(**changes) |
| 709 |
result.update(dict(changed=changed)) |
| 710 |
send_teem(start, self.client, self.module, version) |
| 711 |
return result |
| 712 |
|
| 713 |
def present(self): |
| 714 |
if self.exists(): |
| 715 |
return self.update() |
| 716 |
else: |
| 717 |
return self.create() |
| 718 |
|
| 719 |
def absent(self): |
| 720 |
if self.exists(): |
| 721 |
return self.remove() |
| 722 |
return False |
| 723 |
|
| 724 |
def create(self): |
| 725 |
self._set_changed_options() |
| 726 |
if self.want.traffic_group is None: |
| 727 |
self.want.update({'traffic_group': '/Common/traffic-group-1'}) |
| 728 |
if not self.template_exists(): |
| 729 |
raise F5ModuleError( |
| 730 |
"The specified template does not exist in the provided partition." |
| 731 |
) |
| 732 |
if self.module.check_mode: |
| 733 |
return True |
| 734 |
self.create_on_device() |
| 735 |
return True |
| 736 |
|
| 737 |
def remove(self): |
| 738 |
if self.module.check_mode: |
| 739 |
return True |
| 740 |
self.remove_from_device() |
| 741 |
if self.exists(): |
| 742 |
raise F5ModuleError("Failed to delete the iApp service") |
| 743 |
return True |
| 744 |
|
| 745 |
def update(self): |
| 746 |
self.have = self.read_current_from_device() |
| 747 |
if not self.should_update() and not self.want.force: |
| 748 |
return False |
| 749 |
if self.module.check_mode: |
| 750 |
return True |
| 751 |
self.update_on_device() |
| 752 |
return True |
| 753 |
|
| 754 |
def should_update(self): |
| 755 |
result = self._update_changed_options() |
| 756 |
if result: |
| 757 |
return True |
| 758 |
return False |
| 759 |
|
| 760 |
def exists(self): |
| 761 |
base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 762 |
self.client.provider['server'], |
| 763 |
self.client.provider['server_port'] |
| 764 |
) |
| 765 |
uri = build_service_uri(base_uri, self.want.partition, self.want.name) |
| 766 |
resp = self.client.api.get(uri) |
| 767 |
try: |
| 768 |
response = resp.json() |
| 769 |
except ValueError as ex: |
| 770 |
raise F5ModuleError(str(ex)) |
| 771 |
|
| 772 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 773 |
return False |
| 774 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 775 |
return True |
| 776 |
|
| 777 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 778 |
|
| 779 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 780 |
if 'message' in response: |
| 781 |
raise F5ModuleError(response['message']) |
| 782 |
else: |
| 783 |
raise F5ModuleError(resp.content) |
| 784 |
|
| 785 |
def update_on_device(self): |
| 786 |
params = self.changes.api_params() |
| 787 |
if params or self.want.force: |
| 788 |
params['execute-action'] = 'definition' |
| 789 |
base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 790 |
self.client.provider['server'], |
| 791 |
self.client.provider['server_port'] |
| 792 |
) |
| 793 |
uri = build_service_uri(base_uri, self.want.partition, self.want.name) |
| 794 |
resp = self.client.api.patch(uri, json=params) |
| 795 |
try: |
| 796 |
response = resp.json() |
| 797 |
except ValueError as ex: |
| 798 |
raise F5ModuleError(str(ex)) |
| 799 |
|
| 800 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 801 |
raise F5ModuleError(resp.content) |
| 802 |
|
| 803 |
if self.changes.metadata: |
| 804 |
params = dict(metadata=self.changes.metadata) |
| 805 |
params.update({'execute-action': 'definition'}) |
| 806 |
base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 807 |
self.client.provider['server'], |
| 808 |
self.client.provider['server_port'] |
| 809 |
) |
| 810 |
uri = build_service_uri(base_uri, self.want.partition, self.want.name) |
| 811 |
resp = self.client.api.patch(uri, json=params) |
| 812 |
|
| 813 |
try: |
| 814 |
response = resp.json() |
| 815 |
except ValueError as ex: |
| 816 |
raise F5ModuleError(str(ex)) |
| 817 |
|
| 818 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 819 |
raise F5ModuleError(resp.content) |
| 820 |
|
| 821 |
return True |
| 822 |
|
| 823 |
def read_current_from_device(self): |
| 824 |
base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 825 |
self.client.provider['server'], |
| 826 |
self.client.provider['server_port'] |
| 827 |
) |
| 828 |
uri = build_service_uri(base_uri, self.want.partition, self.want.name) |
| 829 |
resp = self.client.api.get(uri) |
| 830 |
|
| 831 |
try: |
| 832 |
response = resp.json() |
| 833 |
except ValueError as ex: |
| 834 |
raise F5ModuleError(str(ex)) |
| 835 |
|
| 836 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 837 |
return ApiParameters(params=response) |
| 838 |
raise F5ModuleError(resp.content) |
| 839 |
|
| 840 |
def template_exists(self): |
| 841 |
name = fq_name(self.want.partition, self.want.template) |
| 842 |
parts = name.split('/') |
| 843 |
uri = "https://{0}:{1}/mgmt/tm/sys/application/template/{2}".format( |
| 844 |
self.client.provider['server'], |
| 845 |
self.client.provider['server_port'], |
| 846 |
transform_name(parts[1], parts[2]) |
| 847 |
) |
| 848 |
resp = self.client.api.get(uri) |
| 849 |
try: |
| 850 |
response = resp.json() |
| 851 |
except ValueError as ex: |
| 852 |
raise F5ModuleError(str(ex)) |
| 853 |
|
| 854 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 855 |
return False |
| 856 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 857 |
return True |
| 858 |
|
| 859 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 860 |
|
| 861 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 862 |
if 'message' in response: |
| 863 |
raise F5ModuleError(response['message']) |
| 864 |
else: |
| 865 |
raise F5ModuleError(resp.content) |
| 866 |
|
| 867 |
def create_on_device(self): |
| 868 |
params = self.changes.api_params() |
| 869 |
params['name'] = self.want.name |
| 870 |
params['partition'] = self.want.partition |
| 871 |
uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 872 |
self.client.provider['server'], |
| 873 |
self.client.provider['server_port'] |
| 874 |
) |
| 875 |
resp = self.client.api.post(uri, json=params) |
| 876 |
|
| 877 |
try: |
| 878 |
response = resp.json() |
| 879 |
except ValueError as ex: |
| 880 |
raise F5ModuleError(str(ex)) |
| 881 |
|
| 882 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 883 |
raise F5ModuleError(resp.content) |
| 884 |
|
| 885 |
if self.changes.metadata: |
| 886 |
payload = dict(metadata=self.changes.metadata) |
| 887 |
base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 888 |
self.client.provider['server'], |
| 889 |
self.client.provider['server_port'] |
| 890 |
) |
| 891 |
uri = build_service_uri(base_uri, self.want.partition, self.want.name) |
| 892 |
resp = self.client.api.patch(uri, json=payload) |
| 893 |
try: |
| 894 |
response = resp.json() |
| 895 |
except ValueError as ex: |
| 896 |
raise F5ModuleError(str(ex)) |
| 897 |
|
| 898 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 899 |
raise F5ModuleError(resp.content) |
| 900 |
|
| 901 |
return True |
| 902 |
|
| 903 |
def remove_from_device(self): |
| 904 |
base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format( |
| 905 |
self.client.provider['server'], |
| 906 |
self.client.provider['server_port'] |
| 907 |
) |
| 908 |
uri = build_service_uri(base_uri, self.want.partition, self.want.name) |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
payload = dict(metadata=[]) |
| 917 |
resp = self.client.api.patch(uri, json=payload) |
| 918 |
try: |
| 919 |
response = resp.json() |
| 920 |
except ValueError as ex: |
| 921 |
raise F5ModuleError(str(ex)) |
| 922 |
|
| 923 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 924 |
raise F5ModuleError(resp.content) |
| 925 |
|
| 926 |
response = self.client.api.delete(uri) |
| 927 |
|
| 928 |
if response.status in [200, 201]: |
| 929 |
return True |
| 930 |
raise F5ModuleError(response.content) |
| 931 |
|
| 932 |
|
| 933 |
class ArgumentSpec(object): |
| 934 |
def __init__(self): |
| 935 |
self.supports_check_mode = True |
| 936 |
argument_spec = dict( |
| 937 |
name=dict(required=True), |
| 938 |
template=dict(), |
| 939 |
description=dict(), |
| 940 |
device_group=dict(), |
| 941 |
parameters=dict( |
| 942 |
type='dict' |
| 943 |
), |
| 944 |
state=dict( |
| 945 |
default='present', |
| 946 |
choices=['absent', 'present'] |
| 947 |
), |
| 948 |
force=dict( |
| 949 |
default='no', |
| 950 |
type='bool' |
| 951 |
), |
| 952 |
strict_updates=dict( |
| 953 |
type='bool', |
| 954 |
), |
| 955 |
metadata=dict( |
| 956 |
type='list', |
| 957 |
elements='raw', |
| 958 |
), |
| 959 |
traffic_group=dict(), |
| 960 |
partition=dict( |
| 961 |
default='Common', |
| 962 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 963 |
) |
| 964 |
) |
| 965 |
self.argument_spec = {} |
| 966 |
self.argument_spec.update(f5_argument_spec) |
| 967 |
self.argument_spec.update(argument_spec) |
| 968 |
|
| 969 |
|
| 970 |
def main(): |
| 971 |
spec = ArgumentSpec() |
| 972 |
|
| 973 |
module = AnsibleModule( |
| 974 |
argument_spec=spec.argument_spec, |
| 975 |
supports_check_mode=spec.supports_check_mode |
| 976 |
) |
| 977 |
|
| 978 |
try: |
| 979 |
mm = ModuleManager(module=module) |
| 980 |
results = mm.exec_module() |
| 981 |
module.exit_json(**results) |
| 982 |
except F5ModuleError as ex: |
| 983 |
module.fail_json(msg=str(ex)) |
| 984 |
|
| 985 |
|
| 986 |
if __name__ == '__main__': |
| 987 |
main() |