Skip to content

Commit

Permalink
feat(storage/virtio-blk): add delete virtio-blk cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <[email protected]>
  • Loading branch information
artek-koltun authored and sandersms committed Jan 16, 2024
1 parent 6acfd85 commit df0b7d4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/storage-virtio-blk.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,37 @@ func newCreateVirtioBlkCommand() *cobra.Command {

return cmd
}

func newDeleteVirtioBlkCommand() *cobra.Command {
name := ""
allowMissing := false
cmd := &cobra.Command{
Use: "blk",
Aliases: []string{"b"},
Short: "Deletes virtio-blk controller",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
cobra.CheckErr(err)

timeout, err := c.Flags().GetDuration(timeoutCmdLineArg)
cobra.CheckErr(err)

client, err := storage.New(addr)
cobra.CheckErr(err)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

err = client.DeleteVirtioBlk(ctx, name, allowMissing)
cobra.CheckErr(err)
},
}

cmd.Flags().StringVar(&name, "name", "", "name of deleted virtio-blk controller")
cmd.Flags().BoolVar(&allowMissing, "allowMissing", false, "cmd succeeds if attempts to delete a resource that is not present")

cobra.CheckErr(cmd.MarkFlagRequired("name"))

return cmd
}
18 changes: 18 additions & 0 deletions cmd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func newStorageDeleteCommand() *cobra.Command {
}

cmd.AddCommand(newDeleteNvmeCommand())
cmd.AddCommand(newDeleteVirtioCommand())

return cmd
}
Expand All @@ -129,6 +130,23 @@ func newDeleteNvmeCommand() *cobra.Command {
return cmd
}

func newDeleteVirtioCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "virtio",
Aliases: []string{"v"},
Short: "Deletes virtio resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
cobra.CheckErr(err)
},
}

cmd.AddCommand(newDeleteVirtioBlkCommand())

return cmd
}

func printResponse(response string) {
fmt.Fprintln(os.Stdout, response)
}
23 changes: 23 additions & 0 deletions storage/virtio_blk.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,26 @@ func (c *Client) CreateVirtioBlk(

return response, err
}

// DeleteVirtioBlk deletes a virtio-blk controller
func (c *Client) DeleteVirtioBlk(
ctx context.Context,
name string,
allowMissing bool,
) error {
conn, connClose, err := c.connector.NewConn()
if err != nil {
return err
}
defer connClose()

client := c.createVirtioBlkClient(conn)
_, err = client.DeleteVirtioBlk(
ctx,
&pb.DeleteVirtioBlkRequest{
Name: name,
AllowMissing: allowMissing,
})

return err
}

0 comments on commit df0b7d4

Please sign in to comment.