def testEquality(self): dns_entry_one = DnsEntry('@', 300, DnsEntry.TYPE_A, '8.8.8.8') dns_entry_two = DnsEntry('@', 300, DnsEntry.TYPE_A, '8.8.8.8') self.assertEqual(dns_entry_one, dns_entry_two) dns_entry_two.expire = 600 self.assertEqual(dns_entry_one, dns_entry_two) dns_entry_two.content = '127.0.0.1' self.assertNotEqual(dns_entry_one, dns_entry_two)
def testSetDnsEntries(self, mock_client): # SETUP ds = DomainService('sundayafternoon') ds.build_cookie = Mock(return_value={"cookie": "value"}) ds.update_cookie = Mock() i = mock_client.return_value i.service.setDnsEntries.return_value = None dns_entry = DnsEntry('testentry', 86400, DnsEntry.TYPE_A, '127.0.0.1') # CALL result = ds.set_dns_entries('domain1', [ dns_entry, ]) # VERIFY ds.build_cookie.assert_called_with( mode=MODE_RW, method='setDnsEntries', parameters=['domain1', [ dns_entry, ]]) ds.update_cookie.assert_called_with({"cookie": "value"}) i.service.setDnsEntries.assert_called_with('domain1', [ dns_entry, ])
def setUp(self): self.domain_service = mock.MagicMock() with open(KEY_FILE, 'w') as key_file: key_file.write('''-----BEGIN RSA PRIVATE KEY----- foobar -----END RSA PRIVATE KEY-----''') self.transip_client = _TransipClient(username=USERNAME, key_file=KEY_FILE) self.transip_client.domain_service = self.domain_service self.domain_service.get_domain_names.return_value = ['example.com'] self.correct_entry1 = DnsEntry(name='record1', record_type='A', content='127.0.0.1', expire=1) self.correct_entry2 = DnsEntry(name='record2', record_type='TXT', content='f00b4r', expire=1) self.add_record = DnsEntry(name='test.test', record_type='TXT', content='new record', expire=1)
def add_txt_record(self, domain_name, record_name, record_content): """ Add a TXT record using the supplied information. :param str domain_name: The domain to use to associate the record with. :param str record_name: The record name (typically beginning with '_acme-challenge.'). :param str record_content: The record content (typically the challenge validation). :raises certbot.errors.PluginError: if an error occurs communicating with the Transip API """ try: domain = self._find_domain(domain_name) except suds.WebFault as e: self.logger.error('Error finding domain using the Transip API: %s', e) raise errors.PluginError( 'Error finding domain using the Transip API: {0}'.format(e)) try: domain_records = self.domain_service.get_info( domain_name=domain).dnsEntries except suds.WebFault as e: self.logger.error( 'Error getting DNS records using the Transip API: %s', e) return try: new_record = DnsEntry( name=self._compute_record_name(domain, record_name), record_type='TXT', content=record_content, expire=1, ) except suds.WebFault as e: self.logger.error( 'Error getting DNS records using the Transip API: %s', e) return domain_records.append(new_record) try: self.domain_service.set_dns_entries(domain_name=domain, dns_entries=domain_records) self.logger.info('Successfully added TXT record') except suds.WebFault as e: self.logger.error( 'Error adding TXT record using the Transip API: %s', e) raise errors.PluginError( 'Error adding TXT record using the Transip API: {0}'.format(e))
def update_dns(domain_service, args): """ Adds, updates or deletes a DNS entry through the API :param domain_service: an initialized DomainService object :param args: arguments object from argparse """ try: dns_entries = domain_service.get_info(args.domain_name).dnsEntries except WebFault as err: print(err) exit(1) number_of_entries = len(dns_entries) for entry in dns_entries: if args.add_dns_entry and entry.name == args.entry_name and entry.type == args.entry_type and \ entry.content == args.entry_content: print('The DNS entry already exists.') exit(1) elif args.update_dns_entry and entry.name == args.entry_name and entry.type == args.entry_type: dns_entries.remove(entry) elif args.delete_dns_entry and entry.name == args.entry_name and entry.type == args.entry_type and \ entry.expire == args.entry_expire and entry.content == args.entry_content: dns_entries.remove(entry) if args.update_dns_entry or args.delete_dns_entry: if number_of_entries == len(dns_entries): print('The DNS entry was not found.') exit(1) if args.add_dns_entry or args.update_dns_entry: dns_entries.append( DnsEntry(args.entry_name, args.entry_expire, args.entry_type, args.entry_content)) try: result = domain_service.set_dns_entries(args.domain_name, dns_entries) except WebFault as err: print(err) exit(1) if result is None: print('Request finished successfully.') else: print(result)
def create_record(self, type, name, content): records = self.client.get_info(self.options.get('domain')).dnsEntries if self._filter_records(records, type, name, content): # Nothing to do, record already exists print('create_record: already exists') return True records.append(DnsEntry(**{ "name": self._relative_name(name), "record_type": type, "content": self._bind_format_target(type, content), "expire": self.options.get('ttl') })) self.client.set_dns_entries(self.options.get('domain'), records) status = len(self.list_records(type, name, content, show_output=False)) >= 1 print("create_record: {0}".format(status)) return status
def get_info(self, domain_name): """Retrieves information about the requested domain-name.""" cookie = self.build_cookie(mode=MODE_RO, method='getInfo', parameters=[domain_name]) self.update_cookie(cookie) # Perform the call result = self.soap_client.service.getInfo(domain_name) # Parse the result to well-known objects new_dns_entries = [] for dnsentry in result.dnsEntries: if dnsentry.__class__.__name__ == 'DnsEntry': new_dns_entries.append( DnsEntry(dnsentry.name, dnsentry.expire, dnsentry.type, dnsentry.content)) result.dnsEntries = new_dns_entries return result
def testGetInfo(self, mock_client): # SETUP ds = DomainService(login='******') ds.build_cookie = Mock(return_value={"cookie": "value"}) ds.update_cookie = Mock() i = mock_client.return_value getinfo_result = Mock() getinfo_result.dnsEntries = [ DnsEntry('testentry', 86400, DnsEntry.TYPE_A, '127.0.0.1') ] i.service.getInfo.return_value = getinfo_result # CALL result = ds.get_info('example.com') # VERIFY ds.build_cookie.assert_called_with(mode=MODE_RO, method='getInfo', parameters=['example.com']) ds.update_cookie.assert_called_with({"cookie": "value"}) i.service.getInfo.assert_called_with('example.com') self.assertEqual(result, getinfo_result)
def _to_dns_entry(self, _entry): return DnsEntry(self._relative_name(_entry['name']), _entry['ttl'], _entry['type'], _entry['content'])