|
59 | 59 | import org.apache.cloudstack.dns.vo.DnsZoneJoinVO; |
60 | 60 | import org.apache.cloudstack.dns.vo.DnsZoneNetworkMapVO; |
61 | 61 | import org.apache.cloudstack.dns.vo.DnsZoneVO; |
| 62 | +import org.apache.logging.log4j.util.Strings; |
62 | 63 | import org.springframework.stereotype.Component; |
63 | 64 |
|
64 | 65 | import com.cloud.domain.dao.DomainDao; |
|
81 | 82 | import com.cloud.utils.db.TransactionCallback; |
82 | 83 | import com.cloud.utils.exception.CloudRuntimeException; |
83 | 84 | import com.cloud.vm.Nic; |
| 85 | +import com.cloud.vm.NicDetailVO; |
84 | 86 | import com.cloud.vm.VirtualMachine; |
85 | 87 | import com.cloud.vm.dao.NicDao; |
| 88 | +import com.cloud.vm.dao.NicDetailsDao; |
86 | 89 | import com.cloud.vm.dao.UserVmDao; |
87 | 90 |
|
88 | 91 | @Component |
@@ -110,6 +113,8 @@ public class DnsProviderManagerImpl extends ManagerBase implements DnsProviderMa |
110 | 113 | DnsServerJoinDao dnsServerJoinDao; |
111 | 114 | @Inject |
112 | 115 | AccountDao accountDao; |
| 116 | + @Inject |
| 117 | + NicDetailsDao nicDetailsDao; |
113 | 118 |
|
114 | 119 | private DnsProvider getProviderByType(DnsProviderType type) { |
115 | 120 | if (type == null) { |
@@ -649,23 +654,69 @@ public boolean disassociateZoneFromNetwork(DisassociateDnsZoneFromNetworkCmd cmd |
649 | 654 | } |
650 | 655 |
|
651 | 656 | @Override |
652 | | - public String processDnsRecordForInstance(VirtualMachine instance, Network network, Nic nic, boolean isAdd) { |
653 | | - long networkId = network.getId(); |
654 | | - DnsZoneNetworkMapVO dnsZoneNetworkMap = dnsZoneNetworkMapDao.findByNetworkId(networkId); |
| 657 | + public void addDnsRecordForVM(VirtualMachine instance, Network network, Nic nic) { |
| 658 | + DnsZoneNetworkMapVO dnsZoneNetworkMap = dnsZoneNetworkMapDao.findByNetworkId(network.getId()); |
655 | 659 | if (dnsZoneNetworkMap == null) { |
656 | 660 | logger.warn("No DNS zone is mapped to this network. Please associate a zone first."); |
657 | | - return null; |
| 661 | + return; |
658 | 662 | } |
659 | 663 | DnsZoneVO dnsZone = dnsZoneDao.findById(dnsZoneNetworkMap.getDnsZoneId()); |
660 | 664 | if (dnsZone == null || dnsZone.getState() != DnsZone.State.Active) { |
661 | | - return null; |
| 665 | + logger.warn("DNS zone is not available for DNS record setup"); |
| 666 | + return; |
662 | 667 | } |
663 | 668 | DnsServerVO server = dnsServerDao.findById(dnsZone.getDnsServerId()); |
| 669 | + if (server == null) { |
| 670 | + logger.warn("DNS server is not found to process DNS record for Instance: {}", instance.getInstanceName()); |
| 671 | + return; |
| 672 | + } |
664 | 673 | // Construct FQDN Prefix (e.g., "instance-id.dnsZoneName" or "instance-id.subdomain.dnsZoneName") |
665 | 674 | String recordName = String.valueOf(instance.getInstanceName()); |
666 | 675 | if (StringUtils.isNotBlank(dnsZoneNetworkMap.getSubDomain())) { |
667 | 676 | recordName = String.join(".", recordName, dnsZoneNetworkMap.getSubDomain(), dnsZone.getName()); |
668 | 677 | } |
| 678 | + String dnsRecordUrl = processDnsRecordInProvider(recordName, instance, server, dnsZone, nic, true); |
| 679 | + if (Strings.isBlank(dnsRecordUrl)) { |
| 680 | + logger.error("Failed to add DNS record in provider for Instance: {}", instance.getInstanceName()); |
| 681 | + return; |
| 682 | + } |
| 683 | + nicDetailsDao.addDetail(nic.getId(), ApiConstants.NIC_DNS_RECORD, dnsRecordUrl, true); |
| 684 | + } |
| 685 | + |
| 686 | + @Override |
| 687 | + public void deleteDnsRecordForVM(VirtualMachine instance, Network network, Nic nic) { |
| 688 | + String instanceName = instance.getInstanceName(); |
| 689 | + NicDetailVO nicDetailVO = nicDetailsDao.findDetail(nic.getId(), ApiConstants.NIC_DNS_RECORD); |
| 690 | + if (nicDetailVO == null || Strings.isBlank(nicDetailVO.getValue())) { |
| 691 | + logger.debug("No DNS record found for Instance: {}", instance.getInstanceName()); |
| 692 | + return; |
| 693 | + } |
| 694 | + String dnsRecord = nicDetailVO.getValue(); |
| 695 | + try { |
| 696 | + DnsZoneNetworkMapVO dnsZoneNetworkMap = dnsZoneNetworkMapDao.findByNetworkId(network.getId()); |
| 697 | + DnsZoneVO dnsZone = null; |
| 698 | + DnsServerVO dnsServer = null; |
| 699 | + if (dnsZoneNetworkMap != null) { |
| 700 | + dnsZone = dnsZoneDao.findById(dnsZoneNetworkMap.getDnsZoneId()); |
| 701 | + } |
| 702 | + if (dnsZone != null) { |
| 703 | + dnsServer = dnsServerDao.findById(dnsZone.getDnsServerId()); |
| 704 | + } |
| 705 | + if (dnsServer != null) { |
| 706 | + processDnsRecordInProvider(dnsRecord, instance, dnsServer, dnsZone, nic, false); |
| 707 | + } else { |
| 708 | + logger.warn("Skipping deletion of DNS record: {} from provider for Instance: {}.", dnsRecord, instanceName); |
| 709 | + } |
| 710 | + } catch (Exception ex) { |
| 711 | + logger.error("Failed deleting DNS record: {} for Instance: {}, proceeding with DB cleanup.", dnsRecord, instanceName); |
| 712 | + } finally { |
| 713 | + nicDetailsDao.removeDetail(nic.getId(), ApiConstants.NIC_DNS_RECORD); |
| 714 | + logger.debug("Removed DNS record from DB for Instance: {}, NIC ID: {}", instanceName, nic.getUuid()); |
| 715 | + } |
| 716 | + } |
| 717 | + |
| 718 | + private String processDnsRecordInProvider(String recordName, VirtualMachine instance, DnsServer server, DnsZone dnsZone, |
| 719 | + Nic nic, boolean isAdd) { |
669 | 720 |
|
670 | 721 | try { |
671 | 722 | DnsProvider provider = getProviderByType(server.getProviderType()); |
@@ -695,7 +746,7 @@ public String processDnsRecordForInstance(VirtualMachine instance, Network netwo |
695 | 746 | logger.error( |
696 | 747 | "Failed to {} DNS record for Instance {} in zone {}", |
697 | 748 | isAdd ? "register" : "remove", |
698 | | - instance.getHostName(), |
| 749 | + instance.getInstanceName(), |
699 | 750 | dnsZone.getName(), |
700 | 751 | ex |
701 | 752 | ); |
|
0 commit comments