Ask Your Question
0

Problem with Nova REST API format

asked 2014-05-06 11:09:52 -0500

gvdm gravatar image

Hi all,

In a Java application I'm using Openstack's REST APIs to manage my Openstack instance, but I'm having a problem with one of Nova's APIs. The API is

/v2/{tenant_id}/servers/detail

which is documented here.

The documentation states it should return something like

{
    "servers": [
        {
            "accessIPv4": "",
            "accessIPv6": "",
            "addresses": {
                "private": [
                    {
                        "addr": "192.168.0.3",
                        "version": 4
                    }
                ]
            },
           ...
}

This means that in the example the VM is connected to a network named private through a NIC with address 192.168.0.3.

If I try to consume the same API using my instance of (Havana) Openstack I get the following JSON result

{
    "servers": [
        {
            "status": "ACTIVE",
            "addresses": {
                "MyDemo_network": [
                    {
                        "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:24:92:1e",
                        "version": 4,
                        "addr": "192.168.6.2",
                        "OS-EXT-IPS:type": "fixed"
                    }
                ]
            },
            ...
}

As you can see, I connected the VM instance to a network named MyDemo_network.

In my Java application I use jsonschema2pojo to generate the POJOs (Java classes) in which the JSON data is to be copied. The POJOs generated with the documented JSON is as follows (I'm omitting annotations and other non important code for the sake of clarity)

//NovaGetServersResponse.java
public class NovaGetServersResponse {
    private List<Server> servers = new ArrayList<Server>();
    ...
}

//Server.java
public class Server {
    private Addresses addresses;
    ...
}

// Addresses.java
public class Addresses {
    private List<Private> private = new ArrayList<Private>();
    ...
}

The JSON returned with my Nova instance, instead, is serialized in a class with this structure

//NovaGetServersResponse.java
public class NovaGetServersResponse {
    private List<Server> servers = new ArrayList<Server>();
    ...
}

//Server.java
public class Server {
    private Addresses addresses;
    ...
}

// Addresses.java
public class Addresses {
    private List<MyDemoNetwork> myDemoNetwork = new ArrayList<MyDemoNetwork>();
    ....
}

As you can see if two different VMs are connected to different networks they would require two different POJOs for the serialization. The problem is that with this class structure I cannot get the data of, for example, a VM which is connected to a network named "TheNetwork" because is would lack the TheNetwork theNetwork field in its class.

I must be able to get the data of different VMs on different Nova instances so I cannot build a class for each VM.

My suggestion is to change the JSON format to the following

{
    "server": {
        "accessIPv4": "",
        "accessIPv6": "",
        "address": [            
            {
                "networkName": "private"
                "NIC": [
                    {
                        "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:d2:1d:0c",
                        "version": 4,
                        "addr": "192.168.6.2",
                        "OS-EXT-IPS:type": "fixed"
                    }
                ]
            },
        ]
        ...
    }
}

This way the Server POJO would have a List<Address> address list and the network's name would become a POJO field value (instead of a class name). This way this class would fit the information of any VM.

What do you think about that? Can this API change be accomplished?

Thank you

Giulio

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-06 12:02:01 -0500

updated 2014-05-06 12:04:54 -0500

Because the response embeds some data (ie the name of the network) in the keys of the "addresses" object, it's not possible to define a json schema which captures that part fully. As you suggest, it would be easier to use schema tools if the keys were fixed and data only held in the values. That said, I think you can work around the problem by defining your Addresses class like:

public class Addresses {
    @JsonAnyGetter
    private Map<String, Object> addresses = new ....
    // etc etc 
}

This means overwriting some of the output that jsonschema2pojo has generated, but the actual format of the addresses objects is very simple so you should be able to write a couple of helper methods for the specific items you want. The names of the networks would be in addresses.keySet() and you would need to cast to List,Map or String as appropriate while navigating the value of addresses.get(some_network).

Like I said, it's a workaround. Changing the format of the API output is a much bigger deal, because your suggestion (as logical as it is) would be a breaking change to the Nova API so would need to either be added via a separate extension or to go through a full deprecation/reimplementation process which would probably take at least 2 release cycles (ie 12 months).

edit flag offensive delete link more

Comments

By the way, I don't know how you were generating your json schemas, but I found the schema for the call you are talking about in:

https://github.com/openstack/tempest/blob/master/tempest/api_schema/compute/servers.py

It uses the patternProperties feature of jsonschema, which I am not sure if jsonschema2pojo supports.

MatthewGilliard gravatar imageMatthewGilliard ( 2014-05-15 13:39:27 -0500 )edit

I checked, and that is not supported by jsonschema2pojo. I raised this: https://github.com/joelittlejohn/jsonschema2pojo/issues/182 (https://github.com/joelittlejohn/json...)

MatthewGilliard gravatar imageMatthewGilliard ( 2014-05-15 14:14:46 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2014-05-06 11:09:52 -0500

Seen: 385 times

Last updated: May 06 '14