from PLC.Faults import * from PLC.Auth import Auth from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Table import Row from PLC.Nodes import Node, Nodes from PLC.Interfaces import Interface, Interfaces from PLC.IpAddresses import IpAddress, IpAddresses from PLC.Routes import Route, Routes from PLC.TagTypes import TagTypes from PLC.InterfaceTags import InterfaceTags from PLC.Methods.AddInterfaceTag import AddInterfaceTag from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag from PLC.Methods.UpdateNode import UpdateNode cannot_update = ['interface_id', 'node_id'] legacy_interface_fields = { 'interface_id': Parameter(int, "Node interface identifier"), 'method': Parameter(str, "Addressing method (e.g., 'static' or 'dhcp')"), 'type': Parameter(str, "Address type (e.g., 'ipv4')"), 'ip': Parameter(str, "IP address", nullok = True), 'mac': Parameter(str, "MAC address", nullok = True), 'gateway': Parameter(str, "IP address of primary gateway", nullok = True), 'network': Parameter(str, "Subnet address", nullok = True), 'broadcast': Parameter(str, "Network broadcast address", nullok = True), 'netmask': Parameter(str, "Subnet mask", nullok = True), 'dns1': Parameter(str, "IP address of primary DNS server", nullok = True), 'dns2': Parameter(str, "IP address of secondary DNS server", nullok = True), 'bwlimit': Parameter(int, "Bandwidth limit", min = 0, nullok = True), 'hostname': Parameter(str, "(Optional) Hostname", nullok = True), 'node_id': Parameter(int, "Node associated with this interface"), 'is_primary': Parameter(bool, "Is the primary interface for this node"), 'interface_tag_ids' : Parameter([int], "List of interface settings"), 'last_updated': Parameter(int, "Date and time when the interface entry was last updated"), } class AddInterface(Method): """ Adds a new network for a node. Any values specified in interface_fields are used, otherwise defaults are used. If type is static, then ip, gateway, network, broadcast, netmask, and dns1 must all be specified in interface_fields. If type is dhcp, these parameters, even if specified, are ignored. PIs and techs may only add interfaces to their own nodes. Admins may add interfaces to any node. Returns the new interface_id (> 0) if successful, faults otherwise. """ roles = ['admin', 'pi', 'tech'] accepted_fields = Row.accepted_fields(cannot_update, legacy_interface_fields, exclude=True) accepted_fields.update(Interface.tags) accepts = [ Auth(), Mixed(Node.fields['node_id'], Node.fields['hostname']), accepted_fields ] returns = Parameter(int, 'New interface_id (> 0) if successful') # needed for generating the doc and prevent conflicts in the xml ids status = 'legacy' def call(self, auth, node_id_or_hostname, interface_fields): [native,tags,rejected]=Row.split_fields(interface_fields,[legacy_interface_fields,Interface.tags]) # type checking native = Row.check_fields (native, self.accepted_fields) if rejected: raise PLCInvalidArgument, "Cannot add Interface with column(s) %r"%rejected # Check if node exists nodes = Nodes(self.api, [node_id_or_hostname]) if not nodes: raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname node = nodes[0] # Authenticated function assert self.caller is not None # If we are not an admin, make sure that the caller is a # member of the site where the node exists. if 'admin' not in self.caller['roles']: if node['site_id'] not in self.caller['site_ids']: raise PLCPermissionDenied, "Not allowed to add an interface to the specified node" # Add interface interface = Interface(self.api, native) interface['node_id'] = node['node_id'] # if this is the first interface, make it primary if not node['interface_ids']: interface['is_primary'] = True interface.sync() # Add IpAddress to conform with the new object model if native['method'] == 'static': address_fields = {} address_fields['interface_id'] = interface['interface_id'] address_fields['ip_addr'] = native['ip'] address_fields['netmask'] = native['netmask'] address_fields['type'] = native['type'] ip_address = IpAddress(self.api, address_fields) ip_address.sync() # ADD DNS and ROUTES if this is a primary interface if native['is_primary']: route_fields = {} route_fields['node_id'] = interface['node_id'] route_fields['interface_id'] = interface['interface_id'] route_fields['subnet'] = '0.0.0.0/0' route_fields['next_hop'] = interface['gateway'] route = Route(self.api, route_fields) route.sync() dns = "" if native.has_key('dns1'): dns += native['dns1'] if native.has_key('dns2'): dns += ",%s" % native['dns2'] if dns: UpdateNode(self.api).__call__(auth, interface['node_id'], {'dns':dns}) # Logging variables self.event_objects = { 'Node': [node['node_id']], 'Interface' : [interface['interface_id']] } self.message = "Interface %d added" % interface['interface_id'] for (tagname,value) in tags.iteritems(): # the tagtype instance is assumed to exist, just check that if not TagTypes(self.api,{'tagname':tagname}): raise PLCInvalidArgument,"No such TagType %s"%tagname interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']}) if not interface_tags: AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value) else: UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value) return interface['interface_id']