Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incus: add completions for network acls #631

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (g *cmdGlobal) cmpClusterGroups(toComplete string) ([]string, cobra.ShellCo
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = group
} else {
name = fmt.Sprintf("%s:%s", resource.remote, name)
name = fmt.Sprintf("%s:%s", resource.remote, group)
}

results = append(results, name)
Expand Down Expand Up @@ -333,6 +333,78 @@ func (g *cmdGlobal) cmpInstanceNamesFromRemote(toComplete string) ([]string, cob
return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpNetworkACLConfigs(aclName string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(aclName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
client := resource.server

acl, _, err := client.GetNetworkACL(resource.name)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var results []string
for k := range acl.Config {
results = append(results, k)
}

return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellCompDirective) {
results := []string{}
cmpDirectives := cobra.ShellCompDirectiveNoFileComp

resources, _ := g.ParseServers(toComplete)

if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]

acls, err := resource.server.GetNetworkACLNames()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

for _, acl := range acls {
var name string

if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = acl
} else {
name = fmt.Sprintf("%s:%s", resource.remote, acl)
}

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) cmpNetworkACLRuleProperties() ([]string, cobra.ShellCompDirective) {
var results []string

allowedKeys := networkACLRuleJSONStructFieldMap()
for key := range allowedKeys {
results = append(results, fmt.Sprintf("%s=", key))
}

return results, cobra.ShellCompDirectiveNoSpace
}

func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDirective) {
results := []string{}
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
Expand Down
129 changes: 125 additions & 4 deletions cmd/incus/network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func (c *cmdNetworkACLList) 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
}

Expand Down Expand Up @@ -164,6 +172,14 @@ func (c *cmdNetworkACLShow) Command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Show network ACL 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.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -217,6 +233,14 @@ func (c *cmdNetworkACLShowLog) Command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Show network ACL log"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -267,6 +291,18 @@ func (c *cmdNetworkACLGet) Command() *cobra.Command {
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Get the key as a network ACL property"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

if len(args) == 1 {
return c.global.cmpNetworkACLConfigs(args[0])
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -327,6 +363,14 @@ func (c *cmdNetworkACLCreate) 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.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -417,6 +461,14 @@ For backward compatibility, a single configuration key may still be set with:
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Set the key as a network ACL property"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -492,6 +544,19 @@ func (c *cmdNetworkACLUnset) Command() *cobra.Command {
cmd.RunE = c.Run

cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Unset the key as a network ACL property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

if len(args) == 1 {
return c.global.cmpNetworkACLConfigs(args[0])
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -522,6 +587,14 @@ func (c *cmdNetworkACLEdit) 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.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -652,6 +725,14 @@ func (c *cmdNetworkACLRename) Command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Rename network ACLs"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -701,6 +782,14 @@ func (c *cmdNetworkACLDelete) Command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Delete network ACLs"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -765,11 +854,27 @@ func (c *cmdNetworkACLRule) CommandAdd() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Add rules to an ACL"))
cmd.RunE = c.RunAdd

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

if len(args) == 1 {
return []string{"ingress", "egress"}, cobra.ShellCompDirectiveNoFileComp
}

if len(args) == 2 {
return c.global.cmpNetworkACLRuleProperties()
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

// ruleJSONStructFieldMap returns a map of JSON tag names to struct field indices for api.NetworkACLRule.
func (c *cmdNetworkACLRule) ruleJSONStructFieldMap() map[string]int {
// networkACLRuleJSONStructFieldMap returns a map of JSON tag names to struct field indices for api.NetworkACLRule.
func networkACLRuleJSONStructFieldMap() map[string]int {
// Use reflect to get field names in rule from json tags.
ruleType := reflect.TypeOf(api.NetworkACLRule{})
allowedKeys := make(map[string]int, ruleType.NumField())
Expand Down Expand Up @@ -801,7 +906,7 @@ func (c *cmdNetworkACLRule) ruleJSONStructFieldMap() map[string]int {
// parseConfigKeysToRule converts a map of key/value pairs into an api.NetworkACLRule using reflection.
func (c *cmdNetworkACLRule) parseConfigToRule(config map[string]string) (*api.NetworkACLRule, error) {
// Use reflect to get struct field indices in NetworkACLRule for json tags.
allowedKeys := c.ruleJSONStructFieldMap()
allowedKeys := networkACLRuleJSONStructFieldMap()

// Initialise new rule.
rule := api.NetworkACLRule{}
Expand Down Expand Up @@ -888,6 +993,22 @@ func (c *cmdNetworkACLRule) CommandRemove() *cobra.Command {

cmd.RunE = c.RunRemove

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworkACLs(toComplete)
}

if len(args) == 1 {
return []string{"ingress", "egress"}, cobra.ShellCompDirectiveNoFileComp
}

if len(args) == 2 {
return c.global.cmpNetworkACLRuleProperties()
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -923,7 +1044,7 @@ func (c *cmdNetworkACLRule) RunRemove(cmd *cobra.Command, args []string) error {
}

// Use reflect to get struct field indices in NetworkACLRule for json tags.
allowedKeys := c.ruleJSONStructFieldMap()
allowedKeys := networkACLRuleJSONStructFieldMap()

// Check the supplied filters match possible fields.
for k := range filters {
Expand Down
Loading