<commit_before>/**
 * views/provision-vm.js
 *
 * Provision a VM
 */

define(function(require) {
    var Base = require('views/base');
    var Images = require('models/images');
    var Users = require('models/users');
    var Package = require('models/package');
    var Packages = require('models/packages');
    var Networks = require('models/networks');
    var Vm = require('models/vm');

    var Job = require('models/job');
    var JobProgressView = require('views/job-progress');

    var adminui = require('adminui');

    var PackageSelectOption = Backbone.Marionette.ItemView.extend({
        attributes: function() {
            return {
                name: this.model.get('name'),
                value: this.model.get('uuid')
            };
        },
        tagName: 'option',
        template: Handlebars.compile('{{name}} {{version}}')
    });

    var PackageSelect = Backbone.Marionette.CollectionView.extend({
        itemView: PackageSelectOption,
        tagName: 'select',
        events: {
            'change': 'onChange'
        },
        onChange: function(e) {
            var uuid = $(e.target).val();
            this.trigger('select', this.collection.get(uuid));
        }
    });

    var tplProvisionVm = require('tpl!provision-vm');

    var View = Backbone.Marionette.ItemView.extend({
        name: 'provision-vm',

        sidebar: 'vms',

        template: _.template(tplProvisionVm),

        events: {
            'submit form': 'provision',
            'change input': 'checkFields'
        },

        modelEvents: {
            'error': 'onError'
        },

        ui: {
            'form': 'form',
            'alert': '.alert'
        },

        initialize: function(options) {
            this.vent = adminui.vent;
            this.model = new Vm();
            this.packages = new Packages();
            this.packageSelect = new PackageSelect({
                collection: this.packages
            });
            this.selectedPackage = new Package();

            this.packages.on('reset', function(collection) {
                this.selectedPackage.set(collection.models[0].attributes);
            }, this);

            this.packageSelect.on('select', function(pkg) {
                this.selectedPackage.set(pkg.attributes);
            }, this);

            this.packageBinder = new Backbone.ModelBinder();

            this.imagesSource = [];
            this.usersSource = [];

            this.usersCollection = new Users();
            this.imagesCollection = new Images();
            this.networks = new Networks();

            this.usersCollection.on('reset', function(users) {
                this.userSource = [];
                users.each(function(u) {
                    this.usersSource.push(u);
                }, this);
            }, this);

            this.imagesCollection.on('reset', function(images) {
                images.each(function(u) {
                    this.imagesSource.push(u);
                }, this);
            }, this);

            this.networks.on('reset', function(networks) {
                this.populateNetworks(networks);
            }, this);

            this.imagesCollection.fetch();
            this.usersCollection.searchByLogin('');
            this.networks.fetch();
            this.packages.fetchActive();
        },

        onRender: function() {
            this.packageSelect.setElement(this.$('select[name=package]')).render();
            this.packageBinder.bind(this.selectedPackage, this.$('.package-details'));

            this.hideError();

            this.$("input[name=image]").typeahead({
                source: this.imagesSource,
                labeler: function(obj) {
                    return [obj.get('name'), obj.get('version')].join(" ");
                },
                valuer: function(obj) {
                    return obj.get('uuid');
                }
            });

            this.$("input[name=owner]").typeahead({
                source: this.usersSource,
                labeler: function(obj) {
                    return [obj.get('login'), obj.get('cn')].join(" - ");
                },
                valuer: function(obj) {
                    return obj.get('uuid');
                }
            });

            this.checkFields();

            return this;
        },

        populateNetworks: function(networks) {
            var container = this.$('.network-checkboxes');
            var elm = container.find('label:first').clone();
            container.find('label').remove();
            networks.each(function(n) {
                elm.find('.name').html(
                [n.get('name'), n.get('subnet')].join(' - '));
                elm.find('input').val(n.get('uuid'));
                elm.clone().prependTo(container);
            }, this);
        },


        checkFields: function() {
            this.hideError();
            var values = this.extractFormValues();
            if (!values.owner_uuid.length || !values.networks.length || !values.image_uuid.length) {
                this.disableProvisionButton();
            } else {
                this.enableProvisionButton();
            }
        },

        disableProvisionButton: function() {
            this.$('button[type=submit]').attr('disabled', 'disabled');
        },

        enableProvisionButton: function() {
            this.$('button[type=submit]').removeAttr('disabled');
        },

        extractFormValues: function() {
            var formData = this.ui.form.serializeObject();
            var values = {
                image_uuid: formData.image,
                dataset_uuid: formData.image,
                // XXX Backwards compat.
                ram: formData.memory,
                owner_uuid: formData.owner,
                brand: formData.brand,
                alias: formData.alias
            };

            var pkg = this.packages.get(formData['package']);

            if (pkg) {
                values['billing_id'] = pkg.get('uuid');
                values['package_name'] = pkg.get('name');
                values['package_version'] = pkg.get('version');
                values['cpu_cap'] = pkg.get('cpu_cap');
                values['max_lwps'] = pkg.get('max_lwps');
                values['max_swap'] = pkg.get('max_swap');
                values['quota'] = pkg.get('quota');
                values['vcpus'] = pkg.get('vcpus');
                values['zfs_io_priority'] = pkg.get('zfs_io_priority');<commit_msg>ADMINUI-1084 Use image brand requirements for payload when provisioning<commit_after>/**
 * views/provision-vm.js
 *
 * Provision a VM
 */

define(function(require) {
    var Base = require('views/base');
    var Images = require('models/images');
    var Users = require('models/users');
    var Package = require('models/package');
    var Packages = require('models/packages');
    var Networks = require('models/networks');
    var Vm = require('models/vm');

    var Job = require('models/job');
    var JobProgressView = require('views/job-progress');

    var adminui = require('adminui');

    var PackageSelectOption = Backbone.Marionette.ItemView.extend({
        attributes: function() {
            return {
                name: this.model.get('name'),
                value: this.model.get('uuid')
            };
        },
        tagName: 'option',
        template: Handlebars.compile('{{name}} {{version}}')
    });

    var PackageSelect = Backbone.Marionette.CollectionView.extend({
        itemView: PackageSelectOption,
        tagName: 'select',
        events: {
            'change': 'onChange'
        },
        onChange: function(e) {
            var uuid = $(e.target).val();
            this.trigger('select', this.collection.get(uuid));
        }
    });

    var tplProvisionVm = require('tpl!provision-vm');

    var View = Backbone.Marionette.ItemView.extend({
        name: 'provision-vm',

        sidebar: 'vms',

        template: _.template(tplProvisionVm),

        events: {
            'submit form': 'provision',
            'change input': 'checkFields'
        },

        modelEvents: {
            'error': 'onError'
        },

        ui: {
            'form': 'form',
            'alert': '.alert'
        },

        initialize: function(options) {
            this.vent = adminui.vent;
            this.model = new Vm();
            this.packages = new Packages();
            this.packageSelect = new PackageSelect({
                collection: this.packages
            });
            this.selectedPackage = new Package();

            this.packages.on('reset', function(collection) {
                this.selectedPackage.set(collection.models[0].attributes);
            }, this);

            this.packageSelect.on('select', function(pkg) {
                this.selectedPackage.set(pkg.attributes);
            }, this);

            this.packageBinder = new Backbone.ModelBinder();

            this.imagesSource = [];
            this.usersSource = [];

            this.usersCollection = new Users();
            this.imagesCollection = new Images();
            this.networks = new Networks();

            this.usersCollection.on('reset', function(users) {
                this.userSource = [];
                users.each(function(u) {
                    this.usersSource.push(u);
                }, this);
            }, this);

            this.imagesCollection.on('reset', function(images) {
                images.each(function(u) {
                    this.imagesSource.push(u);
                }, this);
            }, this);

            this.networks.on('reset', function(networks) {
                this.populateNetworks(networks);
            }, this);

            this.imagesCollection.fetch();
            this.usersCollection.searchByLogin('');
            this.networks.fetch();
            this.packages.fetchActive();
        },

        onRender: function() {
            this.packageSelect.setElement(this.$('select[name=package]')).render();
            this.packageBinder.bind(this.selectedPackage, this.$('.package-details'));

            this.hideError();

            this.$("input[name=image]").typeahead({
                source: this.imagesSource,
                labeler: function(obj) {
                    return [obj.get('name'), obj.get('version')].join(" ");
                },
                valuer: function(obj) {
                    return obj.get('uuid');
                }
            });

            this.$("input[name=owner]").typeahead({
                source: this.usersSource,
                labeler: function(obj) {
                    return [obj.get('login'), obj.get('cn')].join(" - ");
                },
                valuer: function(obj) {
                    return obj.get('uuid');
                }
            });

            this.checkFields();

            return this;
        },

        populateNetworks: function(networks) {
            var container = this.$('.network-checkboxes');
            var elm = container.find('label:first').clone();
            container.find('label').remove();
            networks.each(function(n) {
                elm.find('.name').html(
                [n.get('name'), n.get('subnet')].join(' - '));
                elm.find('input').val(n.get('uuid'));
                elm.clone().prependTo(container);
            }, this);
        },


        checkFields: function() {
            this.hideError();
            var values = this.extractFormValues();
            if (!values.owner_uuid.length || !values.networks.length || !values.image_uuid.length) {
                this.disableProvisionButton();
            } else {
                this.enableProvisionButton();
            }
        },

        disableProvisionButton: function() {
            this.$('button[type=submit]').attr('disabled', 'disabled');
        },

        enableProvisionButton: function() {
            this.$('button[type=submit]').removeAttr('disabled');
        },

        extractFormValues: function() {
            var formData = this.ui.form.serializeObject();
            var values = {
                image_uuid: formData.image,
                dataset_uuid: formData.image,
                // XXX Backwards compat.
                ram: formData.memory,
                owner_uuid: formData.owner,
                brand: formData.brand,
                alias: formData.alias
            };

            var image = this.imagesCollection.get(formData.image);
            var imageReqs = image.get('requirements') || {};

            if (imageReqs['brand']) {
                values.brand = imageReqs['brand'];
            }

            var pkg = this.packages.get(formData['package']);

            if (pkg) {
                values['billing_id'] = pkg.get('uuid');
                values['package_name'] = pkg.get('name');
                values['package_version'] = pkg.get('version');
                values['cpu_cap'] = pkg.get('cpu_cap');
                values['max_lwps'] = pkg.get('max_lwps');
                values['max_swap'] = pkg.get('max_swap');
                values['quota'] = pkg.get('quota');
                values['vcpus'] = pkg.get('vcpus');
                values['zfs_io_priority'] = pkg.get('zfs_io_priority');
{'fraction_non_alphanumeric': 0.1448031910890344, 'fraction_numerical': 0.0004515692029803567, 'mean_word_length': 1.6365079365079365, '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': '5876963', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3395, 'n_words': 557}