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 11, 2020
1 parent 6a9ed8a commit 0b10c80
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// allCmd represents the all command
var allCmd = &cobra.Command{
Use: "all",
Short: "Get all known hardware for facility",
Short: "get all known hardware for facility",
Run: func(cmd *cobra.Command, args []string) {
alls, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})
if err != nil {
Expand Down
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"
)

// deleteCmd 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)
}
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// idCmd represents the id command
var idCmd = &cobra.Command{
Use: "id",
Short: "Get hardware by id",
Short: "get hardware by id",
Example: "tink hardware id 224ee6ab-ad62-4070-a900-ed816444cec0 cb76ae54-93e9-401c-a5b2-d455bb3800b1",
Args: func(_ *cobra.Command, args []string) error {
return verifyUUIDs(args)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// ipCmd represents the ip command
var ipCmd = &cobra.Command{
Use: "ip",
Short: "Get hardware by any associated ip",
Short: "get hardware by any associated ip",
Example: "tink hardware ip 10.0.0.2 10.0.0.3",
Args: func(_ *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// macCmd represents the mac command
var macCmd = &cobra.Command{
Use: "mac",
Short: "Get hardware by any associated mac",
Short: "get hardware by any associated mac",
Example: "tink hardware mac 00:00:00:00:00:01 00:00:00:00:00:02",
Args: func(_ *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push",
Short: "Push new hardware to tinkerbell",
Short: "push new hardware to tinkerbell",
Example: `cat /tmp/data.json | tink hardware push
tink hardware push --file /tmp/data.json`,
PreRunE: func(c *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// watchCmd represents the watch command
var watchCmd = &cobra.Command{
Use: "watch",
Short: "Register to watch an id for any changes",
Short: "register to watch an id for any changes",
Example: "tink hardware watch 224ee6ab-ad62-4070-a900-ed816444cec0 cb76ae54-93e9-401c-a5b2-d455bb3800b1",
Args: func(_ *cobra.Command, args []string) error {
return verifyUUIDs(args)
Expand Down
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 0b10c80

Please sign in to comment.