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

Update resource_zabbix_host.go #37

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
41 changes: 41 additions & 0 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 @@ -267,6 +274,20 @@ func getTemplates(d *schema.ResourceData, api *zabbix.API) (zabbix.TemplateIDs,
return hostTemplates, nil
}

func getHostMacro(d *schema.ResourceData) zabbix.Macros {
var macros zabbix.Macros

terraformMacros := d.Get("macro").(map[string]interface{})
for i, terraformMacro := range terraformMacros {
macro := zabbix.Macro{
MacroName: fmt.Sprintf("{$%s}", i),
Value: terraformMacro.(string),
}
macros = append(macros, macro)
}
return macros
}

func createHostObj(d *schema.ResourceData, api *zabbix.API) (*zabbix.Host, error) {
host := zabbix.Host{
Host: d.Get("host").(string),
Expand Down Expand Up @@ -302,6 +323,8 @@ func createHostObj(d *schema.ResourceData, api *zabbix.API) (*zabbix.Host, error
}

host.TemplateIDs = templates
host.UserMacros = getHostMacro(d)

return &host, nil
}

Expand Down Expand Up @@ -368,7 +391,25 @@ func resourceZabbixHostRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("templates", templateNames)
macros := 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 fmt.Errorf("Invalid macro name \"%s\"", macro.MacroName)
}
if noSuffix := strings.Split(name, "}"); len(noSuffix) == 2 {
name = noSuffix[0]
} else {
return fmt.Errorf("Invalid macro name \"%s\"", macro.MacroName)
}
macros[name] = macro.Value
}

d.Set("macro", macros)

groups, err := api.HostGroupsGet(params)

if err != nil {
Expand Down