From c52da28ddd2aa0088c5923fd7f4b066888651aae Mon Sep 17 00:00:00 2001 From: Michal Fiedorowicz Date: Tue, 27 Feb 2024 13:23:34 +0000 Subject: [PATCH] feat: OBS-402 - store NetBox object state in redis Signed-off-by: Michal Fiedorowicz --- diode-server/reconciler/component.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/diode-server/reconciler/component.go b/diode-server/reconciler/component.go index 3676b94b..7fa3590e 100644 --- a/diode-server/reconciler/component.go +++ b/diode-server/reconciler/component.go @@ -2,6 +2,7 @@ package reconciler import ( "context" + "encoding/json" "fmt" "log/slog" "net" @@ -118,3 +119,29 @@ func validateRetrieveIngestionDataSourcesRequest(in *pb.RetrieveIngestionDataSou } return nil } + +// AddObjectState adds an object state +func (c *Component) AddObjectState(ctx context.Context, in *pb.AddObjectStateRequest) (*pb.AddObjectStateResponse, error) { + if err := in.ValidateAll(); err != nil { + return nil, err + } + + key := fmt.Sprintf("netbox-object-state.%s:%d", in.GetObjectType(), in.GetObjectId()) + val := map[string]interface{}{ + "object_id": in.GetObjectId(), + "object_type": in.GetObjectType(), + "object_change_id": in.GetObjectChangeId(), + "object": in.GetObject().GetObject(), + } + encodedValue, err := json.Marshal(val) + if err != nil { + c.logger.Error("failed to marshal JSON", "value", val, "error", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) + } + if _, err = c.redisClient.Do(ctx, "JSON.SET", key, "$", encodedValue).Result(); err != nil { + c.logger.Error("failed to set JSON redis key", "key", key, "value", encodedValue, "error", err) + return nil, fmt.Errorf("failed to set JSON redis key: %v", err) + } + + return &pb.AddObjectStateResponse{}, nil +}