<commit_before>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * lib/sapi.js: client library for the SDC Services API (SAPI)
 */

var assert = require('assert-plus');
var async = require('async');
var fs = require('fs');
var qs = require('querystring');
var path = require('path');
var util = require('util');
var vasync = require('vasync');

var sprintf = require('util').format;

var RestifyClient = require('./restifyclient');



// --- API Functions


/**
 * Constructor
 *
 * See the RestifyClient constructor for details
 */
function SAPI(options) {
    assert.object(options, 'options');
    assert.object(options.log, 'options.log');

    this.log = options.log;

    RestifyClient.call(this, options);
}

util.inherits(SAPI, RestifyClient);



// --- Applications

/**
 * Create an application
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createApplication(name, owner_uuid, opts, callback) {
    assert.string(name, 'name');
    assert.string(owner_uuid, 'owner_uuid');

    if (typeof (opts) === 'function') {
        callback = opts;
        opts = {};
    }

    opts.name = name;
    opts.owner_uuid = owner_uuid;

    return (this.post('/applications', opts, callback));
}

SAPI.prototype.createApplication = createApplication;


/**
 * Lists all applications
 *
 * @param {Function} callback: of the form f(err, apps).
 */
function listApplications(search_opts, callback) {
    if (arguments.length === 1) {
        callback = search_opts;
        search_opts = {};
    }

    var uri = '/applications?' + qs.stringify(search_opts);
    return (this.get(uri, callback));
}

SAPI.prototype.listApplications = listApplications;


/**
 * Gets an application by UUID
 *
 * @param {String} uuid: the UUID of the applications.
 * @param {Function} callback: of the form f(err, app).
 */
function getApplication(uuid, callback) {
    return (this.get(sprintf('/applications/%s', uuid), callback));
}

SAPI.prototype.getApplication = getApplication;


/**
 * Updates an application
 *
 * @param {String} uuid: the UUID of the applications.
 * @param {String} opts: new attributes
 * @param {Function} callback: of the form f(err, app).
 */
function updateApplication(uuid, opts, callback) {
    assert.string(uuid, 'uuid');
    assert.object(opts, 'opts');

    return (this.put(sprintf('/applications/%s', uuid), opts, callback));
}

SAPI.prototype.updateApplication = updateApplication;


/**
 * Deletes  an application by UUID
 *
 * @param {String} uuid: the UUID of the applications.
 * @param {Function} callback : of the form f(err).
 */
function deleteApplication(uuid, callback) {
    return (this.del(sprintf('/applications/%s', uuid), callback));
}

SAPI.prototype.deleteApplication = deleteApplication;



// --- Services

/**
 * Create a service
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createService(name, application_uuid, opts, callback) {
    assert.string(name, 'name');
    assert.string(application_uuid, 'application_uuid');

    if (typeof (opts) === 'function') {
        callback = opts;
        opts = {};
    }

    opts.name = name;
    opts.application_uuid = application_uuid;

    return (this.post('/services', opts, callback));
}

SAPI.prototype.createService = createService;


/**
 * Lists all services
 *
 * @param {Function} callback: of the form f(err, apps).
 */
function listServices(search_opts, callback) {
    if (arguments.length === 1) {
        callback = search_opts;
        search_opts = {};
    }

    var uri = '/services?' + qs.stringify(search_opts);
    return (this.get(uri, callback));
}

SAPI.prototype.listServices = listServices;


/**
 * Gets a service by UUID
 *
 * @param {String} uuid: the UUID of the services.
 * @param {Function} callback: of the form f(err, app).
 */
function getService(uuid, callback) {
    return (this.get(sprintf('/services/%s', uuid), callback));
}

SAPI.prototype.getService = getService;


/**
 * Updates a service
 *
 * @param {String} uuid: the UUID of the services.
 * @param {String} opts: new attributes
 * @param {Function} callback: of the form f(err, app).
 */
function updateService(uuid, opts, callback) {
    assert.string(uuid, 'uuid');
    assert.object(opts, 'opts');

    return (this.put(sprintf('/services/%s', uuid), opts, callback));
}

SAPI.prototype.updateService = updateService;


/**
 * Deletes a service by UUID
 *
 * @param {String} uuid: the UUID of a service.
 * @param {Function} callback : of the form f(err).
 */
