3 26fb88e9 2022-01-12 o import "encoding/json"
5 e0c24850 2022-01-14 o // Host represents a Host object. To create a Host, the Name and CheckCommand
6 e0c24850 2022-01-14 o // fields must be set.
7 6e415953 2022-01-06 o type Host struct {
8 46ca0f68 2022-02-01 o Name string `json:"-"`
9 562d5c1e 2022-01-19 o Address string `json:"address"`
10 562d5c1e 2022-01-19 o Address6 string `json:"address6"`
11 46ca0f68 2022-02-01 o Groups []string `json:"groups,omitempty"`
12 46ca0f68 2022-02-01 o State HostState `json:"state,omitempty"`
13 46ca0f68 2022-02-01 o StateType StateType `json:"state_type,omitempty"`
14 562d5c1e 2022-01-19 o CheckCommand string `json:"check_command"`
15 46ca0f68 2022-02-01 o DisplayName string `json:"display_name,omitempty"`
16 46ca0f68 2022-02-01 o Acknowledgement bool `json:",omitempty"`
19 e0c24850 2022-01-14 o type HostGroup struct {
20 46ca0f68 2022-02-01 o Name string `json:"-"`
21 e0c24850 2022-01-14 o DisplayName string `json:"display_name"`
24 76669f9b 2022-01-11 o type HostState int
27 76669f9b 2022-01-11 o HostUp HostState = 0 + iota
29 76669f9b 2022-01-11 o HostUnreachable
32 dca9442c 2022-02-02 o func (state HostState) String() string {
35 76669f9b 2022-01-11 o return "HostUp"
37 76669f9b 2022-01-11 o return "HostDown"
39 dca9442c 2022-02-02 o return "HostUnreachable"
42 76669f9b 2022-01-11 o func (h Host) name() string {
46 76669f9b 2022-01-11 o func (h Host) path() string {
47 76669f9b 2022-01-11 o return "/objects/hosts/" + h.Name
50 e0c24850 2022-01-14 o func (hg HostGroup) name() string {
54 e0c24850 2022-01-14 o func (hg HostGroup) path() string {
55 e0c24850 2022-01-14 o return "/objects/hostgroups/" + hg.Name
58 50558780 2022-01-06 o func (h Host) MarshalJSON() ([]byte, error) {
59 46ca0f68 2022-02-01 o type alias Host
61 46ca0f68 2022-02-01 o return json.Marshal(map[string]interface{}{"attrs": a})
64 562d5c1e 2022-01-19 o // UnmarhsalJSON unmarshals host attributes into more meaningful Host field types.
65 562d5c1e 2022-01-19 o func (h *Host) UnmarshalJSON(data []byte) error {
66 562d5c1e 2022-01-19 o type alias Host
67 562d5c1e 2022-01-19 o aux := &struct {
68 562d5c1e 2022-01-19 o Acknowledgement int
71 562d5c1e 2022-01-19 o alias: (*alias)(h),
73 562d5c1e 2022-01-19 o if err := json.Unmarshal(data, &aux); err != nil {
76 562d5c1e 2022-01-19 o if aux.Acknowledgement != 0 {
77 562d5c1e 2022-01-19 o h.Acknowledgement = true
82 e0c24850 2022-01-14 o func (hg HostGroup) MarshalJSON() ([]byte, error) {
83 46ca0f68 2022-02-01 o type alias HostGroup
85 46ca0f68 2022-02-01 o return json.Marshal(map[string]interface{}{"attrs": a})