virSecretDef: Convert 'usage_type' field to proper enum type

Convert the field and adjust the XML parsers to use
virXMLPropEnum().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2024-02-09 09:03:54 +01:00
parent 6db5362a30
commit bad17c4d88
3 changed files with 13 additions and 17 deletions

View File

@ -403,6 +403,10 @@ virAccessDriverPolkitCheckSecret(virAccessManager *manager,
virAccessPermSecretTypeToString(perm),
attrs);
} break;
case VIR_SECRET_USAGE_TYPE_LAST:
virReportEnumRangeError(virSecretUsageType, secret->usage_type);
return -1;
}
}

View File

@ -51,27 +51,19 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt,
{
xmlNodePtr node = NULL;
VIR_XPATH_NODE_AUTORESTORE(ctxt)
g_autofree char *type_str = NULL;
int type;
if (!(node = virXPathNode("./usage", ctxt)))
return 0;
ctxt->node = node;
type_str = virXMLPropString(node, "type");
if (type_str == NULL) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("unknown secret usage type"));
if (virXMLPropEnum(node, "type",
virSecretUsageTypeFromString,
VIR_XML_PROP_REQUIRED,
&def->usage_type) < 0) {
return -1;
}
type = virSecretUsageTypeFromString(type_str);
if (type < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown secret usage type %1$s"), type_str);
return -1;
}
def->usage_type = type;
switch (def->usage_type) {
case VIR_SECRET_USAGE_TYPE_NONE:
break;
@ -121,6 +113,7 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt,
}
break;
case VIR_SECRET_USAGE_TYPE_LAST:
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected secret usage type %1$d"),
@ -238,9 +231,8 @@ virSecretDefFormatUsage(virBuffer *buf,
break;
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected secret usage type %1$d"),
def->usage_type);
case VIR_SECRET_USAGE_TYPE_LAST:
virReportEnumRangeError(virSecretUsageType, def->usage_type);
return -1;
}
virBufferAdjustIndent(buf, -2);

View File

@ -28,7 +28,7 @@ struct _virSecretDef {
bool isprivate;
unsigned char uuid[VIR_UUID_BUFLEN];
char *description; /* May be NULL */
int usage_type; /* virSecretUsageType */
virSecretUsageType usage_type;
char *usage_id; /* May be NULL */
};