diff --git a/lib/ansible/modules/cloud/vmware/vmware_vswitch.py b/lib/ansible/modules/cloud/vmware/vmware_vswitch.py index 74e0bdfa8d..c5590bdf7b 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_vswitch.py +++ b/lib/ansible/modules/cloud/vmware/vmware_vswitch.py @@ -91,10 +91,12 @@ try: HAS_PYVMOMI = True except ImportError: HAS_PYVMOMI = False +from ansible.module_utils.vmware import vmware_argument_spec, get_all_objs, connect_to_api +from ansible.module_utils.basic import AnsibleModule def find_vswitch_by_name(host, vswitch_name): - for vss in host.config.network.vswitch: + for vss in host.configManager.networkSystem.networkInfo.vswitch: if vss.name == vswitch_name: return vss return None @@ -137,7 +139,6 @@ class VMwareHostVirtualSwitch(object): 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 @@ -180,7 +181,7 @@ class VMwareHostVirtualSwitch(object): if not host: self.module.fail_json(msg="Unable to find host") - self.host_system = host.keys()[0] + self.host_system = list(host.keys())[0] self.vss = find_vswitch_by_name(self.host_system, self.switch_name) if self.vss is None: @@ -192,10 +193,10 @@ class VMwareHostVirtualSwitch(object): def main(): argument_spec = vmware_argument_spec() argument_spec.update(dict(switch_name=dict(required=True, type='str'), - nic_name=dict(required=False, type='str'), - number_of_ports=dict(required=False, type='int', default=128), - mtu=dict(required=False, type='int', default=1500), - state=dict(default='present', choices=['present', 'absent'], type='str'))) + nic_name=dict(required=False, type='str'), + number_of_ports=dict(required=False, type='int', default=128), + mtu=dict(required=False, type='int', default=1500), + state=dict(default='present', choices=['present', 'absent'], type='str'))) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) @@ -205,8 +206,5 @@ def main(): host_virtual_switch = VMwareHostVirtualSwitch(module) host_virtual_switch.process_state() -from ansible.module_utils.vmware import * -from ansible.module_utils.basic import * - if __name__ == '__main__': main() diff --git a/test/integration/targets/vmware_guest_find/tasks/main.yml b/test/integration/targets/vmware_guest_find/tasks/main.yml index d8c29a984b..43ddcd3bc3 100644 --- a/test/integration/targets/vmware_guest_find/tasks/main.yml +++ b/test/integration/targets/vmware_guest_find/tasks/main.yml @@ -1,7 +1,7 @@ - name: make sure pyvmomi is installed pip: name: pyvmomi - state: latest + state: latest - name: store the vcenter container ip set_fact: diff --git a/test/integration/targets/vmware_vswitch/aliases b/test/integration/targets/vmware_vswitch/aliases new file mode 100644 index 0000000000..1c56b8da49 --- /dev/null +++ b/test/integration/targets/vmware_vswitch/aliases @@ -0,0 +1,2 @@ +posix/ci/cloud/vcenter +cloud/vcenter diff --git a/test/integration/targets/vmware_vswitch/tasks/main.yml b/test/integration/targets/vmware_vswitch/tasks/main.yml new file mode 100644 index 0000000000..691361aa44 --- /dev/null +++ b/test/integration/targets/vmware_vswitch/tasks/main.yml @@ -0,0 +1,57 @@ +# Test code for the vmware_vswitch +# (c) 2017, Abhijeet Kasurde + +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# + +- name: make sure pyvmomi is installed + pip: + name: pyvmomi + state: latest + +- name: store the vcenter container ip + set_fact: + vcsim: "{{ lookup('env', 'vcenter_host') }}" +- debug: var=vcsim + +- name: kill vcsim + uri: + url: "{{ 'http://' + vcsim + ':5000/killall' }}" +- name: start vcsim + uri: + url: "{{ 'http://' + vcsim + ':5000/spawn?cluster=2' }}" + register: vcsim_instance + +- debug: var=vcsim_instance + +- name: find folders for each vm + vmware_vswitch: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + switch_name: vmswitch_0001 + nic_name: vnic_1 + register: vswitch + +- debug: var=vswitch + +# Currently, we don't return anything about vswitch, will +# extend this TC once we have something to test more than +# the state of task. +- assert: + that: + - "{{ vswitch.changed == true }}" diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index c0d6d78e8d..371200c1ea 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -203,7 +203,6 @@ lib/ansible/modules/cloud/vmware/vmware_migrate_vmk.py lib/ansible/modules/cloud/vmware/vmware_target_canonical_facts.py lib/ansible/modules/cloud/vmware/vmware_vm_shell.py lib/ansible/modules/cloud/vmware/vmware_vmotion.py -lib/ansible/modules/cloud/vmware/vmware_vswitch.py lib/ansible/modules/cloud/vmware/vsphere_copy.py lib/ansible/modules/cloud/vmware/vsphere_guest.py lib/ansible/modules/cloud/webfaction/webfaction_app.py