javawildflydatasourcejboss-cli

Change datasource property using org.jboss.dmr.ModelNode


I have quite regular data source definition in standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:2.0">
    <datasources>
        <datasource jndi-name="java:jboss/datasources/MYDS" pool-name="MYDS" enabled="true" use-java-context="true">
            <connection-url>jdbc:hsqldb:file:MYDB</connection-url>
            <security>
                <user-name>superman</user-name>
            </security>
        </datasource>
   </datasources>
</subsystem>

Try to change some property from command-line and this works fine:

/subsystem=datasources/data-source=MYDS:write-attribute(name=connection-url,value=achooo)

result:

{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}

But using ModelNode API, it gets me crazy.

ModelNode request = new ModelNode();
request.get(ClientConstants.OP_ADDR).add("subsystem", "datasources");
request.get(ClientConstants.OP_ADDR).add("data-source", "MYDS");
request.get(ClientConstants.OP).set(ClientConstants.WRITE_ATTRIBUTE_OPERATION);
request.get("connection-url").set("achooo");
myCliExecutor.executeCliCommandWithResult(request);

result:

{
    "outcome" => "failed",
    "failure-description" => "JBAS014746: name may not be null",
    "rolled-back" => true,
    "response-headers" => {"process-state" => "reload-required"}
}

Solution

  • You're DMR is not quite correct. For the write-attribute operation you need to define a name and value attribute. It should look something more like:

    final ModelNode address = new ModelNode().setEmptyList();
    address.add("subsystem", "datasources");
    address.add("data-source", "ExampleDS");
    
    ModelNode request = new ModelNode();
    request.get(ClientConstants.OP_ADDR).set(address);
    request.get(ClientConstants.OP).set(ClientConstants.WRITE_ATTRIBUTE_OPERATION);
    request.get("name").set("connection-url");
    request.get("value").set("achooo");
    myCliExecutor.executeCliCommandWithResult(request);
    

    In the org.wildfly.core:controller-client there is also an Operations helper which should help.

    final ModelNode address = Operations.createAddress("subsystem", "datasources", "data-source", "MYDS");
    final ModelNod op = Operations.createWriteAttributeOperation(address, "connection-url", "achooo");
    myCliExecutor.executeCliCommandWithResult(op);