VMware: Fix regression in vmware_portgroup (#35700)

This fix corrects the regression introduced by refactor commit in
vmware_portgroup module.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-02-05 21:49:02 +05:30 committed by ansibot
parent df3265a811
commit 02111bc6c8

View file

@ -2,8 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> # Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
# Copyright: (c) 2017, Ansible Project # Copyright: (c) 2017-18, Ansible Project
# Copyright: (c) 2017, Abhijeet Kasurde <akasurde@redhat.com> # Copyright: (c) 2017-18, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # 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 from __future__ import absolute_import, division, print_function
@ -62,12 +62,13 @@ options:
description: description:
- Name of cluster name for host membership. - Name of cluster name for host membership.
- Portgroup will be created on all hosts of the given cluster. - Portgroup will be created on all hosts of the given cluster.
- This option is required if hosts is not specified. - This option is required if C(hosts) is not specified.
version_added: "2.5" version_added: "2.5"
hosts: hosts:
description: description:
- List of name of host or hosts on which portgroup needs to be added. - List of name of host or hosts on which portgroup needs to be added.
- This option is required if cluster_name is not specified. - This option is required if C(cluster_name) is not specified.
aliases: [ esxi_hostname ]
version_added: "2.5" version_added: "2.5"
state: state:
description: description:
@ -131,6 +132,21 @@ EXAMPLES = r'''
state: absent state: absent
''' '''
RETURN = r'''
result:
description: metadata about the portgroup
returned: always
type: dict
sample: {
"esxi01.example.com": {
"portgroup_name": "pg0010",
"switch_name": "vswitch_0001",
"vlan_id": 1
}
}
'''
try: try:
from pyVmomi import vim, vmodl from pyVmomi import vim, vmodl
except ImportError: except ImportError:
@ -138,14 +154,15 @@ except ImportError:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
from ansible.module_utils._text import to_native
class PyVmomiHelper(PyVmomi): class PyVmomiHelper(PyVmomi):
def __init__(self, module): def __init__(self, module):
super(PyVmomiHelper, self).__init__(module) super(PyVmomiHelper, self).__init__(module)
self.hosts = self.params['hosts'] hosts = self.params['hosts']
self.cluster = self.params['cluster_name'] cluster = self.params['cluster_name']
self.portgroup_name = self.params['portgroup_name'] self.portgroup_name = self.params['portgroup_name']
self.switch_name = self.params['switch_name'] self.switch_name = self.params['switch_name']
self.vlan_id = self.params['vlan_id'] self.vlan_id = self.params['vlan_id']
@ -153,17 +170,27 @@ class PyVmomiHelper(PyVmomi):
self.forged_transmits = self.params['network_policy'].get('forged_transmits') self.forged_transmits = self.params['network_policy'].get('forged_transmits')
self.mac_changes = self.params['network_policy'].get('mac_changes') self.mac_changes = self.params['network_policy'].get('mac_changes')
self.network_policy = self.create_network_policy() self.network_policy = self.create_network_policy()
self.changed = False
self.state = self.params['state'] self.state = self.params['state']
self.host_obj_list = []
if cluster and self.find_cluster_by_name(cluster_name=cluster):
self.host_obj_list = self.get_all_hosts_by_cluster(cluster_name=cluster)
elif hosts:
for host in hosts:
host_system = self.find_hostsystem_by_name(host_name=host)
if host_system:
self.host_obj_list.append(host_system)
else:
self.module.fail_json(msg="Failed to find ESXi %s" % host)
def process_state(self): def process_state(self):
""" """
Function to manage state Function to manage state
""" """
if self.state == 'present': if self.state == 'present':
self.add_portgroup() self.add_hosts_port_group()
elif self.state == 'absent': elif self.state == 'absent':
self.remove_portgroup() self.remove_hosts_port_group()
# Get # Get
def get_port_group_by_name(self, host_system, portgroup_name, vswitch_name): def get_port_group_by_name(self, host_system, portgroup_name, vswitch_name):
@ -184,34 +211,41 @@ class PyVmomiHelper(PyVmomi):
desired_pgs.append(pg) desired_pgs.append(pg)
return desired_pgs return desired_pgs
def check_network_policy_diff(self, current_policy, desired_policy): @staticmethod
def check_network_policy_diff(current_policy, desired_policy):
""" """
Function to find difference between existing network policy and user given network policy Function to find difference between existing network policy and user given network policy
Args: Args:
current_policy: Current network policy current_policy: Current network policy
desired_policy: User defined network policy desired_policy: User defined network policy
Returns: Returns: True if difference found, False if not.
""" """
ret = False ret = False
if current_policy.security.allowPromiscuous != desired_policy.security.allowPromiscuous: if (current_policy.security.allowPromiscuous != desired_policy.security.allowPromiscuous) or \
ret = True (current_policy.security.forgedTransmits != desired_policy.security.forgedTransmits) or \
if current_policy.security.forgedTransmits != desired_policy.security.forgedTransmits: (current_policy.security.macChanges != desired_policy.security.macChanges):
ret = True
if current_policy.security.macChanges != desired_policy.security.macChanges:
ret = True ret = True
return ret return ret
# Add # Add
def add_hosts_port_group(self, hosts): def add_hosts_port_group(self):
""" """
Function to add port group to given hosts Function to add port group to given hosts
Args:
hosts: List of Host System
""" """
for host in hosts: results = dict(changed=False, result=dict())
self.create_host_port_group(host, self.portgroup_name, self.vlan_id, self.switch_name, self.network_policy) host_change_list = []
for host in self.host_obj_list:
change = False
results['result'][host.name] = dict(portgroup_name=self.portgroup_name,
vlan_id=self.vlan_id,
switch_name=self.switch_name)
change = self.create_host_port_group(host, self.portgroup_name, self.vlan_id, self.switch_name, self.network_policy)
host_change_list.append(change)
if any(host_change_list):
results['changed'] = True
self.module.exit_json(**results)
def create_host_port_group(self, host_system, portgroup_name, vlan_id, vswitch_name, network_policy): def create_host_port_group(self, host_system, portgroup_name, vlan_id, vswitch_name, network_policy):
""" """
@ -241,14 +275,23 @@ class PyVmomiHelper(PyVmomi):
host_system.configManager.networkSystem.AddPortGroup(portgrp=port_group.spec) host_system.configManager.networkSystem.AddPortGroup(portgrp=port_group.spec)
self.changed = True self.changed = True
except vim.fault.AlreadyExists as e: except vim.fault.AlreadyExists as e:
self.module.fail_json(msg="Failed to add Portgroup as it already exists: %s" % e.msg) # To match with other vmware_* modules if portgroup
except vim.fault.NotFound as e: # exists, we proceed, as user may want idempotency
self.module.fail_json(msg="Failed to add Portgroup as vSwitch was not found: %s" % e.msg) self.changed = False
except vim.fault.HostConfigFault as e: except vim.fault.NotFound as not_found:
self.module.fail_json(msg="Failed to add Portgroup due to host system configuration failure : %s" % e.msg) self.module.fail_json(msg="Failed to add Portgroup as vSwitch"
except vmodl.fault.InvalidArgument as e: " was not found: %s" % to_native(not_found.msg))
self.module.fail_json(msg="Failed to add Portgroup as VLAN id was not correct as per specifications: %s" % e.msg) except vim.fault.HostConfigFault as host_config_fault:
self.module.fail_json(msg="Failed to add Portgroup due to host system"
" configuration failure : %s" % to_native(host_config_fault.msg))
except vmodl.fault.InvalidArgument as invalid_argument:
self.module.fail_json(msg="Failed to add Portgroup as VLAN id was not"
" correct as per specifications: %s" % to_native(invalid_argument.msg))
except Exception as generic_exception:
self.module.fail_json(msg="Failed to add Portgroup due to generic"
" exception : %s" % to_native(generic_exception))
else: else:
self.changed = False
# Change portgroup # Change portgroup
if desired_pgs[0].spec.vlanId != vlan_id: if desired_pgs[0].spec.vlanId != vlan_id:
port_group.spec.vlanId = vlan_id port_group.spec.vlanId = vlan_id
@ -259,29 +302,26 @@ class PyVmomiHelper(PyVmomi):
if self.changed: if self.changed:
try: try:
host_system.configManager.networkSystem.UpdatePortGroup(pgName=self.portgroup_name, portgrp=port_group.spec) host_system.configManager.networkSystem.UpdatePortGroup(pgName=self.portgroup_name,
portgrp=port_group.spec)
self.changed = True
except vim.fault.AlreadyExists as e: except vim.fault.AlreadyExists as e:
self.module.fail_json(msg="Failed to update Portgroup as it conflicts with already existing Portgroup: %s" % e.msg) # To match with other vmware_* modules if portgroup
except vim.fault.NotFound as e: # exists, we proceed, as user may want idempotency
self.module.fail_json(msg="Failed to update Portgroup as vSwitch was not found: %s" % e.msg) self.changed = False
except vim.fault.HostConfigFault as e: except vim.fault.NotFound as not_found:
self.module.fail_json(msg="Failed to update Portgroup due to host system configuration failure : %s" % e.msg) self.module.fail_json(msg="Failed to update Portgroup as"
except vmodl.fault.InvalidArgument as e: " vSwitch was not found: %s" % to_native(not_found.msg))
self.module.fail_json(msg="Failed to update Portgroup as VLAN id was not correct as per specifications: %s" % e.msg) except vim.fault.HostConfigFault as host_config_fault:
self.changed = False self.module.fail_json(msg="Failed to update Portgroup due to host"
" system configuration failure : %s" % to_native(host_config_fault.msg))
def add_portgroup(self): except vmodl.fault.InvalidArgument as invalid_argument:
""" self.module.fail_json(msg="Failed to update Portgroup as VLAN id was not"
Function to add portgroup to given cluster or given hostsystem " correct as per specifications: %s" % to_native(invalid_argument.msg))
""" except Exception as generic_exception:
if self.cluster and self.find_cluster_by_name(cluster_name=self.cluster): self.module.fail_json(msg="Failed to update Portgroup due to generic"
hosts = self.get_all_hosts_by_cluster(cluster_name=self.cluster) " exception : %s" % to_native(generic_exception))
self.add_hosts_port_group(hosts=hosts) return self.changed
elif self.hosts:
for host in self.hosts:
host_system = self.find_hostsystem_by_name(host_name=host)
if host_system:
self.add_hosts_port_group([host_system])
def create_network_policy(self): def create_network_policy(self):
""" """
@ -298,28 +338,24 @@ class PyVmomiHelper(PyVmomi):
network_policy = vim.host.NetworkPolicy(security=security_policy) network_policy = vim.host.NetworkPolicy(security=security_policy)
return network_policy return network_policy
# Remove def remove_hosts_port_group(self):
def remove_portgroup(self):
"""
Function to remove portgroup depending upon hostsystem and vswitch name
"""
if self.cluster and self.find_cluster_by_name(cluster_name=self.cluster):
hosts = self.get_all_hosts_by_cluster(cluster_name=self.cluster)
self.remove_hosts_port_group(hosts=hosts)
elif self.hosts:
for host in self.hosts:
host_system = self.find_hostsystem_by_name(host_name=host)
if host_system:
self.remove_hosts_port_group(hosts=[host_system])
def remove_hosts_port_group(self, hosts):
""" """
Function to remove port group from given host Function to remove port group from given host
Args:
hosts: List of host system
""" """
for host in hosts: results = dict(changed=False, result=dict())
self.remove_host_port_group(host_system=host, portgroup_name=self.portgroup_name, vswitch_name=self.switch_name) host_change_list = []
for host in self.host_obj_list:
change = False
results['result'][host.name] = dict(portgroup_name=self.portgroup_name,
vlan_id=self.vlan_id,
switch_name=self.switch_name)
change = self.remove_host_port_group(host_system=host,
portgroup_name=self.portgroup_name,
vswitch_name=self.switch_name)
host_change_list.append(change)
if any(host_change_list):
results['changed'] = True
self.module.exit_json(**results)
def remove_host_port_group(self, host_system, portgroup_name, vswitch_name): def remove_host_port_group(self, host_system, portgroup_name, vswitch_name):
""" """
@ -330,19 +366,27 @@ class PyVmomiHelper(PyVmomi):
vswitch_name: Name of vSwitch vswitch_name: Name of vSwitch
""" """
desired_pgs = self.get_port_group_by_name(host_system=host_system, portgroup_name=portgroup_name, vswitch_name=vswitch_name) changed = False
desired_pgs = self.get_port_group_by_name(host_system=host_system,
portgroup_name=portgroup_name,
vswitch_name=vswitch_name)
if desired_pgs: if desired_pgs:
try: try:
host_system.configManager.networkSystem.RemovePortGroup(pgName=self.portgroup_name) host_system.configManager.networkSystem.RemovePortGroup(pgName=self.portgroup_name)
self.changed = True changed = True
except vim.fault.NotFound as e: except vim.fault.NotFound as not_found:
self.module.fail_json(msg="Failed to remove Portgroup as it was not found: %s" % e.msg) self.module.fail_json(msg="Failed to remove Portgroup as it was"
except vim.fault.ResourceInUse as e: " not found: %s" % to_native(not_found.msg))
self.module.fail_json(msg="Failed to remove Portgroup as it is in use: %s" % e.msg) except vim.fault.ResourceInUse as resource_in_use:
except vim.fault.HostConfigFault as e: self.module.fail_json(msg="Failed to remove Portgroup as it is"
self.module.fail_json(msg="Failed to remove Portgroup due to configuration failures: %s" % e.msg) " in use: %s" % to_native(resource_in_use.msg))
else: except vim.fault.HostConfigFault as host_config_fault:
self.changed = False self.module.fail_json(msg="Failed to remove Portgroup due to configuration"
" failures: %s" % to_native(host_config_fault.msg))
except Exception as generic_exception:
self.module.fail_json(msg="Failed to remove Portgroup due to generic"
" exception : %s" % to_native(generic_exception))
return changed
def main(): def main():
@ -351,7 +395,7 @@ def main():
portgroup_name=dict(required=True, type='str'), portgroup_name=dict(required=True, type='str'),
switch_name=dict(required=True, type='str'), switch_name=dict(required=True, type='str'),
vlan_id=dict(required=True, type='int'), vlan_id=dict(required=True, type='int'),
hosts=dict(type='list'), hosts=dict(type='list', aliases=['esxi_hostname']),
cluster_name=dict(type='str'), cluster_name=dict(type='str'),
state=dict(type='str', choices=['present', 'absent'], default='present'), state=dict(type='str', choices=['present', 'absent'], default='present'),
network_policy=dict(type='dict', network_policy=dict(type='dict',
@ -378,18 +422,13 @@ def main():
try: try:
pyv = PyVmomiHelper(module) pyv = PyVmomiHelper(module)
pyv.process_state() pyv.process_state()
changed = pyv.changed
module.exit_json(changed=changed)
except vmodl.RuntimeFault as runtime_fault: except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg) module.fail_json(msg=to_native(runtime_fault.msg))
except vmodl.MethodFault as method_fault: except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg) module.fail_json(msg=to_native(method_fault.msg))
except Exception as e: except Exception as e:
module.fail_json(msg=str(e)) module.fail_json(msg=to_native(e))
if __name__ == '__main__': if __name__ == '__main__':