Skip to content

Commit e3c814a

Browse files
committed
changed hardware push request data to be type *Hardware
1 parent e25c2c3 commit e3c814a

File tree

5 files changed

+123
-123
lines changed

5 files changed

+123
-123
lines changed

cli/tink/cmd/hardware/push.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ var pushCmd = &cobra.Command{
3434
},
3535
Run: func(cmd *cobra.Command, args []string) {
3636
for _, j := range args {
37-
if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: j}); err != nil {
37+
hw := hardware.Hardware{}
38+
err := json.Unmarshal([]byte(j), &hw)
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: &hw}); err != nil {
3843
log.Fatal(err)
3944
}
4045
}

grpc-server/hardware.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,31 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware.
2323
// must be a copy so deferred cacheInFlight.Dec matches the Inc
2424
labels = prometheus.Labels{"method": "Push", "op": ""}
2525

26-
var h struct {
27-
ID string
28-
State string
29-
}
30-
err := json.Unmarshal([]byte(in.Data), &h)
31-
if err != nil {
26+
hw := in.Data
27+
if hw.Id == "" {
3228
metrics.CacheTotals.With(labels).Inc()
3329
metrics.CacheErrors.With(labels).Inc()
34-
err = errors.Wrap(err, "unmarshal json")
30+
err := errors.New("id must be set to a UUID")
3531
logger.Error(err)
3632
return &hardware.Empty{}, err
3733
}
3834

39-
if h.ID == "" {
40-
metrics.CacheTotals.With(labels).Inc()
41-
metrics.CacheErrors.With(labels).Inc()
42-
err = errors.New("id must be set to a UUID")
43-
logger.Error(err)
44-
return &hardware.Empty{}, err
45-
}
46-
47-
logger.With("id", h.ID).Info("data pushed")
35+
logger.With("id", hw.Id).Info("data pushed")
4836

4937
var fn func() error
5038
msg := ""
51-
if h.State != "deleted" {
39+
data, err := json.Marshal(hw)
40+
if err != nil {
41+
logger.Error(err)
42+
}
43+
if hw.Metadata.State != "deleted" {
5244
labels["op"] = "insert"
5345
msg = "inserting into DB"
54-
fn = func() error { return db.InsertIntoDB(ctx, s.db, in.Data) }
46+
fn = func() error { return db.InsertIntoDB(ctx, s.db, string(data)) }
5547
} else {
5648
msg = "deleting from DB"
5749
labels["op"] = "delete"
58-
fn = func() error { return db.DeleteFromDB(ctx, s.db, h.ID) }
50+
fn = func() error { return db.DeleteFromDB(ctx, s.db, hw.Id) }
5951
}
6052

6153
metrics.CacheTotals.With(labels).Inc()
@@ -75,12 +67,12 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware.
7567
}
7668

7769
s.watchLock.RLock()
78-
if ch := s.watch[h.ID]; ch != nil {
70+
if ch := s.watch[hw.Id]; ch != nil {
7971
select {
80-
case ch <- in.Data:
72+
case ch <- string(data):
8173
default:
8274
metrics.WatchMissTotal.Inc()
83-
logger.With("id", h.ID).Info("skipping blocked watcher")
75+
logger.With("id", hw.Id ).Info("skipping blocked watcher")
8476
}
8577
}
8678
s.watchLock.RUnlock()

0 commit comments

Comments
 (0)