Repository: ansible-collections/community.vmware

Issue:
vmware_cluster_ha and APD/PDL Warning not fully applied
<!--- 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
<!--- Explain the problem briefly below -->
module vmware_cluster_ha setting APD or PDL to Warning (default value) when the current cluster setting is Disabled does not fully show the setting change in HTML5 web interface.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
vmware_cluster_ha

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.9.16
  config file = /home/username/AWX/ansible.cfg
  configured module search path = ['/home/username/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Aug 18 2020, 08:33:21) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
ANSIBLE_SSH_ARGS(/home/username/AWX/ansible.cfg) = -C -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
COLLECTIONS_PATHS(/home/username/AWX/ansible.cfg) = ['/home/username/AWX/collections']
DEFAULT_FILTER_PLUGIN_PATH(/home/username/AWX/ansible.cfg) = ['/home/username/AWX/custom_filters']
DEFAULT_ROLES_PATH(/home/username/AWX/ansible.cfg) = ['/home/username/AWX/roles']
```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
vCenter 6.7 U3

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
HA currently is set for APD and PDL Response to "Disabled" in vCenter
module vmware_cluster_ha defaults these settings to "warning"
When applied to cluster in vCenter 6.7 Ansible reports changing the cluster, and an event is logged in vCenter that some settings are changing, but using the HTML5 gui to view the HA settings still shows both items as Disabled. 
Changing the values in the GUI to some other value to Power Off and rerunning the playbook will result in Ansible showing a Change and the GUI properly reflects "Issue events"
Seems to only apply when existing setting is Disabled and Ansible is changing to "warning" that the GUI does not fully reflect it. Unsure if the setting is actually applied or not.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
    - name: Cluster HA Setting
      community.vmware.vmware_cluster_ha:
        hostname: "{{ source_vcenter }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_password }}"
        datacenter_name: "{{ datacenter }}"
        cluster_name: "{{ inventory_hostname }}"
        enable_ha: "{{ ha_settings.enable_ha }}"
        ha_host_monitoring: "{{ ha_settings.ha_host_monitoring | default(omit) }}"
        ha_restart_priority: "{{ ha_settings.ha_restart_priority | default(omit) }}"
        host_isolation_response: "{{ ha_settings.host_isolation_response | default(omit) }}"
        pdl_response: "{{ ha_settings.pdl_response | default(omit) }}"
        apd_response: "{{ ha_settings.apd_response | default(omit) }}"
        ha_vm_monitoring: "{{ ha_settings.ha_vm_monitoring | default(omit) }}"
        reservation_based_admission_control: "{{ ha_settings.reservation_based_admission_control | default(omit) }}"
        validate_certs: "{{ validate_certs }}"
      delegate_to: localhost
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
PDL and APD settings are changed to "Issue Events" in HTML5 web client view

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
Events are logged in vCenter about changes, but PDL and APD settings in HA Settings still shows Disabled

<!--- Paste verbatim command output between quotes -->
```paste below
TASK [Cluster HA Setting] ***************************************************************************************************************************************************************************
task path: /home/username/AWX/VMware/cluster_configuration.yml:141
Trying secret FileVaultSecret(filename='/home/username/vault.txt') for vault_id=default
Trying secret FileVaultSecret(filename='/home/username/vault.txt') for vault_id=default
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: username
<localhost> EXEC /bin/sh -c 'echo ~username && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/username/.ansible/tmp `"&& mkdir "` echo /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659 `" && echo ansible-tmp-1613748144.8424242-2535585-251483034568659="` echo /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659 `" ) && sleep 0'
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: username
<localhost> EXEC /bin/sh -c 'echo ~username && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/username/.ansible/tmp `"&& mkdir "` echo /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471 `" && echo ansible-tmp-1613748144.865427-2535586-231000193206471="` echo /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471 `" ) && sleep 0'
Using module file /home/username/AWX/collections/ansible_collections/community/vmware/plugins/modules/vmware_cluster_ha.py
Using module file /home/username/AWX/collections/ansible_collections/community/vmware/plugins/modules/vmware_cluster_ha.py
<localhost> PUT /home/username/.ansible/tmp/ansible-local-2535478p1et0vol/tmpc_xids7a TO /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659/AnsiballZ_vmware_cluster_ha.py
<localhost> PUT /home/username/.ansible/tmp/ansible-local-2535478p1et0vol/tmpdoq7h_ap TO /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471/AnsiballZ_vmware_cluster_ha.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659/ /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659/AnsiballZ_vmware_cluster_ha.py && sleep 0'
<localhost> EXEC /bin/sh -c 'chmod u+x /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471/ /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471/AnsiballZ_vmware_cluster_ha.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/libexec/platform-python /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471/AnsiballZ_vmware_cluster_ha.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/libexec/platform-python /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659/AnsiballZ_vmware_cluster_ha.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/username/.ansible/tmp/ansible-tmp-1613748144.8424242-2535585-251483034568659/ > /dev/null 2>&1 && sleep 0'
ok: [New Cluster] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "advanced_settings": {},
            "apd_response": "warning",
            "cluster_name": "New Cluster",
            "datacenter": "LabDC",
            "datacenter_name": "LabDC",
            "enable_ha": true,
            "failover_host_admission_control": null,
            "ha_host_monitoring": "enabled",
            "ha_restart_priority": "medium",
            "ha_vm_failure_interval": 30,
            "ha_vm_max_failure_window": -1,
            "ha_vm_max_failures": 3,
            "ha_vm_min_up_time": 120,
            "ha_vm_monitoring": "vmMonitoringDisabled",
            "host_isolation_response": "none",
            "hostname": "stl-avclabt01.smrcy.com",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "pdl_response": "warning",
            "port": 443,
            "proxy_host": null,
            "proxy_port": null,
            "reservation_based_admission_control": {
                "auto_compute_percentages": true,
                "cpu_failover_resources_percent": 50,
                "failover_level": 1,
                "memory_failover_resources_percent": 50
            },
            "slot_based_admission_control": null,
            "username": "AnsibleTest",
            "validate_certs": false
        }
    },
    "result": null
}
<localhost> EXEC /bin/sh -c 'rm -f -r /home/username/.ansible/tmp/ansible-tmp-1613748144.865427-2535586-231000193206471/ > /dev/null 2>&1 && sleep 0'
changed: [Lab01] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "advanced_settings": {},
            "apd_response": "warning",
            "cluster_name": "Lab01",
            "datacenter": "LabDC",
            "datacenter_name": "LabDC",
            "enable_ha": true,
            "failover_host_admission_control": null,
            "ha_host_monitoring": "enabled",
            "ha_restart_priority": "medium",
            "ha_vm_failure_interval": 30,
            "ha_vm_max_failure_window": -1,
            "ha_vm_max_failures": 3,
            "ha_vm_min_up_time": 120,
            "ha_vm_monitoring": "vmMonitoringDisabled",
            "host_isolation_response": "none",
            "hostname": "stl-avclabt01.smrcy.com",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "pdl_response": "warning",
            "port": 443,
            "proxy_host": null,
            "proxy_port": null,
            "reservation_based_admission_control": {
                "auto_compute_percentages": true,
                "cpu_failover_resources_percent": 50,
                "failover_level": 1,
                "memory_failover_resources_percent": 50
            },
            "slot_based_admission_control": null,
            "username": "AnsibleTest",
            "validate_certs": false
        }
    },
    "result": null
}

