Repository: ansible-collections/community.vmware

Issue:
vmware_portgroup throws an error when disabling traffic_shaping
##### SUMMARY

If I use the vmware_portgroup plugin to disable traffic_shaping like so:

```
- name: Disable traffic shaping
  community.vmware.vmware_portgroup:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    esxi_hostname: "{{ inventory_hostname }}"
    switch: "{{ vswitch_name }}"
    portgroup: "{{ portgroup_name }}"
    traffic_shaping:
        enabled: False
```

The plugin throws the error:
```
fatal: [myhostname]: FAILED! => {"changed": false, "msg": "traffic_shaping.average_bandwidth is a required parameter if traffic_shaping is enabled."}
```

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
vmware_portgroup

##### ANSIBLE VERSION
```
ansible 2.10.2
  config file = /Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg
  configured module search path = ['/Users/eruby/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/eruby/.pyenv/versions/3.7.4/lib/python3.7/site-packages/ansible
  executable location = /Users/eruby/.pyenv/versions/3.7.4/bin/ansible
  python version = 3.7.4 (default, Oct 30 2019, 17:03:08) [Clang 11.0.0 (clang-1100.0.33.8)]
```

##### COLLECTION VERSION
```
# /Users/eruby/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.vmware 1.12.0
```

##### CONFIGURATION
```
ANSIBLE_PIPELINING(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = True
ANSIBLE_SSH_ARGS(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = -F ansible_ssh_config
CACHE_PLUGIN(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = /tmp/$USER/
CACHE_PLUGIN_TIMEOUT(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = 300
DEFAULT_BECOME(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = True
DEFAULT_BECOME_USER(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = root
DEFAULT_FORKS(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = 60
DEFAULT_HOST_LIST(env: ANSIBLE_INVENTORY) = ['/Users/eruby/Projects/bitfusion/bitfusionio/infrastructure/ansible/inventory']
DEFAULT_PRIVATE_KEY_FILE(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = /Users/eruby/.ssh/id_rsa_ansible_bitfusion
DEFAULT_REMOTE_USER(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = ansible
HOST_KEY_CHECKING(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = False
INTERPRETER_PYTHON(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = /usr/bin/python3
INVENTORY_ENABLED(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = ['host_list', 'script', 'auto', 'yaml', 'ini', 'toml', 'amaz
LOCALHOST_WARNING(/Users/eruby/Projects/bitfusion/infrastructure/ansible/ansible.cfg) = False
```

##### OS / ENVIRONMENT
Target OS:
VMware ESXi, 7.0.1, 17551050
vCenter 7.0.2, 17958471

##### STEPS TO REPRODUCE
Add a play to disable traffic shaping:

```yaml
- name: Disable traffic shaping
  community.vmware.vmware_portgroup:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    esxi_hostname: "{{ inventory_hostname }}"
    switch: "{{ vswitch_name }}"
    portgroup: "{{ portgroup_name }}"
    traffic_shaping:
        enabled: False
```

##### EXPECTED RESULTS
Traffic shaping disabled.

##### ACTUAL RESULTS

The plugin throws the error:
```
fatal: [myhostname]: FAILED! => {"changed": false, "msg": "traffic_shaping.average_bandwidth is a required parameter if traffic_shaping is enabled."}
```
Parameters to set traffic shaping should not be required if traffic shaping is disabled. 



Discussion:

Resolving patch:
diff --git a/plugins/modules/vmware_portgroup.py b/plugins/modules/vmware_portgroup.py
--- a/plugins/modules/vmware_portgroup.py
+++ b/plugins/modules/vmware_portgroup.py
@@ -296,12 +296,13 @@ def __init__(self, module):
             self.sec_mac_changes = None
         if self.params['traffic_shaping']:
             self.ts_enabled = self.params['traffic_shaping'].get('enabled')
-            for value in ['average_bandwidth', 'peak_bandwidth', 'burst_size']:
-                if not self.params['traffic_shaping'].get(value):
-                    self.module.fail_json(msg="traffic_shaping.%s is a required parameter if traffic_shaping is enabled." % value)
-            self.ts_average_bandwidth = self.params['traffic_shaping'].get('average_bandwidth')
-            self.ts_peak_bandwidth = self.params['traffic_shaping'].get('peak_bandwidth')
-            self.ts_burst_size = self.params['traffic_shaping'].get('burst_size')
+            if self.ts_enabled is True:
+                for value in ['average_bandwidth', 'peak_bandwidth', 'burst_size']:
+                    if not self.params['traffic_shaping'].get(value):
+                        self.module.fail_json(msg="traffic_shaping.%s is a required parameter if traffic_shaping is enabled." % value)
+                self.ts_average_bandwidth = self.params['traffic_shaping'].get('average_bandwidth')
+                self.ts_peak_bandwidth = self.params['traffic_shaping'].get('peak_bandwidth')
+                self.ts_burst_size = self.params['traffic_shaping'].get('burst_size')
         else:
             self.ts_enabled = None
             self.ts_average_bandwidth = None
@@ -641,11 +642,6 @@ def update_host_port_group(self, host_system, portgroup_object):
                     changed = True
                     changed_list.append("Traffic shaping")
                     host_results['traffic_shaping_previous'] = False
-                elif self.ts_enabled is False:
-                    changed = True
-                    changed_list.append("Traffic shaping")
-                    host_results['traffic_shaping_previous'] = True
-                    spec.policy.shapingPolicy.enabled = False
                 elif self.ts_enabled is None:
                     spec.policy.shapingPolicy = None
                     changed = True


Test patch:
diff --git a/tests/integration/targets/vmware_portgroup/tasks/main.yml b/tests/integration/targets/vmware_portgroup/tasks/main.yml
--- a/tests/integration/targets/vmware_portgroup/tasks/main.yml
+++ b/tests/integration/targets/vmware_portgroup/tasks/main.yml
@@ -72,6 +72,46 @@
         that:
           - pg_info.changed
 
+    - name: Create portgroup with traffic shaping
+      vmware_portgroup:
+        hostname: '{{ vcenter_hostname }}'
+        username: '{{ vcenter_username }}'
+        password: '{{ vcenter_password }}'
+        vswitch: '{{ switch1 }}'
+        portgroup: 'pg_ts'
+        cluster_name: "{{ ccr1 }}"
+        traffic_shaping:
+          enabled: true
+          average_bandwidth: 100000
+          peak_bandwidth: 100000
+          burst_size: 102400
+        validate_certs: false
+        state: present
+      register: create_portgroup_with_traffic_shaping_result
+
+    - assert:
+        that:
+          - create_portgroup_with_traffic_shaping_result.changed
+
+    # Issue 955
+    - name: Disable traffic shaping
+      vmware_portgroup:
+        hostname: '{{ vcenter_hostname }}'
+        username: '{{ vcenter_username }}'
+        password: '{{ vcenter_password }}'
+        vswitch: '{{ switch1 }}'
+        portgroup: 'pg_ts'
+        cluster_name: "{{ ccr1 }}"
+        traffic_shaping:
+          enabled: false
+        validate_certs: false
+        state: present
+      register: disable_traffic_shaping_result
+
+    - assert:
+        that:
+          - disable_traffic_shaping_result.changed
+
     - name: Integration test a PortGroup name with special characters
       block:
         - name: Create Switch with special characters