Skip to content

Commit dd4e94b

Browse files
committed
add dns_server_details table, change param externalserverid to details for AddDnsServerCmd
1 parent 80f7983 commit dd4e94b

19 files changed

Lines changed: 334 additions & 61 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/command/user/dns/AddDnsServerCmd.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package org.apache.cloudstack.api.command.user.dns;
1919

20+
import java.util.Collection;
21+
import java.util.HashMap;
2022
import java.util.List;
23+
import java.util.Map;
2124

2225
import org.apache.cloudstack.acl.RoleType;
2326
import org.apache.cloudstack.api.APICommand;
@@ -30,6 +33,7 @@
3033
import org.apache.cloudstack.context.CallContext;
3134
import org.apache.cloudstack.dns.DnsProviderType;
3235
import org.apache.cloudstack.dns.DnsServer;
36+
import org.apache.commons.collections.MapUtils;
3337
import org.apache.commons.lang3.BooleanUtils;
3438

3539
import com.cloud.exception.InvalidParameterValueException;
@@ -82,9 +86,9 @@ public class AddDnsServerCmd extends BaseCmd {
8286
description = "Comma separated list of name servers; used to create NS records for the DNS Zone (for example, ns1.example.com, ns2.example.com)")
8387
private List<String> nameServers;
8488

85-
@Parameter(name = "externalserverid", type = CommandType.STRING,
86-
description = "External server id or hostname for the DNS server, e.g., 'localhost' for PowerDNS")
87-
private String externalServerId;
89+
@Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, description = "Details in key/value pairs using " +
90+
"format details[i].keyname=keyvalue. Example: details[0].externalServerId=localhost")
91+
protected Map details;
8892

8993
/////////////////////////////////////////////////////
9094
/////////////////// Accessors ///////////////////////
@@ -145,11 +149,21 @@ public void execute() {
145149
}
146150
}
147151

148-
public String getExternalServerId() {
149-
return externalServerId;
150-
}
151-
152152
public String getDnsUserName() {
153153
return dnsUserName;
154154
}
155+
156+
public Map<String, String> getDetails() {
157+
Map<String, String> detailsMap = new HashMap<>();
158+
if (MapUtils.isNotEmpty(details)) {
159+
Collection<?> props = details.values();
160+
for (Object prop : props) {
161+
HashMap<String, String> detail = (HashMap<String, String>) prop;
162+
for (Map.Entry<String, String> entry: detail.entrySet()) {
163+
detailsMap.put(entry.getKey(),entry.getValue());
164+
}
165+
}
166+
}
167+
return detailsMap;
168+
}
155169
}

api/src/main/java/org/apache/cloudstack/dns/DnsServer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Date;
2121
import java.util.List;
22+
import java.util.Map;
2223

2324
import org.apache.cloudstack.acl.ControlledEntity;
2425
import org.apache.cloudstack.api.Identity;
@@ -49,7 +50,13 @@ enum State {
4950

5051
String getPublicDomainSuffix();
5152

52-
String getExternalServerId();
53-
5453
Integer getPort();
54+
55+
Map<String, String> getDetails();
56+
57+
String getDetail(String name);
58+
59+
void setDetails(Map<String, String> details);
60+
61+
void appendDetails(String name, String value);
5562
}

api/src/test/java/org/apache/cloudstack/api/command/user/dns/AddDnsServerCmdTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ private AddDnsServerCmd createCmd() throws Exception {
4545
setField(cmd, "isPublic", true);
4646
setField(cmd, "publicDomainSuffix", "public.example.com");
4747
setField(cmd, "nameServers", Arrays.asList("ns1.example.com", "ns2.example.com"));
48-
setField(cmd, "externalServerId", "localhost");
4948
setField(cmd, "dnsUserName", "admin@example.com");
5049
return cmd;
5150
}
@@ -62,7 +61,6 @@ public void testAccessors() throws Exception {
6261
assertEquals("public.example.com", cmd.getPublicDomainSuffix());
6362
assertEquals(Arrays.asList("ns1.example.com", "ns2.example.com"), cmd.getNameServers());
6463
assertEquals(DnsProviderType.PowerDNS, cmd.getProvider());
65-
assertEquals("localhost", cmd.getExternalServerId());
6664
assertEquals("admin@example.com", cmd.getDnsUserName());
6765
}
6866

