We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
vmware_migrate_vmk: tests fail with 7.0.3
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY

I get this failure:

```
TASK [vmware_migrate_vmk : Create a new vmkernel] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [testhost]: FAILED! => {"changed": false, "msg": "Failed to add vmk as IP address or Subnet Mask in the IP configuration are invalid or PortGroup does not exist : A specified parameter was not correct: Vim.Host.VirtualNic.Specification.Ip"}
```##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->


##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```paste below

```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `plugins/modules/vmware_migrate_vmk.py`
Content:
```
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 # Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7 
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10 
11 
12 DOCUMENTATION = r'''
13 ---
14 module: vmware_migrate_vmk
15 short_description: Migrate a VMK interface from VSS to VDS
16 description:
17     - Migrate a VMK interface from VSS to VDS
18 author:
19 - Joseph Callen (@jcpowermac)
20 - Russell Teague (@mtnbikenc)
21 options:
22     esxi_hostname:
23         description:
24             - ESXi hostname to be managed
25         required: true
26         type: str
27     device:
28         description:
29             - VMK interface name
30         required: true
31         type: str
32     current_switch_name:
33         description:
34             - Switch VMK interface is currently on
35         required: true
36         type: str
37     current_portgroup_name:
38         description:
39             - Portgroup name VMK interface is currently on
40         required: true
41         type: str
42     migrate_switch_name:
43         description:
44             - Switch name to migrate VMK interface to
45         required: true
46         type: str
47     migrate_portgroup_name:
48         description:
49             - Portgroup name to migrate VMK interface to
50         required: true
51         type: str
52     migrate_vlan_id:
53         version_added: '2.4.0'
54         description:
55             - VLAN to use for the VMK interface when migrating from VDS to VSS
56             - Will be ignored when migrating from VSS to VDS
57         type: int
58 extends_documentation_fragment:
59 - community.vmware.vmware.documentation
60 
61 '''
62 
63 EXAMPLES = r'''
64 - name: Migrate Management vmk
65   community.vmware.vmware_migrate_vmk:
66     hostname: "{{ vcenter_hostname }}"
67     username: "{{ vcenter_username }}"
68     password: "{{ vcenter_password }}"
69     esxi_hostname: "{{ esxi_hostname }}"
70     device: vmk1
71     current_switch_name: temp_vswitch
72     current_portgroup_name: esx-mgmt
73     migrate_switch_name: dvSwitch
74     migrate_portgroup_name: Management
75   delegate_to: localhost
76 '''
77 try:
78     from pyVmomi import vim, vmodl
79     HAS_PYVMOMI = True
80 except ImportError:
81     HAS_PYVMOMI = False
82 
83 from ansible.module_utils.basic import AnsibleModule
84 from ansible_collections.community.vmware.plugins.module_utils.vmware import (
85     vmware_argument_spec, find_dvs_by_name, find_hostsystem_by_name,
86     connect_to_api, find_dvspg_by_name)
87 
88 
89 class VMwareMigrateVmk(object):
90 
91     def __init__(self, module):
92         self.module = module
93         self.host_system = None
94         self.migrate_switch_name = self.module.params['migrate_switch_name']
95         self.migrate_portgroup_name = self.module.params['migrate_portgroup_name']
96         self.migrate_vlan_id = self.module.params['migrate_vlan_id']
97         self.device = self.module.params['device']
98         self.esxi_hostname = self.module.params['esxi_hostname']
99         self.current_portgroup_name = self.module.params['current_portgroup_name']
100         self.current_switch_name = self.module.params['current_switch_name']
101         self.content = connect_to_api(module)
102 
103     def process_state(self):
104         try:
105             vmk_migration_states = {
106                 'migrate_vss_vds': self.state_migrate_vss_vds,
107                 'migrate_vds_vss': self.state_migrate_vds_vss,
108                 'migrated': self.state_exit_unchanged
109             }
110 
111             vmk_migration_states[self.check_vmk_current_state()]()
112 
113         except vmodl.RuntimeFault as runtime_fault:
114             self.module.fail_json(msg=runtime_fault.msg)
115         except vmodl.MethodFault as method_fault:
116             self.module.fail_json(msg=method_fault.msg)
117         except Exception as e:
118             self.module.fail_json(msg=str(e))
119 
120     def state_exit_unchanged(self):
121         self.module.exit_json(changed=False)
122 
123     def create_host_vnic_config_vds_vss(self):
124         host_vnic_config = vim.host.VirtualNic.Config()
125         host_vnic_config.spec = vim.host.VirtualNic.Specification()
126         host_vnic_config.changeOperation = "edit"
127         host_vnic_config.device = self.device
128         host_vnic_config.spec.portgroup = self.migrate_portgroup_name
129         return host_vnic_config
130 
131     def create_port_group_config_vds_vss(self):
132         port_group_config = vim.host.PortGroup.Config()
133         port_group_config.spec = vim.host.PortGroup.Specification()
134         port_group_config.changeOperation = "add"
135         port_group_config.spec.name = self.migrate_portgroup_name
136         port_group_config.spec.vlanId = self.migrate_vlan_id if self.migrate_vlan_id is not None else 0
137         port_group_config.spec.vswitchName = self.migrate_switch_name
138         port_group_config.spec.policy = vim.host.NetworkPolicy()
139         return port_group_config
140 
141     def state_migrate_vds_vss(self):
142         host_network_system = self.host_system.configManager.networkSystem
143         config = vim.host.NetworkConfig()
144         config.portgroup = [self.create_port_group_config_vds_vss()]
145         host_network_system.UpdateNetworkConfig(config, "modify")
146         config = vim.host.NetworkConfig()
147         config.vnic = [self.create_host_vnic_config_vds_vss()]
148         host_network_system.UpdateNetworkConfig(config, "modify")
149         self.module.exit_json(changed=True)
150 
151     def create_host_vnic_config(self, dv_switch_uuid, portgroup_key):
152         host_vnic_config = vim.host.VirtualNic.Config()
153         host_vnic_config.spec = vim.host.VirtualNic.Specification()
154 
155         host_vnic_config.changeOperation = "edit"
156         host_vnic_config.device = self.device
157         host_vnic_config.portgroup = ""
158         host_vnic_config.spec.distributedVirtualPort = vim.dvs.PortConnection()
159         host_vnic_config.spec.distributedVirtualPort.switchUuid = dv_switch_uuid
160         host_vnic_config.spec.distributedVirtualPort.portgroupKey = portgroup_key
161 
162         return host_vnic_config
163 
164     def create_port_group_config(self):
165         port_group_config = vim.host.PortGroup.Config()
166         port_group_config.spec = vim.host.PortGroup.Specification()
167 
168         port_group_config.changeOperation = "remove"
169         port_group_config.spec.name = self.current_portgroup_name
170         port_group_config.spec.vlanId = -1
171         port_group_config.spec.vswitchName = self.current_switch_name
172         port_group_config.spec.policy = vim.host.NetworkPolicy()
173 
174         return port_group_config
175 
176     def state_migrate_vss_vds(self):
177         host_network_system = self.host_system.configManager.networkSystem
178 
179         dv_switch = find_dvs_by_name(self.content, self.migrate_switch_name)
180         pg = find_dvspg_by_name(dv_switch, self.migrate_portgroup_name)
181 
182         config = vim.host.NetworkConfig()
183         config.portgroup = [self.create_port_group_config()]
184         config.vnic = [self.create_host_vnic_config(dv_switch.uuid, pg.key)]
185         host_network_system.UpdateNetworkConfig(config, "modify")
186         self.module.exit_json(changed=True)
187 
188     def check_vmk_current_state(self):
189         self.host_system = find_hostsystem_by_name(self.content, self.esxi_hostname)
190 
191         for vnic in self.host_system.configManager.networkSystem.networkInfo.vnic:
192             if vnic.device == self.device:
193                 if vnic.spec.distributedVirtualPort is None:
194                     std_vswitches = [vswitch.name for vswitch in self.host_system.configManager.networkSystem.networkInfo.vswitch]
195                     if self.current_switch_name not in std_vswitches:
196                         return "migrated"
197                     if vnic.portgroup == self.current_portgroup_name:
198                         return "migrate_vss_vds"
199                 else:
200                     dvs = find_dvs_by_name(self.content, self.current_switch_name)
201                     if dvs is None:
202                         return "migrated"
203                     if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid:
204                         return "migrate_vds_vss"
205 
206 
207 def main():
208 
209     argument_spec = vmware_argument_spec()
210     argument_spec.update(dict(esxi_hostname=dict(required=True, type='str'),
211                               device=dict(required=True, type='str'),
212                               current_switch_name=dict(required=True, type='str'),
213                               current_portgroup_name=dict(required=True, type='str'),
214                               migrate_switch_name=dict(required=True, type='str'),
215                               migrate_portgroup_name=dict(required=True, type='str'),
216                               migrate_vlan_id=dict(required=False, type='int')))
217 
218     module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
219 
220     if not HAS_PYVMOMI:
221         module.fail_json(msg='pyvmomi required for this module')
222 
223     vmware_migrate_vmk = VMwareMigrateVmk(module)
224     vmware_migrate_vmk.process_state()
225 
226 
227 if __name__ == '__main__':
228     main()
229
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/plugins/modules/vmware_migrate_vmk.py b/plugins/modules/vmware_migrate_vmk.py
--- a/plugins/modules/vmware_migrate_vmk.py
+++ b/plugins/modules/vmware_migrate_vmk.py
@@ -203,6 +203,8 @@
                     if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid:
                         return "migrate_vds_vss"
 
