Skip to content

Commit e011ce1

Browse files
committed
add missing license, cleanup, log std
1 parent 99f8c7d commit e011ce1

11 files changed

Lines changed: 77 additions & 73 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface DnsProvider extends Adapter {
2828
void validate(DnsServer server) throws Exception;
2929

3030
// Zone Operations
31-
void provisionZone(DnsServer server, DnsZone zone);
31+
String provisionZone(DnsServer server, DnsZone zone);
3232
void deleteZone(DnsServer server, DnsZone zone) ;
3333

3434
void addRecord(DnsServer server, DnsZone zone, DnsRecord record);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void validate(String baseUrl, String apiKey) {
9595
}
9696
}
9797

98-
public void createZone(String baseUrl, String apiKey, String zoneName, String nameServers) {
98+
public String createZone(String baseUrl, String apiKey, String zoneName, String nameServers) {
9999
String normalizedZone = formatZoneName(zoneName);
100100
try {
101101
String url = buildApiUrl(baseUrl, "/servers/localhost/zones");
@@ -125,8 +125,12 @@ public void createZone(String baseUrl, String apiKey, String zoneName, String na
125125
String body = response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : null;
126126

127127
if (statusCode == HttpStatus.SC_CREATED) {
128-
logger.debug("Zone {} created successfully", zoneName);
129-
return;
128+
JsonNode root = MAPPER.readTree(body);
129+
String zoneId = root.path("id").asText();
130+
if (StringUtils.isBlank(zoneId)) {
131+
throw new CloudRuntimeException("PowerDNS returned empty zone id");
132+
}
133+
return zoneId;
130134
}
131135

132136
if (statusCode == HttpStatus.SC_CONFLICT) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public void validate(DnsServer server) {
4646
}
4747

4848
@Override
49-
public void provisionZone(DnsServer server, DnsZone zone) {
49+
public String provisionZone(DnsServer server, DnsZone zone) {
5050
validateServerZoneParams(server, zone);
51-
client.createZone(server.getUrl(), server.getApiKey(), zone.getName(), server.getNameServers());
51+
return client.createZone(server.getUrl(), server.getApiKey(), zone.getName(), server.getNameServers());
5252
}
5353

5454
@Override

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.cloudstack.dns;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collections;
2122
import java.util.List;
2223

2324
import javax.inject.Inject;
@@ -96,7 +97,7 @@ private DnsProvider getProvider(DnsProviderType type) {
9697
return provider;
9798
}
9899
}
99-
throw new CloudRuntimeException("No plugin found for DNS Provider type: " + type);
100+
throw new CloudRuntimeException("No plugin found for DNS provider type: " + type);
100101
}
101102

102103
@Override
@@ -105,7 +106,7 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) {
105106
DnsServer existing = dnsServerDao.findByUrlAndAccount(cmd.getUrl(), caller.getId());
106107
if (existing != null) {
107108
throw new InvalidParameterValueException(
108-
"This Account already has a DNS Server integration for URL: " + cmd.getUrl());
109+
"This Account already has a DNS server integration for URL: " + cmd.getUrl());
109110
}
110111
DnsProviderType type = DnsProviderType.fromString(cmd.getProvider());
111112
DnsProvider provider = getProvider(type);
@@ -156,7 +157,7 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
156157
Long dnsServerId = cmd.getId();
157158
DnsServerVO dnsServer = dnsServerDao.findById(dnsServerId);
158159
if (dnsServer == null) {
159-
throw new InvalidParameterValueException(String.format("DNS Server with ID: %s not found.", dnsServerId));
160+
throw new InvalidParameterValueException(String.format("DNS server with ID: %s not found.", dnsServerId));
160161
}
161162

162163
Account caller = CallContext.current().getCallingAccount();
@@ -176,7 +177,7 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
176177
if (!cmd.getUrl().equals(originalUrl)) {
177178
DnsServer duplicate = dnsServerDao.findByUrlAndAccount(cmd.getUrl(), dnsServer.getAccountId());
178179
if (duplicate != null && duplicate.getId() != dnsServer.getId()) {
179-
throw new InvalidParameterValueException("Another DNS Server with this URL already exists.");
180+
throw new InvalidParameterValueException("Another DNS server with this URL already exists.");
180181
}
181182
dnsServer.setUrl(cmd.getUrl());
182183
validationRequired = true;
@@ -230,7 +231,7 @@ public boolean deleteDnsServer(DeleteDnsServerCmd cmd) {
230231
Long dnsServerId = cmd.getId();
231232
DnsServerVO dnsServer = dnsServerDao.findById(dnsServerId);
232233
if (dnsServer == null) {
233-
throw new InvalidParameterValueException(String.format("DNS Server with ID: %s not found.", dnsServerId));
234+
throw new InvalidParameterValueException(String.format("DNS server with ID: %s not found.", dnsServerId));
234235
}
235236
Account caller = CallContext.current().getCallingAccount();
236237
if (!accountMgr.isRootAdmin(caller.getId()) && dnsServer.getAccountId() != caller.getId()) {
@@ -260,7 +261,7 @@ public DnsServer getDnsServer(Long id) {
260261
public boolean deleteDnsZone(Long zoneId) {
261262
DnsZoneVO zone = dnsZoneDao.findById(zoneId);
262263
if (zone == null) {
263-
throw new InvalidParameterValueException("DNS Zone with ID " + zoneId + " not found.");
264+
throw new InvalidParameterValueException("DNS zone with ID " + zoneId + " not found.");
264265
}
265266

266267
Account caller = CallContext.current().getCallingAccount();
@@ -269,10 +270,10 @@ public boolean deleteDnsZone(Long zoneId) {
269270
if (server != null && zone.getState() == DnsZone.State.Active) {
270271
try {
271272
DnsProvider provider = getProvider(server.getProviderType());
272-
logger.debug("Deleting DNS zone {} from provider.", zone.getName());
273+
logger.debug("Deleting DNS zone: {} from provider.", zone.getName());
273274
provider.deleteZone(server, zone);
274275
} catch (Exception ex) {
275-
logger.error("Failed to delete zone from provider", ex);
276+
logger.error("Failed to delete DNS zone from provider", ex);
276277
throw new CloudRuntimeException("Failed to delete DNS zone.");
277278
}
278279
}
@@ -336,7 +337,7 @@ public DnsZone getDnsZone(long id) {
336337
public DnsRecordResponse createDnsRecord(CreateDnsRecordCmd cmd) {
337338
DnsZoneVO zone = dnsZoneDao.findById(cmd.getDnsZoneId());
338339
if (zone == null) {
339-
throw new InvalidParameterValueException("DNS Zone not found.");
340+
throw new InvalidParameterValueException("DNS zone not found.");
340341
}
341342

342343
Account caller = CallContext.current().getCallingAccount();
@@ -358,7 +359,7 @@ public DnsRecordResponse createDnsRecord(CreateDnsRecordCmd cmd) {
358359
public boolean deleteDnsRecord(DeleteDnsRecordCmd cmd) {
359360
DnsZoneVO zone = dnsZoneDao.findById(cmd.getDnsZoneId());
360361
if (zone == null) {
361-
throw new InvalidParameterValueException("DNS Zone not found.");
362+
throw new InvalidParameterValueException("DNS zone not found.");
362363
}
363364

364365
Account caller = CallContext.current().getCallingAccount();
@@ -376,21 +377,21 @@ public boolean deleteDnsRecord(DeleteDnsRecordCmd cmd) {
376377
return true;
377378
} catch (Exception ex) {
378379
logger.error("Failed to delete DNS record via provider", ex);
379-
throw new CloudRuntimeException(String.format("Failed to delete record: %s", cmd.getName()));
380+
throw new CloudRuntimeException(String.format("Failed to delete DNS record: %s", cmd.getName()));
380381
}
381382
}
382383

383384
@Override
384385
public ListResponse<DnsRecordResponse> listDnsRecords(ListDnsRecordsCmd cmd) {
385386
DnsZoneVO zone = dnsZoneDao.findById(cmd.getDnsZoneId());
386387
if (zone == null) {
387-
throw new InvalidParameterValueException(String.format("DNS Zone with ID %s not found.", cmd.getDnsZoneId()));
388+
throw new InvalidParameterValueException(String.format("DNS zone with ID %s not found.", cmd.getDnsZoneId()));
388389
}
389390
Account caller = CallContext.current().getCallingAccount();
390391
accountMgr.checkAccess(caller, null, true, zone);
391392
DnsServerVO server = dnsServerDao.findById(zone.getDnsServerId());
392393
if (server == null) {
393-
throw new CloudRuntimeException("The underlying DNS Server for this zone is missing.");
394+
throw new CloudRuntimeException("The underlying DNS server for this DNS zone is missing.");
394395
}
395396
try {
396397
DnsProvider provider = getProvider(server.getProviderType());
@@ -425,23 +426,23 @@ public DnsZone allocateDnsZone(CreateDnsZoneCmd cmd) {
425426
Account caller = CallContext.current().getCallingAccount();
426427
DnsServerVO server = dnsServerDao.findById(cmd.getDnsServerId());
427428
if (server == null) {
428-
throw new InvalidParameterValueException("DNS Server not found");
429+
throw new InvalidParameterValueException("DNS server not found");
429430
}
430431
boolean isOwner = (server.getAccountId() == caller.getId());
431432
if (!server.isPublic() && !isOwner) {
432-
throw new PermissionDeniedException("You do not have permission to use this DNS Server.");
433+
throw new PermissionDeniedException("You do not have permission to use this DNS server.");
433434
}
434435
DnsZone.ZoneType type = DnsZone.ZoneType.Public;
435436
if (cmd.getType() != null) {
436437
try {
437438
type = DnsZone.ZoneType.valueOf(cmd.getType());
438439
} catch (IllegalArgumentException e) {
439-
throw new InvalidParameterValueException("Invalid Zone Type");
440+
throw new InvalidParameterValueException("Invalid DNS zone Type");
440441
}
441442
}
442443
DnsZoneVO existing = dnsZoneDao.findByNameServerAndType(cmd.getName(), server.getId(), type);
443444
if (existing != null) {
444-
throw new InvalidParameterValueException("Zone already exists on this server.");
445+
throw new InvalidParameterValueException("DNS zone already exists on this server.");
445446
}
446447
DnsZoneVO dnsZoneVO = new DnsZoneVO(cmd.getName(), type, server.getId(), caller.getId(), caller.getDomainId(), cmd.getDescription());
447448
return dnsZoneDao.persist(dnsZoneVO);
@@ -451,20 +452,20 @@ public DnsZone allocateDnsZone(CreateDnsZoneCmd cmd) {
451452
public DnsZone provisionDnsZone(long zoneId) {
452453
DnsZoneVO dnsZone = dnsZoneDao.findById(zoneId);
453454
if (dnsZone == null) {
454-
throw new CloudRuntimeException("DNS Zone not found during provisioning");
455+
throw new CloudRuntimeException("DNS zone not found during provisioning");
455456
}
456457
DnsServerVO server = dnsServerDao.findById(dnsZone.getDnsServerId());
457-
458458
try {
459459
DnsProvider provider = getProvider(server.getProviderType());
460-
logger.debug("Provision DNS zone: {} on DNS server: {}", dnsZone.getName(), server.getName());
461-
provider.provisionZone(server, dnsZone);
460+
String externalReferenceId = provider.provisionZone(server, dnsZone);
461+
dnsZone.setExternalReference(externalReferenceId);
462462
dnsZone.setState(DnsZone.State.Active);
463+
logger.debug("DNS zone: {} created successfully on DNS server: {} with ID: {}", dnsZone.getName(), server.getName(), zoneId);
463464
dnsZoneDao.update(dnsZone.getId(), dnsZone);
464465
} catch (Exception ex) {
465-
logger.error("Failed to provision zone: {} on server: {}", dnsZone.getName(), server.getName(), ex);
466+
logger.error("Failed to provision DNS zone: {} on DNS server: {}", dnsZone.getName(), server.getName(), ex);
466467
dnsZoneDao.remove(zoneId);
467-
throw new CloudRuntimeException("Failed to provision zone: " + dnsZone.getName());
468+
throw new CloudRuntimeException("Failed to provision DNS zone: " + dnsZone.getName());
468469
}
469470
return dnsZone;
470471
}
@@ -495,7 +496,7 @@ public DnsZoneNetworkMapResponse associateZoneToNetwork(AssociateDnsZoneToNetwor
495496
Account caller = CallContext.current().getCallingAccount();
496497
DnsZoneVO zone = dnsZoneDao.findById(cmd.getDnsZoneId());
497498
if (zone == null) {
498-
throw new InvalidParameterValueException("DNS Zone not found.");
499+
throw new InvalidParameterValueException("DNS zone not found.");
499500
}
500501
accountMgr.checkAccess(caller, null, true, zone);
501502

@@ -510,7 +511,7 @@ public DnsZoneNetworkMapResponse associateZoneToNetwork(AssociateDnsZoneToNetwor
510511
accountMgr.checkAccess(caller, null, true, network);
511512
DnsZoneNetworkMapVO existing = dnsZoneNetworkMapDao.findByZoneAndNetwork(zone.getId(), network.getId());
512513
if (existing != null) {
513-
throw new InvalidParameterValueException("This DNS Zone is already associated with this Network.");
514+
throw new InvalidParameterValueException("This DNS zone is already associated with this Network.");
514515
}
515516
DnsZoneNetworkMapVO mapping = new DnsZoneNetworkMapVO(zone.getId(), network.getId(), cmd.getSubDomain());
516517
dnsZoneNetworkMapDao.persist(mapping);
@@ -556,7 +557,7 @@ private boolean processDnsRecordForInstance(Long instanceId, Long networkId, boo
556557
// 1. Fetch VM and verify access
557558
UserVmVO instance = userVmDao.findById(instanceId);
558559
if (instance == null) {
559-
throw new InvalidParameterValueException("Instance not found.");
560+
throw new InvalidParameterValueException("Provided Instance not found.");
560561
}
561562
accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, instance);
562563

@@ -599,7 +600,7 @@ private boolean processDnsRecordForInstance(Long instanceId, Long networkId, boo
599600
DnsProvider provider = getProvider(server.getProviderType());
600601
// Handle IPv4 (A Record)
601602
if (nic.getIPv4Address() != null) {
602-
DnsRecord recordA = new DnsRecord(recordName, DnsRecord.RecordType.A, java.util.Arrays.asList(nic.getIPv4Address()), 3600);
603+
DnsRecord recordA = new DnsRecord(recordName, DnsRecord.RecordType.A, Collections.singletonList(nic.getIPv4Address()), 3600);
603604
if (isAdd) {
604605
provider.addRecord(server, zone, recordA);
605606
} else {
@@ -610,7 +611,7 @@ private boolean processDnsRecordForInstance(Long instanceId, Long networkId, boo
610611

611612
// Handle IPv6 (AAAA Record) if it exists
612613
if (nic.getIPv6Address() != null) {
613-
DnsRecord recordAAAA = new DnsRecord(recordName, DnsRecord.RecordType.AAAA, java.util.Arrays.asList(nic.getIPv6Address()), 3600);
614+
DnsRecord recordAAAA = new DnsRecord(recordName, DnsRecord.RecordType.AAAA, Collections.singletonList(nic.getIPv6Address()), 3600);
614615
if (isAdd) {
615616
provider.addRecord(server, zone, recordAAAA);
616617
} else {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
import com.cloud.utils.db.GenericDao;
2828

2929
public interface DnsServerDao extends GenericDao<DnsServerVO, Long> {
30-
31-
List<DnsServerVO> listByProvider(String provider);
32-
3330
DnsServer findByUrlAndAccount(String url, long accountId);
34-
3531
Pair<List<DnsServerVO>, Integer> searchDnsServers(Long id, String keyword, String provider, Long accountId, Filter filter);
3632
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,11 @@
3333
@Component
3434
public class DnsServerDaoImpl extends GenericDaoBase<DnsServerVO, Long> implements DnsServerDao {
3535
SearchBuilder<DnsServerVO> AllFieldsSearch;
36-
SearchBuilder<DnsServerVO> ProviderSearch;
3736
SearchBuilder<DnsServerVO> AccountUrlSearch;
3837

3938

4039
public DnsServerDaoImpl() {
4140
super();
42-
ProviderSearch = createSearchBuilder();
43-
ProviderSearch.and(ApiConstants.PROVIDER_TYPE, ProviderSearch.entity().getProviderType(), SearchCriteria.Op.EQ);
44-
ProviderSearch.done();
4541

4642
AccountUrlSearch = createSearchBuilder();
4743
AccountUrlSearch.and(ApiConstants.URL, AccountUrlSearch.entity().getUrl(), SearchCriteria.Op.EQ);
@@ -57,13 +53,6 @@ public DnsServerDaoImpl() {
5753

5854
}
5955

60-
@Override
61-
public List<DnsServerVO> listByProvider(String providerType) {
62-
SearchCriteria<DnsServerVO> sc = ProviderSearch.create();
63-
sc.setParameters(ApiConstants.PROVIDER_TYPE, providerType);
64-
return listBy(sc);
65-
}
66-
6756
@Override
6857
public DnsServer findByUrlAndAccount(String url, long accountId) {
6958
SearchCriteria<DnsServerVO> sc = AccountUrlSearch.create();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.cloud.utils.db.GenericDao;
2828

2929
public interface DnsZoneDao extends GenericDao<DnsZoneVO, Long> {
30-
List<DnsZoneVO> listByServerId(long serverId);
3130
List<DnsZoneVO> listByAccount(long accountId);
3231
DnsZoneVO findByNameServerAndType(String name, long dnsServerId, DnsZone.ZoneType type);
3332
Pair<List<DnsZoneVO>, Integer> searchZones(Long id, Long dnsServerId, String keyword, Long accountId, Filter filter);

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@
3333
@Component
3434
public class DnsZoneDaoImpl extends GenericDaoBase<DnsZoneVO, Long> implements DnsZoneDao {
3535
static final String DNS_SERVER_ID = "dnsServerId";
36-
SearchBuilder<DnsZoneVO> ServerSearch;
3736
SearchBuilder<DnsZoneVO> AccountSearch;
3837
SearchBuilder<DnsZoneVO> NameServerTypeSearch;
3938
SearchBuilder<DnsZoneVO> AllFieldsSearch;
4039

4140
public DnsZoneDaoImpl() {
4241
super();
43-
ServerSearch = createSearchBuilder();
44-
ServerSearch.and(DNS_SERVER_ID, ServerSearch.entity().getDnsServerId(), SearchCriteria.Op.EQ);
45-
ServerSearch.done();
4642

4743
AccountSearch = createSearchBuilder();
4844
AccountSearch.and(ApiConstants.ACCOUNT_ID, AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
@@ -62,13 +58,6 @@ public DnsZoneDaoImpl() {
6258
AllFieldsSearch.done();
6359
}
6460

65-
@Override
66-
public List<DnsZoneVO> listByServerId(long serverId) {
67-
SearchCriteria<DnsZoneVO> sc = ServerSearch.create();
68-
sc.setParameters(DNS_SERVER_ID, serverId);
69-
return listBy(sc);
70-
}
71-
7261
@Override
7362
public List<DnsZoneVO> listByAccount(long accountId) {
7463
SearchCriteria<DnsZoneVO> sc = AccountSearch.create();

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
118
package org.apache.cloudstack.dns.dao;
219

320
import java.util.List;
@@ -7,7 +24,6 @@
724
import com.cloud.utils.db.GenericDao;
825

926
public interface DnsZoneNetworkMapDao extends GenericDao<DnsZoneNetworkMapVO, Long> {
10-
List<DnsZoneNetworkMapVO> listByDnsZoneId(long dnsZoneId);
1127
DnsZoneNetworkMapVO findByZoneAndNetwork(long dnsZoneId, long networkId);
1228
List<DnsZoneNetworkMapVO> listByNetworkId(long networkId);
1329
}

0 commit comments

Comments
 (0)