Skip to content

Commit 9c69b8d

Browse files
feat: adjustments endpoints
1 parent a8443e8 commit 9c69b8d

2 files changed

Lines changed: 103 additions & 19 deletions

File tree

src/controllers/adjustEndpoints.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Adjust endpoints endpoint (POST /adjustendpoints)
33
* Filters and adjusts endpoints based on provider capabilities
4+
* Supports A, TXT, and CNAME record types
45
*/
56

67
const config = require('../config');
@@ -41,25 +42,25 @@ function adjustEndpoints(req, res) {
4142
return res.status(400).json({ error: 'Request body must be an array of endpoints' });
4243
}
4344

44-
// Filter endpoints:
45-
// 1. Keep only A and TXT record types
46-
// 2. Remove invalid endpoints
47-
const adjustedEndpoints = endpoints.filter(endpoint => {
48-
// Check if valid endpoint structure
49-
if (!endpoint || typeof endpoint !== 'object') {
50-
logger.warn('Skipping invalid endpoint (not an object)');
51-
return false;
52-
}
53-
54-
// Only support A and TXT records
55-
const recordType = endpoint.recordType;
56-
if (recordType !== 'A' && recordType !== 'TXT') {
57-
logger.debug('Filtering out unsupported record type', {
58-
dnsName: endpoint.dnsName,
59-
recordType
60-
});
61-
return false;
62-
}
45+
// Filter endpoints:
46+
// 1. Keep only A, TXT, and CNAME record types
47+
// 2. Remove invalid endpoints
48+
const adjustedEndpoints = endpoints.filter(endpoint => {
49+
// Check if valid endpoint structure
50+
if (!endpoint || typeof endpoint !== 'object') {
51+
logger.warn('Skipping invalid endpoint (not an object)');
52+
return false;
53+
}
54+
55+
// Support A, TXT, and CNAME records (as advertised in README)
56+
const recordType = endpoint.recordType;
57+
if (recordType !== 'A' && recordType !== 'TXT' && recordType !== 'CNAME') {
58+
logger.debug('Filtering out unsupported record type', {
59+
dnsName: endpoint.dnsName,
60+
recordType
61+
});
62+
return false;
63+
}
6364

6465
// Validate endpoint structure
6566
if (!validator.isValidEndpoint(endpoint)) {

test-adjust-endpoints.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Test script for adjustEndpoints CNAME support
4+
*/
5+
6+
// Mock logger
7+
const logger = {
8+
info: () => {},
9+
warn: () => {},
10+
debug: () => {}
11+
};
12+
13+
// Mock validator
14+
const validator = {
15+
isValidEndpoint: (endpoint) => {
16+
// Simple validation - just check required fields exist
17+
return endpoint && endpoint.dnsName && endpoint.targets && endpoint.recordType;
18+
}
19+
};
20+
21+
// Mock config
22+
const config = {
23+
contentType: 'application/external.dns.webhook+json;version=1'
24+
};
25+
26+
// Mock express objects
27+
const mockReq = {
28+
body: [],
29+
get: () => null
30+
};
31+
32+
const mockRes = {
33+
status: function(code) { this.statusCode = code; return this; },
34+
json: function(data) { this.data = data; return this; },
35+
setHeader: function() {},
36+
send: function(data) { this.sentData = data; }
37+
};
38+
39+
// Import and test adjustEndpoints
40+
const adjustEndpoints = require('./src/controllers/adjustEndpoints');
41+
42+
// Test data
43+
const testEndpoints = [
44+
{ dnsName: 'a.example.com', targets: ['1.2.3.4'], recordType: 'A' },
45+
{ dnsName: 'txt.example.com', targets: ['test=value'], recordType: 'TXT' },
46+
{ dnsName: 'cname.example.com', targets: ['target.example.com'], recordType: 'CNAME' },
47+
{ dnsName: 'invalid.example.com', targets: ['1.2.3.4'], recordType: 'AAAA' } // Should be filtered out
48+
];
49+
50+
console.log('Testing adjustEndpoints CNAME support...');
51+
52+
// Mock the request
53+
mockReq.body = testEndpoints;
54+
55+
// Call adjustEndpoints
56+
adjustEndpoints(mockReq, mockRes);
57+
58+
// Parse the response
59+
const response = JSON.parse(mockRes.sentData);
60+
console.log('Original endpoints:', testEndpoints.length);
61+
console.log('Filtered endpoints:', response.length);
62+
63+
console.log('Results:');
64+
response.forEach(endpoint => {
65+
console.log(` ${endpoint.recordType}: ${endpoint.dnsName}`);
66+
});
67+
68+
const cnameCount = response.filter(e => e.recordType === 'CNAME').length;
69+
const aCount = response.filter(e => e.recordType === 'A').length;
70+
const txtCount = response.filter(e => e.recordType === 'TXT').length;
71+
const invalidCount = response.filter(e => e.recordType === 'AAAA').length;
72+
73+
console.log(`\nSummary:`);
74+
console.log(` A records: ${aCount}`);
75+
console.log(` TXT records: ${txtCount}`);
76+
console.log(` CNAME records: ${cnameCount}`);
77+
console.log(` Invalid records: ${invalidCount}`);
78+
79+
if (cnameCount > 0 && invalidCount === 0) {
80+
console.log('\n✅ SUCCESS: CNAME records are now supported and invalid records are filtered!');
81+
} else {
82+
console.log('\n❌ FAILURE: CNAME support test failed');
83+
}

0 commit comments

Comments
 (0)