[lxc-devel] [lxd/master] lxc/image: Add support for directly getting, setting and unsetting im…

tbobo on Github lxc-bot at linuxcontainers.org
Wed Dec 16 02:44:15 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201215/efa00038/attachment-0001.bin>
-------------- next part --------------
From 92c6b68379fb97f027aa440e157ce8b727b15d7f Mon Sep 17 00:00:00 2001
From: Tate Song <tlsong54321 at gmail.com>
Date: Tue, 15 Dec 2020 20:35:51 -0600
Subject: [PATCH] lxc/image: Add support for directly getting, setting and
 unsetting image properties

Signed-off-by: Tate Song <tlsong54321 at gmail.com>
---
 lxc/image.go | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 149 insertions(+)

diff --git a/lxc/image.go b/lxc/image.go
index 8d3332ac53..9849a3ec79 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -92,6 +92,18 @@ hash or alias name (if one is set).`))
 	imageShowCmd := cmdImageShow{global: c.global, image: c}
 	cmd.AddCommand(imageShowCmd.Command())
 
+	// Get-property
+	imageGetPropCmd := cmdImageGetProp{global: c.global, image: c}
+	cmd.AddCommand(imageGetPropCmd.Command())
+
+	// Set-property
+	imageSetPropCmd := cmdImageSetProp{global: c.global, image: c}
+	cmd.AddCommand(imageSetPropCmd.Command())
+
+	// Unset-property
+	imageUnsetPropCmd := cmdImageUnsetProp{global: c.global, image: c, imageSetProp: &imageSetPropCmd}
+	cmd.AddCommand(imageUnsetPropCmd.Command())
+
 	return cmd
 }
 
@@ -1396,3 +1408,140 @@ func (c *cmdImageShow) Run(cmd *cobra.Command, args []string) error {
 
 	return nil
 }
+
+type cmdImageGetProp struct {
+	global *cmdGlobal
+	image  *cmdImage
+}
+
+func (c *cmdImageGetProp) Command() *cobra.Command {
+	cmd := &cobra.Command{}
+	cmd.Use = usage("get-property", i18n.G("[<remote>:]<image> <key>"))
+	cmd.Short = i18n.G("Get image properties")
+	cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
+		`Get image properties`))
+
+	cmd.RunE = c.Run
+
+	return cmd
+}
+
+func (c *cmdImageGetProp) Run(cmd *cobra.Command, args []string) error {
+	// Sanity checks
+	exit, err := c.global.CheckArgs(cmd, args, 2, 2)
+	if exit {
+		return err
+	}
+
+	// Parse remote
+	remoteName, name, err := c.global.conf.ParseRemote(args[0])
+	if err != nil {
+		return err
+	}
+
+	remoteServer, err := c.global.conf.GetImageServer(remoteName)
+	if err != nil {
+		return err
+	}
+
+	// Get the corresponding property
+	image := c.image.dereferenceAlias(remoteServer, "", name)
+	info, _, err := remoteServer.GetImage(image)
+	if err != nil {
+		return err
+	}
+
+	properties := info.Writable()
+	prop, propFound := properties.Properties[args[1]]
+	if !propFound {
+		return fmt.Errorf(i18n.G("Property not found"))
+	}
+
+	fmt.Println(prop)
+
+	return nil
+}
+
+type cmdImageSetProp struct {
+	global *cmdGlobal
+	image  *cmdImage
+}
+
+func (c *cmdImageSetProp) Command() *cobra.Command {
+	cmd := &cobra.Command{}
+	cmd.Use = usage("set-property", i18n.G("[<remote>:]<image> <key> <value>"))
+	cmd.Short = i18n.G("Set image properties")
+	cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
+		`Set image properties`))
+
+	cmd.RunE = c.Run
+
+	return cmd
+}
+
+func (c *cmdImageSetProp) Run(cmd *cobra.Command, args []string) error {
+	// Sanity checks
+	exit, err := c.global.CheckArgs(cmd, args, 3, 3)
+	if exit {
+		return err
+	}
+
+	// Parse remote
+	resources, err := c.global.ParseServers(args[0])
+	if err != nil {
+		return err
+	}
+
+	resource := resources[0]
+
+	if resource.name == "" {
+		return fmt.Errorf(i18n.G("Image identifier missing: %s"), args[0])
+	}
+
+	// Show properties
+	image := c.image.dereferenceAlias(resource.server, "", resource.name)
+	info, etag, err := resource.server.GetImage(image)
+	if err != nil {
+		return err
+	}
+
+	properties := info.Writable()
+	properties.Properties[args[1]] = args[2]
+
+	// Update image
+	err = resource.server.UpdateImage(image, properties, etag)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+type cmdImageUnsetProp struct {
+	global       *cmdGlobal
+	image        *cmdImage
+	imageSetProp *cmdImageSetProp
+}
+
+func (c *cmdImageUnsetProp) Command() *cobra.Command {
+	cmd := &cobra.Command{}
+	cmd.Use = usage("unset-property", i18n.G("[<remote>:]<image> <key>"))
+	cmd.Short = i18n.G("Unset image properties")
+	cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
+		`Unset image properties`))
+
+	cmd.RunE = c.Run
+
+	return cmd
+}
+
+func (c *cmdImageUnsetProp) Run(cmd *cobra.Command, args []string) error {
+	// Sanity checks
+	exit, err := c.global.CheckArgs(cmd, args, 2, 2)
+	if exit {
+		return err
+	}
+
+	args = append(args, "")
+	return c.imageSetProp.Run(cmd, args)
+}


More information about the lxc-devel mailing list