Esempio n. 1
0
static virSecretPtr
secretDefineXML(virConnectPtr conn,
                const char *xml,
                unsigned int flags)
{
    virSecretPtr ret = NULL;
    virSecretObjPtr obj = NULL;
    virSecretDefPtr objDef;
    virSecretDefPtr backup = NULL;
    virSecretDefPtr def;
    virObjectEventPtr event = NULL;

    virCheckFlags(0, NULL);

    if (!(def = virSecretDefParseString(xml)))
        return NULL;

    if (virSecretDefineXMLEnsureACL(conn, def) < 0)
        goto cleanup;

    if (!(obj = virSecretObjListAdd(driver->secrets, def,
                                    driver->configDir, &backup)))
        goto cleanup;
    VIR_STEAL_PTR(objDef, def);

    if (!objDef->isephemeral) {
        if (backup && backup->isephemeral) {
            if (virSecretObjSaveData(obj) < 0)
                goto restore_backup;
        }

        if (virSecretObjSaveConfig(obj) < 0) {
            if (backup && backup->isephemeral) {
                /* Undo the virSecretObjSaveData() above; ignore errors */
                virSecretObjDeleteData(obj);
            }
            goto restore_backup;
        }
    } else if (backup && !backup->isephemeral) {
        if (virSecretObjDeleteConfig(obj) < 0)
            goto restore_backup;

        virSecretObjDeleteData(obj);
    }
    /* Saved successfully - drop old values */
    virSecretDefFree(backup);

    event = virSecretEventLifecycleNew(objDef->uuid,
                                       objDef->usage_type,
                                       objDef->usage_id,
                                       VIR_SECRET_EVENT_DEFINED,
                                       0);

    ret = virGetSecret(conn,
                       objDef->uuid,
                       objDef->usage_type,
                       objDef->usage_id);
    goto cleanup;

 restore_backup:
    /* If we have a backup, then secret was defined before, so just restore
     * the backup; otherwise, this is a new secret, thus remove it. */
    if (backup) {
        virSecretObjSetDef(obj, backup);
        VIR_STEAL_PTR(def, objDef);
    } else {
        virSecretObjListRemove(driver->secrets, obj);
        virObjectUnref(obj);
        obj = NULL;
    }

 cleanup:
    virSecretDefFree(def);
    virSecretObjEndAPI(&obj);
    if (event)
        virObjectEventStateQueue(driver->secretEventState, event);

    return ret;
}
Esempio n. 2
0
static virSecretPtr
secretDefineXML(virConnectPtr conn,
                const char *xml,
                unsigned int flags)
{
    virSecretPtr ret = NULL;
    virSecretObjPtr secret = NULL;
    virSecretDefPtr backup = NULL;
    virSecretDefPtr new_attrs;

    virCheckFlags(0, NULL);

    if (!(new_attrs = virSecretDefParseString(xml)))
        return NULL;

    if (virSecretDefineXMLEnsureACL(conn, new_attrs) < 0)
        goto cleanup;

    if (!(secret = virSecretObjListAdd(driver->secrets, new_attrs,
                                       driver->configDir, &backup)))
        goto cleanup;

    if (!new_attrs->isephemeral) {
        if (secretEnsureDirectory() < 0)
            goto cleanup;

        if (backup && backup->isephemeral) {
            if (virSecretObjSaveData(secret) < 0)
                goto restore_backup;
        }

        if (virSecretObjSaveConfig(secret) < 0) {
            if (backup && backup->isephemeral) {
                /* Undo the virSecretObjSaveData() above; ignore errors */
                virSecretObjDeleteData(secret);
            }
            goto restore_backup;
        }
    } else if (backup && !backup->isephemeral) {
        if (virSecretObjDeleteConfig(secret) < 0)
            goto restore_backup;

        virSecretObjDeleteData(secret);
    }
    /* Saved successfully - drop old values */
    virSecretDefFree(backup);

    ret = virGetSecret(conn,
                       new_attrs->uuid,
                       new_attrs->usage_type,
                       virSecretUsageIDForDef(new_attrs));
    new_attrs = NULL;
    goto cleanup;

 restore_backup:
    /* If we have a backup, then secret was defined before, so just restore
     * the backup. The current (new_attrs) will be handled below.
     * Otherwise, this is a new secret, thus remove it.
     */
    if (backup)
        virSecretObjSetDef(secret, backup);
    else
        virSecretObjListRemove(driver->secrets, secret);

 cleanup:
    virSecretDefFree(new_attrs);
    virSecretObjEndAPI(&secret);

    return ret;
}