8 76669f9b 2022-01-11 o func (s Service) name() string {
12 76669f9b 2022-01-11 o func (s Service) path() string {
13 76669f9b 2022-01-11 o return "/objects/services/" + s.Name
16 76669f9b 2022-01-11 o func (s Service) attrs() map[string]interface{} {
17 76669f9b 2022-01-11 o m := make(map[string]interface{})
18 76669f9b 2022-01-11 o m["display_name"] = s.DisplayName
22 76669f9b 2022-01-11 o // Service represents a Service object.
23 76669f9b 2022-01-11 o type Service struct {
24 76669f9b 2022-01-11 o Name string `json:"__name"`
25 76669f9b 2022-01-11 o Groups []string
26 76669f9b 2022-01-11 o State ServiceState
27 76669f9b 2022-01-11 o CheckCommand string `json:"check_command"`
28 76669f9b 2022-01-11 o DisplayName string `json:"display_name:"`
31 76669f9b 2022-01-11 o type ServiceState int
34 76669f9b 2022-01-11 o ServiceOK ServiceState = 0 + iota
36 76669f9b 2022-01-11 o ServiceCritical
40 76669f9b 2022-01-11 o func (s ServiceState) String() string {
42 76669f9b 2022-01-11 o case ServiceOK:
43 76669f9b 2022-01-11 o return "ServiceOK"
44 76669f9b 2022-01-11 o case ServiceWarning:
45 76669f9b 2022-01-11 o return "ServiceWarning"
46 76669f9b 2022-01-11 o case ServiceCritical:
47 76669f9b 2022-01-11 o return "ServiceCritical"
48 76669f9b 2022-01-11 o case ServiceUnknown:
49 76669f9b 2022-01-11 o return "ServiceUnknown"
51 76669f9b 2022-01-11 o return "unhandled service state"
54 76669f9b 2022-01-11 o func (s Service) MarshalJSON() ([]byte, error) {
55 76669f9b 2022-01-11 o attrs := make(map[string]interface{})
56 76669f9b 2022-01-11 o if len(s.Groups) > 0 {
57 76669f9b 2022-01-11 o attrs["groups"] = s.Groups
59 76669f9b 2022-01-11 o attrs["check_command"] = s.CheckCommand
60 76669f9b 2022-01-11 o attrs["display_name"] = s.DisplayName
61 76669f9b 2022-01-11 o jservice := &struct {
62 76669f9b 2022-01-11 o Attrs map[string]interface{} `json:"attrs"`
63 76669f9b 2022-01-11 o }{Attrs: attrs}
64 76669f9b 2022-01-11 o return json.Marshal(jservice)
67 76669f9b 2022-01-11 o func (c *Client) CreateService(service Service) error {
68 76669f9b 2022-01-11 o if err := c.createObject(service); err != nil {
69 76669f9b 2022-01-11 o return fmt.Errorf("create service %s: %w", service.Name, err)
74 76669f9b 2022-01-11 o func (c *Client) LookupService(name string) (Service, error) {
75 76669f9b 2022-01-11 o obj, err := c.lookupObject("/objects/services/" + name)
76 76669f9b 2022-01-11 o if err != nil {
77 76669f9b 2022-01-11 o return Service{}, fmt.Errorf("lookup %s: %w", name, err)
79 76669f9b 2022-01-11 o v, ok := obj.(Service)
81 76669f9b 2022-01-11 o return Service{}, fmt.Errorf("lookup %s: result type %T is not service", name, obj)