docs: def assign_types_to_resources(resource_types,**kwargs):
      """
          Assign new types to list of resources.
          This function checks if the necessary
          attributes are present and adds them if needed. Non existing attributes
          are also added when the type is already assigned. This means that this
          function can also be used to update resources, when a resource type has
          changed.
      """
      #Remove duplicate values from types by turning it into a set
      type_ids = list(set([rt.type_id for rt in resource_types]))
  
      db_types = db.DBSession.query(TemplateType).filter(TemplateType.id.in_(type_ids)).options(joinedload_all('typeattrs')).all()
  
      types = {}
      for db_type in db_types:
          if types.get(db_type.id) is None:
              types[db_type.id] = db_type
      log.debug("Retrieved all the appropriate template types")
      res_types = []
      res_attrs = []
      res_scenarios = []
  
      net_id = None
      node_ids = []
      link_ids = []
      grp_ids  = []
      for resource_type in resource_types:
          ref_id  = resource_type.ref_id
          ref_key = resource_type.ref_key
          if resource_type.ref_key == 'NETWORK':
              net_id = ref_id
          elif resource_type.ref_key == 'NODE':
              node_ids.append(ref_id)
          elif resource_type.ref_key == 'LINK':
              link_ids.append(ref_id)
          elif resource_type.ref_key == 'GROUP':
              grp_ids.append(ref_id)
      if net_id:
          net = db.DBSession.query(Network).filter(Network.id==net_id).one()
      nodes = _get_nodes(node_ids)
      links = _get_links(link_ids)
      groups = _get_groups(grp_ids)
      for resource_type in resource_types:
          ref_id  = resource_type.ref_id
          ref_key = resource_type.ref_key
          type_id = resource_type.type_id
          if ref_key == 'NETWORK':
              resource = net
          elif ref_key == 'NODE':
              resource = nodes[ref_id]
          elif ref_key == 'LINK':
              resource = links[ref_id]
          elif ref_key == 'GROUP':
              resource = groups[ref_id]
  
          ra, rt, rs= set_resource_type(resource, type_id, types)
          if rt is not None:
              res_types.append(rt)
          if len(ra) > 0:
              res_attrs.extend(ra)
          if len(rs) > 0:
              res_scenarios.extend(rs)
  
      log.debug("Retrieved all the appropriate resources")
      if len(res_types) > 0:
          new_types = db.DBSession.execute(ResourceType.__table__.insert(), res_types)
      if len(res_attrs) > 0:
          new_res_attrs = db.DBSession.execute(ResourceAttr.__table__.insert(), res_attrs)
          new_ras = db.DBSession.query(ResourceAttr).filter(and_(ResourceAttr.id>=new_res_attrs.lastrowid, ResourceAttr.id<(new_res_attrs.lastrowid+len(res_attrs)))).all()
  
      ra_map = {}
      for ra in new_ras:
          ra_map[(ra.ref_key, ra.attr_id, ra.node_id, ra.link_id, ra.group_id, ra.network_id)] = ra.id
  
      for rs in res_scenarios:
          rs['resource_attr_id'] = ra_map[(rs['ref_key'], rs['attr_id'], rs['node_id'], rs['link_id'], rs['group_id'], rs['network_id'])]
  
      if len(res_scenarios) > 0:
          new_scenarios = db.DBSession.execute(ResourceScenario.__table__.insert(), res_scenarios)
      #Make DBsession 'dirty' to pick up the inserts by doing a fake delete.
  
      db.DBSession.query(ResourceAttr).filter(ResourceAttr.attr_id==None).delete()
  
      ret_val = [t for t in types.values()]
      return ret_val