diff --git a/pkg/device/builder/builder.go b/pkg/device/builder/builder.go index a8ba71a06..1d2694084 100644 --- a/pkg/device/builder/builder.go +++ b/pkg/device/builder/builder.go @@ -3,6 +3,7 @@ package builder import ( "github.com/logicmonitor/k8s-argus/pkg/types" lm "github.com/logicmonitor/lm-sdk-go" + log "github.com/sirupsen/logrus" ) // Builder implements types.DeviceBuilder @@ -48,9 +49,13 @@ func (b *Builder) System(name, value string) types.DeviceOption { func setProperty(name, value string) types.DeviceOption { return func(device *lm.RestDevice) { - device.CustomProperties = append(device.CustomProperties, lm.NameAndValue{ - Name: name, - Value: value, - }) + if value != "" { + device.CustomProperties = append(device.CustomProperties, lm.NameAndValue{ + Name: name, + Value: value, + }) + } else { + log.Warnf("Custom property value is empty for %q, skipping", name) + } } } diff --git a/pkg/device/device.go b/pkg/device/device.go index eba43e887..bbb7f4882 100644 --- a/pkg/device/device.go +++ b/pkg/device/device.go @@ -2,7 +2,6 @@ package device import ( "fmt" - "log" "github.com/logicmonitor/k8s-argus/pkg/config" "github.com/logicmonitor/k8s-argus/pkg/device/builder" @@ -10,6 +9,7 @@ import ( "github.com/logicmonitor/k8s-argus/pkg/utilities" lm "github.com/logicmonitor/lm-sdk-go" + log "github.com/sirupsen/logrus" ) // Manager implements types.DeviceManager @@ -51,10 +51,13 @@ func (m *Manager) FindByDisplayName(name string) (*lm.RestDevice, error) { // Add implements types.DeviceManager. func (m *Manager) Add(options ...types.DeviceOption) (*lm.RestDevice, error) { device := buildDevice(m.Config(), options...) + log.Debugf("%#v", device) + restResponse, apiResponse, err := m.LMClient.AddDevice(*device, false) if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil { - return nil, err + return nil, _err } + log.Debugf("%#v", restResponse) return &restResponse.Data, nil } @@ -62,10 +65,13 @@ func (m *Manager) Add(options ...types.DeviceOption) (*lm.RestDevice, error) { // UpdateAndReplaceByID implements types.DeviceManager. func (m *Manager) UpdateAndReplaceByID(id int32, options ...types.DeviceOption) (*lm.RestDevice, error) { device := buildDevice(m.Config(), options...) + log.Debugf("%#v", device) + restResponse, apiResponse, err := m.LMClient.UpdateDevice(*device, id, "replace") if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil { - return nil, err + return nil, _err } + log.Debugf("%#v", restResponse) return &restResponse.Data, nil } @@ -94,10 +100,13 @@ func (m *Manager) UpdateAndReplaceByName(name string, options ...types.DeviceOpt // UpdateAndReplaceFieldByID implements types.DeviceManager. func (m *Manager) UpdateAndReplaceFieldByID(id int32, field string, options ...types.DeviceOption) (*lm.RestDevice, error) { device := buildDevice(m.Config(), options...) + log.Debugf("%#v", device) + restResponse, apiResponse, err := m.LMClient.PatchDeviceById(*device, id, "replace", field) if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil { - return nil, err + return nil, _err } + log.Debugf("%#v", restResponse) return &restResponse.Data, nil } @@ -110,7 +119,7 @@ func (m *Manager) UpdateAndReplaceFieldByName(name string, field string, options } if d == nil { - log.Printf("Could not find device %q", name) + log.Infof("Could not find device %q", name) return nil, nil } @@ -138,7 +147,7 @@ func (m *Manager) DeleteByName(name string) error { // TODO: Should this return an error? if d == nil { - log.Printf("Could not find device %q", name) + log.Infof("Could not find device %q", name) return nil } @@ -157,6 +166,7 @@ func find(field, name string, client *lm.DefaultApi) (*lm.RestDevice, error) { if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil { return nil, _err } + log.Debugf("%#v", restResponse) if restResponse.Data.Total == 1 { return &restResponse.Data.Items[0], nil } diff --git a/pkg/utilities/utilities.go b/pkg/utilities/utilities.go index 73e0c53b7..f363b9ad3 100644 --- a/pkg/utilities/utilities.go +++ b/pkg/utilities/utilities.go @@ -45,16 +45,16 @@ func CheckAllErrors(restResponse interface{}, apiResponse *logicmonitor.APIRespo } } + if restResponseStatus != http.StatusOK { + metrics.RESTError() + return fmt.Errorf("[REST] [%d] %s", restResponseStatus, restResponseMessage) + } + if apiResponse.StatusCode != http.StatusOK { metrics.APIError() return fmt.Errorf("[API] [%d] %s", apiResponse.StatusCode, restResponseMessage) } - if http.StatusOK != restResponseStatus { - metrics.RESTError() - return fmt.Errorf("[REST] [%d] %s", restResponseStatus, restResponseMessage) - } - if err != nil { return fmt.Errorf("[ERROR] %v", err) } diff --git a/pkg/watch/pod/pod.go b/pkg/watch/pod/pod.go index f9fc3d589..c7ba3bef7 100644 --- a/pkg/watch/pod/pod.go +++ b/pkg/watch/pod/pod.go @@ -106,11 +106,7 @@ func (w *Watcher) update(old, new *v1.Pod) { } func (w *Watcher) move(pod *v1.Pod) { - if _, err := w.UpdateAndReplaceFieldByName( - pod.Name, - constants.CustomPropertiesFieldName, - w.args(pod, constants.PodDeletedCategory)..., - ); err != nil { + if _, err := w.UpdateAndReplaceFieldByName(pod.Name, constants.CustomPropertiesFieldName, w.args(pod, constants.PodDeletedCategory)...); err != nil { log.Errorf("Failed to move pod %q: %v", pod.Name, err) return } diff --git a/pkg/watch/service/service.go b/pkg/watch/service/service.go index f221fddd7..31962c6f3 100644 --- a/pkg/watch/service/service.go +++ b/pkg/watch/service/service.go @@ -114,11 +114,7 @@ func (w *Watcher) update(old, new *v1.Service) { } func (w *Watcher) move(service *v1.Service) { - if _, err := w.UpdateAndReplaceFieldByName( - service.Name, - constants.CustomPropertiesFieldName, - w.args(service, constants.ServiceDeletedCategory)..., - ); err != nil { + if _, err := w.UpdateAndReplaceFieldByName(service.Name, constants.CustomPropertiesFieldName, w.args(service, constants.ServiceDeletedCategory)...); err != nil { log.Errorf("Failed to move service %q: %v", service.Name, err) return }