virsh: Add mac completion to net-dhcp-leases command

Signed-off-by: Lin Ma <lma@suse.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Lin Ma 2020-11-10 17:51:00 +08:00 committed by Michal Privoznik
parent 8a4343a436
commit aa54cb9d35
3 changed files with 50 additions and 1 deletions

View File

@ -174,3 +174,47 @@ virshNetworkUUIDCompleter(vshControl *ctl,
g_free(nets);
return ret;
}
char **
virshNetworkDhcpMacCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags)
{
virshControlPtr priv = ctl->privData;
virNetworkDHCPLeasePtr *leases = NULL;
virNetworkPtr network = NULL;
int nleases;
size_t i = 0;
char **ret = NULL;
VIR_AUTOSTRINGLIST tmp = NULL;
virCheckFlags(0, NULL);
if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
return NULL;
if (!(network = virshCommandOptNetwork(ctl, cmd, NULL)))
return NULL;
if ((nleases = virNetworkGetDHCPLeases(network, NULL, &leases, flags)) < 0)
goto cleanup;
tmp = g_new0(char *, nleases + 1);
for (i = 0; i < nleases; i++) {
virNetworkDHCPLeasePtr lease = leases[i];
tmp[i] = g_strdup(lease->mac);
}
ret = g_steal_pointer(&tmp);
cleanup:
if (leases) {
for (i = 0; i < nleases; i++)
virNetworkDHCPLeaseFree(leases[i]);
VIR_FREE(leases);
}
virNetworkFree(network);
return ret;
}

View File

@ -37,3 +37,7 @@ char ** virshNetworkPortUUIDCompleter(vshControl *ctl,
char ** virshNetworkUUIDCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
char ** virshNetworkDhcpMacCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);

View File

@ -1376,7 +1376,8 @@ static const vshCmdOptDef opts_network_dhcp_leases[] = {
{.name = "mac",
.type = VSH_OT_STRING,
.flags = VSH_OFLAG_NONE,
.help = N_("MAC address")
.help = N_("MAC address"),
.completer = virshNetworkDhcpMacCompleter,
},
{.name = NULL}
};