function deleteService(uuid, callback) {
    return (this.del(sprintf('/services/%s', uuid), callback));
}

SAPI.prototype.deleteService = deleteService;



// --- Instances

/**
 * Create an instance
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createInstance(service_uuid, opts, callback) {
    assert.string(service_uuid, 'service_uuid');

    if (typeof (opts) === 'function') {
        callback = opts;
        opts = {};
    }

    opts.service_uuid = service_uuid;

    return (this.post('/instances', opts, callback));
}

SAPI.prototype.createInstance = createInstance;


/**
 * Lists all instances
 *
 * @param {Function} callback: of the form f(err, instances).
 */
function listInstances(search_opts, callback) {
    if (arguments.length === 1) {
        callback = search_opts;
        search_opts = {};
    }

    var uri = '/instances?' + qs.stringify(search_opts);
    return (this.get(uri, callback));
}

SAPI.prototype.listInstances = listInstances;


/**
 * Gets an instance by UUID
 *
 * @param {String} uuid: the UUID of the instance.
 * @param {Function} callback: of the form f(err, instance).
 */
function getInstance(uuid, callback) {
    return (this.get(sprintf('/instances/%s', uuid), callback));
}

SAPI.prototype.getInstance = getInstance;


/**
 * Updates an instance
 *
 * @param {String} uuid: the UUID of the instances.
 * @param {String} opts: new attributes
 * @param {Function} callback: of the form f(err, app).
 */
function updateInstance(uuid, opts, callback) {
    assert.string(uuid, 'uuid');
    assert.object(opts, 'opts');

    return (this.put(sprintf('/instances/%s', uuid), opts, callback));
}

SAPI.prototype.updateInstance = updateInstance;


/**
 * Deletes an instance by UUID
 *
 * @param {String} uuid: the UUID of an instance.
 * @param {Function} callback : of the form f(err).
 */
function deleteInstance(uuid, callback) {
    return (this.del(sprintf('/instances/%s', uuid), callback));
}

SAPI.prototype.deleteInstance = deleteInstance;



// --- Manifests

/**
 * Create a manifest
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createManifest(manifest, callback) {
    assert.object(manifest, 'manifest');
    assert.string(manifest.name, 'manifest.name');
    assert.string(manifest.template, 'manifest.template');
    assert.string(manifest.path, 'manifest.path');

    return (this.post('/manifests', manifest, callback));
}

SAPI.prototype.createManifest = createManifest;


/**
 * Lists all manifests
 *
 * @param {Function} callback: of the form f(err, manifests).
 */
function listManifests(callback) {
    return (this.get('/manifests', callback));
}

SAPI.prototype.listManifests = listManifests;


/**
 * Gets a manifest by UUID
 *
 * @param {String} uuid: the UUID of the manifest.
 * @param {Function} callback: of the form f(err, manifest).
 */
function getManifest(uuid, callback) {
    return (this.get(sprintf('/manifests/%s', uuid), callback));
}

SAPI.prototype.getManifest = getManifest;


/**
 * Deletes a manifest by UUID
 *
 * @param {String} uuid: the UUID of a manifest.
 * @param {Function} callback : of the form f(err).
 */
function deleteManifest(uuid, callback) {
    return (this.del(sprintf('/manifests/%s', uuid), callback));
}

SAPI.prototype.deleteManifest = deleteManifest;



// -- Images

function searchImages(name, callback) {
    assert.string(name, 'name');
    assert.func(callback, 'callback');

    return (this.get(sprintf('/images?name=%s', name), callback));
}

SAPI.prototype.searchImages = searchImages;

function downloadImage(uuid, callback) {
    assert.string(uuid, 'uuid');
    assert.func(callback, 'callback');

    return (this.post(sprintf('/images/%s', uuid), {}, callback));
}

SAPI.prototype.downloadImage = downloadImage;



// -- Configs

