zabbix / python-zabbix-utils

Python library "zabbix-utils"
MIT License
114 stars 16 forks source link

API: host.create() -- can this API point take an array of multiple hosts to create? #10

Closed matthewsht closed 3 months ago

matthewsht commented 9 months ago

The host.delete() api does, but I can't get this api point to take multiple hosts.

The docs here are unclear (they are for most API points): https://www.zabbix.com/documentation/6.4/en/manual/api/reference/host/create Says "(object/array) Hosts to create.", however the "params" field for the API takes a dictionary whose keys are a single host.

If this API allows multiple hosts, could an example be included in the otherwise really good docs please?

BBT-coder commented 6 months ago

I had the same issue with hostgroup.create and solved that by doing:

groups = [{'name': '_D'},{'name': '_E'},{'name': '_F'}] result = api.hostgroup.create(*groups)

aiantsen commented 3 months ago

@matthewsht, thank you for your attention to this topic. @BBT-coder is right, you can pass a list of hosts to create by specifying them as a python list in advance, then the list can be unpacked as a list of function arguments with the *-operator:

hosts = [
    {
        "host": "test_host1",
        "interfaces": {
            "type": 1,
            "main": 1,
            "useip": 1,
            "ip": "127.0.0.1",
            "dns": "",
            "port": "10050"
        },
        "groups": [{"groupid": 19}]
    },
    {
        "host": "test_host2",
        "interfaces": {
            "type": 1,
            "main": 1,
            "useip": 0,
            "ip": "",
            "dns": "localhost",
            "port": "10050"
        },
        "groups": [{"groupid": 19}]
    }
]
api.host.create(*hosts)

By the way, you can pass parameters of even one host in the same way, but using **-operator to unpack the dictionary:

host = {
        "host": "test_host3",
        "interfaces": {
            "type": 1,
            "main": 1,
            "useip": 0,
            "ip": "",
            "dns": "localhost",
            "port": "10050"
        },
        "groups": [{"groupid": 19}]
}
api.host.create(**host)

We'll consider adding these examples to the library documentation and code examples. Thank you for this advice.

@BBT-coder, thank you for the suggested resolution.