SnakeYAML and Puppet ReST API

120 views
Skip to first unread message

indigo

unread,
Jan 31, 2012, 8:29:25 AM1/31/12
to SnakeYAML
Hello group,

I played with the ReST API with puppet and used SnakeYAML to parse the
output. I run in some trouble to convert the YAML output in Java
objects.

I use maven to handle my dependencies and extended my small project
pom.xml with

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.9</version>
</dependency>

I've added also the repository as suggested on the project page like
this:

<repository>
<id>Sonatype-public</id>
<name>SnakeYAML repository</name>
<url>http://oss.sonatype.org/content/groups/public/</url>
</repository>

I started first with a simple structure with a list of strings to
figure out if I can use SnakeYAML in my project. Hint: it is
implemented via ReST so service is my web resource. The code snippet
and the output is here:

http://paste2.org/p/1889527

The next was to get a more complex YAML structure from puppet - a
puppet node:
The link below shows the YAML structure as plain text:

http://paste2.org/p/1889520

If I want to parse this into an object I retrieve the following
exception:

Exception in thread "main" Can't construct a java object for !ruby/
object:Puppet::Node; exception=Invalid tag: !ruby/object:Puppet::Node
in "<string>", line 1, column 5:
--- !ruby/object:Puppet::Node
^

With some google search I found an entry to strip off "!ruby/" from
the object. I gave it another try with another exception

Exception in thread "main" mapping values are not allowed here
in "<string>", line 2, column 10:
classes: []

For me it is just enough to have the puppet node as a plain java bean
object. It would be nice if someone can shed some light on my first
steps in YAML and Java.

Thank you in advance

Ronny

Andrey

unread,
Jan 31, 2012, 9:05:28 AM1/31/12
to snakeya...@googlegroups.com
Hi Ronny,
I will try to "shed some light on your first steps in YAML"

1) you only need to add the repository definition if you wish to work with the latest and greatest SNAPSHOT. 
Remove it from your pom. It should still work

2) One of the ideas behind YAML is to exchange documents between different platforms and programming languages.
But it only works if language-specific stuff is not included in the YAML document. It would be better if you can contact the creators of the document and ask them to remove all the deficiencies from the document.

3) If you have to deal with the YAML documents full of Ruby-specific tags, then you have to explicitly instruct SnakeYAML how to treat these tags. See the example here:

Fee free to take the source and experiment with the tests.

4)
>For me it is just enough to have the puppet node as a plain java bean object.

This is a lot !!! Java is a statically compiled language. What are 'classes" ? List of Strings, integers, classes, IP addresses ?
What is 'expiration' ? Date or String ?

You can either design a JavaBean which can fit the data you parse, or you can parse the data into a basic structure (Map, List, String) by SnakeYAML and then transform the data to your business structure.

Good luck,
-
Andrey

Reply all
Reply to author
Forward
0 new messages