示例#1
0
    def get(self, id):
        # if not inst.config.network, just add

        inst = self.get_instance(id, isowner=True)
        if not inst: return

        form = NetworkForm()
        self.update_form( form )

        if inst.config:
            config = json.loads(inst.config)
            if 'network' in config.keys():
                network = config['network']
                if len(network) > 0:
                    network = network[0]
                    form.type.data = network['type']
                    form.ip.data = network['ip']
                    form.netmask.data = network['netmask']
                    form.gateway.data = network['gateway']


        d = { 'title': _('Edit Network'),
              'instance': inst, 'form': form }

        self.render('instance/network_edit.html', **d)
示例#2
0
    def post(self, id):

        inst = self.get_instance(id, isowner=True)
        if not inst: return

        form = NetworkForm( self.request.arguments )
        self.update_form( form )

        if form.validate():

            network = {
                'type': form.type.data,
                'mac': inst.mac, # TODO: use old mac
                'ip': form.ip.data,
                'netmask': form.netmask.data,
                'gateway': form.gateway.data
                }

            if inst.config:
                config = json.loads(inst.config)
            else:
                config = {}

            config['network'] = [network]
            inst.config = json.dumps(config)
            #print 'config = ', inst.config
            self.db2.commit()

            # Update IpAssign table
            ipassign = self.db2.query(IpAssign).filter_by( ip = network['ip'] ).first()
            if ipassign:
                ipassign.user_id = self.current_user.id
                ipassign.instance_id = id
                updated = datetime.utcnow()
            else:
                c = IpAssign( ip = network['ip'],
                              user = self.current_user,
                              instance = inst )
                self.db2.add( c )

            if inst.is_running:
                inst.ischanged = True
            self.db2.commit()

            url = self.reverse_url('instance:view', id)
            url += '?view=network'
            return self.redirect( url )

        # Get error
        d = { 'title': _('Edit Network'),
              'instance': inst, 'form': form }
        self.render('instance/network_edit.html', **d)