

def associate_dhcp_options(context, dhcp_options_id, vpc_id):
    vpc = ec2utils.get_db_item(context, vpc_id)
    rollback_dhcp_options_id = vpc.get('dhcp_options_id')
    if (dhcp_options_id == 'default'):
        dhcp_options_id = None
        dhcp_options = None
    else:
        dhcp_options = ec2utils.get_db_item(context, dhcp_options_id)
        dhcp_options_id = dhcp_options['id']
    neutron = clients.neutron(context)
    os_ports = neutron.list_ports(tenant_id=context.project_id)['ports']
    network_interfaces = db_api.get_items(context, 'eni')
    rollback_dhcp_options_object = (db_api.get_item_by_id(context, rollback_dhcp_options_id) if (dhcp_options_id is not None) else None)
    with common.OnCrashCleaner() as cleaner:
        _associate_vpc_item(context, vpc, dhcp_options_id)
        cleaner.addCleanup(_associate_vpc_item, context, vpc, rollback_dhcp_options_id)
        for network_interface in network_interfaces:
            os_port = next((p for p in os_ports if (p['id'] == network_interface['os_id'])), None)
            if (not os_port):
                continue
            _add_dhcp_opts_to_port(context, dhcp_options, network_interface, os_port, neutron)
            cleaner.addCleanup(_add_dhcp_opts_to_port, context, rollback_dhcp_options_object, network_interface, os_port, neutron)
    return True

dataset/ETHPy150Open openstack/ec2-api/ec2api/api/dhcp_options.py associate_dhcp_options/original