voxpupuli / puppet-zabbix

Puppet module for creating and maintaining zabbix components with puppet.
https://forge.puppet.com/puppet/zabbix
Apache License 2.0
80 stars 228 forks source link

Add code to ensure interfacedetails is always a hash #793

Closed oraziobattaglia closed 2 years ago

oraziobattaglia commented 2 years ago

Pull Request (PR) description

This PR follow PR #785 to ensure the host object idempotency. When interface details are empty the zabbix API returns an empty array and I need to convert it in an empty hash.

Example

This is returned by the api call:

      "interfaces": [
        {
          "interfaceid": "36",
          "type": "1",
          "main": "1",
          "ip": "10.10.10.1",
          "port": "10050",
          "useip": "1",
          "details": []
        }
      ]

and the error on puppet is:

interfacedetails changed [] to {} (corrective)
bastelfreak commented 2 years ago

hey @oraziobattaglia, thanks for the PR. do you think there is an easy way to add tests for this?

root-expert commented 2 years ago

Hey @oraziobattaglia according to the API reference details is supposed to be an Array. Maybe it returns an array of hashes every time?

oraziobattaglia commented 2 years ago

Hi @root-expert me and my colleague working on this have seen that if interfacedetails has data the API returns an hash of data like

          "details": {
            "version": "2",
            "bulk": "1",
            "community": "{$SNMP_COMMUNITY}"
          }

In case interfacedetails has no data the API returns an empty array like

          "details": []

I don't know why, maybe is a question related to the API, but the resource isn't idempotent.

So the solution I found here is to declare a variant type for interfacedetails:

Variant[Array, Hash] $interfacedetails = [],

We also tried to declare interfacedetails as an array of hashes and seems to work fine too:

Array[Hash] $interfacedetails = [],

But in this second case I don't full understand how it works!! :-O

Let me know if you have suggestions. Thanks for the help.

root-expert commented 2 years ago

I would say that this could be a Zabbix bug or incorrect documentation. Can you try raising an issue to their Jira before continuing further with this PR?

The Variant solution seems okay to me. The Array[Hash] we have to ensure that passing

"details": [
  { "foo": "bar" }
]

to the API is okay, or the API throws an error

oraziobattaglia commented 2 years ago

Hi @root-expert seems it's a known bug, here they have similar problems with ansible https://github.com/ansible-collections/community.zabbix/pull/51

oraziobattaglia commented 2 years ago

The interface details as array of hashes doesn't work:

"details": [ { "foo": "bar" } ]

Puppet doesn't converge and there is always the error:

expected "details": [ { "foo": "bar" } ]
received "details": { "foo": "bar" }

This is because, I think, there is a bug on the zabbix API. When interface details are given as hash the API returns an hash of data (not an array of hash). When interface details are not given the API returns an empty array [].

I add a test, test4.example.com, that show that an empty interface details returns an empty array.

If you have suggestions they are welcome! Thank you