function getConfig(uuid, callback) {
    assert.string(uuid, 'uuid');
    assert.func(callback, 'callback');

    return (this.get(sprintf('/configs/%s', uuid), callback));<commit_msg>SAPI-89 SearchImages needs ability to search by version<commit_after>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * lib/sapi.js: client library for the SDC Services API (SAPI)
 */

var assert = require('assert-plus');
var async = require('async');
var fs = require('fs');
var qs = require('querystring');
var path = require('path');
var util = require('util');
var vasync = require('vasync');

var sprintf = require('util').format;

var RestifyClient = require('./restifyclient');



// --- API Functions


/**
 * Constructor
 *
 * See the RestifyClient constructor for details
 */
function SAPI(options) {
    assert.object(options, 'options');
    assert.object(options.log, 'options.log');

    this.log = options.log;

    RestifyClient.call(this, options);
}

util.inherits(SAPI, RestifyClient);



// --- Applications

/**
 * Create an application
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createApplication(name, owner_uuid, opts, callback) {
    assert.string(name, 'name');
    assert.string(owner_uuid, 'owner_uuid');

    if (typeof (opts) === 'function') {
        callback = opts;
        opts = {};
    }

    opts.name = name;
    opts.owner_uuid = owner_uuid;

    return (this.post('/applications', opts, callback));
}

SAPI.prototype.createApplication = createApplication;


/**
 * Lists all applications
 *
 * @param {Function} callback: of the form f(err, apps).
 */
function listApplications(search_opts, callback) {
    if (arguments.length === 1) {
        callback = search_opts;
        search_opts = {};
    }

    var uri = '/applications?' + qs.stringify(search_opts);
    return (this.get(uri, callback));
}

SAPI.prototype.listApplications = listApplications;


/**
 * Gets an application by UUID
 *
 * @param {String} uuid: the UUID of the applications.
 * @param {Function} callback: of the form f(err, app).
 */
function getApplication(uuid, callback) {
    return (this.get(sprintf('/applications/%s', uuid), callback));
}

SAPI.prototype.getApplication = getApplication;


/**
 * Updates an application
 *
 * @param {String} uuid: the UUID of the applications.
 * @param {String} opts: new attributes
 * @param {Function} callback: of the form f(err, app).
 */
function updateApplication(uuid, opts, callback) {
    assert.string(uuid, 'uuid');
    assert.object(opts, 'opts');

    return (this.put(sprintf('/applications/%s', uuid), opts, callback));
}

SAPI.prototype.updateApplication = updateApplication;


/**
 * Deletes  an application by UUID
 *
 * @param {String} uuid: the UUID of the applications.
 * @param {Function} callback : of the form f(err).
 */
function deleteApplication(uuid, callback) {
    return (this.del(sprintf('/applications/%s', uuid), callback));
}

SAPI.prototype.deleteApplication = deleteApplication;



// --- Services

/**
 * Create a service
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createService(name, application_uuid, opts, callback) {
    assert.string(name, 'name');
    assert.string(application_uuid, 'application_uuid');

    if (typeof (opts) === 'function') {
        callback = opts;
        opts = {};
    }

    opts.name = name;
    opts.application_uuid = application_uuid;

    return (this.post('/services', opts, callback));
}

SAPI.prototype.createService = createService;


/**
 * Lists all services
 *
 * @param {Function} callback: of the form f(err, apps).
 */
function listServices(search_opts, callback) {
    if (arguments.length === 1) {
        callback = search_opts;
        search_opts = {};
    }

    var uri = '/services?' + qs.stringify(search_opts);
    return (this.get(uri, callback));
}

SAPI.prototype.listServices = listServices;


/**
 * Gets a service by UUID
 *
 * @param {String} uuid: the UUID of the services.
 * @param {Function} callback: of the form f(err, app).
 */
function getService(uuid, callback) {
    return (this.get(sprintf('/services/%s', uuid), callback));
}

SAPI.prototype.getService = getService;


/**
 * Updates a service
 *
 * @param {String} uuid: the UUID of the services.
 * @param {String} opts: new attributes
 * @param {Function} callback: of the form f(err, app).
 */
function updateService(uuid, opts, callback) {
    assert.string(uuid, 'uuid');
    assert.object(opts, 'opts');

    return (this.put(sprintf('/services/%s', uuid), opts, callback));
}

SAPI.prototype.updateService = updateService;


/**
 * Deletes a service by UUID
 *
 * @param {String} uuid: the UUID of a service.
 * @param {Function} callback : of the form f(err).
 */
function deleteService(uuid, callback) {
    return (this.del(sprintf('/services/%s', uuid), callback));
}

SAPI.prototype.deleteService = deleteService;



// --- Instances

/**
 * Create an instance
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createInstance(service_uuid, opts, callback) {
    assert.string(service_uuid, 'service_uuid');

    if (typeof (opts) === 'function') {
        callback = opts;
        opts = {};
    }

    opts.service_uuid = service_uuid;

    return (this.post('/instances', opts, callback));
}

SAPI.prototype.createInstance = createInstance;


/**
 * Lists all instances
 *
 * @param {Function} callback: of the form f(err, instances).
 */
function listInstances(search_opts, callback) {
    if (arguments.length === 1) {
        callback = search_opts;
        search_opts = {};
    }

    var uri = '/instances?' + qs.stringify(search_opts);
    return (this.get(uri, callback));
}

SAPI.prototype.listInstances = listInstances;


/**
 * Gets an instance by UUID
 *
 * @param {String} uuid: the UUID of the instance.
 * @param {Function} callback: of the form f(err, instance).
 */
function getInstance(uuid, callback) {
    return (this.get(sprintf('/instances/%s', uuid), callback));
}

SAPI.prototype.getInstance = getInstance;


/**
 * Updates an instance
 *
 * @param {String} uuid: the UUID of the instances.
 * @param {String} opts: new attributes
 * @param {Function} callback: of the form f(err, app).
 */
function updateInstance(uuid, opts, callback) {
    assert.string(uuid, 'uuid');
    assert.object(opts, 'opts');

    return (this.put(sprintf('/instances/%s', uuid), opts, callback));
}

SAPI.prototype.updateInstance = updateInstance;


/**
 * Deletes an instance by UUID
 *
 * @param {String} uuid: the UUID of an instance.
 * @param {Function} callback : of the form f(err).
 */
function deleteInstance(uuid, callback) {
    return (this.del(sprintf('/instances/%s', uuid), callback));
}

SAPI.prototype.deleteInstance = deleteInstance;



// --- Manifests

/**
 * Create a manifest
 *
 * @param {Function} callback: of the form f(err, app).
 */
function createManifest(manifest, callback) {
    assert.object(manifest, 'manifest');
    assert.string(manifest.name, 'manifest.name');
    assert.string(manifest.template, 'manifest.template');
    assert.string(manifest.path, 'manifest.path');

    return (this.post('/manifests', manifest, callback));
}

SAPI.prototype.createManifest = createManifest;


/**
 * Lists all manifests
 *
 * @param {Function} callback: of the form f(err, manifests).
 */
function listManifests(callback) {
    return (this.get('/manifests', callback));
}

SAPI.prototype.listManifests = listManifests;


/**
 * Gets a manifest by UUID
 *
 * @param {String} uuid: the UUID of the manifest.
 * @param {Function} callback: of the form f(err, manifest).
 */
function getManifest(uuid, callback) {
    return (this.get(sprintf('/manifests/%s', uuid), callback));
}

SAPI.prototype.getManifest = getManifest;


/**
 * Deletes a manifest by UUID
 *
 * @param {String} uuid: the UUID of a manifest.
 * @param {Function} callback : of the form f(err).
 */
function deleteManifest(uuid, callback) {
    return (this.del(sprintf('/manifests/%s', uuid), callback));
}

SAPI.prototype.deleteManifest = deleteManifest;



// -- Images

function searchImages(name, version, callback) {
    if (arguments.length === 2) {
        callback = version;
        version = null;
    }

    assert.string(name, 'name');
    assert.optionalString(version, 'version');
    assert.func(callback, 'callback');

    var uri = sprintf('/images?name=%s', name);

    if (version)
        uri = uri + sprintf('&version=%s', version);

    return (this.get(uri, callback));
}

SAPI.prototype.searchImages = searchImages;

function downloadImage(uuid, callback) {
    assert.string(uuid, 'uuid');
    assert.func(callback, 'callback');

    return (this.post(sprintf('/images/%s', uuid), {}, callback));
}

SAPI.prototype.downloadImage = downloadImage;



// -- Configs

function getConfig(uuid, callback) {
    assert.string(uuid, 'uuid');
    assert.func(callback, 'callback');

    return (this.get(sprintf('/configs/%s', uuid), callback));
{'fraction_non_alphanumeric': 0.15437947769817426, 'fraction_numerical': 0.0009822047608042525, 'mean_word_length': 3.730527466520907, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5057645', 'n_tokens_mistral': 5369, 'n_tokens_neox': 5294, 'n_words': 1427}