Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
fix(device): set custom properties only if value is not empty (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Sep 22, 2017
1 parent d123866 commit 4312009
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
13 changes: 9 additions & 4 deletions pkg/device/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
22 changes: 16 additions & 6 deletions pkg/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package device

import (
"fmt"
"log"

"github.com/logicmonitor/k8s-argus/pkg/config"
"github.com/logicmonitor/k8s-argus/pkg/device/builder"
"github.com/logicmonitor/k8s-argus/pkg/types"

"github.com/logicmonitor/k8s-argus/pkg/utilities"
lm "github.com/logicmonitor/lm-sdk-go"
log "github.com/sirupsen/logrus"
)

// Manager implements types.DeviceManager
Expand Down Expand Up @@ -51,21 +51,27 @@ 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
}

// 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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/utilities/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/watch/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/watch/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4312009

Please sign in to comment.