Merge pull request #1580 from jcpowermac/vmware_vswitch_fix_20

Resolves issue with vmware_vswitch module for v2.0
This commit is contained in:
Brian Coca 2016-03-01 00:10:02 -05:00
commit 6049368b45

View file

@ -88,76 +88,95 @@ def find_vswitch_by_name(host, vswitch_name):
return None
class VMwareHostVirtualSwitch(object):
def __init__(self, module):
self.host_system = None
self.content = None
self.vss = None
self.module = module
self.switch_name = module.params['switch_name']
self.number_of_ports = module.params['number_of_ports']
self.nic_name = module.params['nic_name']
self.mtu = module.params['mtu']
self.state = module.params['state']
self.content = connect_to_api(self.module)
def process_state(self):
try:
vswitch_states = {
'absent': {
'present': self.state_destroy_vswitch,
'absent': self.state_exit_unchanged,
},
'present': {
'update': self.state_update_vswitch,
'present': self.state_exit_unchanged,
'absent': self.state_create_vswitch,
}
}
vswitch_states[self.state][self.check_vswitch_configuration()]()
except vmodl.RuntimeFault as runtime_fault:
self.module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault:
self.module.fail_json(msg=method_fault.msg)
except Exception as e:
self.module.fail_json(msg=str(e))
# Source from
# https://github.com/rreubenur/pyvmomi-community-samples/blob/patch-1/samples/create_vswitch.py
def state_create_vswitch(module):
switch_name = module.params['switch_name']
number_of_ports = module.params['number_of_ports']
nic_name = module.params['nic_name']
mtu = module.params['mtu']
host = module.params['host']
def state_create_vswitch(self):
vss_spec = vim.host.VirtualSwitch.Specification()
vss_spec.numPorts = number_of_ports
vss_spec.mtu = mtu
vss_spec.bridge = vim.host.VirtualSwitch.BondBridge(nicDevice=[nic_name])
host.configManager.networkSystem.AddVirtualSwitch(vswitchName=switch_name, spec=vss_spec)
module.exit_json(changed=True)
vss_spec.numPorts = self.number_of_ports
vss_spec.mtu = self.mtu
vss_spec.bridge = vim.host.VirtualSwitch.BondBridge(nicDevice=[self.nic_name])
self.host_system.configManager.networkSystem.AddVirtualSwitch(vswitchName=self.switch_name, spec=vss_spec)
self.module.exit_json(changed=True)
def state_exit_unchanged(self):
self.module.exit_json(changed=False)
def state_exit_unchanged(module):
module.exit_json(changed=False)
def state_destroy_vswitch(module):
vss = module.params['vss']
host = module.params['host']
def state_destroy_vswitch(self):
config = vim.host.NetworkConfig()
for portgroup in host.configManager.networkSystem.networkInfo.portgroup:
if portgroup.spec.vswitchName == vss.name:
for portgroup in self.host_system.configManager.networkSystem.networkInfo.portgroup:
if portgroup.spec.vswitchName == self.vss.name:
portgroup_config = vim.host.PortGroup.Config()
portgroup_config.changeOperation = "remove"
portgroup_config.spec = vim.host.PortGroup.Specification()
portgroup_config.spec.name = portgroup.spec.name
portgroup_config.spec.name = portgroup.spec.name
portgroup_config.spec.vlanId = portgroup.spec.vlanId
portgroup_config.spec.vswitchName = portgroup.spec.vswitchName
portgroup_config.spec.policy = vim.host.NetworkPolicy()
config.portgroup.append(portgroup_config)
host.configManager.networkSystem.UpdateNetworkConfig(config, "modify")
host.configManager.networkSystem.RemoveVirtualSwitch(vss.name)
module.exit_json(changed=True)
self.host_system.configManager.networkSystem.UpdateNetworkConfig(config, "modify")
self.host_system.configManager.networkSystem.RemoveVirtualSwitch(self.vss.name)
self.module.exit_json(changed=True)
def state_update_vswitch(self):
self.module.exit_json(changed=False, msg="Currently not implemented.")
def state_update_vswitch(module):
module.exit_json(changed=False, msg="Currently not implemented.")
def check_vswitch_configuration(module):
switch_name = module.params['switch_name']
content = connect_to_api(module)
module.params['content'] = content
host = get_all_objs(content, [vim.HostSystem])
def check_vswitch_configuration(self):
host = get_all_objs(self.content, [vim.HostSystem])
if not host:
module.fail_json(msg="Unble to find host")
self.module.fail_json(msg="Unable to find host")
host_system = host.keys()[0]
module.params['host'] = host_system
vss = find_vswitch_by_name(host_system, switch_name)
self.host_system = host.keys()[0]
self.vss = find_vswitch_by_name(self.host_system, self.switch_name)
if vss is None:
if self.vss is None:
return 'absent'
else:
module.params['vss'] = vss
return 'present'
def main():
argument_spec = vmware_argument_spec()
argument_spec.update(dict(switch_name=dict(required=True, type='str'),
nic_name=dict(required=True, type='str'),
@ -170,27 +189,8 @@ def main():
if not HAS_PYVMOMI:
module.fail_json(msg='pyvmomi is required for this module')
try:
vswitch_states = {
'absent': {
'present': state_destroy_vswitch,
'absent': state_exit_unchanged,
},
'present': {
'update': state_update_vswitch,
'present': state_exit_unchanged,
'absent': state_create_vswitch,
}
}
vswitch_states[module.params['state']][check_vswitch_configuration(module)](module)
except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg)
except Exception as e:
module.fail_json(msg=str(e))
host_virtual_switch = VMwareHostVirtualSwitch(module)
host_virtual_switch.process_state()
from ansible.module_utils.vmware import *
from ansible.module_utils.basic import *