commit - 717a9e7823638c994c6620883fcee2ef98da294b
commit + c26e0f9dab045063f9fb15a64233322532b18dcb
blob - cebae45cbcad614a015ead354171c783af804a6c
blob + acd4390c9d48fa0fd5d47a465bf3e067da19816c
--- host.go
+++ host.go
return "/objects/hostgroups/" + hg.Name
}
-func (h Host) MarshalJSON() ([]byte, error) {
- type alias Host
- a := alias(h)
- return json.Marshal(map[string]interface{}{"attrs": a})
-}
-
// UnmarhsalJSON unmarshals host attributes into more meaningful Host field types.
func (h *Host) UnmarshalJSON(data []byte) error {
type alias Host
}
return nil
}
-
-func (hg HostGroup) MarshalJSON() ([]byte, error) {
- type alias HostGroup
- a := alias(hg)
- return json.Marshal(map[string]interface{}{"attrs": a})
-}
blob - 981b6c85fc470333686a0a81cd94f54d8da819e3
blob + 18943250b3644b6953b6f9f2ba10819721dcc176
--- host_test.go
+++ host_test.go
package icinga
import (
- "encoding/json"
"os"
"reflect"
"testing"
)
-func TestHostMarshal(t *testing.T) {
- b := []byte(`{"attrs":{"address":"192.0.2.1","address6":"2001:db8::","check_command":"dummy","display_name":"Example host","groups":["test"]}}`)
- want := make(map[string]interface{})
- if err := json.Unmarshal(b, &want); err != nil {
- t.Fatal(err)
- }
-
- p, err := json.Marshal(Host{
- Name: "example.com",
- Address: "192.0.2.1",
- Address6: "2001:db8::",
- Groups: []string{"test"},
- StateType: StateSoft,
- CheckCommand: "dummy",
- DisplayName: "Example host",
- })
- if err != nil {
- t.Fatal(err)
- }
- got := make(map[string]interface{})
- if err := json.Unmarshal(p, &got); err != nil {
- t.Fatal(err)
- }
- if !reflect.DeepEqual(want, got) {
- t.Error("want", want, "got", got)
- }
-}
-
func TestHostUnmarshal(t *testing.T) {
want := Host{
Name: "VuS9jZ8u.example.org",
blob - faef3a21b4d7a2dd7c852d98da51a75a11c30355
blob + c9d34273251c4488a2de996b0fefc36114881964
--- object.go
+++ object.go
buf := &bytes.Buffer{}
switch v := obj.(type) {
case Host, Service, User, HostGroup:
- if err := json.NewEncoder(buf).Encode(v); err != nil {
+ m := make(map[string]interface{})
+ m["attrs"] = v
+ if err := json.NewEncoder(buf).Encode(m); err != nil {
return err
}
default:
blob - 3d3a9245b3ff138b1503e66793d7f0faaa7be94f
blob + 06a63ce3ee17549ea8e373106a3ad58600e9e86f
--- service.go
+++ service.go
// Service represents a Service object.
type Service struct {
- Name string `json:"-"`
- Groups []string `json:"groups,omitempty"`
- State ServiceState
- StateType StateType `json:"state_type"`
- CheckCommand string `json:"check_command"`
- DisplayName string `json:"display_name,omitempty"`
- LastCheckResult CheckResult `json:"last_check_result,omitempty"`
- Acknowledgement bool `json:",omitempty"`
+ Name string `json:"-"`
+ Groups []string `json:"groups,omitempty"`
+ State ServiceState `json:"state,omitempty"`
+ StateType StateType `json:"state_type,omitempty"`
+ CheckCommand string `json:"check_command"`
+ DisplayName string `json:"display_name,omitempty"`
+ LastCheckResult *CheckResult `json:"last_check_result,omitempty"`
+ Acknowledgement bool `json:",omitempty"`
}
type CheckResult struct {
return "ServiceUnknown"
}
-func (s Service) MarshalJSON() ([]byte, error) {
- attrs := make(map[string]interface{})
- if len(s.Groups) > 0 {
- attrs["groups"] = s.Groups
- }
- attrs["check_command"] = s.CheckCommand
- attrs["display_name"] = s.DisplayName
- jservice := &struct {
- Attrs map[string]interface{} `json:"attrs"`
- }{Attrs: attrs}
- return json.Marshal(jservice)
-}
-
// UnmarshalJSON unmarshals service attributes into more meaningful Service field types.
func (s *Service) UnmarshalJSON(data []byte) error {
type alias Service
blob - 66fe315b19691f68e64f8f90ba8971310d7fffbb
blob + b1658421cdbb8d78aba72e7a5683d1e94b840947
--- service_test.go
+++ service_test.go
package icinga
import (
- "encoding/json"
"os"
"reflect"
"testing"
StateType: StateHard,
CheckCommand: "http",
DisplayName: "http",
- LastCheckResult: CheckResult{
+ LastCheckResult: &CheckResult{
Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 1.083 second response time ",
},
}
t.Errorf("want %+v, got %+v", want, got)
}
}
-
-func TestServiceMarshal(t *testing.T) {
- want := `{"attrs":{"check_command":"http","display_name":"http"}}`
-
- b, err := json.Marshal(Service{
- Name: "9p.io!http",
- Groups: []string{},
- State: ServiceOK,
- StateType: StateHard,
- CheckCommand: "http",
- DisplayName: "http",
- LastCheckResult: CheckResult{
- Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 0.703 second response time ",
- },
- })
- if err != nil {
- t.Error(err)
- }
- got := string(b)
- if want != got {
- t.Error("want", want, "got", got)
- }
-}
blob - 740b452988d7f0b545fbfb14b7880844d4cf5edb
blob + ca94556b9c34783670c817a97810129b60dc06ac
--- user.go
+++ user.go
package icinga
-import "encoding/json"
-
// User represents a User object.
// Note that this is different from an ApiUser.
type User struct {
Groups []string `json:"groups,omitempty"`
}
-func (u User) MarshalJSON() ([]byte, error) {
- type alias User
- a := alias(u)
- return json.Marshal(map[string]interface{}{"attrs": a})
-}
-
func (u User) name() string {
return u.Name
}
blob - 9b67b01b2c6089f55ee6c002f992a2b07d971139
blob + cc85c8bc8cfc9ee0b85ba89d77039b94dc9d8e3b
--- user_test.go
+++ user_test.go
package icinga
import (
- "encoding/json"
"os"
"reflect"
"testing"
t.Errorf("want: %+v, got %+v", want, got)
}
}
-
-func TestUserMarshal(t *testing.T) {
- user := &User{Name: "test", Email: "test@example.com", Groups: []string{}}
- want := `{"attrs":{"email":"test@example.com"}}`
- got, err := json.Marshal(user)
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != want {
- t.Errorf("want %s got %s", want, got)
- }
-}