engine/schema/src/main/resources/META-INF/cloudstack/core/spring-engine-schema-core-daos-context.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,6 @@
318318
<bean id="dnsServerJoinDao" class="org.apache.cloudstack.dns.dao.DnsServerJoinDaoImpl" />
319319
<bean id="dnsZoneJoinDao" class="org.apache.cloudstack.dns.dao.DnsZoneJoinDaoImpl" />
320320
<bean id="dnsNicJoinDao" class="org.apache.cloudstack.dns.dao.NicDnsJoinDaoImpl" />
321+
<bean id="dnsServerDetailsDao" class="org.apache.cloudstack.dns.dao.DnsServerDetailsDaoImpl" />
321322

322323
</beans>

engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ CREATE TABLE IF NOT EXISTS `cloud`.`dns_server` (
132132
`url` varchar(1024) NOT NULL COMMENT 'dns server url',
133133
`dns_username` varchar(255) COMMENT 'username or email for dns server credentials',
134134
`dns_api_key` varchar(255) NOT NULL COMMENT 'api key or token for the dns server ',
135-
`external_server_id` varchar(255) COMMENT 'dns server id e.g. localhost for powerdns',
136135
`port` int(11) DEFAULT NULL COMMENT 'optional dns server port',
137136
`name_servers` varchar(1024) DEFAULT NULL COMMENT 'Comma separated list of name servers',
138137
`is_public` tinyint(1) NOT NULL DEFAULT '0',
@@ -147,6 +146,17 @@ CREATE TABLE IF NOT EXISTS `cloud`.`dns_server` (
147146
CONSTRAINT `fk_dns_server__account_id` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`) ON DELETE CASCADE
148147
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
149148

149+
-- DNS Server Details Table
150+
CREATE TABLE IF NOT EXISTS `cloud`.`dns_server_details` (
151+
`id` bigint unsigned UNIQUE NOT NULL AUTO_INCREMENT COMMENT 'id',
152+
`dns_server_id` bigint unsigned NOT NULL COMMENT 'dns_server the detail is related to',
153+
`name` varchar(255) NOT NULL COMMENT 'name of the detail',
154+
`value` varchar(255) NOT NULL COMMENT 'value of the detail',
155+
`display` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'Should detail be displayed to the end user',
156+
PRIMARY KEY (`id`),
157+
CONSTRAINT `fk_dns_server_details__dns_server_id` FOREIGN KEY (`dns_server_id`) REFERENCES `dns_server`(`id`)
158+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
159+
150160
-- DNS Zone Table (Stores DNS Zone Metadata)
151161
CREATE TABLE IF NOT EXISTS `cloud`.`dns_zone` (
152162
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the dns zone',

plugins/dns/powerdns/src/main/java/org/apache/cloudstack/dns/powerdns/PowerDnsProvider.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
import org.apache.cloudstack.dns.DnsServer;
2929
import org.apache.cloudstack.dns.DnsZone;
3030
import org.apache.cloudstack.dns.exception.DnsProviderException;
31+
import org.apache.logging.log4j.util.Strings;
3132

3233
import com.cloud.utils.StringUtils;
3334
import com.cloud.utils.component.AdapterBase;
3435
import com.fasterxml.jackson.databind.JsonNode;
3536

3637
public class PowerDnsProvider extends AdapterBase implements DnsProvider {
37-
3838
private PowerDnsClient client;
39+
static String PDNS_SERVER_ID = "pdnsServerId";
3940

4041
@Override
4142
public DnsProviderType getProviderType() {
@@ -44,13 +45,17 @@ public DnsProviderType getProviderType() {
4445

4546
public void validate(DnsServer server) throws DnsProviderException {
4647
validateRequiredServerFields(server);
47-
client.validateServerId(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getExternalServerId());
48+
client.validateServerId(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getDetail(PDNS_SERVER_ID));
4849
}
4950

5051
@Override
5152
public String validateAndResolveServer(DnsServer server) throws Exception {
5253
validateRequiredServerFields(server);
53-
return client.resolveServerId(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getExternalServerId());
54+
String resolvedDnsServerId = client.resolveServerId(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getDetail(PDNS_SERVER_ID));
55+
if (Strings.isNotBlank(resolvedDnsServerId)) {
56+
server.appendDetails(PDNS_SERVER_ID, resolvedDnsServerId);
57+
}
58+
return resolvedDnsServerId;
5459
}
5560

5661
@Override
@@ -60,7 +65,7 @@ public String provisionZone(DnsServer server, DnsZone zone) throws DnsProviderEx
6065
server.getUrl(),
6166
server.getPort(),
6267
server.getDnsApiKey(),
63-
server.getExternalServerId(),
68+
server.getDetail(PDNS_SERVER_ID),
6469
zone.getName(),
6570
ApiConstants.NATIVE_ZONE, false, server.getNameServers()
6671
);
@@ -69,7 +74,7 @@ public String provisionZone(DnsServer server, DnsZone zone) throws DnsProviderEx
6974
@Override
7075
public void deleteZone(DnsServer server, DnsZone zone) throws DnsProviderException {
7176
validateRequiredServerAndZoneFields(server, zone);
72-
client.deleteZone(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getExternalServerId(), zone.getName());
77+
client.deleteZone(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getDetail(PDNS_SERVER_ID), zone.getName());
7378
}
7479

7580
@Override
@@ -79,7 +84,7 @@ public void updateZone(DnsServer server, DnsZone zone) throws DnsProviderExcepti
7984
server.getUrl(),
8085
server.getPort(),
8186
server.getDnsApiKey(),
82-
server.getExternalServerId(),
87+
server.getDetail(PDNS_SERVER_ID),
8388
zone.getName(), ApiConstants.NATIVE_ZONE, false, server.getNameServers());
8489
}
8590

@@ -94,7 +99,7 @@ public String addRecord(DnsServer server, DnsZone zone, DnsRecord record) throws
9499
server.getUrl(),
95100
server.getPort(),
96101
server.getDnsApiKey(),
97-
server.getExternalServerId(),
102+
server.getDetail(PDNS_SERVER_ID),
98103
zone.getName(), record, ChangeType.REPLACE);
99104
}
100105

@@ -110,7 +115,7 @@ public String deleteRecord(DnsServer server, DnsZone zone, DnsRecord record) thr
110115
return applyRecord(server.getUrl(),
111116
server.getPort(),
112117
server.getDnsApiKey(),
113-
server.getExternalServerId(),
118+
server.getDetail(PDNS_SERVER_ID),
114119
zone.getName(), record, ChangeType.DELETE);
115120
}
116121

@@ -126,7 +131,7 @@ public List<DnsRecord> listRecords(DnsServer server, DnsZone zone) throws DnsPro
126131
validateRequiredServerAndZoneFields(server, zone);
127132
List<DnsRecord> records = new ArrayList<>();
128133
Iterable<JsonNode> rrsetNodes = client.listRecords(server.getUrl(), server.getPort(), server.getDnsApiKey(),
129-
server.getExternalServerId(), zone.getName());
134+
server.getDetail(PDNS_SERVER_ID), zone.getName());
130135

131136
for (JsonNode rrset : rrsetNodes) {
132137
String name = rrset.path(ApiConstants.NAME).asText();
@@ -155,7 +160,7 @@ public List<DnsRecord> listRecords(DnsServer server, DnsZone zone) throws DnsPro
155160

156161
public boolean dnsRecordExists(DnsServer server, DnsZone zone, String recordName, String recordType) throws DnsProviderException {
157162
return client.dnsRecordExists(server.getUrl(), server.getPort(), server.getDnsApiKey(),
158-
server.getExternalServerId(), zone.getName(), recordName, recordType);
163+
server.getDetail(PDNS_SERVER_ID), zone.getName(), recordName, recordType);
159164
}
160165

161166
void validateRequiredServerAndZoneFields(DnsServer server, DnsZone zone) {

plugins/dns/powerdns/src/test/java/org/apache/cloudstack/dns/powerdns/PowerDnsProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void setUp() {
7575
when(serverMock.getUrl()).thenReturn("http://pdns:8081");
7676
when(serverMock.getDnsApiKey()).thenReturn("secret");
7777
when(serverMock.getPort()).thenReturn(8081);
78-
when(serverMock.getExternalServerId()).thenReturn("localhost");
78+
when(serverMock.getDetail(PowerDnsProvider.PDNS_SERVER_ID)).thenReturn("localhost");
7979
when(serverMock.getNameServers()).thenReturn(Arrays.asList("ns1.example.com"));
8080

8181
when(zoneMock.getName()).thenReturn("example.com");

server/src/main/java/org/apache/cloudstack/dns/DnsProviderManagerImpl.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,27 @@
5757
import org.apache.cloudstack.api.response.DnsZoneResponse;
5858
import org.apache.cloudstack.api.response.ListResponse;
5959
import org.apache.cloudstack.context.CallContext;
60-
import org.apache.cloudstack.dns.dao.NicDnsJoinDao;
6160
import org.apache.cloudstack.dns.dao.DnsServerDao;
61+
import org.apache.cloudstack.dns.dao.DnsServerDetailsDao;
6262
import org.apache.cloudstack.dns.dao.DnsServerJoinDao;
6363
import org.apache.cloudstack.dns.dao.DnsZoneDao;
6464
import org.apache.cloudstack.dns.dao.DnsZoneJoinDao;
6565
import org.apache.cloudstack.dns.dao.DnsZoneNetworkMapDao;
66+
import org.apache.cloudstack.dns.dao.NicDnsJoinDao;
6667
import org.apache.cloudstack.dns.exception.DnsConflictException;
6768
import org.apache.cloudstack.dns.exception.DnsNotFoundException;
6869
import org.apache.cloudstack.dns.exception.DnsProviderException;
6970
import org.apache.cloudstack.dns.exception.DnsTransportException;
70-
import org.apache.cloudstack.dns.vo.NicDnsJoinVO;
7171
import org.apache.cloudstack.dns.vo.DnsServerJoinVO;
7272
import org.apache.cloudstack.dns.vo.DnsServerVO;
7373
import org.apache.cloudstack.dns.vo.DnsZoneJoinVO;
7474
import org.apache.cloudstack.dns.vo.DnsZoneNetworkMapVO;
7575
import org.apache.cloudstack.dns.vo.DnsZoneVO;
76+
import org.apache.cloudstack.dns.vo.NicDnsJoinVO;
7677
import org.apache.cloudstack.framework.messagebus.MessageBus;
7778
import org.apache.cloudstack.framework.messagebus.MessageSubscriber;
7879
import org.apache.commons.collections.CollectionUtils;
80+
import org.apache.commons.collections.MapUtils;
7981
import org.springframework.stereotype.Component;
8082

8183
import com.cloud.domain.Domain;
@@ -142,6 +144,8 @@ public class DnsProviderManagerImpl extends ManagerBase implements DnsProviderMa
142144
VMInstanceDao vmInstanceDao;
143145
@Inject
144146
NicDnsJoinDao nicDnsJoinDao;
147+
@Inject
148+
DnsServerDetailsDao dnsServerDetailsDao;
145149

146150
private static final Set<VirtualMachine.State> VM_ALLOWED_STATES = EnumSet.of(VirtualMachine.State.Running, VirtualMachine.State.Stopped);
147151

@@ -180,15 +184,17 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) {
180184
}
181185

182186
DnsProviderType type = cmd.getProvider();
183-
DnsServerVO server = new DnsServerVO(cmd.getName(), cmd.getUrl(), cmd.getPort(), cmd.getExternalServerId(), type,
187+
DnsServerVO server = new DnsServerVO(cmd.getName(), cmd.getUrl(), cmd.getPort(), type,
184188
cmd.getDnsUserName(), cmd.getDnsApiKey(), isDnsPublic, publicDomainSuffix, cmd.getNameServers(),
185189
caller.getAccountId(), caller.getDomainId());
190+
191+
if (MapUtils.isNotEmpty(cmd.getDetails())) {
192+
server.setDetails(cmd.getDetails());
193+
}
194+
186195
try {
187196
DnsProvider provider = getProviderByType(type);
188-
String dnsServerId = provider.validateAndResolveServer(server); // returns localhost for PowerDNS
189-
if (StringUtils.isNotBlank(dnsServerId)) {
190-
server.setExternalServerId(dnsServerId);
191-
}
197+
provider.validateAndResolveServer(server);
192198
return dnsServerDao.persist(server);
193199
} catch (Exception ex) {
194200
logger.error("Failed to validate DNS server", ex);

server/src/main/java/org/apache/cloudstack/dns/dao/DnsServerDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ public interface DnsServerDao extends GenericDao<DnsServerVO, Long> {
3434
List<Long> listDnsServerIdsByAccountId(Long accountId);
3535

3636
Pair<List<DnsServerVO>, Integer> searchDnsServer(Long dnsServerId, Long accountId, Set<Long> domainIds, DnsProviderType providerType, String keyword, Filter filter);
37+
38+
void loadDetails(DnsServer dnsServer);
39+
40+
void saveDetails(DnsServer dnsServer);
3741
}

0 commit comments

Comments
 (0)