<commit_before>// Copyright 2012 Joyent, Inc.  All rights reserved.

var assert = require('assert');
var util = require('util');
var s = require('semver');
var restify = require('restify');



// --- Globals

var ResourceNotFoundError = restify.ResourceNotFoundError;



// --- Helpers

function translate(req, dataset) {
    assert.ok(req);
    assert.ok(dataset);

    var obj = {
        id: dataset.uuid,
        name: dataset.name,
        os: dataset.os,
        type: (dataset.type === 'zvol' ? 'virtualmachine': 'smartmachine'),
        description: dataset.description,
        'default': dataset['default'],
        requirements: {
            password: <PASSWORD>.requirements && dataset.requirements.password
        },
        version: dataset.version,
        created: dataset.created_at || dataset.published_at
    };


    if (dataset.requirements && dataset.requirements.max_ram) {
        obj.requirements.max_memory = dataset.requirements.max_ram;
    }

    if (dataset.requirements && dataset.requirements.min_ram) {
        obj.requirements.min_memory = dataset.requirements.min_ram;
    }

    if (/6\.5/.test(req.getVersion())) {
        obj.urn = dataset.urn;
    }

    return obj;
}



// --- Functions

function load_65(req, res, next) {
    var log = req.log;

    req.dataset = false;

    return req.sdc.imgapi.listImages({}, function (err, datasets) {
        if (err) {
            return next(err);
        }

        req.datasets = datasets || [];

        var dataset, _d;
        if (req.params.dataset || req.params.image) {
            _d = req.params.dataset || req.params.image;
            dataset = req.datasets.filter(function (d) {
                if (_d === d.uuid || _d === d.urn || _d === d.name) {
                    return d;
                }
                return undefined;
            });
        } else {
            dataset = req.datasets.filter(function (d) {
                if (d.name === 'smartos') {
                    return d;
                }
                return undefined;
            });
        }

        if (dataset.length) {
            req.dataset = dataset.reduce(function (a, b) {
                if (s.gte(s.valid(a.version), s.valid(b.version))) {
                    return a;
                } else {
                    return b;
                }
            });
            log.debug('load selected image %j', req.dataset);
        }

        return next();
    });
}


function load(req, res, next) {

    assert.ok(req.account);
    assert.ok(req.sdc.imgapi);

    var log = req.log;

    req.dataset = false;

    // Skip dataset loading and filtering if we're neither on datasets
    // or machines end-points.
    if (!/\/datasets/.test(req.url) && !/\/machines/.test(req.url)) {
        return next();
    }

    // TODO: Remember to remove once we get rid of 6.5 support.
    if (/6\.5/.test(req.getVersion())) {
        return load_65(req, res, next);
    }

    return req.sdc.imgapi.listImages({}, function (err, datasets) {
        if (err) {<commit_msg>PUBAPI-439: We need to retrieve all the datasets, including disabled ones.<commit_after>// Copyright 2012 Joyent, Inc.  All rights reserved.

var assert = require('assert');
var util = require('util');
var s = require('semver');
var restify = require('restify');



// --- Globals

var ResourceNotFoundError = restify.ResourceNotFoundError;



// --- Helpers

function translate(req, dataset) {
    assert.ok(req);
    assert.ok(dataset);

    var obj = {
        id: dataset.uuid,
        name: dataset.name,
        os: dataset.os,
        type: (dataset.type === 'zvol' ? 'virtualmachine': 'smartmachine'),
        description: dataset.description,
        'default': dataset['default'],
        requirements: {
            password: dataset.requirements && dataset.requirements.password
        },
        version: dataset.version,
        created: dataset.created_at || dataset.published_at
    };


    if (dataset.requirements && dataset.requirements.max_ram) {
        obj.requirements.max_memory = dataset.requirements.max_ram;
    }

    if (dataset.requirements && dataset.requirements.min_ram) {
        obj.requirements.min_memory = dataset.requirements.min_ram;
    }

    if (/6\.5/.test(req.getVersion())) {
        obj.urn = dataset.urn;
    }

    return obj;
}



// --- Functions

function load_65(req, res, next) {
    var log = req.log;

    req.dataset = false;

    return req.sdc.imgapi.listImages({}, function (err, datasets) {
        if (err) {
            return next(err);
        }

        req.datasets = datasets || [];

        var dataset, _d;
        if (req.params.dataset || req.params.image) {
            _d = req.params.dataset || req.params.image;
            dataset = req.datasets.filter(function (d) {
                if (_d === d.uuid || _d === d.urn || _d === d.name) {
                    return d;
                }
                return undefined;
            });
        } else {
            dataset = req.datasets.filter(function (d) {
                if (d.name === 'smartos') {
                    return d;
                }
                return undefined;
            });
        }

        if (dataset.length) {
            req.dataset = dataset.reduce(function (a, b) {
                if (s.gte(s.valid(a.version), s.valid(b.version))) {
                    return a;
                } else {
                    return b;
                }
            });
            log.debug('load selected image %j', req.dataset);
        }

        return next();
    });
}


function load(req, res, next) {

    assert.ok(req.account);
    assert.ok(req.sdc.imgapi);

    var log = req.log;

    req.dataset = false;

    // Skip dataset loading and filtering if we're neither on datasets
    // or machines end-points.
    if (!/\/datasets/.test(req.url) && !/\/machines/.test(req.url)) {
        return next();
    }

    // TODO: Remember to remove once we get rid of 6.5 support.
    if (/6\.5/.test(req.getVersion())) {
        return load_65(req, res, next);
    }

    return req.sdc.imgapi.listImages({
        state: 'all'
    }, function (err, datasets) {
        if (err) {
{'fraction_non_alphanumeric': 0.13978668390433097, 'fraction_numerical': 0.005009696186166775, 'mean_word_length': 1.7580213903743316, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '7342198', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1789, 'n_words': 414}