1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

VMware: Add folder option for vmware_dvswitch (#56707)

* Added folder param
* Mark datacenter as optional parameter
* Updated docs
* Updated tests

Fixes: #54986

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-05-22 11:40:09 +05:30 committed by GitHub
parent 4065692e13
commit 459776eb82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 21 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- Add folder option in vmware_dvswitch to place distributed switch in a network specific folder (https://github.com/ansible/ansible/issues/54986).

View file

@ -66,7 +66,8 @@ No notable changes
Noteworthy module changes Noteworthy module changes
------------------------- -------------------------
No notable changes * `vmware_dvswitch <vmware_dvswitch_module>` accepts `folder` parameter to place dvswitch in user defined folder.
This option makes `datacenter` as optional parameter.
Plugins Plugins

View file

@ -170,8 +170,8 @@ def find_datastore_by_name(content, datastore_name):
return find_object_by_name(content, datastore_name, [vim.Datastore]) return find_object_by_name(content, datastore_name, [vim.Datastore])
def find_dvs_by_name(content, switch_name): def find_dvs_by_name(content, switch_name, folder=None):
return find_object_by_name(content, switch_name, [vim.DistributedVirtualSwitch]) return find_object_by_name(content, switch_name, [vim.DistributedVirtualSwitch], folder=folder)
def find_hostsystem_by_name(content, hostname): def find_hostsystem_by_name(content, hostname):

View file

@ -37,13 +37,17 @@ options:
datacenter_name: datacenter_name:
description: description:
- The name of the datacenter that will contain the Distributed Switch. - The name of the datacenter that will contain the Distributed Switch.
required: True - This parameter is optional, if C(folder) is provided.
- Mutually exclusive with C(folder) parameter.
required: False
aliases: ['datacenter'] aliases: ['datacenter']
type: str
switch_name: switch_name:
description: description:
- The name of the distribute vSwitch to create or remove. - The name of the distribute vSwitch to create or remove.
required: True required: True
aliases: ['switch', 'dvswitch'] aliases: ['switch', 'dvswitch']
type: str
switch_version: switch_version:
description: description:
- The version of the Distributed Switch to create. - The version of the Distributed Switch to create.
@ -55,6 +59,7 @@ options:
version_added: 2.5 version_added: 2.5
choices: ['5.0.0', '5.1.0', '5.5.0', '6.0.0', '6.5.0', '6.6.0'] choices: ['5.0.0', '5.1.0', '5.5.0', '6.0.0', '6.5.0', '6.6.0']
aliases: ['version'] aliases: ['version']
type: str
mtu: mtu:
description: description:
- The switch maximum transmission unit. - The switch maximum transmission unit.
@ -78,6 +83,7 @@ options:
- The uplink quantity can be increased or decreased, but a decrease will only be successfull if the uplink isn't used by a portgroup. - The uplink quantity can be increased or decreased, but a decrease will only be successfull if the uplink isn't used by a portgroup.
- Required parameter for C(state) both C(present) and C(absent), before Ansible 2.6 version. - Required parameter for C(state) both C(present) and C(absent), before Ansible 2.6 version.
- Required only if C(state) is set to C(present), for Ansible 2.6 and onwards. - Required only if C(state) is set to C(present), for Ansible 2.6 and onwards.
type: int
uplink_prefix: uplink_prefix:
description: description:
- The prefix used for the naming of the uplinks. - The prefix used for the naming of the uplinks.
@ -85,6 +91,7 @@ options:
- Uplinks are created as Uplink 1, Uplink 2, etc. pp. by default. - Uplinks are created as Uplink 1, Uplink 2, etc. pp. by default.
default: 'Uplink ' default: 'Uplink '
version_added: 2.8 version_added: 2.8
type: str
discovery_proto: discovery_proto:
description: description:
- Link discovery protocol between Cisco and Link Layer discovery. - Link discovery protocol between Cisco and Link Layer discovery.
@ -96,6 +103,7 @@ options:
choices: ['cdp', 'lldp', 'disabled'] choices: ['cdp', 'lldp', 'disabled']
default: 'cdp' default: 'cdp'
aliases: [ 'discovery_protocol' ] aliases: [ 'discovery_protocol' ]
type: str
discovery_operation: discovery_operation:
description: description:
- Select the discovery operation. - Select the discovery operation.
@ -103,6 +111,7 @@ options:
- Required only if C(state) is set to C(present), for Ansible 2.6 and onwards. - Required only if C(state) is set to C(present), for Ansible 2.6 and onwards.
choices: ['both', 'advertise', 'listen'] choices: ['both', 'advertise', 'listen']
default: 'listen' default: 'listen'
type: str
contact: contact:
description: description:
- Dictionary which configures administrtor contact name and description for the Distributed Switch. - Dictionary which configures administrtor contact name and description for the Distributed Switch.
@ -140,6 +149,24 @@ options:
- If set to C(absent) and the Distributed Switch exists then the Distributed Switch will be deleted. - If set to C(absent) and the Distributed Switch exists then the Distributed Switch will be deleted.
default: 'present' default: 'present'
choices: ['present', 'absent'] choices: ['present', 'absent']
type: str
folder:
description:
- Destination folder, absolute path to place dvswitch in.
- The folder should include the datacenter.
- This parameter is case sensitive.
- This parameter is optional, if C(datacenter) is provided.
- 'Examples:'
- ' folder: /datacenter1/network'
- ' folder: datacenter1/network'
- ' folder: /datacenter1/network/folder1'
- ' folder: datacenter1/network/folder1'
- ' folder: /folder1/datacenter1/network'
- ' folder: folder1/datacenter1/network'
- ' folder: /folder1/datacenter1/network/folder2'
required: False
type: str
version_added: 2.9
extends_documentation_fragment: vmware.documentation extends_documentation_fragment: vmware.documentation
''' '''
@ -229,7 +256,7 @@ except ImportError:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
from ansible.module_utils.vmware import ( from ansible.module_utils.vmware import (
PyVmomi, find_datacenter_by_name, TaskError, find_dvs_by_name, vmware_argument_spec, wait_for_task PyVmomi, TaskError, find_dvs_by_name, vmware_argument_spec, wait_for_task
) )
@ -238,14 +265,26 @@ class VMwareDvSwitch(PyVmomi):
def __init__(self, module): def __init__(self, module):
super(VMwareDvSwitch, self).__init__(module) super(VMwareDvSwitch, self).__init__(module)
self.dvs = None self.dvs = None
self.datacenter = None
self.switch_name = self.module.params['switch_name'] self.switch_name = self.module.params['switch_name']
self.switch_version = self.module.params['switch_version'] self.switch_version = self.module.params['switch_version']
if self.content.about.version == '6.7.0': if self.content.about.version == '6.7.0':
self.vcenter_switch_version = '6.6.0' self.vcenter_switch_version = '6.6.0'
else: else:
self.vcenter_switch_version = self.content.about.version self.vcenter_switch_version = self.content.about.version
self.datacenter_name = self.module.params['datacenter_name'] folder = self.params['folder']
if folder:
self.folder_obj = self.content.searchIndex.FindByInventoryPath(folder)
if not self.folder_obj:
self.module.fail_json(msg="Failed to find the folder specified by %(folder)s" % self.params)
else:
datacenter_name = self.params.get('datacenter_name')
datacenter_obj = self.find_datacenter_by_name(datacenter_name)
if not datacenter_obj:
self.module.fail_json(msg="Failed to find datacenter '%s' required"
" for managing distributed vSwitch." % datacenter_name)
self.folder_obj = datacenter_obj.networkFolder
self.mtu = self.module.params['mtu'] self.mtu = self.module.params['mtu']
# MTU sanity check # MTU sanity check
if not 1280 <= self.mtu <= 9000: if not 1280 <= self.mtu <= 9000:
@ -295,12 +334,7 @@ class VMwareDvSwitch(PyVmomi):
def check_dvs(self): def check_dvs(self):
"""Check if DVS is present""" """Check if DVS is present"""
datacenter = find_datacenter_by_name(self.content, self.datacenter_name) self.dvs = find_dvs_by_name(self.content, self.switch_name, folder=self.folder_obj)
if datacenter is None:
self.module.fail_json(msg="Failed to find datacenter %s" % self.datacenter_name)
else:
self.datacenter = datacenter
self.dvs = find_dvs_by_name(self.content, self.switch_name)
if self.dvs is None: if self.dvs is None:
return 'absent' return 'absent'
return 'present' return 'present'
@ -346,7 +380,7 @@ class VMwareDvSwitch(PyVmomi):
result = "DVS would be created" result = "DVS would be created"
else: else:
# Create DVS # Create DVS
network_folder = self.datacenter.networkFolder network_folder = self.folder_obj
task = network_folder.CreateDVS_Task(spec) task = network_folder.CreateDVS_Task(spec)
try: try:
wait_for_task(task) wait_for_task(task)
@ -654,7 +688,8 @@ def main():
argument_spec = vmware_argument_spec() argument_spec = vmware_argument_spec()
argument_spec.update( argument_spec.update(
dict( dict(
datacenter_name=dict(required=True, aliases=['datacenter']), datacenter_name=dict(aliases=['datacenter']),
folder=dict(),
switch_name=dict(required=True, aliases=['switch', 'dvswitch']), switch_name=dict(required=True, aliases=['switch', 'dvswitch']),
mtu=dict(type='int', default=1500), mtu=dict(type='int', default=1500),
multicast_filtering_mode=dict(type='str', default='basic', choices=['basic', 'snooping']), multicast_filtering_mode=dict(type='str', default='basic', choices=['basic', 'snooping']),
@ -702,6 +737,12 @@ def main():
('state', 'present', ('state', 'present',
['uplink_quantity']), ['uplink_quantity']),
], ],
required_one_of=[
['folder', 'datacenter_name'],
],
mutually_exclusive=[
['folder', 'datacenter_name'],
],
supports_check_mode=True, supports_check_mode=True,
) )

View file

@ -5,9 +5,8 @@
- import_role: - import_role:
name: prepare_vmware_tests name: prepare_vmware_tests
# Testcase 0001: Add Distributed vSwitch
- &dvs_data - &dvs_data
name: add distributed vSwitch name: Add distributed vSwitch
vmware_dvswitch: vmware_dvswitch:
validate_certs: False validate_certs: False
hostname: "{{ vcenter_hostname }}" hostname: "{{ vcenter_hostname }}"
@ -22,12 +21,31 @@
discovery_operation: both discovery_operation: both
register: dvs_result_0001 register: dvs_result_0001
- name: ensure distributed vswitch is present - name: Ensure distributed vswitch is present
assert: assert:
that: that:
- dvs_result_0001.changed - dvs_result_0001.changed
# Testcase 0002: Add Distributed vSwitch again - name: Add distributed vSwitch using folder
vmware_dvswitch:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "/F0/{{ dc1 }}/network/F0"
state: present
switch_name: dvswitch_0002
mtu: 9000
uplink_quantity: 2
discovery_proto: lldp
discovery_operation: both
register: dvs_result_0002
- name: Ensure distributed vswitch is present
assert:
that:
- dvs_result_0002.changed
# vcsim doesn't support ldp check (self.dvs.config.linkDiscoveryProtocolConfig.protocol) # vcsim doesn't support ldp check (self.dvs.config.linkDiscoveryProtocolConfig.protocol)
- when: vcsim is not defined - when: vcsim is not defined
block: block:
@ -40,7 +58,6 @@
that: that:
- not dvs_result_0002.changed - not dvs_result_0002.changed
# FIXME: Remove this testcase from block once vcsim supports distributed vswitch delete method # FIXME: Remove this testcase from block once vcsim supports distributed vswitch delete method
# Currently, vcsim does not support distributed vswitch delete option, # Currently, vcsim does not support distributed vswitch delete option,
# Once this feature is available we can move this out of this block # Once this feature is available we can move this out of this block