I am working with Elastic Search, i came across a plugin called ReadOnlyRest plugin for Auth purpose. To set up this plugin we need to add user to Elastic search yml.
So i searched how to add "key : value"
pair data to yml using Java. Came across SnakeYAML to add data.
I am able to send the data of user from Java.
Java code.
package com.test.elasticsearch;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
public class YAMLSample {
protected static Logger logger = Logger.getLogger(YAMLSample.class);
final String fileName = "/home/Installation/elasticsearch-2.3.1/config/elasticsearch.yml";
public void writeToYML() throws IOException {
logger.debug("Write to YML");
Map<String, Object> data = new HashMap<String, Object>();
data.put("name", "user5");
data.put("type", "allow");
data.put("auth_key", "user5:user5");
data.put("kibana_access", "ro");
data.put("indices", new String[] { ".kibana*", "abc", "def" });
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(data);
DumperOptions options = new DumperOptions();
options.setIndent(5);
Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter(fileName, true);
yaml.dump(list, writer);
logger.debug("DONE!");
}
public static void main(String[] args) throws IOException {
// new YAMLSample().readYML();
new YAMLSample().writeToYML();
}
}
Output from the above code is:
- name: user5
indices: [.kibana*, abc, def]
kibana_access: ro
type: allow
auth_key: user5:user5
But, expected output is:
- name: user5
indices: [.kibana*, abc, def]
kibana_access: ro
type: allow
auth_key: user5:user5
the "Hyphen-minus" should have just one space and before the "Hyphen-minus" there should be 4 spaces.
I mean to say i am expecting this to appear as Array of Users. else than "Hyphen-minus" then few spaces.
Please assist me with finding out the solution.
I've modified your code and got the expected result. Below is how the code look like:
public class YAMLSample {
final String fileName = "/tmp/rest.yml";
public void writeToYML() throws IOException {
log( "Write to YML" );
Map<String, Object> user = new HashMap<>();
user.put( "name", "user5" );
user.put( "type", "allow" );
user.put( "auth_key", "user5:user5" );
user.put( "kibana_access", "ro" );
user.put( "indices", new String[] { ".kibana*", "abc", "def" } );
Map<String, Object> user2 = new HashMap<>();
user2.put("name", "user2");
user2.put("type", "allow");
user2.put("auth_key", "user2:user2");
user2.put("kibana_access", "ro");
user2.put("indices", new String[] { ".kibana*", "abc", "def" });
List<Map<String, Object>> list = new ArrayList<>();
list.add(user);
list.add(user2);
Map<String, List<Map<String, Object>>> config = new HashMap<>();
config.put( "access_control_rules", list );
DumperOptions options = new DumperOptions();
options.setIndent( 6 );
options.setIndicatorIndent( 4 );
options.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);
Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter(fileName, true);
yaml.dump( config, writer );
log( "DONE!" );
}
public static void main(String[] args) throws IOException {
new YAMLSample().writeToYML();
}
public void log(String str) {
System.out.println(str);
}
}
Basically I added this two options to your Dumper
options.setIndicatorIndent(4);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);
and updated from 5 to 6 the options.setIndent(6);