I'm trying to add a template to a host in Zabbix (3.0) using this specific nuget package (version 1.1.3).
While I'm able to get/delete specific hosts and templates I'm unable to update one. Looking at Zabbix documentation for updating hosts I found this description of templates parameter:
Templates to replace the currently linked templates. Templates that are not passed are only unlinked.
So I gathered that I should add a template to parentTemplates property in a host object and the pass the host to the Update method of HostService:
Context context = new Context();
var templateService = new TemplateService(context);
Template template = templateService.Get(new { host = "Template_test" }).First();
var hostService = new HostService(context);
Host host = hostService.GetByName("testhost");
host.parentTemplates.Add(template);
hostService.Update(host);
(Note that Context context = new Context()
will work as I'm using a .config file.)
After compiling and executing, the program runs with no errors, but the host is still templateless.
Has anyone tried this before? Am I missing something obvious?
Notes on Zabbix configuration:
=== EDIT ===
There four requests being made:
The last one is causing problems. The full request is here.
The response:
{"jsonrpc":"2.0","result":{"hostids":["10135"]},"id":"ca04d839-e6ec-4017-81b0-cc7f8e01fcfc"}
The request is needlessly big. I'll check if i can trim it down a bit.
As mentioned in the comment of the question: the parameter has an invalid name. This comes from the fact that Zabbix host.get method returns templates in parentTemplates property, while host.update uses templates.
The creator of the NuGet did not account for this, so I created a new issue in his project.
I managed to get my code working by deriving a class from Host:
private class NewHost : Host
{
public IList<Template> templates { get; set; }
}
Then i can make a request using this class:
Context context = new Context();
var templateService = new TemplateService(context);
Template template = templateService.Get(new { host = "Template_test" }).First();
var hostService = new HostService(context);
Host host = hostService.GetByName("testhost");
host.parentTemplates.Add(template);
var newhost = new NewHost
{
Id = host.Id,
templates = host.parentTemplates
}
hostService.Update(newhost);
Thanks to this not only a proper name is used, but also the request body is way smaller.