From b7906f9c2757af53c0b21d88d3f49a822ef6d768 Mon Sep 17 00:00:00 2001 From: David Soper Date: Mon, 15 Jan 2018 09:22:17 -0600 Subject: [PATCH] Module renamed to ucs_mac_pool to match naming convention for other ucs modules. (#34826) Documentation updated to match UCS Manager online help. Coding style updated to match other ucs modules. --- .../remote_management/ucs/ucs_mac_pool.py | 186 ++++++++++++++++++ .../remote_management/ucs/ucs_macpool.py | 145 -------------- .../{ucs_macpool => ucs_mac_pool}/aliases | 0 .../targets/ucs_mac_pool/tasks/main.yml | 129 ++++++++++++ .../targets/ucs_macpool/tasks/main.yml | 142 ------------- 5 files changed, 315 insertions(+), 287 deletions(-) create mode 100644 lib/ansible/modules/remote_management/ucs/ucs_mac_pool.py delete mode 100644 lib/ansible/modules/remote_management/ucs/ucs_macpool.py rename test/integration/targets/{ucs_macpool => ucs_mac_pool}/aliases (100%) create mode 100644 test/integration/targets/ucs_mac_pool/tasks/main.yml delete mode 100644 test/integration/targets/ucs_macpool/tasks/main.yml diff --git a/lib/ansible/modules/remote_management/ucs/ucs_mac_pool.py b/lib/ansible/modules/remote_management/ucs/ucs_mac_pool.py new file mode 100644 index 0000000000..0353609be5 --- /dev/null +++ b/lib/ansible/modules/remote_management/ucs/ucs_mac_pool.py @@ -0,0 +1,186 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = r''' +--- +module: ucs_mac_pool +short_description: Configures MAC address pools on Cisco UCS Manager +description: +- Configures MAC address pools and MAC address blocks on Cisco UCS Manager. +- Examples can be used with the UCS Platform Emulator U(https://communities.cisco.com/ucspe). +extends_documentation_fragment: ucs +options: + state: + description: + - If C(present), will verify MAC pool is present and will create if needed. + - If C(absent), will verify MAC pool is absent and will delete if needed. + choices: [present, absent] + default: present + name: + description: + - The name of the MAC pool. + - This name can be between 1 and 32 alphanumeric characters. + - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)." + - You cannot change this name after the MAC pool is created. + required: yes + descrption: + description: + - A description of the MAC pool. + - Enter up to 256 characters. + - "You can use any characters or spaces except the following:" + - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)." + aliases: [ descr ] + order: + description: + - The Assignment Order field. + - "This can be one of the following:" + - "default - Cisco UCS Manager selects a random identity from the pool." + - "sequential - Cisco UCS Manager selects the lowest available identity from the pool." + choices: [default, sequential] + default: default + first_addr: + description: + - The first MAC address in the block of addresses. + - This is the From field in the UCS Manager MAC Blocks menu. + last_addr: + description: + - The last MAC address in the block of addresses. + - This is the To field in the UCS Manager Add MAC Blocks menu. + org_dn: + description: + - The distinguished name (dn) of the organization where the resource is assigned. + default: org-root +requirements: +- ucsmsdk +author: +- David Soper (@dsoper2) +- CiscoUcs (@CiscoUcs) +version_added: '2.5' +''' + +EXAMPLES = r''' +- name: Configure MAC address pool + ucs_mac_pool: + hostname: 172.16.143.150 + username: admin + password: password + name: mac-A + first_addr: 00:25:B5:00:66:00 + last_addr: 00:25:B5:00:67:F3 + order: sequential + +- name: Remove MAC address pool + ucs_mac_pool: + hostname: 172.16.143.150 + username: admin + password: password + name: mac-A + state: absent +''' + +RETURN = r''' +# +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.remote_management.ucs import UCSModule, ucs_argument_spec + + +def main(): + argument_spec = ucs_argument_spec + argument_spec.update( + org_dn=dict(type='str', default='org-root'), + name=dict(type='str', required=True), + descr=dict(type='str', default=''), + order=dict(type='str', default='default', choices=['default', 'sequential']), + first_addr=dict(type='str'), + last_addr=dict(type='str'), + state=dict(default='present', choices=['present', 'absent'], type='str'), + ) + module = AnsibleModule( + argument_spec, + supports_check_mode=True, + ) + # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. + ucs = UCSModule(module) + + err = False + + from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool + from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock + + changed = False + try: + mo_exists = False + props_match = False + # dn is /mac-pool- + dn = module.params['org_dn'] + '/mac-pool-' + module.params['name'] + mo = ucs.login_handle.query_dn(dn) + if mo: + mo_exists = True + + if module.params['state'] == 'absent': + if mo_exists: + if not module.check_mode: + ucs.login_handle.remove_mo(mo) + ucs.login_handle.commit() + changed = True + else: + if mo_exists: + # check top-level mo props + kwargs = dict(assignment_order=module.params['order']) + kwargs['descr'] = module.params['descr'] + if (mo.check_prop_match(**kwargs)): + # top-level props match, check next level mo/props + if module.params['last_addr'] and module.params['first_addr']: + # mac address block specified, check properties + block_dn = dn + '/block-' + module.params['first_addr'].upper() + '-' + module.params['last_addr'].upper() + mo_1 = ucs.login_handle.query_dn(block_dn) + if mo_1: + props_match = True + else: + # no MAC address block specified, but top-level props matched + props_match = True + + if not props_match: + if not module.check_mode: + # create if mo does not already exist + mo = MacpoolPool( + parent_mo_or_dn=module.params['org_dn'], + name=module.params['name'], + descr=module.params['descr'], + assignment_order=module.params['order'], + ) + + if module.params['last_addr'] and module.params['first_addr']: + mo_1 = MacpoolBlock( + parent_mo_or_dn=mo, + to=module.params['last_addr'], + r_from=module.params['first_addr'], + ) + + ucs.login_handle.add_mo(mo, True) + ucs.login_handle.commit() + changed = True + + except Exception as e: + err = True + ucs.result['msg'] = "setup error: %s " % str(e) + + ucs.result['changed'] = changed + if err: + module.fail_json(**ucs.result) + module.exit_json(**ucs.result) + + +if __name__ == '__main__': + main() diff --git a/lib/ansible/modules/remote_management/ucs/ucs_macpool.py b/lib/ansible/modules/remote_management/ucs/ucs_macpool.py deleted file mode 100644 index 3c01fa0914..0000000000 --- a/lib/ansible/modules/remote_management/ucs/ucs_macpool.py +++ /dev/null @@ -1,145 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -from __future__ import absolute_import, division, print_function -__metaclass__ = type - -ANSIBLE_METADATA = {'metadata_version': '1.1', - 'status': ['preview'], - 'supported_by': 'community'} - -DOCUMENTATION = r''' ---- -module: ucs_macpool -short_description: Configures MAC address pools on Cisco UCS Manager -description: -- Configures MAC address pools and MAC pool blocks on Cisco UCS Manager. -- Examples can be used with the UCS Platform Emulator U(https://communities.cisco.com/ucspe). -extends_documentation_fragment: ucs -options: - state: - description: - - If C(present), will verify MAC pool is present and will create if needed. - - If C(absent), will verify MAC pool is absent and will delete if needed. - choices: [present, absent] - default: present - mac_list: - description: - - List of MAC pools which contain the following properties - - name (Name of the MAC pool (required)) - - descr (Description for the MAC pool) - - order (Assignment order which is default or sequential) - - first_addr (First MAC address in the MAC addresses block) - - last_addr (Last MAC address in the MAC addresses block) - required: yes - org_dn: - description: - - Org dn (distinguished name) - default: org-root -requirements: -- ucsmsdk -author: -- David Soper (@dsoper2) -- CiscoUcs (@CiscoUcs) -version_added: '2.5' -''' - -EXAMPLES = r''' -- name: Configure MAC address pool - ucs_macpool: - hostname: 172.16.143.150 - username: admin - password: password - mac_list: - - name: mac-A - first_addr: 00:25:B5:00:66:00 - last_addr: 00:25:B5:00:67:F3 - order: sequential -''' - -RETURN = r''' -# -''' - -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.remote_management.ucs import UCSModule, ucs_argument_spec - - -def main(): - argument_spec = ucs_argument_spec - argument_spec.update(mac_list=dict(required=True, type='list'), - org_dn=dict(type='str', default='org-root'), - state=dict(default='present', choices=['present', 'absent'], type='str')) - module = AnsibleModule(argument_spec, - supports_check_mode=True) - ucs = UCSModule(module) - - err = False - - from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool - from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock - - changed = False - try: - for mac in module.params['mac_list']: - exists = False - dn = module.params['org_dn'] + '/mac-pool-' + mac['name'] - mo = ucs.login_handle.query_dn(dn) - if mo: - # check top-level mo props - kwargs = {} - if 'order' in mac: - kwargs['assignment_order'] = mac['order'] - if 'descr' in mac: - kwargs['descr'] = mac['descr'] - if (mo.check_prop_match(**kwargs)): - # top-level props match, check next level mo/props - if 'last_addr' in mac and 'first_addr' in mac: - block_dn = dn + '/block-' + mac['first_addr'].upper() + '-' + mac['last_addr'].upper() - mo_1 = ucs.login_handle.query_dn(block_dn) - if mo_1: - exists = True - else: - exists = True - - if module.params['state'] == 'absent': - if exists: - if not module.check_mode: - ucs.login_handle.remove_mo(mo) - ucs.login_handle.commit() - changed = True - else: - if not exists: - if not module.check_mode: - # create if mo does not already exist - if 'order' not in mac: - mac['order'] = 'default' - if 'descr' not in mac: - mac['descr'] = '' - mo = MacpoolPool(parent_mo_or_dn=module.params['org_dn'], - name=mac['name'], - descr=mac['descr'], - assignment_order=mac['order']) - - if 'last_addr' in mac and 'first_addr' in mac: - mo_1 = MacpoolBlock(parent_mo_or_dn=mo, - to=mac['last_addr'], - r_from=mac['first_addr']) - ucs.login_handle.add_mo(mo, True) - ucs.login_handle.commit() - changed = True - - except Exception as e: - err = True - ucs.result['msg'] = "setup error: %s " % str(e) - - ucs.result['changed'] = changed - if err: - module.fail_json(**ucs.result) - module.exit_json(**ucs.result) - - -if __name__ == '__main__': - main() diff --git a/test/integration/targets/ucs_macpool/aliases b/test/integration/targets/ucs_mac_pool/aliases similarity index 100% rename from test/integration/targets/ucs_macpool/aliases rename to test/integration/targets/ucs_mac_pool/aliases diff --git a/test/integration/targets/ucs_mac_pool/tasks/main.yml b/test/integration/targets/ucs_mac_pool/tasks/main.yml new file mode 100644 index 0000000000..ebb6bb8757 --- /dev/null +++ b/test/integration/targets/ucs_mac_pool/tasks/main.yml @@ -0,0 +1,129 @@ +# Test code for the UCS modules +# Copyright 2017, David Soper (@dsoper2) + +- name: Test that we have a UCS host, UCS username, and UCS password + fail: + msg: 'Please define the following variables: ucs_hostname, ucs_username and ucs_password.' + when: ucs_hostname is not defined or ucs_username is not defined or ucs_password is not defined + vars: + login_info: &login_info + hostname: "{{ ucs_hostname }}" + username: "{{ ucs_username }}" + password: "{{ ucs_password }}" + + +# Setup (clean environment) +- name: MAC pools absent + ucs_mac_pool: &mac_pools_absent + <<: *login_info + name: mac-A + state: absent + + +# Test present (check_mode) +- name: MAC pools present (check_mode) + ucs_mac_pool: &mac_pools_present + <<: *login_info + name: mac-A + first_addr: 00:25:B5:00:66:00 + last_addr: 00:25:B5:00:67:F3 + order: sequential + check_mode: yes + register: cm_mac_pools_present + + +# Present (normal mode) +- name: MAC pools present (normal mode) + ucs_mac_pool: *mac_pools_present + register: nm_mac_pools_present + + +# Test present again (idempotent) +- name: MAC pools present again (check_mode) + ucs_mac_pool: *mac_pools_present + check_mode: yes + register: cm_mac_pools_present_again + + +# Present again (normal mode) +- name: MAC pools present again (normal mode) + ucs_mac_pool: *mac_pools_present + register: nm_mac_pools_present_again + + +# Verfiy present +- name: Verify MAC present results + assert: + that: + - cm_mac_pools_present.changed == nm_mac_pools_present.changed == true + - cm_mac_pools_present_again.changed == nm_mac_pools_present_again.changed == false + + +# Test change (check_mode) +- name: MAC pools description change (check_mode) + ucs_mac_pool: &mac_pools_change + <<: *mac_pools_present + descr: Testing Ansible + check_mode: yes + register: cm_mac_pools_descr_change + + +# Change (normal mode) +- name: MAC pools description change (normal mode) + ucs_mac_pool: *mac_pools_change + register: nm_mac_pools_descr_change + + +# Test change again (idempotent) +- name: MAC pools description again (check_mode) + ucs_mac_pool: *mac_pools_change + check_mode: yes + register: cm_mac_pools_descr_change_again + + +# Change again (normal mode) +- name: MAC pools description change again (normal mode) + ucs_mac_pool: *mac_pools_change + register: nm_mac_pools_descr_change_again + + +# Verfiy change +- name: Verify MAC change results + assert: + that: + - cm_mac_pools_descr_change.changed == nm_mac_pools_descr_change.changed == true + - cm_mac_pools_descr_change_again.changed == nm_mac_pools_descr_change_again.changed == false + + +# Teardown (clean environment) +- name: MAC pools absent (check_mode) + ucs_mac_pool: *mac_pools_absent + check_mode: yes + register: cm_mac_pools_absent + + +# Absent (normal mode) +- name: MAC pools absent (normal mode) + ucs_mac_pool: *mac_pools_absent + register: nm_mac_pools_absent + + +# Test absent again (idempotent) +- name: MAC pools absent again (check_mode) + ucs_mac_pool: *mac_pools_absent + check_mode: yes + register: cm_mac_pools_absent_again + + +# Absent again (normal mode) +- name: MAC pools absent again (normal mode) + ucs_mac_pool: *mac_pools_absent + register: nm_mac_pools_absent_again + + +# Verfiy absent +- name: Verify MAC absent results + assert: + that: + - cm_mac_pools_absent.changed == nm_mac_pools_absent.changed == true + - cm_mac_pools_absent_again.changed == nm_mac_pools_absent_again.changed == false diff --git a/test/integration/targets/ucs_macpool/tasks/main.yml b/test/integration/targets/ucs_macpool/tasks/main.yml deleted file mode 100644 index 8bc6980a06..0000000000 --- a/test/integration/targets/ucs_macpool/tasks/main.yml +++ /dev/null @@ -1,142 +0,0 @@ -# Test code for the UCS modules -# Copyright 2017, David Soper (@dsoper2) - -- name: Test that we have a UCS host, UCS username, and UCS password - fail: - msg: 'Please define the following variables: ucs_hostname, ucs_username and ucs_password.' - when: ucs_hostname is not defined or ucs_username is not defined or ucs_password is not defined - - -# Setup (clean environment) -- name: MAC pools absent - ucs_macpool: &macpools_absent - hostname: "{{ ucs_hostname }}" - username: "{{ ucs_username }}" - password: "{{ ucs_password }}" - mac_list: - - name: mac-A - - name: mac-B - state: absent - - -# Test present (check_mode) -- name: MAC pools present (check_mode) - ucs_macpool: &macpools_present - hostname: "{{ ucs_hostname }}" - username: "{{ ucs_username }}" - password: "{{ ucs_password }}" - mac_list: - - name: mac-A - first_addr: 00:25:B5:00:66:00 - last_addr: 00:25:B5:00:67:F3 - order: sequential - - name: mac-B - first_addr: 00:25:B5:0b:66:00 - last_addr: 00:25:B5:0b:67:F3 - check_mode: yes - register: cm_macpools_present - - -# Present (normal mode) -- name: MAC pools present (normal mode) - ucs_macpool: *macpools_present - register: nm_macpools_present - - -# Test present again (idempotent) -- name: MAC pools present again (check_mode) - ucs_macpool: *macpools_present - check_mode: yes - register: cm_macpools_present_again - - -# Present again (normal mode) -- name: MAC pools present again (normal mode) - ucs_macpool: *macpools_present - register: nm_macpools_present_again - - -# Verfiy present -- name: Verify MAC present results - assert: - that: - - cm_macpools_present.changed == nm_macpools_present.changed == true - - cm_macpools_present_again.changed == nm_macpools_present_again.changed == false - - -# Test change (check_mode) -- name: MAC pools description change (check_mode) - ucs_macpool: &macpools_change - <<: *macpools_present - mac_list: - - name: mac-A - descr: Testing Ansible - first_addr: 00:25:B5:00:66:00 - last_addr: 00:25:B5:00:67:F3 - order: sequential - - name: mac-B - first_addr: 00:25:B5:0b:66:00 - last_addr: 00:25:B5:0b:67:F3 - check_mode: yes - register: cm_macpools_descr_change - - -# Change (normal mode) -- name: MAC pools description change (normal mode) - ucs_macpool: *macpools_change - register: nm_macpools_descr_change - - -# Test change again (idempotent) -- name: MAC pools description again (check_mode) - ucs_macpool: *macpools_change - check_mode: yes - register: cm_macpools_descr_change_again - - -# Change again (normal mode) -- name: MAC pools description change again (normal mode) - ucs_macpool: *macpools_change - register: nm_macpools_descr_change_again - - -# Verfiy change -- name: Verify MAC change results - assert: - that: - - cm_macpools_descr_change.changed == nm_macpools_descr_change.changed == true - - cm_macpools_descr_change_again.changed == nm_macpools_descr_change_again.changed == false - - -# Teardown (clean environment) -- name: MAC pools absent (check_mode) - ucs_macpool: *macpools_absent - check_mode: yes - register: cm_macpools_absent - - -# Absent (normal mode) -- name: MAC pools absent (normal mode) - ucs_macpool: *macpools_absent - register: nm_macpools_absent - - -# Test absent again (idempotent) -- name: MAC pools absent again (check_mode) - ucs_macpool: *macpools_absent - check_mode: yes - register: cm_macpools_absent_again - - -# Absent again (normal mode) -- name: MAC pools absent again (normal mode) - ucs_macpool: *macpools_absent - register: nm_macpools_absent_again - - -# Verfiy absent -- name: Verify MAC absent results - assert: - that: - - cm_macpools_absent.changed == nm_macpools_absent.changed == true - - cm_macpools_absent_again.changed == nm_macpools_absent_again.changed == false