Merge pull request #1580 from jcpowermac/vmware_vswitch_fix_20
Resolves issue with vmware_vswitch module for v2.0
This commit is contained in:
commit
6049368b45
1 changed files with 84 additions and 84 deletions
|
@ -82,82 +82,101 @@ except ImportError:
|
|||
|
||||
|
||||
def find_vswitch_by_name(host, vswitch_name):
|
||||
for vss in host.config.network.vswitch:
|
||||
if vss.name == vswitch_name:
|
||||
return vss
|
||||
return None
|
||||
for vss in host.config.network.vswitch:
|
||||
if vss.name == vswitch_name:
|
||||
return vss
|
||||
return None
|
||||
|
||||
|
||||
# Source from
|
||||
# https://github.com/rreubenur/pyvmomi-community-samples/blob/patch-1/samples/create_vswitch.py
|
||||
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 state_create_vswitch(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,
|
||||
}
|
||||
}
|
||||
|
||||
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']
|
||||
vswitch_states[self.state][self.check_vswitch_configuration()]()
|
||||
|
||||
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)
|
||||
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))
|
||||
|
||||
|
||||
def state_exit_unchanged(module):
|
||||
module.exit_json(changed=False)
|
||||
# Source from
|
||||
# https://github.com/rreubenur/pyvmomi-community-samples/blob/patch-1/samples/create_vswitch.py
|
||||
|
||||
def state_create_vswitch(self):
|
||||
vss_spec = vim.host.VirtualSwitch.Specification()
|
||||
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_destroy_vswitch(module):
|
||||
vss = module.params['vss']
|
||||
host = module.params['host']
|
||||
config = vim.host.NetworkConfig()
|
||||
def state_destroy_vswitch(self):
|
||||
config = vim.host.NetworkConfig()
|
||||
|
||||
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)
|
||||
|
||||
self.host_system.configManager.networkSystem.UpdateNetworkConfig(config, "modify")
|
||||
self.host_system.configManager.networkSystem.RemoveVirtualSwitch(self.vss.name)
|
||||
self.module.exit_json(changed=True)
|
||||
|
||||
for portgroup in host.configManager.networkSystem.networkInfo.portgroup:
|
||||
if portgroup.spec.vswitchName == 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.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)
|
||||
|
||||
|
||||
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])
|
||||
if not host:
|
||||
module.fail_json(msg="Unble to find host")
|
||||
|
||||
host_system = host.keys()[0]
|
||||
module.params['host'] = host_system
|
||||
vss = find_vswitch_by_name(host_system, switch_name)
|
||||
|
||||
if vss is None:
|
||||
return 'absent'
|
||||
else:
|
||||
module.params['vss'] = vss
|
||||
return 'present'
|
||||
def state_update_vswitch(self):
|
||||
self.module.exit_json(changed=False, msg="Currently not implemented.")
|
||||
|
||||
def check_vswitch_configuration(self):
|
||||
host = get_all_objs(self.content, [vim.HostSystem])
|
||||
if not host:
|
||||
self.module.fail_json(msg="Unable to find host")
|
||||
|
||||
self.host_system = host.keys()[0]
|
||||
self.vss = find_vswitch_by_name(self.host_system, self.switch_name)
|
||||
|
||||
if self.vss is None:
|
||||
return 'absent'
|
||||
else:
|
||||
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 *
|
||||
|
|
Loading…
Reference in a new issue