Searching for Generic Objects by attribute value


#1

I needed to find Generic Objects based on attribute values.

vmdb_production=# \d generic_objects;
                                              Table "public.generic_objects"
            Column            |            Type             |                          Modifiers
------------------------------+-----------------------------+--------------------------------------------------------------
 id                           | bigint                      | not null default nextval('generic_objects_id_seq'::regclass)
 name                         | character varying           |
 uid                          | character varying           |
 generic_object_definition_id | bigint                      |
 created_at                   | timestamp without time zone | not null
 updated_at                   | timestamp without time zone | not null
 properties                   | jsonb                       | default '{}'::jsonb
Indexes:
    "generic_objects_pkey" PRIMARY KEY, btree (id)

So the name of the column is properties and its type is JSON. To find all objects with status == 'in-use' I ended up with the following code based on this howto:

$evm.vmdb(:generic_object).where("properties ->> 'status' = 'in-use'")

If the actual value is in a variable:

value = 'in-use'
$evm.vmdb(:generic_object).where("properties ->> 'status' = '" + value + "'")

HTH


#2

Method find_objects defined on GenericObjectDefinition can be used for the search based on GenericObject attributes and attributes defined in properties column.
For example:

$evm.vmdb(:generic_object_definition, go_def.id).find_objects(:status => 'in-use', :attr2 => value2)