-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
308 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package delete | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/tinkerbell/tink/client" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type Options struct { | ||
// DeleteByID is used to delete a resource | ||
DeleteByID func(context.Context, *client.FullClient, string) (interface{}, error) | ||
|
||
clientConnOpt *client.ConnOptions | ||
fullClient *client.FullClient | ||
} | ||
|
||
func (o *Options) SetClientConnOpt(co *client.ConnOptions) { | ||
o.clientConnOpt = co | ||
} | ||
|
||
func (o *Options) SetFullClient(cl *client.FullClient) { | ||
o.fullClient = cl | ||
} | ||
|
||
const shortDescr = "delete one or more resources" | ||
|
||
const longDescr = `Deletes one or more resources and prints the status of | ||
the deleted resource. | ||
# Delete template resource (success) | ||
tink template delete 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 | ||
Deleted 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 | ||
# Delete template resource (not found) | ||
tink template delete 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 | ||
Error 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 not found | ||
# Delete template resources (one not found) | ||
tink template delete 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 e4115856-4358-429d-a8f6-9e1b7d794b72 | ||
Deleted 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 | ||
Error e4115856-4358-429d-a8f6-9e1b7d794b72 not found | ||
# Delete resources and extract resource ID with awk | ||
tink template delete 8ae1cc24-6a9c-11eb-a0fc-0242ac120005 e4115856-4358-429d-a8f6-9e1b7d794b72 | awk {print $2} > result | ||
cat result | ||
8ae1cc24-6a9c-11eb-a0fc-0242ac120005 | ||
e4115856-4358-429d-a8f6-9e1b7d794b72 | ||
` | ||
|
||
const exampleDescr = `# Delete template resource | ||
tink template delete [id] | ||
# Delete hardware resource | ||
tink hardware delete [id] | ||
# Delete workflow resource | ||
tink workflow delete [id] | ||
# Delete multiple workflow resources | ||
tink workflow delete [id_1] [id_2] [id_3] | ||
` | ||
|
||
func NewDeleteCommand(opt Options) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete", | ||
Short: shortDescr, | ||
Long: longDescr, | ||
Example: exampleDescr, | ||
DisableFlagsInUseLine: true, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if opt.fullClient != nil { | ||
return nil | ||
} | ||
if opt.clientConnOpt == nil { | ||
opt.SetClientConnOpt(&client.ConnOptions{}) | ||
} | ||
opt.clientConnOpt.SetFlags(cmd.PersistentFlags()) | ||
return nil | ||
}, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if opt.fullClient == nil { | ||
var err error | ||
var conn *grpc.ClientConn | ||
conn, err = client.NewClientConn(opt.clientConnOpt) | ||
if err != nil { | ||
println("Flag based client configuration failed with err: %s. Trying with env var legacy method...", err) | ||
// Fallback to legacy Setup via env var | ||
conn, err = client.GetConnection() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to setup connection to tink-server") | ||
} | ||
} | ||
opt.SetFullClient(client.NewFullClient(conn)) | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if opt.DeleteByID == nil { | ||
return errors.New("DeleteByID is not implemented for this resource yet. Please have a look at the issue in GitHub or open a new one.") | ||
} | ||
for _, requestedID := range args { | ||
_, err := opt.DeleteByID(cmd.Context(), opt.fullClient, requestedID) | ||
if err != nil { | ||
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound { | ||
fmt.Fprintf(cmd.ErrOrStderr(), "Error\t%s\tnot found\n", requestedID) | ||
continue | ||
} else { | ||
return err | ||
} | ||
} | ||
fmt.Fprintf(cmd.OutOrStdout(), "Deleted\t%s\n", requestedID) | ||
} | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package delete | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tinkerbell/tink/client" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func TestNewDeleteCommand(t *testing.T) { | ||
table := []struct { | ||
name string | ||
expectedOutput string | ||
args []string | ||
opt Options | ||
}{ | ||
{ | ||
name: "happy-path", | ||
expectedOutput: "Deleted\tbeeb5c79\n", | ||
args: []string{"beeb5c79"}, | ||
opt: Options{ | ||
DeleteByID: func(c context.Context, fc *client.FullClient, s string) (interface{}, error) { | ||
return struct{}{}, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "happy-path-multiple-resources", | ||
expectedOutput: "Deleted\tbeeb5c79\nDeleted\t14810952\nDeleted\te7a91fe9\n", | ||
args: []string{"beeb5c79", "14810952", "e7a91fe9"}, | ||
opt: Options{ | ||
DeleteByID: func(c context.Context, fc *client.FullClient, s string) (interface{}, error) { | ||
return struct{}{}, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "resource-not-found", | ||
expectedOutput: "Error\tbeeb5c79\tnot found\n", | ||
args: []string{"beeb5c79"}, | ||
opt: Options{ | ||
DeleteByID: func(c context.Context, fc *client.FullClient, s string) (interface{}, error) { | ||
return struct{}{}, status.Error(codes.NotFound, "") | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple-resources-not-found", | ||
expectedOutput: "Error\tbeeb5c79\tnot found\nError\t14810952\tnot found\n", | ||
args: []string{"beeb5c79", "14810952"}, | ||
opt: Options{ | ||
DeleteByID: func(c context.Context, fc *client.FullClient, s string) (interface{}, error) { | ||
return struct{}{}, status.Error(codes.NotFound, "") | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "only-one-resource-of-two-was-deleted", | ||
expectedOutput: "Deleted\tbeeb5c79\nError\t14810952\tnot found\n", | ||
args: []string{"beeb5c79", "14810952"}, | ||
opt: Options{ | ||
DeleteByID: func(c context.Context, fc *client.FullClient, s string) (interface{}, error) { | ||
if s == "beeb5c79" { | ||
return struct{}{}, nil | ||
} | ||
return struct{}{}, status.Error(codes.NotFound, "") | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range table { | ||
t.Run(test.name, func(t *testing.T) { | ||
stdout := bytes.NewBufferString("") | ||
test.opt.SetFullClient(&client.FullClient{}) | ||
cmd := NewDeleteCommand(test.opt) | ||
cmd.SetOut(stdout) | ||
cmd.SetErr(stdout) | ||
cmd.SetArgs(test.args) | ||
err := cmd.Execute() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
out, err := ioutil.ReadAll(stdout) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if diff := cmp.Diff(string(out), test.expectedOutput); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package delete provides a reusable implementation of the Delete command | ||
// for the tink cli. The Delete command deletes resources. It is designed | ||
// to be extendible and usable across resources. | ||
package delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.