def make_sec_rule(entity, sec_grp_id):
    """
    Create and validate the security rule.
    """
    # TODO: remove this one!
    name = random.randrange(0, 99999999)
    sg_rule = {'id': name,
               'parent_group_id': sec_grp_id}
    prot = \
        entity.attributes['occi.network.security.protocol'].lower().strip()
    if prot in ('tcp', 'udp', 'icmp'):
        sg_rule['protocol'] = prot
    else:
        raise AttributeError('Invalid protocol defined:' + prot)
    from_p = entity.attributes['occi.network.security.to'].strip()
    from_p = int(from_p)
    if (type(from_p) is int) and 0 < from_p <= 65535:
        sg_rule['from_port'] = from_p
    else:
        raise AttributeError('No valid from port defined.')
    to_p = entity.attributes['occi.network.security.to'].strip()
    to_p = int(to_p)
    if (type(to_p) is int) and 0 < to_p <= 65535:
        sg_rule['to_port'] = to_p
    else:
        raise AttributeError('No valid to port defined.')
    if from_p > to_p:
        raise AttributeError('From port is bigger than to port defined.')
    cidr = entity.attributes['occi.network.security.range'].strip()
    if len(cidr) <= 0:
        cidr = '0.0.0.0/0'
    if True:
        sg_rule['cidr'] = cidr
    else:
        raise AttributeError('No valid CIDR defined.')
    sg_rule['group'] = {}
    return sg_rule
dataset/ETHPy150Open tmetsch/occi-os/occi_os_api/backends/openstack.py/make_sec_rule/original