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

Add support for selecting service template #10

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions icinga2/icinga.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type MockClient struct {
Actions map[string][]Action
mutex sync.Mutex
URL string
Templates []string
}

type Vars map[string]interface{}
Expand Down Expand Up @@ -128,6 +129,7 @@ func NewMockClient() (c *MockClient) {
c.Services = make(map[string]Service)
c.Actions = make(map[string][]Action)
c.mutex = sync.Mutex{}
c.Templates = []string{}
return
}

Expand Down
36 changes: 19 additions & 17 deletions icinga2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import (
)

type Service struct {
Name string `json:"name,omitempty"`
DisplayName string `json:"display_name"`
HostName string `json:"host_name"`
CheckCommand string `json:"check_command"`
EnableActiveChecks bool `json:"enable_active_checks"`
Notes string `json:"notes"`
NotesURL string `json:"notes_url"`
ActionURL string `json:"action_url"`
Vars Vars `json:"vars"`
pdorschner marked this conversation as resolved.
Show resolved Hide resolved
Zone string `json:"zone,omitempty"`
CheckInterval float64 `json:"check_interval"`
RetryInterval float64 `json:"retry_interval"`
MaxCheckAttempts float64 `json:"max_check_attempts"`
CheckPeriod string `json:"check_period,omitempty"`
State float64 `json:"state,omitempty"`
LastStateChange float64 `json:"last_state_change,omitempty"`
Name string `json:"name,omitempty"`
DisplayName string `json:"display_name"`
HostName string `json:"host_name"`
CheckCommand string `json:"check_command"`
EnableActiveChecks bool `json:"enable_active_checks"`
Notes string `json:"notes"`
NotesURL string `json:"notes_url"`
ActionURL string `json:"action_url"`
Vars Vars `json:"vars,omitempty"`
Zone string `json:"zone,omitempty"`
CheckInterval float64 `json:"check_interval"`
RetryInterval float64 `json:"retry_interval"`
MaxCheckAttempts float64 `json:"max_check_attempts"`
CheckPeriod string `json:"check_period,omitempty"`
State float64 `json:"state,omitempty"`
LastStateChange float64 `json:"last_state_change,omitempty"`
Templates []string `json:"templates,omitempty"`
}

type ServiceResults struct {
Expand Down Expand Up @@ -68,10 +69,11 @@ func (s *WebClient) GetService(name string) (Service, error) {
}

func (s *WebClient) CreateService(service Service) error {
serviceCreate := ServiceCreate{Templates: []string{"generic-service"}, Attrs: service}
serviceCreate := ServiceCreate{Templates: service.Templates, Attrs: service}
// Strip "name" from create payload
serviceCreate.Attrs.Name = ""
err := s.CreateObject("/services/"+service.FullName(), serviceCreate)

return err
}

Expand Down
55 changes: 55 additions & 0 deletions icinga2/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package icinga2

import (
"encoding/json"
"strconv"
)

func (s Service) MarshalJSON() ([]byte, error) {
// Prevent json.Marshal() recursion
type service Service

svc := service(s)
// Clear top-level Vars field, so it's not added to the marshalled JSON. We marshal all service variables into individual top-level `vars.<variable name>` fields below.

svc.Vars = Vars{}

serviceAsJson, err := json.Marshal(svc)
if err != nil {
return nil, err
}

var serviceAsMap map[string]interface{}
if err := json.Unmarshal(serviceAsJson, &serviceAsMap); err != nil {
return nil, err
}

// This loop flattens the json, so each var will be at the same level
for k, v := range Flatten(s.Vars) {
serviceAsMap["vars."+k] = v
}

return json.Marshal(serviceAsMap)
}

func Flatten(m map[string]interface{}) map[string]interface{} {
flat := map[string]interface{}{}

for k, v := range m {
switch child := v.(type) {
case map[string]interface{}:
flat_child := Flatten(child)
for ck, cv := range flat_child {
flat[k+"."+ck] = cv
}
case []interface{}:
for i := 0; i < len(child); i++ {
flat[k+"["+strconv.Itoa(i)+"]"] = child[i]
}
default:
flat[k] = v
}
}

return flat
}