|
51 | 51 | import org.apache.cloudstack.dns.dao.DnsZoneDao; |
52 | 52 | import org.apache.cloudstack.dns.dao.DnsZoneJoinDao; |
53 | 53 | import org.apache.cloudstack.dns.dao.DnsZoneNetworkMapDao; |
| 54 | +import org.apache.cloudstack.dns.exception.DnsConflictException; |
54 | 55 | import org.apache.cloudstack.dns.exception.DnsNotFoundException; |
| 56 | +import org.apache.cloudstack.dns.exception.DnsTransportException; |
55 | 57 | import org.apache.cloudstack.dns.vo.DnsServerJoinVO; |
56 | 58 | import org.apache.cloudstack.dns.vo.DnsServerVO; |
57 | 59 | import org.apache.cloudstack.dns.vo.DnsZoneJoinVO; |
@@ -176,18 +178,10 @@ public ListResponse<DnsServerResponse> listDnsServers(ListDnsServersCmd cmd) { |
176 | 178 | } |
177 | 179 |
|
178 | 180 | private Pair<List<DnsServerVO>, Integer> searchForDnsServerInternal(ListDnsServersCmd cmd) { |
179 | | - Long dnsServerId = cmd.getId(); |
180 | 181 | Account caller = CallContext.current().getCallingAccount(); |
181 | | - if (dnsServerId != null) { |
182 | | - DnsServerVO dnsServerVO = dnsServerDao.findById(dnsServerId); |
183 | | - if (dnsServerVO == null) { |
184 | | - return null; |
185 | | - } |
186 | | - return new Pair<>(Collections.singletonList(dnsServerVO), 1); |
187 | | - } |
188 | 182 | Set<Long> parentDomainIds = domainDao.getDomainParentIds(caller.getDomainId()); |
189 | 183 | Filter searchFilter = new Filter(DnsServerVO.class, ApiConstants.ID, true, cmd.getStartIndex(), cmd.getPageSizeVal()); |
190 | | - return dnsServerDao.searchDnsServer(dnsServerId, caller.getAccountId(), parentDomainIds, cmd.getProviderType(), cmd.getKeyword(), searchFilter); |
| 184 | + return dnsServerDao.searchDnsServer(cmd.getId(), caller.getAccountId(), parentDomainIds, cmd.getProviderType(), cmd.getKeyword(), searchFilter); |
191 | 185 | } |
192 | 186 |
|
193 | 187 | @Override |
@@ -272,6 +266,9 @@ public boolean deleteDnsServer(DeleteDnsServerCmd cmd) { |
272 | 266 | } |
273 | 267 | Account caller = CallContext.current().getCallingAccount(); |
274 | 268 | accountMgr.checkAccess(caller, dnsServer); |
| 269 | + if (cmd.getCleanup()) { |
| 270 | + // ToDo cleanup associated dnsZones |
| 271 | + } |
275 | 272 | return dnsServerDao.remove(dnsServerId); |
276 | 273 | } |
277 | 274 |
|
@@ -322,6 +319,7 @@ public DnsZone updateDnsZone(UpdateDnsZoneCmd cmd) { |
322 | 319 | try { |
323 | 320 | DnsProvider provider = getProviderByType(server.getProviderType()); |
324 | 321 | provider.updateZone(server, dnsZone); |
| 322 | + dnsZoneDao.update(dnsZone.getId(), dnsZone); |
325 | 323 | } catch (Exception ex) { |
326 | 324 | logger.error("Failed to update DNS zone: {} on DNS server: {}", dnsZone.getName(), server.getName(), ex); |
327 | 325 | throw new CloudRuntimeException("Failed to update DNS zone: " + dnsZone.getName()); |
@@ -375,20 +373,23 @@ public DnsRecordResponse createDnsRecord(CreateDnsRecordCmd cmd) { |
375 | 373 | if (StringUtils.isBlank(recordName)) { |
376 | 374 | throw new InvalidParameterValueException("Empty DNS record name is not allowed"); |
377 | 375 | } |
378 | | - DnsZoneVO zone = dnsZoneDao.findById(cmd.getDnsZoneId()); |
379 | | - if (zone == null) { |
| 376 | + DnsZoneVO dnsZone = dnsZoneDao.findById(cmd.getDnsZoneId()); |
| 377 | + if (dnsZone == null) { |
380 | 378 | throw new InvalidParameterValueException("DNS zone not found."); |
381 | 379 | } |
382 | 380 | Account caller = CallContext.current().getCallingAccount(); |
383 | | - accountMgr.checkAccess(caller, null, true, zone); |
384 | | - DnsServerVO server = dnsServerDao.findById(zone.getDnsServerId()); |
| 381 | + accountMgr.checkAccess(caller, null, true, dnsZone); |
| 382 | + DnsServerVO server = dnsServerDao.findById(dnsZone.getDnsServerId()); |
| 383 | + if (server == null) { |
| 384 | + throw new CloudRuntimeException("The underlying DNS server for this DNS zone is missing."); |
| 385 | + } |
385 | 386 | try { |
386 | 387 | DnsRecord.RecordType type = cmd.getType(); |
387 | 388 | List<String> normalizedContents = cmd.getContents().stream() |
388 | 389 | .map(value -> DnsProviderUtil.normalizeDnsRecordValue(value, type)).collect(Collectors.toList()); |
389 | 390 | DnsRecord record = new DnsRecord(recordName, type, normalizedContents, cmd.getTtl()); |
390 | 391 | DnsProvider provider = getProviderByType(server.getProviderType()); |
391 | | - String normalizedRecordName = provider.addRecord(server, zone, record); |
| 392 | + String normalizedRecordName = provider.addRecord(server, dnsZone, record); |
392 | 393 | record.setName(normalizedRecordName); |
393 | 394 | return createDnsRecordResponse(record); |
394 | 395 | } catch (Exception ex) { |
@@ -507,7 +508,13 @@ public DnsZone provisionDnsZone(long dnsZoneId) { |
507 | 508 | } catch (Exception ex) { |
508 | 509 | dnsZoneDao.remove(dnsZoneId); |
509 | 510 | logger.error("Failed to provision DNS zone: {} on DNS server: {}", dnsZone.getName(), server.getName(), ex); |
510 | | - throw new CloudRuntimeException("Failed to provision DNS zone: " + dnsZone.getName()); |
| 511 | + String errorMsg = ""; |
| 512 | + if ( ex instanceof DnsConflictException) { |
| 513 | + errorMsg = String.format("DNS zone: %s already exists", dnsZone.getName()); |
| 514 | + } else if (ex instanceof DnsTransportException){ |
| 515 | + errorMsg = String.format("DNS server: %s not reachable", server.getName()); |
| 516 | + } |
| 517 | + throw new CloudRuntimeException(errorMsg); |
511 | 518 | } |
512 | 519 | return dnsZone; |
513 | 520 | } |
@@ -562,6 +569,7 @@ public DnsRecordResponse createDnsRecordResponse(DnsRecord record) { |
562 | 569 | res.setName(record.getName()); |
563 | 570 | res.setType(record.getType()); |
564 | 571 | res.setContent(record.getContents()); |
| 572 | + res.setTtl(record.getTtl()); |
565 | 573 | return res; |
566 | 574 | } |
567 | 575 |
|
|
0 commit comments