From f171b15f4fe9569ab2306ed332bd58b6788e94e0 Mon Sep 17 00:00:00 2001 From: Adam Stephens Date: Mon, 25 Mar 2024 21:13:27 -0400 Subject: [PATCH] lxc: add completions for network zones Signed-off-by: Adam Stephens (cherry picked from commit 7b359e934edc6f3b170176f44f71e54ba2f638f7) Signed-off-by: Kadin Sayani License: Apache-2.0 --- lxc/completion.go | 103 +++++++++++++++++++++++ lxc/network_zone.go | 200 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 303 insertions(+) diff --git a/lxc/completion.go b/lxc/completion.go index 7071289f97cb..b2a06ed1c00c 100644 --- a/lxc/completion.go +++ b/lxc/completion.go @@ -608,6 +608,109 @@ func (g *cmdGlobal) cmpNetworkProfiles(networkName string) ([]string, cobra.Shel return results, cobra.ShellCompDirectiveNoFileComp } +func (g *cmdGlobal) cmpNetworkZoneConfigs(zoneName string) ([]string, cobra.ShellCompDirective) { + // Parse remote + resources, err := g.ParseServers(zoneName) + if err != nil || len(resources) == 0 { + return nil, cobra.ShellCompDirectiveError + } + + resource := resources[0] + client := resource.server + + zone, _, err := client.GetNetworkZone(zoneName) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + var results []string + for k := range zone.Config { + results = append(results, k) + } + + return results, cobra.ShellCompDirectiveNoFileComp +} + +func (g *cmdGlobal) cmpNetworkZoneRecordConfigs(zoneName string, recordName string) ([]string, cobra.ShellCompDirective) { + var results []string + cmpDirectives := cobra.ShellCompDirectiveNoFileComp + + resources, _ := g.ParseServers(zoneName) + + if len(resources) <= 0 { + return nil, cobra.ShellCompDirectiveError + } + + resource := resources[0] + + peer, _, err := resource.server.GetNetworkZoneRecord(resource.name, recordName) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + for k := range peer.Config { + results = append(results, k) + } + + return results, cmpDirectives +} + +func (g *cmdGlobal) cmpNetworkZoneRecords(zoneName string) ([]string, cobra.ShellCompDirective) { + var results []string + cmpDirectives := cobra.ShellCompDirectiveNoFileComp + + resources, _ := g.ParseServers(zoneName) + + if len(resources) <= 0 { + return nil, cobra.ShellCompDirectiveError + } + + resource := resources[0] + + results, err := resource.server.GetNetworkZoneRecordNames(zoneName) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + return results, cmpDirectives +} + +func (g *cmdGlobal) cmpNetworkZones(toComplete string) ([]string, cobra.ShellCompDirective) { + var results []string + cmpDirectives := cobra.ShellCompDirectiveNoFileComp + + resources, _ := g.ParseServers(toComplete) + + if len(resources) > 0 { + resource := resources[0] + + zones, err := resource.server.GetNetworkZoneNames() + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + for _, project := range zones { + var name string + + if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) { + name = project + } else { + name = fmt.Sprintf("%s:%s", resource.remote, project) + } + + results = append(results, name) + } + } + + if !strings.Contains(toComplete, ":") { + remotes, directives := g.cmpRemotes(false) + results = append(results, remotes...) + cmpDirectives |= directives + } + + return results, cmpDirectives +} + func (g *cmdGlobal) cmpProfileConfigs(profileName string) ([]string, cobra.ShellCompDirective) { resources, err := g.ParseServers(profileName) if err != nil || len(resources) == 0 { diff --git a/lxc/network_zone.go b/lxc/network_zone.go index edd2de12c0f6..0b3fcf628500 100644 --- a/lxc/network_zone.go +++ b/lxc/network_zone.go @@ -88,6 +88,14 @@ func (c *cmdNetworkZoneList) command() *cobra.Command { cmd.RunE = c.run cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``") + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpRemotes(false) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -157,6 +165,14 @@ func (c *cmdNetworkZoneShow) command() *cobra.Command { cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Show network zone configurations")) cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -213,6 +229,19 @@ func (c *cmdNetworkZoneGet) command() *cobra.Command { cmd.RunE = c.run cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Get the key as a network zone property")) + + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneConfigs(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -277,6 +306,14 @@ lxc network zone create z1 < config.yaml cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -365,6 +402,14 @@ For backward compatibility, a single configuration key may still be set with: cmd.RunE = c.run cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Set the key as a network zone property")) + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -441,6 +486,18 @@ func (c *cmdNetworkZoneUnset) command() *cobra.Command { cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Unset the key as a network zone property")) + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneConfigs(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -471,6 +528,14 @@ func (c *cmdNetworkZoneEdit) command() *cobra.Command { cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -589,6 +654,14 @@ func (c *cmdNetworkZoneDelete) command() *cobra.Command { cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Delete network zones")) cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -696,6 +769,14 @@ func (c *cmdNetworkZoneRecordList) command() *cobra.Command { cmd.RunE = c.run cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``") + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -764,6 +845,18 @@ func (c *cmdNetworkZoneRecordShow) command() *cobra.Command { cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Show network zone record configurations")) cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -817,6 +910,23 @@ func (c *cmdNetworkZoneRecordGet) command() *cobra.Command { cmd.RunE = c.run cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Get the key as a network zone record property")) + + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + if len(args) == 2 { + return c.global.cmpNetworkZoneRecordConfigs(args[0], args[1]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -880,6 +990,18 @@ lxc network zone record create z1 r1 < config.yaml cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -964,6 +1086,19 @@ func (c *cmdNetworkZoneRecordSet) command() *cobra.Command { cmd.RunE = c.run cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Set the key as a network zone record property")) + + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -1038,6 +1173,23 @@ func (c *cmdNetworkZoneRecordUnset) command() *cobra.Command { cmd.RunE = c.run cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Unset the key as a network zone record property")) + + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + if len(args) == 2 { + return c.global.cmpNetworkZoneRecordConfigs(args[0], args[1]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -1068,6 +1220,18 @@ func (c *cmdNetworkZoneRecordEdit) command() *cobra.Command { cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -1185,6 +1349,18 @@ func (c *cmdNetworkZoneRecordDelete) command() *cobra.Command { cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Delete network zone record")) cmd.RunE = c.run + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -1250,6 +1426,18 @@ func (c *cmdNetworkZoneRecordEntry) commandAdd() *cobra.Command { cmd.RunE = c.runAdd cmd.Flags().Uint64Var(&c.flagTTL, "ttl", 0, i18n.G("Entry TTL")+"``") + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd } @@ -1295,6 +1483,18 @@ func (c *cmdNetworkZoneRecordEntry) commandRemove() *cobra.Command { cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Remove entries from a network zone record")) cmd.RunE = c.runRemove + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return c.global.cmpNetworkZones(toComplete) + } + + if len(args) == 1 { + return c.global.cmpNetworkZoneRecords(args[0]) + } + + return nil, cobra.ShellCompDirectiveNoFileComp + } + return cmd }