rvojcik / rtapi

Racktables API
https://rtapi.readthedocs.io
GNU General Public License v2.0
29 stars 16 forks source link

GetObjectAttributes() #12

Closed laimaretto closed 4 years ago

laimaretto commented 4 years ago

Hi!

I've seen there is a method called rt.GetObjectTags(id) which provides all of the tags assigned to a given object.

How difficult is it to implement the same but for the Attributes? The idea is to have a method that will list all of the attributes assigned to an object.

I have on my own some SQL queries that do the job. If interested, I can provide those ...

thanks!

rvojcik commented 4 years ago

Hi again, I think it would be great if you have some queries already prepared. GetObjectAttributes is good idea, I can imagine how useful it can be.

laimaretto commented 4 years ago

Hi @rvojcik !

Sure, find it here:

def fnc_sql_build_query3(var_obj_id):

    SQL_QUERY3 = (

        "select "
        "ob.id, "
        "ob.name as routerName, "
        "ob.label, "
        "a.name as attrName, "
        "a.type, "
        "d.dict_value, "
        "av.* "
        "from Object as ob "
        "join AttributeValue as av on (ob.id=av.object_id) "
        "join Attribute as a on (av.attr_id=a.id) left "
        "join Dictionary as d on (d.dict_key=av.uint_value) "
        "where ob.id = '" + var_obj_id + "' order by a.name;"
    )

    # This return fills the 'var_atrib' variable
    return SQL_QUERY3

The output for this query for a given var_obj_id should be the following table (the values are an example of my actual DB). In order to keep the column names when returning, I'm using the read_sql(query) from Pandas[1]. Using the read_sql() you directly obtain a DataFrame which is really nice in order to manipulate data.

#+------+---------+---------------------------+--------+------------------+--------+---------+---------+--------------+------------+-------------+
#| 0 id | 1 name  | 2 label  | 3 attr         | 4 type | 5 dict_value     |6obj_id |7obj_tid |8attr_id |9string_value |10uint_value|11float_value|
#+------+---------+---------------------------+--------+------------------+--------+---------+---------+--------------+------------+-------------+
#| 4791 | objName | objLabel | AS-Number      | uint   | NULL             |   4791 |       7 |   10015 | NULL         |      11664 |        NULL |
#| 4791 | objName | objLabel | BGP-family     | dict   | vpn-ipv4         |   4791 |       7 |   10115 | NULL         |      50129 |        NULL |
#| 4791 | objName | objLabel | BGP-status     | dict   | BGP_activo       |   4791 |       7 |   10143 | NULL         |      50237 |        NULL |
#| 4791 | objName | objLabel | BGP-Zone       | dict   | SUR:10.2.22.120  |   4791 |       7 |   10078 | NULL         |      50054 |        NULL |
#| 4791 | objName | objLabel | HW type        | dict   | ALU%GPASS%SARX   |   4791 |       7 |       2 | NULL         |      50218 |        NULL |
#| 4791 | objName | objLabel | Integrado      | dict   | swap             |   4791 |       7 |   10127 | NULL         |      50239 |        NULL |
#| 4791 | objName | objLabel | MDA01          | dict   | i8-chds1-x:NA:NA |   4791 |       7 |   10052 | NULL         |      50219 |        NULL |
#| 4791 | objName | objLabel | MDA02          | dict   | i7-mix-eth:NA:NA |   4791 |       7 |   10060 | NULL         |      50220 |        NULL |
#| 4791 | objName | objLabel | MDA03          | dict   | i7-mix-eth:NA:NA |   4791 |       7 |   10061 | NULL         |      50220 |        NULL |
#| 4791 | objName | objLabel | SW ALU         | dict   | TiMOS-B-7.0.R7   |   4791 |       7 |   10114 | NULL         |      50215 |        NULL |
#| 4791 | objName | objLabel | Sync_Ref_Order | dict   | ref1 ref2 exter  |   4791 |       7 |   10075 | NULL         |      50044 |        NULL |
#| 4791 | objName | objLabel | Sync_Ref1      | string | NULL             |   4791 |       7 |   10048 | 1/3/6        |       NULL |        NULL |
#| 4791 | objName | objLabel | Sync_Ref2      | string | NULL             |   4791 |       7 |   10050 | 1/2/6        |       NULL |        NULL |
#| 4791 | objName | objLabel | Sync_Type      | dict   | sync-e:na        |   4791 |       7 |   10112 | NULL         |      50127 |        NULL |
#+------+---------+----------+----------------+--------+------------------+--------+---------+---------+--------------+------------+-------------+

I've written a plugin for RackTables[2] that allows you to plot network topologies. In such I've written my own SQL queries but I've been looking long time for an API. But this is a really good one of yours ... :-)

I have in mind many more methods to implement. I'd like to contribute with you in order to -hopefully- establish a de facto python API to work with RackTables. What do you think?

Thanks for this neat work!

[1] - https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html [2] - https://github.com/RackTables/racktables-contribs/tree/master/python-graph-topology

rvojcik commented 4 years ago

Sorry for delay. I'm little bit more busy in last few weeks.

Sounds great, I'd love to see any contribution to this. I have problem to find time for this project, I named it racktables-api because my original plan was to implement Rest API. Unfortunately it ends in this state, small python module.

laimaretto commented 4 years ago

HI Robert,

Yes, I understand, no worries: I also do this on my spare time, which happens to be less and less spare ... jaja ... :-)

Rest API would be the best solution. I talk to colleagues of mine which are migrating towards NetBox but only because NetBox has a Rest API.

I have several installations of RackTables and having an API is definitively a must to interact in a healthy way with the DB.

I know nothing about programming a Rest API or PHP. But I know Python. As said before, I can contribute -in my spare time- to add more methods to your ptyhon-api library.

Best!

Lucas

ps: by the way, thanks for adding those new methods ... :-)

rvojcik commented 4 years ago

Great :) in my opinion best place to start when you want to start creating new API is https://swagger.io/specification/ Just learn the spec, create spec for your API and generate server-stub in your favourite language. You can learn from generated code and don't wory about some basic structure. Then just implement methots which handle every request and that's it.