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

View File

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