+        self.module.fail_json(msg='Unable to find the specified device %s.' % self.device)
+
 
 def main():

{"golden_diff": "diff --git a/plugins/modules/vmware_migrate_vmk.py b/plugins/modules/vmware_migrate_vmk.py\n--- a/plugins/modules/vmware_migrate_vmk.py\n+++ b/plugins/modules/vmware_migrate_vmk.py\n@@ -203,6 +203,8 @@\n                     if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid:\n                         return \"migrate_vds_vss\"\n \n+        self.module.fail_json(msg='Unable to find the specified device %s.' % self.device)\n+\n \n def main():\n", "issue": "vmware_migrate_vmk: tests fail with 7.0.3\n<!--- Verify first that your issue is not already reported on GitHub -->\r\n<!--- Also test if the latest release and devel branch are affected too -->\r\n<!--- Complete *all* sections as described, this form is processed automatically -->\r\n\r\n##### SUMMARY\r\n\r\nI get this failure:\r\n\r\n```\r\nTASK [vmware_migrate_vmk : Create a new vmkernel] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************\r\nfatal: [testhost]: FAILED! => {\"changed\": false, \"msg\": \"Failed to add vmk as IP address or Subnet Mask in the IP configuration are invalid or PortGroup does not exist : A specified parameter was not correct: Vim.Host.VirtualNic.Specification.Ip\"}\r\n```##### EXPECTED RESULTS\r\n<!--- Describe what you expected to happen when running the steps above -->\r\n\r\n\r\n##### ACTUAL RESULTS\r\n<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->\r\n\r\n<!--- Paste verbatim command output between quotes -->\r\n```paste below\r\n\r\n```\r\n\n", "before_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>\n# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)\n# SPDX-License-Identifier: GPL-3.0-or-later\n\nfrom __future__ import absolute_import, division, print_function\n__metaclass__ = type\n\n\nDOCUMENTATION = r'''\n---\nmodule: vmware_migrate_vmk\nshort_description: Migrate a VMK interface from VSS to VDS\ndescription:\n    - Migrate a VMK interface from VSS to VDS\nauthor:\n- Joseph Callen (@jcpowermac)\n- Russell Teague (@mtnbikenc)\noptions:\n    esxi_hostname:\n        description:\n            - ESXi hostname to be managed\n        required: true\n        type: str\n    device:\n        description:\n            - VMK interface name\n        required: true\n        type: str\n    current_switch_name:\n        description:\n            - Switch VMK interface is currently on\n        required: true\n        type: str\n    current_portgroup_name:\n        description:\n            - Portgroup name VMK interface is currently on\n        required: true\n        type: str\n    migrate_switch_name:\n        description:\n            - Switch name to migrate VMK interface to\n        required: true\n        type: str\n    migrate_portgroup_name:\n        description:\n            - Portgroup name to migrate VMK interface to\n        required: true\n        type: str\n    migrate_vlan_id:\n        version_added: '2.4.0'\n        description:\n            - VLAN to use for the VMK interface when migrating from VDS to VSS\n            - Will be ignored when migrating from VSS to VDS\n        type: int\nextends_documentation_fragment:\n- community.vmware.vmware.documentation\n\n'''\n\nEXAMPLES = r'''\n- name: Migrate Management vmk\n  community.vmware.vmware_migrate_vmk:\n    hostname: \"{{ vcenter_hostname }}\"\n    username: \"{{ vcenter_username }}\"\n    password: \"{{ vcenter_password }}\"\n    esxi_hostname: \"{{ esxi_hostname }}\"\n    device: vmk1\n    current_switch_name: temp_vswitch\n    current_portgroup_name: esx-mgmt\n    migrate_switch_name: dvSwitch\n    migrate_portgroup_name: Management\n  delegate_to: localhost\n'''\ntry:\n    from pyVmomi import vim, vmodl\n    HAS_PYVMOMI = True\nexcept ImportError:\n    HAS_PYVMOMI = False\n\nfrom ansible.module_utils.basic import AnsibleModule\nfrom ansible_collections.community.vmware.plugins.module_utils.vmware import (\n    vmware_argument_spec, find_dvs_by_name, find_hostsystem_by_name,\n    connect_to_api, find_dvspg_by_name)\n\n\nclass VMwareMigrateVmk(object):\n\n    def __init__(self, module):\n        self.module = module\n        self.host_system = None\n        self.migrate_switch_name = self.module.params['migrate_switch_name']\n        self.migrate_portgroup_name = self.module.params['migrate_portgroup_name']\n        self.migrate_vlan_id = self.module.params['migrate_vlan_id']\n        self.device = self.module.params['device']\n        self.esxi_hostname = self.module.params['esxi_hostname']\n        self.current_portgroup_name = self.module.params['current_portgroup_name']\n        self.current_switch_name = self.module.params['current_switch_name']\n        self.content = connect_to_api(module)\n\n    def process_state(self):\n        try:\n            vmk_migration_states = {\n                'migrate_vss_vds': self.state_migrate_vss_vds,\n                'migrate_vds_vss': self.state_migrate_vds_vss,\n                'migrated': self.state_exit_unchanged\n            }\n\n            vmk_migration_states[self.check_vmk_current_state()]()\n\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n        except Exception as e:\n            self.module.fail_json(msg=str(e))\n\n    def state_exit_unchanged(self):\n        self.module.exit_json(changed=False)\n\n    def create_host_vnic_config_vds_vss(self):\n        host_vnic_config = vim.host.VirtualNic.Config()\n        host_vnic_config.spec = vim.host.VirtualNic.Specification()\n        host_vnic_config.changeOperation = \"edit\"\n        host_vnic_config.device = self.device\n        host_vnic_config.spec.portgroup = self.migrate_portgroup_name\n        return host_vnic_config\n\n    def create_port_group_config_vds_vss(self):\n        port_group_config = vim.host.PortGroup.Config()\n        port_group_config.spec = vim.host.PortGroup.Specification()\n        port_group_config.changeOperation = \"add\"\n        port_group_config.spec.name = self.migrate_portgroup_name\n        port_group_config.spec.vlanId = self.migrate_vlan_id if self.migrate_vlan_id is not None else 0\n        port_group_config.spec.vswitchName = self.migrate_switch_name\n        port_group_config.spec.policy = vim.host.NetworkPolicy()\n        return port_group_config\n\n    def state_migrate_vds_vss(self):\n        host_network_system = self.host_system.configManager.networkSystem\n        config = vim.host.NetworkConfig()\n        config.portgroup = [self.create_port_group_config_vds_vss()]\n        host_network_system.UpdateNetworkConfig(config, \"modify\")\n        config = vim.host.NetworkConfig()\n        config.vnic = [self.create_host_vnic_config_vds_vss()]\n        host_network_system.UpdateNetworkConfig(config, \"modify\")\n        self.module.exit_json(changed=True)\n\n    def create_host_vnic_config(self, dv_switch_uuid, portgroup_key):\n        host_vnic_config = vim.host.VirtualNic.Config()\n        host_vnic_config.spec = vim.host.VirtualNic.Specification()\n\n        host_vnic_config.changeOperation = \"edit\"\n        host_vnic_config.device = self.device\n        host_vnic_config.portgroup = \"\"\n        host_vnic_config.spec.distributedVirtualPort = vim.dvs.PortConnection()\n        host_vnic_config.spec.distributedVirtualPort.switchUuid = dv_switch_uuid\n        host_vnic_config.spec.distributedVirtualPort.portgroupKey = portgroup_key\n\n        return host_vnic_config\n\n    def create_port_group_config(self):\n        port_group_config = vim.host.PortGroup.Config()\n        port_group_config.spec = vim.host.PortGroup.Specification()\n\n        port_group_config.changeOperation = \"remove\"\n        port_group_config.spec.name = self.current_portgroup_name\n        port_group_config.spec.vlanId = -1\n        port_group_config.spec.vswitchName = self.current_switch_name\n        port_group_config.spec.policy = vim.host.NetworkPolicy()\n\n        return port_group_config\n\n    def state_migrate_vss_vds(self):\n        host_network_system = self.host_system.configManager.networkSystem\n\n        dv_switch = find_dvs_by_name(self.content, self.migrate_switch_name)\n        pg = find_dvspg_by_name(dv_switch, self.migrate_portgroup_name)\n\n        config = vim.host.NetworkConfig()\n        config.portgroup = [self.create_port_group_config()]\n        config.vnic = [self.create_host_vnic_config(dv_switch.uuid, pg.key)]\n        host_network_system.UpdateNetworkConfig(config, \"modify\")\n        self.module.exit_json(changed=True)\n\n    def check_vmk_current_state(self):\n        self.host_system = find_hostsystem_by_name(self.content, self.esxi_hostname)\n\n        for vnic in self.host_system.configManager.networkSystem.networkInfo.vnic:\n            if vnic.device == self.device:\n                if vnic.spec.distributedVirtualPort is None:\n                    std_vswitches = [vswitch.name for vswitch in self.host_system.configManager.networkSystem.networkInfo.vswitch]\n                    if self.current_switch_name not in std_vswitches:\n                        return \"migrated\"\n                    if vnic.portgroup == self.current_portgroup_name:\n                        return \"migrate_vss_vds\"\n                else:\n                    dvs = find_dvs_by_name(self.content, self.current_switch_name)\n                    if dvs is None:\n                        return \"migrated\"\n                    if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid:\n                        return \"migrate_vds_vss\"\n\n\ndef main():\n\n    argument_spec = vmware_argument_spec()\n    argument_spec.update(dict(esxi_hostname=dict(required=True, type='str'),\n                              device=dict(required=True, type='str'),\n                              current_switch_name=dict(required=True, type='str'),\n                              current_portgroup_name=dict(required=True, type='str'),\n                              migrate_switch_name=dict(required=True, type='str'),\n                              migrate_portgroup_name=dict(required=True, type='str'),\n                              migrate_vlan_id=dict(required=False, type='int')))\n\n    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)\n\n    if not HAS_PYVMOMI:\n        module.fail_json(msg='pyvmomi required for this module')\n\n    vmware_migrate_vmk = VMwareMigrateVmk(module)\n    vmware_migrate_vmk.process_state()\n\n\nif __name__ == '__main__':\n    main()\n", "path": "plugins/modules/vmware_migrate_vmk.py"}], "after_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>\n# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)\n# SPDX-License-Identifier: GPL-3.0-or-later\n\nfrom __future__ import absolute_import, division, print_function\n__metaclass__ = type\n\n\nDOCUMENTATION = r'''\n---\nmodule: vmware_migrate_vmk\nshort_description: Migrate a VMK interface from VSS to VDS\ndescription:\n    - Migrate a VMK interface from VSS to VDS\nauthor:\n- Joseph Callen (@jcpowermac)\n- Russell Teague (@mtnbikenc)\noptions:\n    esxi_hostname:\n        description:\n            - ESXi hostname to be managed\n        required: true\n        type: str\n    device:\n        description:\n            - VMK interface name\n        required: true\n        type: str\n    current_switch_name:\n        description:\n            - Switch VMK interface is currently on\n        required: true\n        type: str\n    current_portgroup_name:\n        description:\n            - Portgroup name VMK interface is currently on\n        required: true\n        type: str\n    migrate_switch_name:\n        description:\n            - Switch name to migrate VMK interface to\n        required: true\n        type: str\n    migrate_portgroup_name:\n        description:\n            - Portgroup name to migrate VMK interface to\n        required: true\n        type: str\n    migrate_vlan_id:\n        version_added: '2.4.0'\n        description:\n            - VLAN to use for the VMK interface when migrating from VDS to VSS\n            - Will be ignored when migrating from VSS to VDS\n        type: int\nextends_documentation_fragment:\n- community.vmware.vmware.documentation\n\n'''\n\nEXAMPLES = r'''\n- name: Migrate Management vmk\n  community.vmware.vmware_migrate_vmk:\n    hostname: \"{{ vcenter_hostname }}\"\n    username: \"{{ vcenter_username }}\"\n    password: \"{{ vcenter_password }}\"\n    esxi_hostname: \"{{ esxi_hostname }}\"\n    device: vmk1\n    current_switch_name: temp_vswitch\n    current_portgroup_name: esx-mgmt\n    migrate_switch_name: dvSwitch\n    migrate_portgroup_name: Management\n  delegate_to: localhost\n'''\ntry:\n    from pyVmomi import vim, vmodl\n    HAS_PYVMOMI = True\nexcept ImportError:\n    HAS_PYVMOMI = False\n\nfrom ansible.module_utils.basic import AnsibleModule\nfrom ansible_collections.community.vmware.plugins.module_utils.vmware import (\n    vmware_argument_spec, find_dvs_by_name, find_hostsystem_by_name,\n    connect_to_api, find_dvspg_by_name)\n\n\nclass VMwareMigrateVmk(object):\n\n    def __init__(self, module):\n        self.module = module\n        self.host_system = None\n        self.migrate_switch_name = self.module.params['migrate_switch_name']\n        self.migrate_portgroup_name = self.module.params['migrate_portgroup_name']\n        self.migrate_vlan_id = self.module.params['migrate_vlan_id']\n        self.device = self.module.params['device']\n        self.esxi_hostname = self.module.params['esxi_hostname']\n        self.current_portgroup_name = self.module.params['current_portgroup_name']\n        self.current_switch_name = self.module.params['current_switch_name']\n        self.content = connect_to_api(module)\n\n    def process_state(self):\n        try:\n            vmk_migration_states = {\n                'migrate_vss_vds': self.state_migrate_vss_vds,\n                'migrate_vds_vss': self.state_migrate_vds_vss,\n                'migrated': self.state_exit_unchanged\n            }\n\n            vmk_migration_states[self.check_vmk_current_state()]()\n\n        except vmodl.RuntimeFault as runtime_fault:\n            self.module.fail_json(msg=runtime_fault.msg)\n        except vmodl.MethodFault as method_fault:\n            self.module.fail_json(msg=method_fault.msg)\n        except Exception as e:\n            self.module.fail_json(msg=str(e))\n\n    def state_exit_unchanged(self):\n        self.module.exit_json(changed=False)\n\n    def create_host_vnic_config_vds_vss(self):\n        host_vnic_config = vim.host.VirtualNic.Config()\n        host_vnic_config.spec = vim.host.VirtualNic.Specification()\n        host_vnic_config.changeOperation = \"edit\"\n        host_vnic_config.device = self.device\n        host_vnic_config.spec.portgroup = self.migrate_portgroup_name\n        return host_vnic_config\n\n    def create_port_group_config_vds_vss(self):\n        port_group_config = vim.host.PortGroup.Config()\n        port_group_config.spec = vim.host.PortGroup.Specification()\n        port_group_config.changeOperation = \"add\"\n        port_group_config.spec.name = self.migrate_portgroup_name\n        port_group_config.spec.vlanId = self.migrate_vlan_id if self.migrate_vlan_id is not None else 0\n        port_group_config.spec.vswitchName = self.migrate_switch_name\n        port_group_config.spec.policy = vim.host.NetworkPolicy()\n        return port_group_config\n\n    def state_migrate_vds_vss(self):\n        host_network_system = self.host_system.configManager.networkSystem\n        config = vim.host.NetworkConfig()\n        config.portgroup = [self.create_port_group_config_vds_vss()]\n        host_network_system.UpdateNetworkConfig(config, \"modify\")\n        config = vim.host.NetworkConfig()\n        config.vnic = [self.create_host_vnic_config_vds_vss()]\n        host_network_system.UpdateNetworkConfig(config, \"modify\")\n        self.module.exit_json(changed=True)\n\n    def create_host_vnic_config(self, dv_switch_uuid, portgroup_key):\n        host_vnic_config = vim.host.VirtualNic.Config()\n        host_vnic_config.spec = vim.host.VirtualNic.Specification()\n\n        host_vnic_config.changeOperation = \"edit\"\n        host_vnic_config.device = self.device\n        host_vnic_config.portgroup = \"\"\n        host_vnic_config.spec.distributedVirtualPort = vim.dvs.PortConnection()\n        host_vnic_config.spec.distributedVirtualPort.switchUuid = dv_switch_uuid\n        host_vnic_config.spec.distributedVirtualPort.portgroupKey = portgroup_key\n\n        return host_vnic_config\n\n    def create_port_group_config(self):\n        port_group_config = vim.host.PortGroup.Config()\n        port_group_config.spec = vim.host.PortGroup.Specification()\n\n        port_group_config.changeOperation = \"remove\"\n        port_group_config.spec.name = self.current_portgroup_name\n        port_group_config.spec.vlanId = -1\n        port_group_config.spec.vswitchName = self.current_switch_name\n        port_group_config.spec.policy = vim.host.NetworkPolicy()\n\n        return port_group_config\n\n    def state_migrate_vss_vds(self):\n        host_network_system = self.host_system.configManager.networkSystem\n\n        dv_switch = find_dvs_by_name(self.content, self.migrate_switch_name)\n        pg = find_dvspg_by_name(dv_switch, self.migrate_portgroup_name)\n\n        config = vim.host.NetworkConfig()\n        config.portgroup = [self.create_port_group_config()]\n        config.vnic = [self.create_host_vnic_config(dv_switch.uuid, pg.key)]\n        host_network_system.UpdateNetworkConfig(config, \"modify\")\n        self.module.exit_json(changed=True)\n\n    def check_vmk_current_state(self):\n        self.host_system = find_hostsystem_by_name(self.content, self.esxi_hostname)\n\n        for vnic in self.host_system.configManager.networkSystem.networkInfo.vnic:\n            if vnic.device == self.device:\n                if vnic.spec.distributedVirtualPort is None:\n                    std_vswitches = [vswitch.name for vswitch in self.host_system.configManager.networkSystem.networkInfo.vswitch]\n                    if self.current_switch_name not in std_vswitches:\n                        return \"migrated\"\n                    if vnic.portgroup == self.current_portgroup_name:\n                        return \"migrate_vss_vds\"\n                else:\n                    dvs = find_dvs_by_name(self.content, self.current_switch_name)\n                    if dvs is None:\n                        return \"migrated\"\n                    if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid:\n                        return \"migrate_vds_vss\"\n\n        self.module.fail_json(msg='Unable to find the specified device %s.' % self.device)\n\n\ndef main():\n\n    argument_spec = vmware_argument_spec()\n    argument_spec.update(dict(esxi_hostname=dict(required=True, type='str'),\n                              device=dict(required=True, type='str'),\n                              current_switch_name=dict(required=True, type='str'),\n                              current_portgroup_name=dict(required=True, type='str'),\n                              migrate_switch_name=dict(required=True, type='str'),\n                              migrate_portgroup_name=dict(required=True, type='str'),\n                              migrate_vlan_id=dict(required=False, type='int')))\n\n    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)\n\n    if not HAS_PYVMOMI:\n        module.fail_json(msg='pyvmomi required for this module')\n\n    vmware_migrate_vmk = VMwareMigrateVmk(module)\n    vmware_migrate_vmk.process_state()\n\n\nif __name__ == '__main__':\n    main()\n", "path": "plugins/modules/vmware_migrate_vmk.py"}]}