Skip to content

Commit

Permalink
lxc: add completions for network zones
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Stephens <[email protected]>
(cherry picked from commit 7b359e934edc6f3b170176f44f71e54ba2f638f7)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
adamcstephens authored and kadinsayani committed Sep 4, 2024
1 parent 49f1aa7 commit f171b15
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 0 deletions.
103 changes: 103 additions & 0 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit f171b15

Please sign in to comment.