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

Adds hosts macro management support #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions zabbix/resource_zabbix_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"strings"

"github.com/claranet/go-zabbix-api"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -103,6 +104,12 @@ func resourceZabbixHost() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"macro": &schema.Schema{
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "User macros for the host.",
},
},
}
}
Expand Down Expand Up @@ -269,9 +276,10 @@ func getTemplates(d *schema.ResourceData, api *zabbix.API) (zabbix.TemplateIDs,

func createHostObj(d *schema.ResourceData, api *zabbix.API) (*zabbix.Host, error) {
host := zabbix.Host{
Host: d.Get("host").(string),
Name: d.Get("name").(string),
Status: 0,
Host: d.Get("host").(string),
Name: d.Get("name").(string),
Status: 0,
UserMacros: createZabbixMacro(d),
}

//0 is monitored, 1 - unmonitored host
Expand Down Expand Up @@ -302,6 +310,11 @@ func createHostObj(d *schema.ResourceData, api *zabbix.API) (*zabbix.Host, error
}

host.TemplateIDs = templates

if host.UserMacros == nil {
host.UserMacros = zabbix.Macros{}
}

return &host, nil
}

Expand Down Expand Up @@ -353,6 +366,7 @@ func resourceZabbixHostRead(d *schema.ResourceData, meta interface{}) error {
"hostids": []string{
d.Id(),
},
"selectMacros": "extend",
}

templates, err := api.TemplatesGet(params)
Expand Down Expand Up @@ -382,6 +396,12 @@ func resourceZabbixHostRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("groups", groupNames)

terraformMacros, err := createTerraformMacroOnHost(*host)
if err != nil {
return err
}
d.Set("macro", terraformMacros)

return nil
}
Expand Down Expand Up @@ -419,3 +439,23 @@ func resourceZabbixHostDelete(d *schema.ResourceData, meta interface{}) error {

return api.HostsDeleteByIds([]string{d.Id()})
}

func createTerraformMacroOnHost(host zabbix.Host) (map[string]interface{}, error) {
terraformMacros := make(map[string]interface{}, len(host.UserMacros))

for _, macro := range host.UserMacros {
var name string
if noPrefix := strings.Split(macro.MacroName, "{$"); len(noPrefix) == 2 {
name = noPrefix[1]
} else {
return nil, fmt.Errorf("Invalid macro name \"%s\"", macro.MacroName)
}
if noSuffix := strings.Split(name, "}"); len(noSuffix) == 2 {
name = noSuffix[0]
} else {
return nil, fmt.Errorf("Invalid macro name \"%s\"", macro.MacroName)
}
terraformMacros[name] = macro.Value
}
return terraformMacros, nil
}