[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lxc-devel
Subject:    [lxc-devel] =?utf-8?q?=5Blxd/master=5D_lxc/image=3A_Add_support_?= =?utf-8?q?for_directly_getting=2C
From:       tbobo on Github <lxc-bot () linuxcontainers ! org>
Date:       2020-12-16 2:44:15
Message-ID: 5fd9747f.1c69fb81.d358e.6cbfSMTPIN_ADDED_MISSING () mx ! google ! com
[Download RAW message or body]

[Attachment #2 (text/x-mailbox)]

The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/8259

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
…age properties

Signed-off-by: Tate Song <tlsong54321@gmail.com>

[Attachment #3 (text/plain)]

From 92c6b68379fb97f027aa440e157ce8b727b15d7f Mon Sep 17 00:00:00 2001
From: Tate Song <tlsong54321@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@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)
+}

[Attachment #4 (text/plain)]

_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic