Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_mac_pool.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'certified'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: ucs_mac_pool
16 short_description: Configures MAC address pools on Cisco UCS Manager
17 description:
18 - Configures MAC address pools and MAC address blocks on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify MAC pool is present and will create if needed.
24 - If C(absent), will verify MAC pool is absent and will delete if needed.
25 choices: [present, absent]
26 default: present
27 type: str
28 name:
29 description:
30 - The name of the MAC pool.
31 - This name can be between 1 and 32 alphanumeric characters.
32 - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)."
33 - You cannot change this name after the MAC pool is created.
34 required: true
35 type: str
36 description:
37 description:
38 - A description of the MAC pool.
39 - Enter up to 256 characters.
40 - "You can use any characters or spaces except the following:"
41 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
42 aliases: [ descr ]
43 type: str
44 order:
45 description:
46 - The Assignment Order field.
47 - "This can be one of the following:"
48 - "default - Cisco UCS Manager selects a random identity from the pool."
49 - "sequential - Cisco UCS Manager selects the lowest available identity from the pool."
50 choices: [default, sequential]
51 default: default
52 type: str
53 first_addr:
54 description:
55 - The first MAC address in the block of addresses.
56 - This is the From field in the UCS Manager MAC Blocks menu.
57 type: str
58 last_addr:
59 description:
60 - The last MAC address in the block of addresses.
61 - This is the To field in the UCS Manager Add MAC Blocks menu.
62 type: str
63 org_dn:
64 description:
65 - The distinguished name (dn) of the organization where the resource is assigned.
66 default: org-root
67 type: str
68 requirements:
69 - ucsmsdk
70 author:
71 - David Soper (@dsoper2)
72 - CiscoUcs (@CiscoUcs)
73 '''
74
75 EXAMPLES = r'''
76 - name: Configure MAC address pool
77 cisco.ucs.ucs_mac_pool:
78 hostname: 172.16.143.150
79 username: admin
80 password: password
81 name: mac-A
82 first_addr: 00:25:B5:00:66:00
83 last_addr: 00:25:B5:00:67:F3
84 order: sequential
85
86 - name: Remove MAC address pool
87 cisco.ucs.ucs_mac_pool:
88 hostname: 172.16.143.150
89 username: admin
90 password: password
91 name: mac-A
92 state: absent
93 '''
94
95 RETURN = r'''
96 #
97 '''
98
99 from ansible.module_utils.basic import AnsibleModule
100 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
101
102
103 def main():
104 argument_spec = ucs_argument_spec.copy()
105 argument_spec.update(
106 org_dn=dict(type='str', default='org-root'),
107 name=dict(type='str', required=True),
108 description=dict(type='str', aliases=['descr']),
109 order=dict(type='str', default='default', choices=['default', 'sequential']),
110 first_addr=dict(type='str'),
111 last_addr=dict(type='str'),
112 state=dict(default='present', choices=['present', 'absent'], type='str'),
113 )
114 module = AnsibleModule(
115 argument_spec,
116 supports_check_mode=True,
117 )
118 # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation.
119 ucs = UCSModule(module)
120
121 err = False
122
123 from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
124 from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock
125
126 changed = False
127 try:
128 mo_exists = False
129 props_match = False
130 # dn is <org_dn>/mac-pool-<name>
131 dn = module.params['org_dn'] + '/mac-pool-' + module.params['name']
132 mo = ucs.login_handle.query_dn(dn)
133 if mo:
134 mo_exists = True
135
136 if module.params['state'] == 'absent':
137 if mo_exists:
138 if not module.check_mode:
139 ucs.login_handle.remove_mo(mo)
140 ucs.login_handle.commit()
141 changed = True
142 else:
143 if mo_exists:
144 # check top-level mo props
145 kwargs = dict(assignment_order=module.params['order'])
146 kwargs['descr'] = module.params['description']
147 if mo.check_prop_match(**kwargs):
148 # top-level props match, check next level mo/props
149 if module.params['last_addr'] and module.params['first_addr']:
150 # mac address block specified, check properties
151 block_dn = dn + '/block-' + module.params['first_addr'].upper() + '-' + module.params['last_addr'].upper()
152 mo_1 = ucs.login_handle.query_dn(block_dn)
153 if mo_1:
154 props_match = True
155 else:
156 # no MAC address block specified, but top-level props matched
157 props_match = True
158
159 if not props_match:
160 if not module.check_mode:
161 # create if mo does not already exist
162 mo = MacpoolPool(
163 parent_mo_or_dn=module.params['org_dn'],
164 name=module.params['name'],
165 descr=module.params['description'],
166 assignment_order=module.params['order'],
167 )
168
169 if module.params['last_addr'] and module.params['first_addr']:
170 mo_1 = MacpoolBlock(
171 parent_mo_or_dn=mo,
172 to=module.params['last_addr'],
173 r_from=module.params['first_addr'],
174 )
175
176 ucs.login_handle.add_mo(mo, True)
177 ucs.login_handle.commit()
178 changed = True
179
180 except Exception as e:
181 err = True
182 ucs.result['msg'] = "setup error: %s " % str(e)
183
184 ucs.result['changed'] = changed
185 if err:
186 module.fail_json(**ucs.result)
187 module.exit_json(**ucs.result)
188
189
190 if __name__ == '__main__':
191 main()