#coding:utf-8

import sunhpc
class Command(sunhpc.commands.set.host.command):
    """
    Sets the logical interface of a mac address for particular hosts.

    <arg type='string' name='host' repeat='1'>
    One or more named hosts.
    </arg>
    
    <arg type='string' name='mac'>
    MAC address of the interface  whose logical interface will be reassigned
    </arg>
    
    <arg type='string' name='iface'>
    Logical interface.
    </arg>

    <param type='string' name='mac'>
    Can be used in place of the mac argument.
    </param>

    <param type='string' name='iface'>
    Can be used in place of the iface argument.
    </param>
    

    <example cmd='set host interface iface compute-0-0 00:0e:0c:a7:5d:ff eth1'>
    Sets the logical interface of MAC address 00:0e:0c:a7:5d:ff to be eth1 
    </example>

    <example cmd='set host interface iface compute-0-0 iface=eth1 mac=00:0e:0c:a7:5d:ff'>
    Same as above.
    </example>
    
    <!-- cross refs do not exist yet
    <related>set host interface iface</related>
    <related>set host interface ip</related>
    <related>set host interface module</related>
    -->
    <related>add host</related>
    """
    
    def run(self, params, args):

        (args, mac, iface) = self.fillPositionalArgs(('mac', 'iface'))

        if not len(args):
            self.abort('must supply host')
        if not mac:
            self.abort('must supply mac')
        if not iface:
            self.abort('must supply iface')

        for host in self.getHostnames(args):
            self.db.execute("""update networks set device='%s' 
                where nodes.name='%s' and networks.node=nodes.id and
                networks.mac='%s'""" %
                (iface, host, mac))

RollName = "base"
