"Fossies" - the Fresh Open Source Software Archive

Member "ansible-2.9.27/lib/ansible/modules/network/aci/aci_domain_to_vlan_pool.py" (11 Oct 2021, 10689 Bytes) of package /linux/misc/ansible-2.9.27.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "aci_domain_to_vlan_pool.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.9.24_vs_4.3.0.

    1 #!/usr/bin/python
    2 # -*- coding: utf-8 -*-
    3 
    4 # Copyright: (c) 2017, Dag Wieers <dag@wieers.com>
    5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
    6 
    7 from __future__ import absolute_import, division, print_function
    8 __metaclass__ = type
    9 
   10 ANSIBLE_METADATA = {'metadata_version': '1.1',
   11                     'status': ['preview'],
   12                     'supported_by': 'certified'}
   13 
   14 DOCUMENTATION = r'''
   15 ---
   16 module: aci_domain_to_vlan_pool
   17 short_description: Bind Domain to VLAN Pools (infra:RsVlanNs)
   18 description:
   19 - Bind Domain to VLAN Pools on Cisco ACI fabrics.
   20 version_added: '2.5'
   21 options:
   22   domain:
   23     description:
   24     - Name of the domain being associated with the VLAN Pool.
   25     type: str
   26     aliases: [ domain_name, domain_profile ]
   27   domain_type:
   28     description:
   29     - Determines if the Domain is physical (phys) or virtual (vmm).
   30     type: str
   31     choices: [ fc, l2dom, l3dom, phys, vmm ]
   32   pool:
   33     description:
   34     - The name of the pool.
   35     type: str
   36     aliases: [ pool_name, vlan_pool ]
   37   pool_allocation_mode:
   38     description:
   39     - The method used for allocating VLANs to resources.
   40     type: str
   41     required: yes
   42     choices: [ dynamic, static]
   43     aliases: [ allocation_mode, mode ]
   44   state:
   45     description:
   46     - Use C(present) or C(absent) for adding or removing.
   47     - Use C(query) for listing an object or multiple objects.
   48     type: str
   49     choices: [ absent, present, query ]
   50     default: present
   51   vm_provider:
   52     description:
   53     - The VM platform for VMM Domains.
   54     - Support for Kubernetes was added in ACI v3.0.
   55     - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
   56     type: str
   57     choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware ]
   58 extends_documentation_fragment: aci
   59 notes:
   60 - The C(domain) and C(vlan_pool) parameters should exist before using this module.
   61   The M(aci_domain) and M(aci_vlan_pool) can be used for these.
   62 seealso:
   63 - module: aci_domain
   64 - module: aci_vlan_pool
   65 - name: APIC Management Information Model reference
   66   description: More information about the internal APIC class B(infra:RsVlanNs).
   67   link: https://developer.cisco.com/docs/apic-mim-ref/
   68 author:
   69 - Dag Wieers (@dagwieers)
   70 '''
   71 
   72 EXAMPLES = r'''
   73 - name: Bind a VMM domain to VLAN pool
   74   aci_domain_to_vlan_pool:
   75     host: apic
   76     username: admin
   77     password: SomeSecretPassword
   78     domain: vmw_dom
   79     domain_type: vmm
   80     pool: vmw_pool
   81     pool_allocation_mode: dynamic
   82     vm_provider: vmware
   83     state: present
   84   delegate_to: localhost
   85 
   86 - name: Remove a VMM domain to VLAN pool binding
   87   aci_domain_to_vlan_pool:
   88     host: apic
   89     username: admin
   90     password: SomeSecretPassword
   91     domain: vmw_dom
   92     domain_type: vmm
   93     pool: vmw_pool
   94     pool_allocation_mode: dynamic
   95     vm_provider: vmware
   96     state: absent
   97   delegate_to: localhost
   98 
   99 - name: Bind a physical domain to VLAN pool
  100   aci_domain_to_vlan_pool:
  101     host: apic
  102     username: admin
  103     password: SomeSecretPassword
  104     domain: phys_dom
  105     domain_type: phys
  106     pool: phys_pool
  107     pool_allocation_mode: static
  108     state: present
  109   delegate_to: localhost
  110 
  111 - name: Bind a physical domain to VLAN pool
  112   aci_domain_to_vlan_pool:
  113     host: apic
  114     username: admin
  115     password: SomeSecretPassword
  116     domain: phys_dom
  117     domain_type: phys
  118     pool: phys_pool
  119     pool_allocation_mode: static
  120     state: absent
  121   delegate_to: localhost
  122 
  123 - name: Query an domain to VLAN pool binding
  124   aci_domain_to_vlan_pool:
  125     host: apic
  126     username: admin
  127     password: SomeSecretPassword
  128     domain: phys_dom
  129     domain_type: phys
  130     pool: phys_pool
  131     pool_allocation_mode: static
  132     state: query
  133   delegate_to: localhost
  134   register: query_result
  135 
  136 - name: Query all domain to VLAN pool bindings
  137   aci_domain_to_vlan_pool:
  138     host: apic
  139     username: admin
  140     password: SomeSecretPassword
  141     domain_type: phys
  142     state: query
  143   delegate_to: localhost
  144   register: query_result
  145 '''
  146 
  147 RETURN = r'''
  148 current:
  149   description: The existing configuration from the APIC after the module has finished
  150   returned: success
  151   type: list
  152   sample:
  153     [
  154         {
  155             "fvTenant": {
  156                 "attributes": {
  157                     "descr": "Production environment",
  158                     "dn": "uni/tn-production",
  159                     "name": "production",
  160                     "nameAlias": "",
  161                     "ownerKey": "",
  162                     "ownerTag": ""
  163                 }
  164             }
  165         }
  166     ]
  167 error:
  168   description: The error information as returned from the APIC
  169   returned: failure
  170   type: dict
  171   sample:
  172     {
  173         "code": "122",
  174         "text": "unknown managed object class foo"
  175     }
  176 raw:
  177   description: The raw output returned by the APIC REST API (xml or json)
  178   returned: parse error
  179   type: str
  180   sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
  181 sent:
  182   description: The actual/minimal configuration pushed to the APIC
  183   returned: info
  184   type: list
  185   sample:
  186     {
  187         "fvTenant": {
  188             "attributes": {
  189                 "descr": "Production environment"
  190             }
  191         }
  192     }
  193 previous:
  194   description: The original configuration from the APIC before the module has started
  195   returned: info
  196   type: list
  197   sample:
  198     [
  199         {
  200             "fvTenant": {
  201                 "attributes": {
  202                     "descr": "Production",
  203                     "dn": "uni/tn-production",
  204                     "name": "production",
  205                     "nameAlias": "",
  206                     "ownerKey": "",
  207                     "ownerTag": ""
  208                 }
  209             }
  210         }
  211     ]
  212 proposed:
  213   description: The assembled configuration from the user-provided parameters
  214   returned: info
  215   type: dict
  216   sample:
  217     {
  218         "fvTenant": {
  219             "attributes": {
  220                 "descr": "Production environment",
  221                 "name": "production"
  222             }
  223         }
  224     }
  225 filter_string:
  226   description: The filter string used for the request
  227   returned: failure or debug
  228   type: str
  229   sample: ?rsp-prop-include=config-only
  230 method:
  231   description: The HTTP method used for the request to the APIC
  232   returned: failure or debug
  233   type: str
  234   sample: POST
  235 response:
  236   description: The HTTP response from the APIC
  237   returned: failure or debug
  238   type: str
  239   sample: OK (30 bytes)
  240 status:
  241   description: The HTTP status from the APIC
  242   returned: failure or debug
  243   type: int
  244   sample: 200
  245 url:
  246   description: The HTTP url used for the request to the APIC
  247   returned: failure or debug
  248   type: str
  249   sample: https://10.11.12.13/api/mo/uni/tn-production.json
  250 '''
  251 
  252 from ansible.module_utils.basic import AnsibleModule
  253 from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
  254 
  255 VM_PROVIDER_MAPPING = dict(
  256     cloudfoundry='CloudFoundry',
  257     kubernetes='Kubernetes',
  258     microsoft='Microsoft',
  259     openshift='OpenShift',
  260     openstack='OpenStack',
  261     redhat='Redhat',
  262     vmware='VMware',
  263 )
  264 
  265 
  266 def main():
  267     argument_spec = aci_argument_spec()
  268     argument_spec.update(
  269         domain_type=dict(type='str', required=True, choices=['fc', 'l2dom', 'l3dom', 'phys', 'vmm']),
  270         domain=dict(type='str', aliases=['domain_name', 'domain_profile']),  # Not required for querying all objects
  271         pool=dict(type='str', aliases=['pool_name', 'vlan_pool']),  # Not required for querying all objects
  272         pool_allocation_mode=dict(type='str', required=True, aliases=['allocation_mode', 'mode'], choices=['dynamic', 'static']),
  273         state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
  274         vm_provider=dict(type='str', choices=['cloudfoundry', 'kubernetes', 'microsoft', 'openshift', 'openstack', 'redhat', 'vmware']),
  275     )
  276 
  277     module = AnsibleModule(
  278         argument_spec=argument_spec,
  279         supports_check_mode=True,
  280         required_if=[
  281             ['domain_type', 'vmm', ['vm_provider']],
  282             ['state', 'absent', ['domain', 'domain_type', 'pool']],
  283             ['state', 'present', ['domain', 'domain_type', 'pool']],
  284         ],
  285     )
  286 
  287     domain = module.params['domain']
  288     domain_type = module.params['domain_type']
  289     pool = module.params['pool']
  290     pool_allocation_mode = module.params['pool_allocation_mode']
  291     vm_provider = module.params['vm_provider']
  292     state = module.params['state']
  293 
  294     # Report when vm_provider is set when type is not virtual
  295     if domain_type != 'vmm' and vm_provider is not None:
  296         module.fail_json(msg="Domain type '{0}' cannot have a 'vm_provider'".format(domain_type))
  297 
  298     # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
  299     pool_name = pool
  300     if pool is not None:
  301         pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
  302 
  303     # Compile the full domain for URL building
  304     if domain_type == 'fc':
  305         domain_class = 'fcDomP'
  306         domain_mo = 'uni/fc-{0}'.format(domain)
  307         domain_rn = 'fc-{0}'.format(domain)
  308     elif domain_type == 'l2dom':
  309         domain_class = 'l2extDomP'
  310         domain_mo = 'uni/l2dom-{0}'.format(domain)
  311         domain_rn = 'l2dom-{0}'.format(domain)
  312     elif domain_type == 'l3dom':
  313         domain_class = 'l3extDomP'
  314         domain_mo = 'uni/l3dom-{0}'.format(domain)
  315         domain_rn = 'l3dom-{0}'.format(domain)
  316     elif domain_type == 'phys':
  317         domain_class = 'physDomP'
  318         domain_mo = 'uni/phys-{0}'.format(domain)
  319         domain_rn = 'phys-{0}'.format(domain)
  320     elif domain_type == 'vmm':
  321         domain_class = 'vmmDomP'
  322         domain_mo = 'uni/vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING[vm_provider], domain)
  323         domain_rn = 'vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING[vm_provider], domain)
  324 
  325     # Ensure that querying all objects works when only domain_type is provided
  326     if domain is None:
  327         domain_mo = None
  328 
  329     aci_mo = 'uni/infra/vlanns-{0}'.format(pool_name)
  330 
  331     aci = ACIModule(module)
  332     aci.construct_url(
  333         root_class=dict(
  334             aci_class=domain_class,
  335             aci_rn=domain_rn,
  336             module_object=domain_mo,
  337             target_filter={'name': domain},
  338         ),
  339         child_classes=['infraRsVlanNs'],
  340     )
  341 
  342     aci.get_existing()
  343 
  344     if state == 'present':
  345         aci.payload(
  346             aci_class=domain_class,
  347             class_config=dict(name=domain),
  348             child_configs=[
  349                 {'infraRsVlanNs': {'attributes': {'tDn': aci_mo}}},
  350             ]
  351         )
  352 
  353         aci.get_diff(aci_class=domain_class)
  354 
  355         aci.post_config()
  356 
  357     elif state == 'absent':
  358         aci.delete_config()
  359 
  360     aci.exit_json()
  361 
  362 
  363 if __name__ == "__main__":
  364     main()