mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 11:35:19 +00:00
virsh: domain: Split out code to lookup domain from string
Split out guts of the function to reuse it to get domain objects from string.
This commit is contained in:
parent
76a5bc4eef
commit
3b20e50ddb
@ -60,15 +60,63 @@
|
|||||||
# define SA_SIGINFO 0
|
# define SA_SIGINFO 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static virDomainPtr
|
||||||
|
vshLookupDomainInternal(vshControl *ctl,
|
||||||
|
const char *cmdname,
|
||||||
|
const char *name,
|
||||||
|
unsigned int flags)
|
||||||
|
{
|
||||||
|
virDomainPtr dom = NULL;
|
||||||
|
int id;
|
||||||
|
virCheckFlags(VSH_BYID | VSH_BYUUID | VSH_BYNAME, NULL);
|
||||||
|
|
||||||
|
/* try it by ID */
|
||||||
|
if (flags & VSH_BYID) {
|
||||||
|
if (virStrToLong_i(name, NULL, 10, &id) == 0 && id >= 0) {
|
||||||
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> looks like ID\n",
|
||||||
|
cmdname);
|
||||||
|
dom = virDomainLookupByID(ctl->conn, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* try it by UUID */
|
||||||
|
if (!dom && (flags & VSH_BYUUID) &&
|
||||||
|
strlen(name) == VIR_UUID_STRING_BUFLEN-1) {
|
||||||
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> trying as domain UUID\n",
|
||||||
|
cmdname);
|
||||||
|
dom = virDomainLookupByUUIDString(ctl->conn, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* try it by NAME */
|
||||||
|
if (!dom && (flags & VSH_BYNAME)) {
|
||||||
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> trying as domain NAME\n",
|
||||||
|
cmdname);
|
||||||
|
dom = virDomainLookupByName(ctl->conn, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dom)
|
||||||
|
vshError(ctl, _("failed to get domain '%s'"), name);
|
||||||
|
|
||||||
|
return dom;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virDomainPtr
|
||||||
|
vshLookupDomainBy(vshControl *ctl,
|
||||||
|
const char *name,
|
||||||
|
unsigned int flags)
|
||||||
|
{
|
||||||
|
return vshLookupDomainInternal(ctl, "unknown", name, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
virDomainPtr
|
virDomainPtr
|
||||||
vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
||||||
const char **name, unsigned int flags)
|
const char **name, unsigned int flags)
|
||||||
{
|
{
|
||||||
virDomainPtr dom = NULL;
|
|
||||||
const char *n = NULL;
|
const char *n = NULL;
|
||||||
int id;
|
|
||||||
const char *optname = "domain";
|
const char *optname = "domain";
|
||||||
virCheckFlags(VSH_BYID | VSH_BYUUID | VSH_BYNAME, NULL);
|
|
||||||
|
|
||||||
if (!vshCmdHasOption(ctl, cmd, optname))
|
if (!vshCmdHasOption(ctl, cmd, optname))
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -82,33 +130,7 @@ vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
|||||||
if (name)
|
if (name)
|
||||||
*name = n;
|
*name = n;
|
||||||
|
|
||||||
/* try it by ID */
|
return vshLookupDomainInternal(ctl, cmd->def->name, n, flags);
|
||||||
if (flags & VSH_BYID) {
|
|
||||||
if (virStrToLong_i(n, NULL, 10, &id) == 0 && id >= 0) {
|
|
||||||
vshDebug(ctl, VSH_ERR_DEBUG,
|
|
||||||
"%s: <%s> seems like domain ID\n",
|
|
||||||
cmd->def->name, optname);
|
|
||||||
dom = virDomainLookupByID(ctl->conn, id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* try it by UUID */
|
|
||||||
if (!dom && (flags & VSH_BYUUID) &&
|
|
||||||
strlen(n) == VIR_UUID_STRING_BUFLEN-1) {
|
|
||||||
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain UUID\n",
|
|
||||||
cmd->def->name, optname);
|
|
||||||
dom = virDomainLookupByUUIDString(ctl->conn, n);
|
|
||||||
}
|
|
||||||
/* try it by NAME */
|
|
||||||
if (!dom && (flags & VSH_BYNAME)) {
|
|
||||||
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain NAME\n",
|
|
||||||
cmd->def->name, optname);
|
|
||||||
dom = virDomainLookupByName(ctl->conn, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dom)
|
|
||||||
vshError(ctl, _("failed to get domain '%s'"), n);
|
|
||||||
|
|
||||||
return dom;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_ENUM_DECL(vshDomainVcpuState)
|
VIR_ENUM_DECL(vshDomainVcpuState)
|
||||||
|
@ -28,6 +28,10 @@
|
|||||||
|
|
||||||
# include "virsh.h"
|
# include "virsh.h"
|
||||||
|
|
||||||
|
virDomainPtr vshLookupDomainBy(vshControl *ctl,
|
||||||
|
const char *name,
|
||||||
|
unsigned int flags);
|
||||||
|
|
||||||
virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
||||||
const char **name, unsigned int flags);
|
const char **name, unsigned int flags);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user