def render(self, generate, session, logger, switch, **kwargs): dbswitch = Switch.get_unique(session, switch, compel=True) plenary_info = PlenarySwitch(dbswitch, logger=logger) if generate: return plenary_info._generate_content() else: return plenary_info.read()
def render(self, session, logger, switch, label, model, rack, type, ip, interface, mac, vendor, serial, comments, **arguments): dbmodel = Model.get_unique(session, name=model, vendor=vendor, machine_type='switch', compel=True) dblocation = get_location(session, rack=rack) dbdns_rec, newly_created = grab_address(session, switch, ip, allow_restricted_domain=True, allow_reserved=True, preclude=True) if not label: label = dbdns_rec.fqdn.name try: Switch.check_label(label) except ArgumentError: raise ArgumentError("Could not deduce a valid hardware label " "from the switch name. Please specify " "--label.") # FIXME: What do the error messages for an invalid enum (switch_type) # look like? dbswitch = Switch(label=label, switch_type=type, location=dblocation, model=dbmodel, serial_no=serial, comments=comments) session.add(dbswitch) dbswitch.primary_name = dbdns_rec # FIXME: get default name from the model iftype = "oa" if not interface: interface = "xge" ifcomments = "Created automatically by add_switch" else: ifcomments = None if interface.lower().startswith("lo"): iftype = "loopback" dbinterface = get_or_create_interface(session, dbswitch, name=interface, mac=mac, interface_type=iftype, comments=ifcomments) dbnetwork = get_net_id_from_ip(session, ip) # TODO: should we call check_ip_restrictions() here? assign_address(dbinterface, ip, dbnetwork) session.flush() plenary = PlenarySwitch(dbswitch, logger=logger) with plenary.get_write_key() as key: plenary.stash() try: plenary.write(locked=True) dsdb_runner = DSDBRunner(logger=logger) dsdb_runner.update_host(dbswitch, None) dsdb_runner.commit_or_rollback("Could not add switch to DSDB") except: plenary.restore_stash() raise return