| 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: bigiq_regkey_pool |
| 13 |
short_description: Manages registration key pools on BIG-IQ |
| 14 |
description: |
| 15 |
- Manages registration key (regkey) pools on a BIG-IQ. These pools function as |
| 16 |
a container in which you add lists of registration keys. To add registration |
| 17 |
keys, use the C(bigiq_regkey_license) module. |
| 18 |
version_added: "1.0.0" |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- Specifies the name of the registration key pool. |
| 23 |
- You must name your registration pools unique names. While |
| 24 |
BIG-IQ does not require this, this module does. If you do not, |
| 25 |
the behavior of the module is undefined, and you may end up putting |
| 26 |
licenses in the wrong registration key pool. |
| 27 |
type: str |
| 28 |
required: True |
| 29 |
description: |
| 30 |
description: |
| 31 |
- A description to attach to the pool. |
| 32 |
type: str |
| 33 |
state: |
| 34 |
description: |
| 35 |
- The state of the regkey pool on the system. |
| 36 |
- When C(present), guarantees the pool exists. |
| 37 |
- When C(absent), removes the pool, and the licenses it contains, from the |
| 38 |
system. |
| 39 |
type: str |
| 40 |
choices: |
| 41 |
- absent |
| 42 |
- present |
| 43 |
default: present |
| 44 |
requirements: |
| 45 |
- BIG-IQ >= 5.3.0 |
| 46 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 47 |
author: |
| 48 |
- Tim Rupp (@caphrim007) |
| 49 |
''' |
| 50 |
|
| 51 |
EXAMPLES = r''' |
| 52 |
- name: Create a registration key (regkey) pool to hold individual device licenses |
| 53 |
bigiq_regkey_pool: |
| 54 |
name: foo-pool |
| 55 |
state: present |
| 56 |
provider: |
| 57 |
user: admin |
| 58 |
password: secret |
| 59 |
server: cm.mydomain.com |
| 60 |
delegate_to: localhost |
| 61 |
''' |
| 62 |
|
| 63 |
RETURN = r''' |
| 64 |
description: |
| 65 |
description: New description of the regkey pool. |
| 66 |
returned: changed |
| 67 |
type: str |
| 68 |
sample: My description |
| 69 |
''' |
| 70 |
from datetime import datetime |
| 71 |
|
| 72 |
from ansible.module_utils.basic import AnsibleModule |
| 73 |
|
| 74 |
from ..module_utils.bigip import F5RestClient |
| 75 |
from ..module_utils.common import ( |
| 76 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec |
| 77 |
) |
| 78 |
from ..module_utils.icontrol import bigiq_version |
| 79 |
from ..module_utils.teem import send_teem |
| 80 |
|
| 81 |
|
| 82 |
class Parameters(AnsibleF5Parameters): |
| 83 |
api_map = { |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
api_attributes = [ |
| 88 |
'description' |
| 89 |
] |
| 90 |
|
| 91 |
returnables = [ |
| 92 |
'description' |
| 93 |
] |
| 94 |
|
| 95 |
updatables = [ |
| 96 |
'description' |
| 97 |
] |
| 98 |
|
| 99 |
def to_return(self): |
| 100 |
result = {} |
| 101 |
try: |
| 102 |
for returnable in self.returnables: |
| 103 |
result[returnable] = getattr(self, returnable) |
| 104 |
result = self._filter_params(result) |
| 105 |
except Exception: |
| 106 |
raise |
| 107 |
return result |
| 108 |
|
| 109 |
|
| 110 |
class ModuleParameters(Parameters): |
| 111 |
@property |
| 112 |
def uuid(self): |
| 113 |
"""Returns UUID of a given name |
| 114 |
|
| 115 |
Will search for a given name and return the first one returned to us. If no name, |
| 116 |
and therefore no ID, is found, will return the string "none". The string "none" |
| 117 |
is returned because if we were to return the None value, it would cause the |
| 118 |
license loading code to append a None string to the URI; essentially asking the |
| 119 |
remote device for its collection (which we dont want and which would cause the SDK |
| 120 |
to return an False error. |
| 121 |
|
| 122 |
:return: |
| 123 |
""" |
| 124 |
collection = self.read_current_from_device() |
| 125 |
resource = next((x for x in collection if x.name == self._values['name']), None) |
| 126 |
if resource: |
| 127 |
return resource.id |
| 128 |
else: |
| 129 |
return "none" |
| 130 |
|
| 131 |
def read_current_from_device(self): |
| 132 |
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses".format( |
| 133 |
self.client.provider['server'], |
| 134 |
self.client.provider['server_port'], |
| 135 |
) |
| 136 |
resp = self.client.api.get(uri) |
| 137 |
try: |
| 138 |
response = resp.json() |
| 139 |
except ValueError as ex: |
| 140 |
raise F5ModuleError(str(ex)) |
| 141 |
|
| 142 |
if 'code' in response and response['code'] == 400: |
| 143 |
if 'message' in response: |
| 144 |
raise F5ModuleError(response['message']) |
| 145 |
else: |
| 146 |
raise F5ModuleError(resp.content) |
| 147 |
if 'items' not in response: |
| 148 |
return [] |
| 149 |
result = [ApiParameters(params=r) for r in response['items']] |
| 150 |
return result |
| 151 |
|
| 152 |
|
| 153 |
class ApiParameters(Parameters): |
| 154 |
@property |
| 155 |
def uuid(self): |
| 156 |
return self._values['id'] |
| 157 |
|
| 158 |
|
| 159 |
class Changes(Parameters): |
| 160 |
pass |
| 161 |
|
| 162 |
|
| 163 |
class ReportableChanges(Changes): |
| 164 |
pass |
| 165 |
|
| 166 |
|
| 167 |
class UsableChanges(Changes): |
| 168 |
pass |
| 169 |
|
| 170 |
|
| 171 |
class Difference(object): |
| 172 |
def __init__(self, want, have=None): |
| 173 |
self.want = want |
| 174 |
self.have = have |
| 175 |
|
| 176 |
def compare(self, param): |
| 177 |
try: |
| 178 |
result = getattr(self, param) |
| 179 |
return result |
| 180 |
except AttributeError: |
| 181 |
return self.__default(param) |
| 182 |
|
| 183 |
def __default(self, param): |
| 184 |
attr1 = getattr(self.want, param) |
| 185 |
try: |
| 186 |
attr2 = getattr(self.have, param) |
| 187 |
if attr1 != attr2: |
| 188 |
return attr1 |
| 189 |
except AttributeError: |
| 190 |
return attr1 |
| 191 |
|
| 192 |
|
| 193 |
class ModuleManager(object): |
| 194 |
def __init__(self, *args, **kwargs): |
| 195 |
self.module = kwargs.get('module', None) |
| 196 |
self.client = F5RestClient(**self.module.params) |
| 197 |
self.want = ModuleParameters(client=self.client, params=self.module.params) |
| 198 |
self.have = ApiParameters() |
| 199 |
self.changes = UsableChanges() |
| 200 |
|
| 201 |
def _set_changed_options(self): |
| 202 |
changed = {} |
| 203 |
for key in Parameters.returnables: |
| 204 |
if getattr(self.want, key) is not None: |
| 205 |
changed[key] = getattr(self.want, key) |
| 206 |
if changed: |
| 207 |
self.changes = UsableChanges(params=changed) |
| 208 |
|
| 209 |
def _update_changed_options(self): |
| 210 |
diff = Difference(self.want, self.have) |
| 211 |
updatables = Parameters.updatables |
| 212 |
changed = dict() |
| 213 |
for k in updatables: |
| 214 |
change = diff.compare(k) |
| 215 |
if change is None: |
| 216 |
continue |
| 217 |
else: |
| 218 |
if isinstance(change, dict): |
| 219 |
changed.update(change) |
| 220 |
else: |
| 221 |
changed[k] = change |
| 222 |
if changed: |
| 223 |
self.changes = Changes(params=changed) |
| 224 |
return True |
| 225 |
return False |
| 226 |
|
| 227 |
def should_update(self): |
| 228 |
result = self._update_changed_options() |
| 229 |
if result: |
| 230 |
return True |
| 231 |
return False |
| 232 |
|
| 233 |
def exec_module(self): |
| 234 |
start = datetime.now().isoformat() |
| 235 |
version = bigiq_version(self.client) |
| 236 |
changed = False |
| 237 |
result = dict() |
| 238 |
state = self.want.state |
| 239 |
|
| 240 |
if state == "present": |
| 241 |
changed = self.present() |
| 242 |
elif state == "absent": |
| 243 |
changed = self.absent() |
| 244 |
|
| 245 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 246 |
changes = reportable.to_return() |
| 247 |
result.update(**changes) |
| 248 |
result.update(dict(changed=changed)) |
| 249 |
self._announce_deprecations(result) |
| 250 |
send_teem(start, self.client, self.module, version) |
| 251 |
return result |
| 252 |
|
| 253 |
def _announce_deprecations(self, result): |
| 254 |
warnings = result.pop('__warnings', []) |
| 255 |
for warning in warnings: |
| 256 |
self.module.deprecate( |
| 257 |
msg=warning['msg'], |
| 258 |
version=warning['version'] |
| 259 |
) |
| 260 |
|
| 261 |
def present(self): |
| 262 |
if self.exists(): |
| 263 |
return self.update() |
| 264 |
else: |
| 265 |
return self.create() |
| 266 |
|
| 267 |
def exists(self): |
| 268 |
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format( |
| 269 |
self.client.provider['server'], |
| 270 |
self.client.provider['server_port'], |
| 271 |
self.want.uuid, |
| 272 |
) |
| 273 |
resp = self.client.api.get(uri) |
| 274 |
try: |
| 275 |
response = resp.json() |
| 276 |
except ValueError: |
| 277 |
return False |
| 278 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 279 |
return False |
| 280 |
return True |
| 281 |
|
| 282 |
def update(self): |
| 283 |
self.have = self.read_current_from_device() |
| 284 |
if not self.should_update(): |
| 285 |
return False |
| 286 |
if self.module.check_mode: |
| 287 |
return True |
| 288 |
self.update_on_device() |
| 289 |
return True |
| 290 |
|
| 291 |
def remove(self): |
| 292 |
if self.module.check_mode: |
| 293 |
return True |
| 294 |
self.remove_from_device() |
| 295 |
if self.exists(): |
| 296 |
raise F5ModuleError("Failed to delete the resource.") |
| 297 |
return True |
| 298 |
|
| 299 |
def create(self): |
| 300 |
self._set_changed_options() |
| 301 |
if self.module.check_mode: |
| 302 |
return True |
| 303 |
self.create_on_device() |
| 304 |
return True |
| 305 |
|
| 306 |
def create_on_device(self): |
| 307 |
params = self.want.api_params() |
| 308 |
params['name'] = self.want.name |
| 309 |
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/".format( |
| 310 |
self.client.provider['server'], |
| 311 |
self.client.provider['server_port'], |
| 312 |
) |
| 313 |
resp = self.client.api.post(uri, json=params) |
| 314 |
try: |
| 315 |
response = resp.json() |
| 316 |
except ValueError as ex: |
| 317 |
raise F5ModuleError(str(ex)) |
| 318 |
|
| 319 |
if 'code' in response and response['code'] in [400, 403]: |
| 320 |
if 'message' in response: |
| 321 |
raise F5ModuleError(response['message']) |
| 322 |
else: |
| 323 |
raise F5ModuleError(resp.content) |
| 324 |
|
| 325 |
def update_on_device(self): |
| 326 |
params = self.changes.api_params() |
| 327 |
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format( |
| 328 |
self.client.provider['server'], |
| 329 |
self.client.provider['server_port'], |
| 330 |
self.want.uuid |
| 331 |
) |
| 332 |
resp = self.client.api.patch(uri, json=params) |
| 333 |
try: |
| 334 |
response = resp.json() |
| 335 |
except ValueError as ex: |
| 336 |
raise F5ModuleError(str(ex)) |
| 337 |
|
| 338 |
if 'code' in response and response['code'] == 400: |
| 339 |
if 'message' in response: |
| 340 |
raise F5ModuleError(response['message']) |
| 341 |
else: |
| 342 |
raise F5ModuleError(resp.content) |
| 343 |
|
| 344 |
def absent(self): |
| 345 |
if self.exists(): |
| 346 |
return self.remove() |
| 347 |
return False |
| 348 |
|
| 349 |
def remove_from_device(self): |
| 350 |
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format( |
| 351 |
self.client.provider['server'], |
| 352 |
self.client.provider['server_port'], |
| 353 |
self.want.uuid |
| 354 |
) |
| 355 |
resp = self.client.api.delete(uri) |
| 356 |
if resp.status == 200: |
| 357 |
return True |
| 358 |
|
| 359 |
def read_current_from_device(self): |
| 360 |
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format( |
| 361 |
self.client.provider['server'], |
| 362 |
self.client.provider['server_port'], |
| 363 |
self.want.uuid |
| 364 |
) |
| 365 |
resp = self.client.api.get(uri) |
| 366 |
try: |
| 367 |
response = resp.json() |
| 368 |
except ValueError as ex: |
| 369 |
raise F5ModuleError(str(ex)) |
| 370 |
|
| 371 |
if 'code' in response and response['code'] == 400: |
| 372 |
if 'message' in response: |
| 373 |
raise F5ModuleError(response['message']) |
| 374 |
else: |
| 375 |
raise F5ModuleError(resp.content) |
| 376 |
return ApiParameters(params=response) |
| 377 |
|
| 378 |
|
| 379 |
class ArgumentSpec(object): |
| 380 |
def __init__(self): |
| 381 |
self.supports_check_mode = True |
| 382 |
argument_spec = dict( |
| 383 |
name=dict(required=True), |
| 384 |
description=dict(), |
| 385 |
state=dict( |
| 386 |
default='present', |
| 387 |
choices=['absent', 'present'] |
| 388 |
) |
| 389 |
) |
| 390 |
self.argument_spec = {} |
| 391 |
self.argument_spec.update(f5_argument_spec) |
| 392 |
self.argument_spec.update(argument_spec) |
| 393 |
|
| 394 |
|
| 395 |
def main(): |
| 396 |
spec = ArgumentSpec() |
| 397 |
|
| 398 |
module = AnsibleModule( |
| 399 |
argument_spec=spec.argument_spec, |
| 400 |
supports_check_mode=spec.supports_check_mode, |
| 401 |
) |
| 402 |
|
| 403 |
try: |
| 404 |
mm = ModuleManager(module=module) |
| 405 |
results = mm.exec_module() |
| 406 |
module.exit_json(**results) |
| 407 |
except F5ModuleError as ex: |
| 408 |
module.fail_json(msg=str(ex)) |
| 409 |
|
| 410 |
|
| 411 |
if __name__ == '__main__': |
| 412 |
main() |