Skip to content

Commit

Permalink
feat: OBS-402 - store NetBox object state in redis
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com>
  • Loading branch information
mfiedorowicz committed Feb 27, 2024
1 parent ebe8b44 commit c52da28
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions diode-server/reconciler/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reconciler

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
Expand Down Expand Up @@ -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
}

0 comments on commit c52da28

Please sign in to comment.