def format_hwadapters(self, hws): # This is never called if p_json is true resp_list = [] for i, hw in enumerate(hws): for k, v in hw.iteritems(): resp_list.append("\t{0} | {1}: {2}".format(i, k, v)) return resp_list def format_response(self, nas, resp_msg, user_msg): # Override this so we can display hwadapters better resp_list = [] if nas.p_json: resp_list.append(json.dumps(resp_msg, indent=2)) else: resp_list.append(user_msg) for k, v in resp_msg.iteritems(): if k == 'hwadapter_set': continue # handle these last # indent these else: resp_list.append("{0}: {1}".format(k, v)) if resp_msg.get('hwadapter_set', None): resp_list.append("Hardware Adapters: {0}".format('-' * 20)) resp_list += self.format_hwadapters(resp_msg['hwadapter_set']) return resp_list # Uncomment when dhcp is rolled out registrar.register(DispatchSREG()) registrar.register(DispatchHW())
delete_args = [ delete_pk_argument('pk', dtype) ] detail_args = [detail_pk_argument('pk', dtype)] def get_create_data(self, nas): data = super(DispatchNetwork, self).get_create_data(nas) return set_ip_type('network_str', data) def get_update_data(self, nas): data = super(DispatchNetwork, self).get_update_data(nas) return set_ip_type('network_str', data) registrar.register(DispatchNetwork()) class DispatchSite(CoreDispatch): resource_name = 'site' dtype = 'SITE' dgroup = 'core' update_args = [ name_argument('full_name'), comment_argument('comment'), description_argument('description'), update_pk_argument('pk', dtype) ] create_args = [
return self.handle_resp(nas, main_json, resp) def format_response(self, nas, resp_msg, user_msg): resp_list = [] if nas.p_json: resp_list.append(json.dumps(resp_msg, indent=2)) else: resp_list.append(user_msg) messages = None for k, v in resp_msg.iteritems(): if k == 'options': resp_list.append("Decommission options used:") for k_i, v_i in v.iteritems(): resp_list.append("\t{0}: {1}".format(k_i, v_i)) elif k == 'systems': resp_list.append("systems: {0}".format(', '.join(v))) elif k == 'messages': messages = v else: resp_list.append("{0}: {1}".format(k, v)) if messages: resp_list.append( "Additional information returned by Inventory:" ) resp_list += messages return resp_list registrar.register(DecommissionDispatch())
service_export = base_parser.add_parser('service_export', help="Export services.", add_help=True) service_export.add_argument( 'query', type=str, help="A search string describing the services to export") service_export.add_argument( '--yaml', action='store_true', default=False, dest='p_yaml', help="Export in YAML format", ) def service_export(self, nas): tmp_url = "/core/service/export/" url = "{0}{1}".format(REMOTE, tmp_url) headers = {'content-type': 'application/json'} query = {'search': nas.query} resp = requests.get(url, params=query, headers=headers, auth=auth()) if nas.DEBUG: sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format( 'get', url, query)) nas.p_json = True # Do this so we can play with the response return self.handle_service_resp(nas, query, resp) registrar.register(ServiceImportDispatch()) registrar.register(ServiceExportDispatch())
if nas.p_json: return 0, [json.dumps(results, indent=2)] resp_list = ["# of Used IPs: {0}".format(results['used']), "# of Unused IPs: {0}".format(results['unused']), "------ Vacant IP ranges ------"] resp_list += display_ranges(results['free_ranges']) return 0, resp_list def query(self, nas): tmp_url = "/core/search/search_dns_text/" url = "{0}{1}".format(REMOTE, tmp_url) headers = {'content-type': 'application/json'} search = {'search': nas.query} resp = requests.get(url, params=search, headers=headers, auth=auth) if resp.status_code == 500: resp_list = [ "CLIENT ERROR! (Please email this output to a code monkey)" ] self.error_out(nas, search, resp, resp_list=resp_list) return results = self.get_resp_dict(resp) if 'text_response' not in results: return 1, [] else: if nas.p_json: return 0, [json.dumps(results, indent=2)] return 0, [results['text_response']] registrar.register(SearchDispatch())
def build_parser(self, base_parser): service_export = base_parser.add_parser( 'service_export', help="Export services.", add_help=True ) service_export.add_argument( 'query', type=str, help="A search string describing the services to export" ) service_export.add_argument( '--yaml', action='store_true', default=False, dest='p_yaml', help="Export in YAML format", ) def service_export(self, nas): tmp_url = "/core/service/export/" url = "{0}{1}".format(REMOTE, tmp_url) headers = {'content-type': 'application/json'} query = {'search': nas.query} resp = requests.get(url, params=query, headers=headers, auth=auth()) if nas.DEBUG: sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format( 'get', url, query )) nas.p_json = True # Do this so we can play with the response return self.handle_service_resp(nas, query, resp) registrar.register(ServiceImportDispatch()) registrar.register(ServiceExportDispatch())
kvlist_args = [kvlist_pk_argument('obj_pk', dtype)] def do_test_setup(self, test_case): command = [EXEC, DispatchNetwork.dtype, 'create'] for add_arg, extract_arg, tm in DispatchNetwork.create_args: if tm()[0] in ('site-pk', 'vlan-pk'): continue command.append(test_method_to_params(tm())) command_str = ' '.join(command) ret, errors, rc = call_to_json(command_str) if errors: test_case.fail(errors) return ret['pk'] registrar.register(NetworkKV()) class SiteKV(DispatchKV, TestKVSetupMixin): kv_class = 'sitekeyvalue' dtype = 'SITE_kv' dgroup = 'kv' create_args = [ key_argument('key'), value_argument('value'), create_pk_argument('obj_pk', dtype) ] update_args = [ key_argument('key'),
] # When a system is updated, there may be a new hostname. A user can use # 'new-hostname' to specify a new hostname and use --pk <hostname> to # specify which host is getting the new hostname. update_args = [ system_update_pk_argument(), new_hostname_argument() ] + create_args # When a system is created, there must be a hostname create_args = create_args + [ hostname_argument('hostname', 'A valid hostname'), ] delete_args = [ delete_pk_argument('pk', dtype), comment_argument('comment') ] detail_args = [system_detail_pk_argument()] def update_url(self, nas): pk = nas.pk or nas.hostname return self.object_url.format( API_MAJOR_VERSION, self.resource_name, pk ) registrar.register(DispatchSystem())
except KeyError: raise KeyError( "'systems' wasn't in the exported blob. The API changed" ) result = [] for hostname, blob in blobs.iteritems(): result.append("--- Lookup values for {0}".format(hostname)) result += self._show_lookup_paths(prefix, blob) return result def _show_lookup_paths(self, prefix, blob): result = [] more = [] for key, value in blob.iteritems(): if isinstance(value, dict): more += self._show_lookup_paths( prefix + key + '.', value) elif isinstance(value, list): # CNAME exception for cn in value: more += self._show_lookup_paths( prefix, {key: cn} ) else: result.append("{0}{1} = {2}".format(prefix, key, value)) return result + more registrar.register(BAImportDispatch()) registrar.register(BAExportDispatch()) registrar.register(BAShowDispatch())
"# of Unused IPs: {0}".format(results['unused']), "------ Vacant IP ranges ------" ] resp_list += display_ranges(results['free_ranges']) return 0, resp_list def query(self, nas): tmp_url = "/core/search/search_dns_text/" url = "{0}{1}".format(REMOTE, tmp_url) headers = {'content-type': 'application/json'} search = {'search': nas.query} resp = requests.get(url, params=search, headers=headers, auth=auth()) if nas.DEBUG: sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format( 'get', url, search)) was_json = nas.p_json nas.p_json = True # Do this so we can play with the response ret_code, raw_results = self.handle_resp(nas, search, resp) if ret_code: return (ret_code, raw_results) # repack and go home results = json.loads(raw_results[0]) if 'text_response' not in results: return 1, [] else: if was_json: return 0, raw_results return 0, [results['text_response']] registrar.register(SearchDispatch())
] delete_args = [delete_pk_argument('pk', dtype)] detail_args = [detail_pk_argument('pk', dtype)] def get_create_data(self, nas): data = super(DispatchNetwork, self).get_create_data(nas) return set_ip_type('network_str', data) def get_update_data(self, nas): data = super(DispatchNetwork, self).get_update_data(nas) return set_ip_type('network_str', data) registrar.register(DispatchNetwork()) class DispatchSite(CoreDispatch): resource_name = 'site' dtype = 'SITE' dgroup = 'core' update_args = [ name_argument('full_name'), comment_argument('comment'), description_argument('description'), update_pk_argument('pk', dtype) ] create_args = [
] delete_args = [ delete_pk_argument('kv_pk', dtype) ] detail_args = [detail_pk_argument('kv_pk', dtype)] kvlist_args = [kvlist_pk_argument('obj_pk', dtype)] def do_test_setup(self, test_case): sreg_pk = self.do_setup(DispatchSREG, test_case) # Create a HW command = [EXEC, DispatchHW.dtype, 'create'] for add_arg, extract_arg, test_method in DispatchHW.create_args: command.append(test_method_to_params(test_method())) command_str = ' '.join(command) # lol, we are jinja2. evaluate our placeholder command_str = command_str.replace('{{ sreg_pk }}', str(sreg_pk)) hw_ret, hw_errors, hw_rc = call_to_json(command_str) if hw_errors: test_case.fail(hw_errors) return hw_ret['pk'] # Uncomment when dhcp is rolled out registrar.register(StaticRegKV()) registrar.register(HWAdapterKV())
if nas.query: return self.query(nas) else: return (0, ['What do you want?']) def query(self, nas): tmp_url = "/en-US/csv/ajax_csv_exporter/" url = "{0}{1}".format(REMOTE, tmp_url) headers = {'content-type': 'application/json'} search = {'search': nas.query} resp = requests.get(url, params=search, headers=headers, auth=auth()) if nas.DEBUG: sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format( 'get', url, search )) was_json = nas.p_json nas.p_json = True # Do this so we can play with the response ret_code, raw_results = self.handle_resp(nas, search, resp) if ret_code: return (ret_code, raw_results) # repack and go home results = json.loads(raw_results[0]) if 'csv_content' not in results: return 1, [] else: if was_json: return 0, raw_results return 0, [''.join(results['csv_content'])] registrar.register(CSVDispatch())
create_args = [ key_argument('key'), value_argument('value'), create_pk_argument('obj_pk', dtype) ] update_args = [ key_argument('key'), value_argument('value'), update_pk_argument('kv_pk', dtype) ] delete_args = [delete_pk_argument('kv_pk', dtype)] detail_args = [detail_pk_argument('kv_pk', dtype)] kvlist_args = [kvlist_pk_argument('obj_pk', dtype)] def do_test_setup(self, test_case): command = [EXEC, DispatchSystem.dtype, 'create'] for add_arg, extract_arg, tm in DispatchSystem.create_args: command.append(test_method_to_params(tm())) command_str = ' '.join(command) ret, errors, rc = call_to_json(command_str) if errors: test_case.fail(errors) return ret['pk'] registrar.register(SystemKV())
'yyyy-mm-dd'), date_argument('warranty-end', 'Warrenty end date yyyy-mm-dd'), notes_argument('description', 'Notes about this system'), comment_argument('comment'), ] # When a system is updated, there may be a new hostname. A user can use # 'new-hostname' to specify a new hostname and use --pk <hostname> to # specify which host is getting the new hostname. update_args = [system_pk_argument(), new_hostname_argument()] + create_args # When a system is created, there must be a hostname create_args = create_args + [ hostname_argument('hostname', 'A valid hostname'), ] delete_args = [ system_pk_argument(action='deleting'), comment_argument('comment') ] detail_args = [system_pk_argument(action='detailing')] def update_url(self, nas): pk = nas.pk or nas.hostname return self.object_url.format(API_MAJOR_VERSION, self.resource_name, pk) registrar.register(DispatchSystem())
kvlist_args = [kvlist_pk_argument('obj_pk', dtype)] def do_test_setup(self, test_case): command = [EXEC, DispatchNetwork.dtype, 'create'] for add_arg, extract_arg, tm in DispatchNetwork.create_args: if tm()[0] in ('site-pk', 'vlan-pk'): continue command.append(test_method_to_params(tm())) command_str = ' '.join(command) ret, errors, rc = call_to_json(command_str) if errors: test_case.fail(errors) return ret['pk'] registrar.register(NetworkKV()) class SiteKV(DispatchKV, TestKVSetupMixin): kv_class = 'sitekeyvalue' dtype = 'SITE_kv' dgroup = 'kv' create_args = [ key_argument('key'), value_argument('value'), create_pk_argument('obj_pk', dtype) ] update_args = [ key_argument('key'),
description_argument('description'), comment_argument('comment') ] update_args = create_args + [ update_pk_argument('pk', dtype) ] delete_args = [ delete_pk_argument('pk', dtype) ] detail_args = [detail_pk_argument('pk', dtype)] registrar.register(DispatchA()) registrar.register(DispatchAAAA()) registrar.register(DispatchCNAME()) registrar.register(DispatchMX()) registrar.register(DispatchPTR()) registrar.register(DispatchSRV()) registrar.register(DispatchTXT()) def build_dns_parsers(base_parser): # Build all the records for dispatch in [d for d in registrar.dispatches if d.dgroup == 'dns']: record_base_parser = base_parser.add_parser( dispatch.dtype, help="Interface for {0} records".format(dispatch.dtype), add_help=True
def build_parser(self, base_parser): base_parser.add_parser( 'status', help="Print config vars.", add_help=True ) def status(self, nas): """ Look at the config module and print out interesting things. """ # Items of interest items = ( 'API_MAJOR_VERSION', 'INVTOOL_VERSION', 'CONFIG_FILE', 'GLOBAL_CONFIG_FILE', 'LOCAL_CONFIG_FILE', 'AUTH_TYPE', 'REMOTE', 'dev', 'host', 'port', ) ret = {} for item in items: ret[item] = getattr(config, item) return 0, self.format_response(nas, ret, 'Status Vars') registrar.register(StatusDispatch())
value_argument('value'), create_pk_argument('obj_pk', dtype) ] update_args = [ key_argument('key'), value_argument('value'), update_pk_argument('kv_pk', dtype) ] delete_args = [ delete_pk_argument('kv_pk', dtype) ] detail_args = [detail_pk_argument('kv_pk', dtype)] kvlist_args = [kvlist_pk_argument('obj_pk', dtype)] def do_test_setup(self, test_case): command = [EXEC, DispatchSystem.dtype, 'create'] for add_arg, extract_arg, tm in DispatchSystem.create_args: command.append(test_method_to_params(tm())) command_str = ' '.join(command) ret, errors, rc = call_to_json(command_str) if errors: test_case.fail(errors) return ret['pk'] registrar.register(SystemKV())
def csv(self, nas): if nas.query: return self.query(nas) else: return (0, ['What do you want?']) def query(self, nas): tmp_url = "/en-US/csv/ajax_csv_exporter/" url = "{0}{1}".format(REMOTE, tmp_url) headers = {'content-type': 'application/json'} search = {'search': nas.query} resp = requests.get(url, params=search, headers=headers, auth=auth()) if nas.DEBUG: sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format( 'get', url, search)) was_json = nas.p_json nas.p_json = True # Do this so we can play with the response ret_code, raw_results = self.handle_resp(nas, search, resp) if ret_code: return (ret_code, raw_results) # repack and go home results = json.loads(raw_results[0]) if 'csv_content' not in results: return 1, [] else: if was_json: return 0, raw_results return 0, [''.join(results['csv_content'])] registrar.register(CSVDispatch())