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 storage pools and volumes #597

Merged
merged 2 commits into from
Mar 9, 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
277 changes: 274 additions & 3 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
return results, cobra.ShellCompDirectiveNoFileComp
}

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

resources, _ := g.ParseServers(toComplete)

if len(resources) > 0 {
resource := resources[0]

containers, _ := resource.server.GetInstanceNames("container")
results = append(results, containers...)
vms, _ := resource.server.GetInstanceNames("virtual-machine")
results = append(results, vms...)
}

return results, cobra.ShellCompDirectiveNoFileComp
}

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

Expand Down Expand Up @@ -158,7 +175,7 @@ func (g *cmdGlobal) cmpNetworkConfigs(networkName string) ([]string, cobra.Shell
results = append(results, k)
}

return results, cobra.ShellCompDirectiveError
return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpNetworkInstances(networkName string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -186,7 +203,7 @@ func (g *cmdGlobal) cmpNetworkInstances(networkName string) ([]string, cobra.She
}
}

return results, cobra.ShellCompDirectiveError
return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpNetworkProfiles(networkName string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -214,7 +231,7 @@ func (g *cmdGlobal) cmpNetworkProfiles(networkName string) ([]string, cobra.Shel
}
}

return results, cobra.ShellCompDirectiveError
return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpProfileConfigs(profileName string) ([]string, cobra.ShellCompDirective) {
Expand All @@ -239,6 +256,21 @@ func (g *cmdGlobal) cmpProfileConfigs(profileName string) ([]string, cobra.Shell
return configs, cobra.ShellCompDirectiveNoFileComp
}

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

resources, _ := g.ParseServers(toComplete)

if len(resources) > 0 {
resource := resources[0]

profiles, _ := resource.server.GetProfileNames()
results = append(results, profiles...)
}

return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpProfiles(toComplete string, includeRemotes bool) ([]string, cobra.ShellCompDirective) {
results := []string{}

Expand Down Expand Up @@ -349,3 +381,242 @@ func (g *cmdGlobal) cmpRemoteNames() ([]string, cobra.ShellCompDirective) {

return results, cobra.ShellCompDirectiveNoFileComp
}

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

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

if strings.Contains(poolName, ":") {
poolName = strings.Split(poolName, ":")[1]
}

pool, _, err := client.GetStoragePool(poolName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

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

return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpStoragePoolWithVolume(toComplete string) ([]string, cobra.ShellCompDirective) {
if !strings.Contains(toComplete, "/") {
pools, compdir := g.cmpStoragePools(toComplete)
if compdir == cobra.ShellCompDirectiveError {
return nil, compdir
}

results := []string{}
for _, pool := range pools {
if strings.HasSuffix(pool, ":") {
results = append(results, pool)
} else {
results = append(results, fmt.Sprintf("%s/", pool))
}
}

return results, cobra.ShellCompDirectiveNoSpace
}

pool := strings.Split(toComplete, "/")[0]
volumes, compdir := g.cmpStoragePoolVolumes(pool)
if compdir == cobra.ShellCompDirectiveError {
return nil, compdir
}

results := []string{}
for _, volume := range volumes {
results = append(results, fmt.Sprintf("%s/%s", pool, volume))
}

return results, cobra.ShellCompDirectiveNoFileComp
}

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

resources, _ := g.ParseServers(toComplete)

if len(resources) > 0 {
resource := resources[0]

storagePools, _ := resource.server.GetStoragePoolNames()

for _, storage := range storagePools {
var name string

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

results = append(results, name)
}
}

if !strings.Contains(toComplete, ":") {
remotes, _ := g.cmpRemotes(false)
results = append(results, remotes...)
}

return results, cobra.ShellCompDirectiveNoFileComp
}

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

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

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
}

volName, volType := parseVolume("custom", volumeName)

volume, _, err := client.GetStoragePoolVolume(pool, volType, volName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

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

return results, cobra.ShellCompDirectiveNoFileComp
}

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

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

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
}

volName, volType := parseVolume("custom", volumeName)

volume, _, err := client.GetStoragePoolVolume(pool, volType, volName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var results []string
for _, i := range volume.UsedBy {
r := regexp.MustCompile(`/1.0/instances/(.*)`)
match := r.FindStringSubmatch(i)

if len(match) == 2 {
results = append(results, match[1])
}
}

return results, cobra.ShellCompDirectiveNoFileComp
}

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

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

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
}

volName, volType := parseVolume("custom", volumeName)

volume, _, err := client.GetStoragePoolVolume(pool, volType, volName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var results []string
for _, i := range volume.UsedBy {
r := regexp.MustCompile(`/1.0/profiles/(.*)`)
match := r.FindStringSubmatch(i)

if len(match) == 2 {
results = append(results, match[1])
}
}

return results, cobra.ShellCompDirectiveNoFileComp
}

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

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

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
}

volName, volType := parseVolume("custom", volumeName)

snapshots, err := client.GetStoragePoolVolumeSnapshotNames(pool, volType, volName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

return snapshots, cobra.ShellCompDirectiveNoFileComp
}

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

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

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
}

volumes, err := client.GetStoragePoolVolumeNames(pool)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

return volumes, cobra.ShellCompDirectiveNoFileComp
}
Loading
Loading