Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest API - Put/Patch/Post Logic Causes Failures #422

Open
steve-sander-chtr opened this issue Mar 18, 2022 · 6 comments
Open

Rest API - Put/Patch/Post Logic Causes Failures #422

steve-sander-chtr opened this issue Mar 18, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@steve-sander-chtr
Copy link

Describe the bug

Lines in code 219 - 234 it looks like the module is attempting to determine the current version prior to issuing the requested put/post/patch operation by sending a GET to the target URL (with payload). Since not every URL supports GET ( or errors on the payload), the requested action of put/post/patch is not completed. Maybe use try logic here - so if the GET fails, then the requested action is still performed ?

Reproduction steps

RUN THIS PLAYBOOK:
- hosts: 127.0.0.1
  connection: local
  become: yes
  vars_files:
    - deploy_nsx_cluster_vars.yml
  tasks:  
    - name: Create Cluster IP
      ansible.builtin.uri:
        #hostname: "{{ nsx_node1.mgmt_ip }}"
        url_username: "{{ nsx_username }}"
        url_password: "{{ nsx_password }}"
        validate_certs: false
        method: post
        force_basic_auth: yes
        url: "https://{{ nsx_node1.mgmt_ip }}/api/v1/cluster/api-virtual-ip?action=set_virtual_ip&ip_address={{nsx_cluster_ip}}"

    - name: Cluster IP Rest 
      vmware.ansible_for_nsxt.nsxt_rest:
        hostname: "{{ nsx_node1.mgmt_ip }}"
        username: "{{ nsx_username }}"
        password: "{{ nsx_password }}"
        validate_certs: false
        method: post
        path: /api/v1/cluster/api-virtual-ip?action=set_virtual_ip&ip_address={{nsx_cluster_ip}}

Expected behavior

Both API calls should succeed:

PLAY [127.0.0.1] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Create Cluster IP] ***************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Cluster IP Rest] *****************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "error_code: 405, error_message: Method is not allowed."}

PLAY RECAP *****************************************************************************************************************************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

The 405 error is on the preceding GET ( method not allowed ). And the post is never issued.

Additional context

Other URL which failed with REST API:
POST - /policy/api/v1/aaa/role-bindings
PUT - /api/v1/node/services/snmp ( GET with payload not supported )
POST - /api/v1/node/services/syslog/exporters

@steve-sander-chtr steve-sander-chtr added the bug Something isn't working label Mar 18, 2022
@smesguich-orange
Copy link

Hello

I confirm the bug.
I've found a workaround (but very dirty). Works on my side but I'm not using a lot of function of this ansible collection.

I've change

def execute(self):
if self.method == "get":
resp = self.operate_nsxt(method=self.method)
self.module.exit_json(changed=False, body=resp)
if self.method == "post" or self.method == "put" or self.method == "patch":
before_resp = self.operate_nsxt(method="get", ignore_errors=True)
if before_resp:
before_revision = before_resp["_revision"]
else:
before_revision = ""

into

    def execute(self):
        if self.method == "get" or self.method == "post":
            resp = self.operate_nsxt(method=self.method)
            self.module.exit_json(changed=False, body=resp)

        if self.method == "put" or self.method == "patch":
            before_resp = self.operate_nsxt(method="get", ignore_errors=True)
            if before_resp:
                before_revision = before_resp["_revision"]
            else:
                before_revision = ""

@smesguich-orange
Copy link

@steve-sander-chtr
Are you running NSX-T under 3.2 ? We are affected by this bug in 3.1.2.
The _revision setting is available in 3.2 on affected request.

@steve-sander-chtr
Copy link
Author

steve-sander-chtr commented Nov 23, 2022 via email

@smesguich-orange
Copy link

On my side, I confirm that this bug is not present in NSX-T 3.2.

So on my side, VMware should not announced that the ansible collection tagged NSX-T 3.2 IS NOT fully compatible back in 3.1

@smesguich-orange
Copy link

smesguich-orange commented Feb 20, 2023

Correction : Issue PERSIST in NSX-T 3.2.

Workaround mentioned in #422 (comment) still work

@smesguich-orange
Copy link

@steve-sander-chtr : can you confirm that this code correct you issues ?
#461

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants