'Return a security group rule in desired API response format.
If group_rule_data is passed in that is used rather than querying
for it.'

def _format_security_group_rule(self, context, rule, group_rule_data=None):

    sg_rule = {}
    sg_rule['id'] = rule['id']
    sg_rule['parent_group_id'] = rule['parent_group_id']
    sg_rule['ip_protocol'] = rule['protocol']
    sg_rule['from_port'] = rule['from_port']
    sg_rule['to_port'] = rule['to_port']
    sg_rule['group'] = {}
    sg_rule['ip_range'] = {}
    if group_rule_data:
        sg_rule['group'] = group_rule_data
    elif rule['group_id']:
        try:
            source_group = self.security_group_api.get(context, id=rule['group_id'])
        except exception.SecurityGroupNotFound:
            LOG.debug('Security    Group    ID    %s    does    not    exist', rule['group_id'])
            return
        sg_rule['group'] = {'name': source_group.get('name'), 'tenant_id': source_group.get('project_id')}
    else:
        sg_rule['ip_range'] = {'cidr': rule['cidr']}
    return sg_rule
