class DispatchSRV(DNSDispatch): resource_name = 'srv' dtype = 'SRV' dgroup = 'dns' create_args = [ fqdn_argument('fqdn', dtype), # ~> (labmda, lambda) ttl_argument('ttl'), port_argument('port'), weight_argument('weight'), priority_argument('priority'), target_argument('target'), view_arguments('views'), 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)]
class DispatchA(DNSDispatch): resource_name = 'addressrecord' dtype = 'A' dgroup = 'dns' ip_type = '4' create_args = [ fqdn_argument('fqdn', dtype), # ~> (labmda, lambda) ttl_argument('ttl'), ip_argument('ip_str', ip_type), view_arguments('views'), 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)] def get_create_data(self, nas): data = super(DispatchA, self).get_create_data(nas) data['ip_type'] = self.ip_type return data def get_update_data(self, nas): data = super(DispatchA, self).get_update_data(nas) data['ip_type'] = self.ip_type return data
class DispatchHW(ObjectDispatch): object_url = "/en-US/core/api/v1_core/{1}/{2}/" object_list_url = "/en-US/core/api/v1_core/{1}/" resource_name = 'hwadapter' dtype = 'HW' dgroup = 'dhcp' update_args = [ name_argument('name'), mac_argument('mac'), group_argument('group'), enable_dhcp_argument('enable_dhcp'), description_argument('description'), comment_argument('comment') ] create_args = update_args + [ sreg_argument('sreg'), ] update_args = [update_pk_argument('pk', dtype)] + update_args delete_args = [delete_pk_argument('pk', dtype)] detail_args = [detail_pk_argument('pk', dtype)] 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) for k, v in resp_msg.iteritems(): resp_list.append("{0}: {1}".format(k, v)) return resp_list
class DispatchAAAA(DispatchA): dtype = 'AAAA' dgroup = 'dns' create_args = [ fqdn_argument('fqdn', dtype), # ~> (labmda, lambda) ttl_argument('ttl'), ip_argument('ip_str', '6'), view_arguments('views'), 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)]
class DispatchTXT(DNSDispatch): resource_name = 'txt' dtype = 'TXT' dgroup = 'dns' create_args = [ fqdn_argument('fqdn', dtype), # ~> (labmda, lambda) ttl_argument('ttl'), target_argument('txt_data'), view_arguments('views'), 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)]
class DispatchMX(DNSDispatch): resource_name = 'mx' dtype = 'MX' dgroup = 'dns' create_args = [ fqdn_argument('fqdn', dtype), # ~> (labmda, lambda) ttl_argument('ttl'), priority_argument('priority'), target_argument('server'), view_arguments('views'), 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)]
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 = [ name_argument('full_name', required=True), comment_argument('comment'), description_argument('description') ] delete_args = [delete_pk_argument('pk', dtype)] detail_args = [detail_pk_argument('pk', dtype)]
class DispatchPTR(DNSDispatch): resource_name = 'ptr' dtype = 'PTR' dgroup = 'dns' create_args = [ ttl_argument('ttl'), ip_argument('ip_str', '4'), view_arguments('views'), target_argument('name'), 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)] def determine_ip_type(self, ip_str): if ip_str.find(':') > -1: ip_type = '6' else: ip_type = '4' # Default to 4 return ip_type def get_create_data(self, nas): data = super(DispatchPTR, self).get_create_data(nas) data['ip_type'] = self.determine_ip_type(data.get('ip_str', '')) return data def get_update_data(self, nas): data = super(DispatchPTR, self).get_update_data(nas) data['ip_type'] = self.determine_ip_type(data.get('ip_str', '')) return data
class DispatchA(DNSDispatch): resource_name = 'addressrecord' dtype = 'A' dgroup = 'dns' create_args = [ fqdn_argument('fqdn', dtype), # ~> (labmda, lambda) ttl_argument('ttl'), ip_argument('ip_str', '4'), view_arguments('views'), 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)] def determine_ip_type(self, ip_str): if ip_str.find(':') > -1: ip_type = '6' else: ip_type = '4' # Default to 4 return ip_type def get_create_data(self, nas): data = super(DispatchA, self).get_create_data(nas) data['ip_type'] = self.determine_ip_type(data.get('ip_str', '')) return data def get_update_data(self, nas): data = super(DispatchA, self).get_update_data(nas) data['ip_type'] = self.determine_ip_type(data.get('ip_str', '')) return data
class DispatchVlan(CoreDispatch): resource_name = 'vlan' dtype = 'VLAN' dgroup = 'core' update_args = [ name_argument('name'), number_argument('number'), comment_argument('comment'), description_argument('description'), update_pk_argument('pk', dtype) ] create_args = [ name_argument('name', required=True), number_argument('number', required=True), comment_argument('comment'), description_argument('description') ] delete_args = [delete_pk_argument('pk', dtype)] detail_args = [detail_pk_argument('pk', dtype)]
class DispatchSREG(DNSDispatch): object_url = "/en-US/core/api/v1_core/{1}/{2}/" object_list_url = "/en-US/core/api/v1_core/{1}/" resource_name = 'staticreg' dtype = 'SREG' dgroup = 'dns' ip_type = '4' # Used for testing common_args = [ fqdn_argument('fqdn', dtype), ttl_argument('ttl'), ip_argument('ip_str', ip_type), view_arguments('views'), description_argument('description'), comment_argument('comment') ] update_args = common_args + [ name_argument('name', prefix='nic'), update_pk_argument('pk', dtype) ] create_args = common_args + [ system_argument('system_hostname'), ] delete_args = [delete_pk_argument('pk', dtype)] detail_args = [detail_pk_argument('pk', dtype)] def get_create_data(self, nas): data = super(DispatchSREG, self).get_create_data(nas) return set_ip_type('ip_str', data) def get_update_data(self, nas): data = super(DispatchSREG, self).get_update_data(nas) return set_ip_type('ip_str', data) 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
class DispatchNetwork(CoreDispatch): resource_name = 'network' dtype = 'NET' dgroup = 'core' ip_type = None def test_setup(self): def setUp(self): def create_site(): command = [EXEC, DispatchSite.dtype, 'create'] for add_arg, extract_arg, test_f in DispatchSite.create_args: command.append(test_method_to_params(test_f())) site_ret, site_errors, site_rc = call_to_json( ' '.join(command)) if site_errors: self.fail(site_errors) self.assertEqual(0, site_rc) # Make sure the SREG was created correctly self.assertTrue('http_status' in site_ret) self.assertEqual(site_ret['http_status'], 201) self.assertTrue('pk' in site_ret) return site_ret['pk'] def create_vlan(): command = [EXEC, DispatchVlan.dtype, 'create'] for add_arg, extract_arg, test_f in DispatchVlan.create_args: command.append(test_method_to_params(test_f())) vlan_ret, vlan_errors, vlan_rc = call_to_json( ' '.join(command)) if vlan_errors: self.fail(vlan_errors) self.assertEqual(0, vlan_rc) # Make sure the SREG was created correctly self.assertTrue('http_status' in vlan_ret) self.assertEqual(vlan_ret['http_status'], 201) self.assertTrue('pk' in vlan_ret) return vlan_ret['pk'] self.site_pk = create_site() self.vlan_pk = create_site() def modify_command(command_str): command_str = command_str.replace('{{ site }}', str(self.site_pk)) command_str = command_str.replace('{{ vlan }}', str(self.vlan_pk)) return command_str self.modify_command = modify_command return setUp def test_teardown(self): def tearDown(self): # Delete the object def delete_vlan(): delete_command = "{0} {1} delete --pk {2}".format( EXEC, 'VLAN', self.vlan_pk) ret, errors, rc = call_to_json(delete_command) if errors: self.fail(errors) self.assertEqual(0, rc) self.assertTrue('http_status' in ret) self.assertEqual(ret['http_status'], 204) def delete_site(): delete_command = "{0} {1} delete --pk {2}".format( EXEC, 'SITE', self.vlan_pk) ret, errors, rc = call_to_json(delete_command) if errors: self.fail(errors) self.assertEqual(0, rc) self.assertTrue('http_status' in ret) self.assertEqual(ret['http_status'], 204) delete_vlan() delete_site() return tearDown update_args = [ site_argument('site'), vlan_argument('vlan'), network_str_argument('network-str'), comment_argument('comment'), description_argument('description'), update_pk_argument('pk', dtype) ] create_args = [ site_argument('site'), vlan_argument('vlan'), network_str_argument('network-str'), comment_argument('comment'), description_argument('description'), ] 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)