Allow removing properties with set-prop

Implement removal of properties in ConfigGroup.setProp,
set_instance_config_prop, and the set-prop clusterioctl command.
This commit is contained in:
Hornwitser committed 2021-01-22 12:53:53 +01:00
1 parent dea3a30cae
commit 0e21060ea3
4 files changed
+24 -11

No files matched your search

+12 -10
View File
@@ -150,7 +150,7 @@ instanceConfigCommands.add(new libCommand.Command({
}));
instanceConfigCommands.add(new libCommand.Command({
definition: ["set-prop <instance> <field> <prop> <value>", "Set property of field in instance config", (yargs) => {
definition: ["set-prop <instance> <field> <prop> [value]", "Set property of field in instance config", (yargs) => {
yargs.positional("instance", { describe: "Instance to set config on", type: "string" });
yargs.positional("field", { describe: "Field to set", type: "string" });
yargs.positional("prop", { describe: "Property to set", type: "string" });
@@ -158,9 +158,15 @@ instanceConfigCommands.add(new libCommand.Command({
}],
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
let value = args.value;
let request = {
instance_id: instanceId,
field: args.field,
prop: args.prop,
};
try {
value = JSON.parse(value);
if (args.value !== undefined) {
request.value = JSON.parse(args.value);
}
} catch (err) {
// If this is looks like an array, object or string literal
// throw the parse error, otherwise assume this is a string.
@@ -176,15 +182,11 @@ instanceConfigCommands.add(new libCommand.Command({
// bash + npx : '""That'\''s a \" quote""'
// bash + npx -s sh : "'\"\"That'\\''s a \\\" quote\"\"'"
if (/^(\[.*]|{.*}|".*")$/.test(value)) {
throw new libErrors.CommandError(`In parsing value '${value}': ${err.message}`);
throw new libErrors.CommandError(`In parsing value '${args.value}': ${err.message}`);
}
request.value = args.value;
}
await libLink.messages.setInstanceConfigProp.send(control, {
instance_id: instanceId,
field: args.field,
prop: args.prop,
value,
});
await libLink.messages.setInstanceConfigProp.send(control, request);
},
}));
instanceCommands.add(instanceConfigCommands);
+5 -1
View File
@@ -448,7 +448,11 @@ class ConfigGroup {
let prev = this._fields.get(name);
let updated = {...prev || {}};
updated[prop] = value;
if (value !== undefined) {
updated[prop] = value;
} else {
delete updated[prop];
}
this._fields.set(name, updated);
if (this._config && !isDeepStrictEqual(updated, prev)) {
this._config.emit("fieldChanged", this, name, prev);
+1
View File
@@ -407,6 +407,7 @@ messages.setInstanceConfigProp = new Request({
type: "set_instance_config_prop",
links: ["control-master"],
permission: "core.instance.update_config",
requestRequired: ["instance_id", "field", "prop"],
requestProperties: {
"instance_id": { type: "integer" },
"field": { type: "string" },
+6
View File
@@ -397,6 +397,12 @@ describe("lib/config/classes", function() {
testInstance.setProp("json", "test", true);
assert.deepEqual(testInstance.get("json"), { test: true });
});
it("should unset field if passed undefined", function() {
testInstance.set("json", { test: true, extra: "yes" });
testInstance.setProp("json", "extra", undefined);
assert.deepEqual(testInstance.get("json"), { test: true });
});
});
});