示例#1
0
def objects_starting_with(obj_type, text = "", key = None):
    """
    The function returns a list of matching keys from table/model
    identified by the 'obj_type' parameter

    If the table/model has a 'alias' field, then this field's
    values are also examined for matches

    The first argument is the name of a table/model in the store,
    while the second argument is a prefix to filter the results.
    The filter is applied to the key of the table/model, which
    was previously populated.
    """

    if key:
        if not mi.obj_type_has_field(obj_type, key):
            sdnsh.warning("objects_starting_with: %s doesn't have field %s" %
                          (obj_type, key))
    else:
        key = mi.pk(obj_type)
        if key == None:
            sdnsh.warning("objects_starting_with: %s doesn't have pk" %
                          (obj_type))
    key_entries = []

    # Next, find the object
    #  Deal with any changes to the lookup name based on the 'contenation'
    #  of the config mode name to the named identifer.
    #


    case = mi.get_obj_type_field_case_sensitive(obj_type, key)
    id_value = utif.convert_case(case, text)

    if mi.obj_type_has_model(obj_type):
        # from the database
        try:
            entries = sdnsh.get_table_from_store(obj_type, key, id_value)
            errors = None
        except Exception, e:
            errors = sdnsh.rest_error_to_dict(e)
        if errors:
            print sdnsh.rest_error_dict_to_message(errors)
            return key_entries
def alias_to_value_handler(value, obj_type, data, field, other = None):
    """
    Compute the alias value for the named field for the obj_type.
    Place the resulting converted field into the data dictionary.

    Since this is a data-handler, the data dict must be updated
    even if this isn't an alias, otherwise the field value is lost.
    """
    global sdnsh
    if sdnsh.description:
        print 'alias_to_value_handler: ', value, obj_type, data, field

    if field != mi.pk(obj_type):
        # if this is a forgeign key, use the obj_type of the fk.
        if mi.is_foreign_key(obj_type, field):
            (obj_type, fk_name) = mi.foreign_key_references(obj_type, field)
        else:
            # XXX possibly other choices to determine alias_obj_type?
            if sdnsh.description:
                print 'alias_to_value_handler: field %s no obj-type ref %s ' % \
                      (field, obj_type)
        
    if other:
        parts = other.split('|')
        key = mi.pk(parts[0]) # parts[0] <- first part of other
        if len(parts) > 0:
            other = parts[0]
            key = field
            # not clear whether the part[1] is useful, two parts used
            # in other functions, example complete-from-another()
        other = mi.obj_type_related_config_obj_type(other)
        converted_value = convert_alias_to_object_key(other, value)

        pk = mi.pk(other)
        if mi.is_compound_key(other, pk) and converted_value != value:
            pk_dict = { pk : converted_value }
            mi.split_compound_into_dict(other, pk, pk_dict, is_prefix = True)
            for (k,v) in pk_dict.items():
                if k != pk:
                    data[k] = v
                    if sdnsh.description:
                        print "alias_to_value_handler: compound (other) %s:%s <- %s:%s" % \
                               (k, data[k], other, converted_value)
        else:
            case = mi.get_obj_type_field_case_sensitive(other, field)
            data[field] = utif.convert_case(case, converted_value)

        if sdnsh.description:
            print "alias_to_value_handler: (other) %s:%s <- %s:%s" % \
                   (key, data[key], other, value)
    else:
        # Some obj_types, for example, host, have no cassandra data,
        # but do have a related obj_type which is in the store
        obj_type = mi.obj_type_related_config_obj_type(obj_type)
        converted_value = convert_alias_to_object_key(obj_type, value)

        pk = mi.pk(obj_type)
        if mi.is_compound_key(obj_type, pk) and converted_value != value:
            pk_dict = { pk : converted_value }
            split_obj_type = other if other != None else obj_type 
            mi.split_compound_into_dict(split_obj_type, pk, pk_dict, is_prefix = True)
            for (k,v) in pk_dict.items():
                if k != pk:
                    data[k] = v
                    if sdnsh.description:
                        print "alias_to_value_handler: compound %s:%s <- %s:%s" % \
                               (k, data[k], obj_type, converted_value)
        else:
            case = mi.get_obj_type_field_case_sensitive(obj_type, field)
            data[field] = utif.convert_case(case, converted_value)

            if sdnsh.description:
                print "alias_to_value_handler:  %s:%s <- %s:%s" % (field,
                                                               data[field], obj_type, value)