```
vCenter logs show:
Reconfigured cluster Lab01 in datacenter LabDC Modified: configurationEx.dasConfig.defaultVmSettings.vmComponentProtectionSettings.vmStorageProtectionForAPD: "disabled" -> "warning"; configurationEx.dasConfig.defaultVmSettings.vmComponentProtectionSettings.vmStorageProtectionForPDL: "disabled" -> "warning"; Added: Deleted:

But t he HTML5 GUI still shows both as Disabled


Discussion:
Files identified in the description:
* [`plugins/modules/vmware_cluster_ha.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/vmware_cluster_ha.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @Tomorrow9 @goneri @lparkes @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I've tested this with Ansible 3 and vSphere 7 and get the same result. What the module does to manage `apd_response` and `pdl_response` is setting `vmStorageProtectionForAPD` and `vmStorageProtectionForPDL` in [configurationEx.dasConfig.defaultVmSettings.vmComponentProtectionSettings](https://vdc-download.vmware.com/vmwb-repository/dcr-public/b50dcbbf-051d-4204-a3e7-e1b618c1e384/538cf2ec-b34f-4bae-a332-3820ef9e7773/vim.cluster.VmComponentProtectionSettings.html).

Now when I change APD or PDL settings in the UI, these two are updated. But the other way round doesn't work. It looks like there is an additional place where these are set. But I don't know where :-(
I think I've found the problem. If either `vmStorageProtectionForAPD ` or `vmStorageProtectionForPDL` isn't "disabled", I also need to set `vmComponentProtecting` in [configurationEx.dasConfig](https://vdc-download.vmware.com/vmwb-repository/dcr-public/b50dcbbf-051d-4204-a3e7-e1b618c1e384/538cf2ec-b34f-4bae-a332-3820ef9e7773/vim.cluster.DasConfigInfo.html) to "enabled".

Resolving patch:
diff --git a/plugins/modules/vmware_cluster_ha.py b/plugins/modules/vmware_cluster_ha.py
--- a/plugins/modules/vmware_cluster_ha.py
+++ b/plugins/modules/vmware_cluster_ha.py
@@ -395,6 +395,10 @@ def configure_ha(self):
                     das_vm_config.vmComponentProtectionSettings = vim.cluster.VmComponentProtectionSettings()
                     das_vm_config.vmComponentProtectionSettings.vmStorageProtectionForAPD = self.params.get('apd_response')
                     das_vm_config.vmComponentProtectionSettings.vmStorageProtectionForPDL = self.params.get('pdl_response')
+                    if (self.params['apd_response'] != "disabled" or self.params['pdl_response'] != "disabled"):
+                        cluster_config_spec.dasConfig.vmComponentProtecting = 'enabled'
+                    else:
+                        cluster_config_spec.dasConfig.vmComponentProtecting = 'disabled'
 
                     cluster_config_spec.dasConfig.defaultVmSettings = das_vm_config
 


Test patch: