1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-01-09 01:18:00 +03:00

virsh: Expose virDomainGetHostnameFlags

Our virsh already has 'domhostname' command. Add '--source'
argument to it so that users can chose between 'lease' and
'agent' sources. Also, implement completer for the argument.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Julio Faracco 2019-12-27 17:36:25 -03:00 committed by Michal Privoznik
parent 1becd935c5
commit 0f814c0fed
5 changed files with 73 additions and 2 deletions

View File

@ -1797,10 +1797,15 @@ domhostname
.. code-block::
domhostname domain
domhostname domain [--source lease|agent]
Returns the hostname of a domain, if the hypervisor makes it available.
The *--source* argument specifies what data source to use for the
hostnames, currently 'lease' to read DHCP leases or 'agent' to query
the guest OS via an agent. If unspecified, driver returns the default
method available (some drivers support only one type of source).
domid
-----

View File

@ -316,3 +316,22 @@ virshDomainInterfaceAddrSourceCompleter(vshControl *ctl G_GNUC_UNUSED,
return ret;
}
char **
virshDomainHostnameSourceCompleter(vshControl *ctl G_GNUC_UNUSED,
const vshCmd *cmd G_GNUC_UNUSED,
unsigned int flags)
{
char **ret = NULL;
size_t i;
virCheckFlags(0, NULL);
ret = g_new0(typeof(*ret), VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST + 1);
for (i = 0; i < VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST; i++)
ret[i] = g_strdup(virshDomainHostnameSourceTypeToString(i));
return ret;
}

View File

@ -58,3 +58,7 @@ char **
virshDomainInterfaceAddrSourceCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
char ** virshDomainHostnameSourceCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);

View File

@ -11742,20 +11742,55 @@ static const vshCmdInfo info_domhostname[] = {
static const vshCmdOptDef opts_domhostname[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = "source",
.type = VSH_OT_STRING,
.flags = VSH_OFLAG_NONE,
.completer = virshDomainHostnameSourceCompleter,
.help = N_("address source: 'lease' or 'agent'")},
{.name = NULL}
};
VIR_ENUM_IMPL(virshDomainHostnameSource,
VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST,
"agent",
"lease");
static bool
cmdDomHostname(vshControl *ctl, const vshCmd *cmd)
{
char *hostname;
virDomainPtr dom;
bool ret = false;
const char *sourcestr = NULL;
int flags = 0; /* Use default value. Drivers can have its own default. */
if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
return false;
hostname = virDomainGetHostname(dom, 0);
if (vshCommandOptStringReq(ctl, cmd, "source", &sourcestr) < 0)
goto error;
if (sourcestr) {
int source = virshDomainHostnameSourceTypeFromString(sourcestr);
if (source < 0) {
vshError(ctl, _("Unknown data source '%s'"), sourcestr);
goto error;
}
switch ((virshDomainHostnameSource) source) {
case VIRSH_DOMAIN_HOSTNAME_SOURCE_AGENT:
flags |= VIR_DOMAIN_GET_HOSTNAME_AGENT;
break;
case VIRSH_DOMAIN_HOSTNAME_SOURCE_LEASE:
flags |= VIR_DOMAIN_GET_HOSTNAME_LEASE;
break;
case VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST:
break;
}
}
hostname = virDomainGetHostname(dom, flags);
if (hostname == NULL) {
vshError(ctl, "%s", _("failed to get hostname"));
goto error;

View File

@ -30,4 +30,12 @@ typedef struct virshDomainEventCallback virshDomainEventCallback;
extern virshDomainEventCallback virshDomainEventCallbacks[];
typedef enum {
VIRSH_DOMAIN_HOSTNAME_SOURCE_AGENT,
VIRSH_DOMAIN_HOSTNAME_SOURCE_LEASE,
VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST
} virshDomainHostnameSource;
VIR_ENUM_DECL(virshDomainHostnameSource);
extern const vshCmdDef domManagementCmds[];