|
| 1 | +# Copyright 2016 F5 Networks Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +import logging |
| 17 | +from pprint import pprint as pp |
| 18 | + |
| 19 | +from f5.bigip.mixins import UnsupportedTmosVersion |
| 20 | +from f5.bigip.resource import Collection |
| 21 | +from f5.bigip.resource import OrganizingCollection |
| 22 | + |
| 23 | + |
| 24 | +def register_collection_atoms(collection): |
| 25 | + '''Given a collection return a registry of all of its atoms (elements). |
| 26 | +
|
| 27 | + Registries are dictionaries with selfLink keys, and PathElement values. |
| 28 | + A registry provides a snapshot of all resources of a type on the device. |
| 29 | + ''' |
| 30 | + resource_registry = {} |
| 31 | + try: |
| 32 | + resources = collection.get_collection() |
| 33 | + except Exception as ex: |
| 34 | + logging.debug(ex) |
| 35 | + return resource_registry |
| 36 | + for resource in resources: |
| 37 | + try: |
| 38 | + resource_registry[resource.selfLink] = resource |
| 39 | + except KeyError as ex: |
| 40 | + pp(resource.raw) |
| 41 | + raise ex |
| 42 | + return resource_registry |
| 43 | + |
| 44 | + |
| 45 | +def register_OC_atoms(organizing_collection): |
| 46 | + '''Given an OrganizingCollection (OC) return a registry of its atoms. |
| 47 | +
|
| 48 | + Registries are dictionaries with selfLink keys, and PathElement values. |
| 49 | + A registry provides a snapshot of all resources of a type on the device. |
| 50 | + ''' |
| 51 | + OC_atoms_registry = {} |
| 52 | + OC_types = organizing_collection._meta_data['allowed_lazy_attributes'] |
| 53 | + for OC_type in OC_types: |
| 54 | + try: |
| 55 | + lazy_instance =\ |
| 56 | + getattr(organizing_collection, OC_type.__name__.lower()) |
| 57 | + except UnsupportedTmosVersion as ex: |
| 58 | + logging.debug(ex) |
| 59 | + continue |
| 60 | + if isinstance(lazy_instance, Collection): |
| 61 | + OC_atoms_registry.update(register_collection_atoms(lazy_instance)) |
| 62 | + elif isinstance(lazy_instance, OrganizingCollection): |
| 63 | + OC_atoms_registry.update(register_OC_atoms(lazy_instance)) |
| 64 | + return OC_atoms_registry |
| 65 | + |
| 66 | + |
| 67 | +def register_device(mgmt_rt): |
| 68 | + OCs = [getattr(mgmt_rt, c.__name__.lower()) |
| 69 | + for c in mgmt_rt._meta_data['allowed_lazy_attributes']] |
| 70 | + grand_registry = {} |
| 71 | + for OC in OCs: |
| 72 | + grand_registry.update(register_OC_atoms(OC)) |
| 73 | + return grand_registry |
| 74 | + |
| 75 | + |
| 76 | +def register_loadbalancer_elements(mgmt_rt): |
| 77 | + monitor_registry = register_OC_atoms(mgmt_rt.tm.ltm.monitor) |
| 78 | + pool_registry = register_collection_atoms(mgmt_rt.tm.ltm.pools) |
| 79 | + snat_registry = register_collection_atoms(mgmt_rt.tm.ltm.snats) |
| 80 | + virtual_registry = register_collection_atoms(mgmt_rt.tm.ltm.virtuals) |
| 81 | + virtual_address_s_registry =\ |
| 82 | + register_collection_atoms(mgmt_rt.tm.ltm.virtual_address_s) |
| 83 | + member_registry = {} |
| 84 | + for pool in pool_registry.values(): |
| 85 | + mc = pool.members_s |
| 86 | + member_registry.update(register_collection_atoms(mc)) |
| 87 | + folder_registry = register_collection_atoms(mgmt_rt.tm.sys.folders) |
| 88 | + registries = {'monitor_registry': monitor_registry, |
| 89 | + 'pool_registry': pool_registry, |
| 90 | + 'snat_registry': snat_registry, |
| 91 | + 'virtual_registry': virtual_registry, |
| 92 | + 'virtual_address_s_registry': virtual_address_s_registry, |
| 93 | + 'member_registry': member_registry, |
| 94 | + 'folder_registry': folder_registry} |
| 95 | + return registries |
0 commit comments