Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_dvs_portgroup_find.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r'''
13 ---
14 module: vmware_dvs_portgroup_find
15 short_description: Find portgroup(s) in a VMware environment
16 description:
17 - Find portgroup(s) based on different criteria such as distributed vSwitch, VLAN id or a string in the name.
18 author:
19 - David Martinez (@dx0xm)
20 options:
21 dvswitch:
22 description:
23 - Name of a distributed vSwitch to look for.
24 type: str
25 vlanid:
26 description:
27 - VLAN id can be any number between 1 and 4094.
28 - This search criteria will looks into VLAN ranges to find possible matches.
29 required: false
30 type: int
31 name:
32 description:
33 - string to check inside the name of the portgroup.
34 - Basic containment check using python C(in) operation.
35 type: str
36 show_uplink:
37 description:
38 - Show or hide uplink portgroups.
39 - Only relevant when O(vlanid) is supplied.
40 type: bool
41 default: false
42 extends_documentation_fragment:
43 - vmware.vmware.base_options
44
45 '''
46
47 EXAMPLES = r'''
48 - name: Get all portgroups in dvswitch vDS
49 community.vmware.vmware_dvs_portgroup_find:
50 hostname: "{{ vcenter_hostname }}"
51 username: "{{ vcenter_username }}"
52 password: "{{ vcenter_password }}"
53 dvswitch: 'vDS'
54 delegate_to: localhost
55
56 - name: Confirm if vlan 15 is present
57 community.vmware.vmware_dvs_portgroup_find:
58 hostname: "{{ vcenter_hostname }}"
59 username: "{{ vcenter_username }}"
60 password: "{{ vcenter_password }}"
61 vlanid: '15'
62 delegate_to: localhost
63 '''
64
65 RETURN = r'''
66 dvs_portgroups:
67 description: basic details of portgroups found
68 returned: on success
69 type: list
70 sample: [
71 {
72 "dvswitch": "vDS",
73 "name": "N-51",
74 "pvlan": true,
75 "trunk": true,
76 "vlan_id": "0"
77 }
78 ]
79 '''
80
81 try:
82 from pyVmomi import vim
83 except ImportError:
84 pass
85
86 from ansible.module_utils.basic import AnsibleModule
87 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
88 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
89 from urllib.parse import unquote
90
91
92 class DVSPortgroupFindManager(PyVmomi):
93 def __init__(self, module):
94 super(DVSPortgroupFindManager, self).__init__(module)
95 self.dvs_name = self.params['dvswitch']
96 self.vlan = self.params['vlanid']
97 self.cmp_vlans = True if self.vlan else False
98 self.pgs = self.find_portgroups_by_name(self.content, self.module.params['name'])
99
100 if self.dvs_name:
101 self.pgs = self.find_portgroups_by_dvs(self.pgs, self.dvs_name)
102
103 def find_portgroups_by_name(self, content, name=None):
104 vimtype = [vim.dvs.DistributedVirtualPortgroup]
105 container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
106 if not name:
107 obj = container.view
108 else:
109 obj = []
110 for c in container.view:
111 if name in c.name:
112 obj.append(c)
113
114 return obj
115
116 def find_portgroups_by_dvs(self, pgl, dvs):
117 obj = []
118 for c in pgl:
119 if dvs in c.config.distributedVirtualSwitch.name:
120 obj.append(c)
121
122 return obj
123
124 def vlan_match(self, pgup, userup, vlanlst):
125 res = False
126 if pgup and userup:
127 return True
128
129 for ln in vlanlst:
130 if '-' in ln:
131 arr = ln.split('-')
132 if int(arr[0]) < self.vlan and self.vlan < int(arr[1]):
133 res = True
134 elif ln == str(self.vlan):
135 res = True
136
137 return res
138
139 def get_dvs_portgroup(self):
140 pgroups = self.pgs
141
142 pglist = []
143 for pg in pgroups:
144 trunk = False
145 pvlan = False
146 vlanInfo = pg.config.defaultPortConfig.vlan
147 cl1 = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec
148 cl2 = vim.dvs.VmwareDistributedVirtualSwitch.PvlanSpec
149 vlan_id_list = []
150 if isinstance(vlanInfo, cl1):
151 trunk = True
152 for item in vlanInfo.vlanId:
153 if item.start == item.end:
154 vlan_id_list.append(str(item.start))
155 else:
156 vlan_id_list.append(str(item.start) + '-' + str(item.end))
157 elif isinstance(vlanInfo, cl2):
158 pvlan = True
159 vlan_id_list.append(str(vlanInfo.pvlanId))
160 else:
161 vlan_id_list.append(str(vlanInfo.vlanId))
162
163 if self.cmp_vlans:
164 if self.vlan_match(pg.config.uplink, self.module.params['show_uplink'], vlan_id_list):
165 pglist.append(dict(
166 name=unquote(pg.name),
167 trunk=trunk,
168 pvlan=pvlan,
169 vlan_id=','.join(vlan_id_list),
170 dvswitch=pg.config.distributedVirtualSwitch.name))
171 else:
172 pglist.append(dict(
173 name=unquote(pg.name),
174 trunk=trunk,
175 pvlan=pvlan,
176 vlan_id=','.join(vlan_id_list),
177 dvswitch=pg.config.distributedVirtualSwitch.name))
178
179 return pglist
180
181
182 def main():
183 argument_spec = base_argument_spec()
184 argument_spec.update(
185 dvswitch=dict(type='str', required=False),
186 vlanid=dict(type='int', required=False),
187 name=dict(type='str', required=False),
188 show_uplink=dict(type='bool', default=False),
189 )
190 module = AnsibleModule(
191 argument_spec=argument_spec,
192 supports_check_mode=True,
193 required_if=[
194 ['show_uplink', 'True', ['vlanid']]
195 ]
196 )
197
198 dvs_pg_mgr = DVSPortgroupFindManager(module)
199 module.exit_json(changed=False,
200 dvs_portgroups=dvs_pg_mgr.get_dvs_portgroup())
201
202
203 if __name__ == "__main__":
204 main()