<commit_before>/** @jsx React.DOM */

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */
"use strict";

var Backbone = require('backbone');
var _ = require('underscore');

var React = require('react');

var NicConfigComponent = React.createFactory(require('../components/nic-config'));


var JobProgress = require('./job-progress');

var VMNicForm = Backbone.Marionette.ItemView.extend({
    template: require('../tpl/vm-nic-form.hbs'),
    id: 'vm-nic-form',
    attributes: {
        'class': 'modal'
    },
    events: {
        'click button[type=submit]': 'onSubmit'
    },

    initialize: function(options) {
        if (! this.model) {
            this.model = new Backbone.Model();
        }
        this.vm = options.vm;
    },

    onSubmit: function() {
        var self = this;
        var vm = this.vm;
        var nic = this.nicConfig.getValue();
        nic.uuid = nic.network_uuid;
        delete nic.network_uuid;

        if (! nic.mac) {
            vm.addNics([nic], function(err, job) {
                if (err) {
                    window.alert('Error adding network interface ' + err);
                    return;
                }
                self.$el.modal('hide').remove();
                var view = new JobProgress({ model: job });
                view.show();

                self.listenTo(view, 'execution', function(st) {
                    if (st === 'succeeded') {
                        vm.fetch();
                    }
                });
            });
        } else {
            var nics = vm.get('nics');
            if (nic.primary) {
                nics = nics.map(function(n) {
                    delete n.primary;
                    return n;
                });
            }

            var existingNic = _.findWhere(nics, {mac: nic.mac});

            if (existingNic) {
                _.extend(existingNic, nic);
            } else {
                nics.push(nic);
            }
            nics = nics.map(function(n) {
                delete n.vlan_id;
                delete n.nic_tag;
                delete n.ip;
                delete n.netmask;
                delete n.state;
                delete n.belongs_to_type;
                delete n.belongs_to_uuid;
                delete n.gateway;
                delete n.resolvers;
                delete n.network_uuid;
                delete n.owner_uuid;
                delete n.uuid;
                return n;
            });

            vm.updateNics(nics, function(err, job) {
                if (err) {
                    window.alert('Error updating network interfaces ' + err);
                    return;
                }
                self.$el.modal('hide').remove();
                var view = new JobProgress({
                    model: job
                });
                view.show();
                self.listenTo(view, 'execution', function(st) {
                    if (st === 'succeeded') {
                        vm.fetch();
                    }
                });
            });
        }
    },

    disableButtons: function() {
        this.$('button[type=submit]').prop('disabled', true);
    },

    enableButtons: function() {
        this.$('button[type=submit]').prop('disabled', false);
    },

    onNicPropertyChange: function(prop, value, nic) {
        if (nic.network_uuid) {
            this.enableButtons();
        } else {
            this.disableButtons();
        }
    },

    onRender: function() {
        this.nicConfig = React.render(
            new NicConfigComponent({
                nic: this.model.toJSON(),
                readonlyNetwork: true,
                expandAntispoofOptions: false,
                onPropertyChange: this.onNicPropertyChange.bind(this)
            }),
            this.$('.nic-config-component').get(0));

        this.$el.modal().on('hidden', this.remove.bind(this));
        this.$el.modal('show');
        this.disableButtons();
    }
});

module.exports = VMNicForm;
<commit_msg>ADMINUI-1937 add nic to vm missing networks<commit_after>/** @jsx React.DOM */

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */
"use strict";

var Backbone = require('backbone');
var _ = require('underscore');

var React = require('react');

var NicConfigComponent = React.createFactory(require('../components/nic-config'));


var JobProgress = require('./job-progress');

var VMNicForm = Backbone.Marionette.ItemView.extend({
    template: require('../tpl/vm-nic-form.hbs'),
    id: 'vm-nic-form',
    attributes: {
        'class': 'modal'
    },
    events: {
        'click button[type=submit]': 'onSubmit'
    },

    initialize: function(options) {
        if (! this.model) {
            this.model = new Backbone.Model();
        }
        this.vm = options.vm;
    },

    onSubmit: function() {
        var self = this;
        var vm = this.vm;
        var nic = this.nicConfig.getValue();
        nic.uuid = nic.network_uuid;
        delete nic.network_uuid;

        if (! nic.mac) {
            vm.addNics([nic], function(err, job) {
                if (err) {
                    window.alert('Error adding network interface ' + err);
                    return;
                }
                self.$el.modal('hide').remove();
                var view = new JobProgress({ model: job });
                view.show();

                self.listenTo(view, 'execution', function(st) {
                    if (st === 'succeeded') {
                        vm.fetch();
                    }
                });
            });
        } else {
            var nics = vm.get('nics');
            if (nic.primary) {
                nics = nics.map(function(n) {
                    delete n.primary;
                    return n;
                });
            }

            var existingNic = _.findWhere(nics, {mac: nic.mac});

            if (existingNic) {
                _.extend(existingNic, nic);
            } else {
                nics.push(nic);
            }
            nics = nics.map(function(n) {
                delete n.vlan_id;
                delete n.nic_tag;
                delete n.ip;
                delete n.netmask;
                delete n.state;
                delete n.belongs_to_type;
                delete n.belongs_to_uuid;
                delete n.gateway;
                delete n.resolvers;
                delete n.network_uuid;
                delete n.owner_uuid;
                delete n.uuid;
                return n;
            });

            vm.updateNics(nics, function(err, job) {
                if (err) {
                    window.alert('Error updating network interfaces ' + err);
                    return;
                }
                self.$el.modal('hide').remove();
                var view = new JobProgress({
                    model: job
                });
                view.show();
                self.listenTo(view, 'execution', function(st) {
                    if (st === 'succeeded') {
                        vm.fetch();
                    }
                });
            });
        }
    },

    disableButtons: function() {
        this.$('button[type=submit]').prop('disabled', true);
    },

    enableButtons: function() {
        this.$('button[type=submit]').prop('disabled', false);
    },

    onNicPropertyChange: function(prop, value, nic) {
        if (nic.network_uuid) {
            this.enableButtons();
        } else {
            this.disableButtons();
        }
    },

    onRender: function() {
        this.nicConfig = React.render(
            new NicConfigComponent({
                nic: this.model.toJSON(),
                readonlyNetwork: !this.model.isNew(),
                expandAntispoofOptions: false,
                onPropertyChange: this.onNicPropertyChange.bind(this)
            }),
            this.$('.nic-config-component').get(0));

        this.$el.modal().on('hidden', this.remove.bind(this));
        this.$el.modal('show');
        this.disableButtons();
    }
});

module.exports = VMNicForm;

{'fraction_non_alphanumeric': 0.1312329740613526, 'fraction_numerical': 0.0026057088712542935, 'mean_word_length': 1.3442531926707384, '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': '93999', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2248, 'n_words': 484}