<commit_before>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * Syncs the rule and remote VM state with FWAPI and VMAPI
 */

var assert = require('assert-plus');
var async = require('async');
var fw = require('../fw');
var FWAPI = require('sdc-clients').FWAPI;
var mod_vm = require('../vm');
var mod_vmapi = require('../vmapi');
var prim = require('jsprim');
var vasync = require('vasync');
var VMAPI = require('sdc-clients').VMAPI;



function updateOwnerRules(opts, callback) {
    async.eachSeries(opts.resolve, function _update(payload, cb) {
        opts.fwapi.post('/resolve', payload, function (err, resolved) {
            if (err) {
                // XXX: retry?
                return cb(err);
            }

            if (!resolved || prim.isEmpty(resolved) || !resolved.rules ||
                resolved.rules.length === 0) {
                opts.log.debug({ payload: payload, resolved: resolved },
                    'No resolve data: returning');
                return cb();
            }

            opts.cache.addResolveData(payload);
            opts.data = resolved;
            opts.data.serverUUID = opts.config.serverUUID;

            mod_vmapi.list(opts, function (err2, vms) {
                if (err2) {
                    return cb(err2);
                }

                var updatePayload = {
                    log: opts.log,
                    owner_uuid: resolved.owner_uuid,
                    payload: {
                        rules: resolved.rules,
                        vms: opts.vms
                    }
                };

                if (vms) {
                    updatePayload.payload.remoteVMs = vms;
                }

                fw.update(updatePayload, function (err3, res) {
                    if (err3) {
                        return cb(err3);
                    }

                    opts.log.debug(res, 'Updated rules for owner_uuid="%s"',
                        resolved.owner_uuid);
                    return cb(null, res);
                });
            });
        });

    }, function _done(err) {
        opts.log.debug(opts.cache.state, 'cache state after resolve');
        return callback(err);
    });
}



function syncFromAPIs(opts, callback) {
    mod_vm.list({ firewall_enabled: true }, function (err, vms) {
        if (err) {
            return callback(err);
        }

        var owner;
        var ownerVMs = {};
        var t;

        for (var v in vms) {<commit_msg>FWAPI-120: firewaller sync failing with: Command failed: Invalid lookup key: "firewall_enabled"<commit_after>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * Syncs the rule and remote VM state with FWAPI and VMAPI
 */

var assert = require('assert-plus');
var async = require('async');
var fw = require('../fw');
var FWAPI = require('sdc-clients').FWAPI;
var mod_vm = require('../vm');
var mod_vmapi = require('../vmapi');
var prim = require('jsprim');
var vasync = require('vasync');
var VMAPI = require('sdc-clients').VMAPI;



function updateOwnerRules(opts, callback) {
    async.eachSeries(opts.resolve, function _update(payload, cb) {
        opts.fwapi.post('/resolve', payload, function (err, resolved) {
            if (err) {
                // XXX: retry?
                return cb(err);
            }

            if (!resolved || prim.isEmpty(resolved) || !resolved.rules ||
                resolved.rules.length === 0) {
                opts.log.debug({ payload: payload, resolved: resolved },
                    'No resolve data: returning');
                return cb();
            }

            opts.cache.addResolveData(payload);
            opts.data = resolved;
            opts.data.serverUUID = opts.config.serverUUID;

            mod_vmapi.list(opts, function (err2, vms) {
                if (err2) {
                    return cb(err2);
                }

                var updatePayload = {
                    log: opts.log,
                    owner_uuid: resolved.owner_uuid,
                    payload: {
                        rules: resolved.rules,
                        vms: opts.vms
                    }
                };

                if (vms) {
                    updatePayload.payload.remoteVMs = vms;
                }

                fw.update(updatePayload, function (err3, res) {
                    if (err3) {
                        return cb(err3);
                    }

                    opts.log.debug(res, 'Updated rules for owner_uuid="%s"',
                        resolved.owner_uuid);
                    return cb(null, res);
                });
            });
        });

    }, function _done(err) {
        opts.log.debug(opts.cache.state, 'cache state after resolve');
        return callback(err);
    });
}



function syncFromAPIs(opts, callback) {
    mod_vm.list({ }, function (err, vms) {
        if (err) {
            return callback(err);
        }

        var owner;
        var ownerVMs = {};
        var t;

        for (var v in vms) {
{'fraction_non_alphanumeric': 0.12204333134565692, 'fraction_numerical': 0.004969191015702644, 'mean_word_length': 1.4285714285714286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3695104', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1330, 'n_words': 328}