Skip to content

Commit

Permalink
Added 'delete' command in 'tink hardware' cli
Browse files Browse the repository at this point in the history
  • Loading branch information
parauliya committed Jun 4, 2020
1 parent 28fe0aa commit 5f6521f
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 23 deletions.
35 changes: 35 additions & 0 deletions cmd/tink-cli/cmd/hardware/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © 2018 packet.net

package hardware

import (
"context"
"log"

"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/hardware"
)

// idCmd represents the id command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete hardware by id",
Example: "tink hardware delete 224ee6ab-ad62-4070-a900-ed816444cec0 cb76ae54-93e9-401c-a5b2-d455bb3800b1",
Args: func(_ *cobra.Command, args []string) error {
return verifyUUIDs(args)
},
Run: func(cmd *cobra.Command, args []string) {
for _, id := range args {
_, err := client.HardwareClient.Delete(context.Background(), &hardware.DeleteRequest{ID: id})
if err != nil {
log.Fatal(err)
}
log.Println("Hardware data with id", id, "deleted successfully")
}
},
}

func init() {
SubCommands = append(SubCommands, deleteCmd)
}
54 changes: 54 additions & 0 deletions grpc-server/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,57 @@ func (s *server) Cert() []byte {
func (s *server) ModTime() time.Time {
return s.modT
}

func (s *server) Delete(ctx context.Context, in *hardware.DeleteRequest) (*hardware.Empty, error) {
logger.Info("delete")
labels := prometheus.Labels{"method": "Delete", "op": ""}
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

// must be a copy so deferred cacheInFlight.Dec matches the Inc
labels = prometheus.Labels{"method": "Delete", "op": ""}

if in.ID == "" {
metrics.CacheTotals.With(labels).Inc()
metrics.CacheErrors.With(labels).Inc()
err := errors.New("id must be set to a UUID")
logger.Error(err)
return &hardware.Empty{}, err
}

logger.With("id", in.ID).Info("data deleted")

var fn func() error
labels["op"] = "delete"
msg := "deleting into DB"
fn = func() error { return db.DeleteFromDB(ctx, s.db, in.ID) }

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err := fn()
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
if pqErr := db.Error(err); pqErr != nil {
l = l.With("detail", pqErr.Detail, "where", pqErr.Where)
}
l.Error(err)
}

s.watchLock.RLock()
if ch := s.watch[in.ID]; ch != nil {
select {
case ch <- in.ID:
default:
metrics.WatchMissTotal.Inc()
logger.With("id", in.ID).Info("skipping blocked watcher")
}
}
s.watchLock.RUnlock()

return &hardware.Empty{}, err
}
121 changes: 100 additions & 21 deletions protos/hardware/hardware.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions protos/hardware/hardware.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ service HardwareService {
rpc All(Empty) returns (stream Hardware);
rpc Ingest(Empty) returns (Empty);
rpc Watch(GetRequest) returns (stream Hardware);
rpc Delete(DeleteRequest) returns (Empty);
}

message PushRequest {
Expand All @@ -30,3 +31,7 @@ message GetRequest {
message Hardware {
string JSON = 1;
}

message DeleteRequest {
string ID = 1;
}
3 changes: 2 additions & 1 deletion protos/template/template.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion protos/workflow/workflow.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f6521f

Please sign in to comment.