Search in sources :

Example 1 with DnsRecord

use of org.platformlayer.dns.model.DnsRecord in project platformlayer by platformlayer.

the class NginxFrontendDns method buildItemTemplate.

@Override
protected DnsRecord buildItemTemplate() throws OpsException {
    // TODO: Idempotency etc
    // Machine machine = OpsContext.get().getInstance(Machine.class);
    NginxService nginxService = OpsContext.get().getInstance(NginxService.class);
    NginxFrontend nginxFrontend = OpsContext.get().getInstance(NginxFrontend.class);
    Machine machine = instanceHelpers.getMachine(nginxService);
    String address = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());
    DnsRecord record = new DnsRecord();
    record.setDnsName(nginxFrontend.hostname);
    record.getAddress().add(address);
    Tag parentTag = Tag.buildParentTag(nginxFrontend.getKey());
    record.getTags().add(parentTag);
    Tag uniqueTag = UniqueTag.build(nginxService, nginxFrontend);
    record.getTags().add(uniqueTag);
    record.key = PlatformLayerKey.fromId(nginxFrontend.hostname);
    return record;
}
Also used : NginxFrontend(org.openstack.service.nginx.model.NginxFrontend) UniqueTag(org.platformlayer.ops.UniqueTag) Tag(org.platformlayer.core.model.Tag) DnsRecord(org.platformlayer.dns.model.DnsRecord) Machine(org.platformlayer.ops.Machine) NginxService(org.openstack.service.nginx.model.NginxService)

Example 2 with DnsRecord

use of org.platformlayer.dns.model.DnsRecord in project platformlayer by platformlayer.

the class DnsHelpers method buildDnsFile.

public ZoneFile buildDnsFile(DnsZone dnsZone) throws OpsException {
    ZoneFile dnsFile = new ZoneFile(dnsZone.dnsName);
    Iterable<DnsServer> dnsServers = platformLayer.listItems(DnsServer.class);
    for (DnsServer dnsServer : dnsServers) {
        switch(dnsServer.getState()) {
            case DELETE_REQUESTED:
            case DELETED:
                log.info("Skipping server (deleted/deleting): " + dnsServer);
                continue;
            case ACTIVE:
                // Good
                break;
            default:
                log.warn("Dns server not yet active: " + dnsServer);
                // failed = true;
                continue;
        }
        List<EndpointInfo> dnsServerEndpoints = EndpointInfo.findEndpoints(dnsServer.getTags(), 53);
        if (dnsServerEndpoints.isEmpty()) {
            throw new OpsException("Cannot find endpoint for: " + dnsServer);
        }
        // Use the ID to produce a stable identifier
        // TODO: What if we shutdown nameservers? Should we do something like consistent hashing instead?
        // i.e. always create ns1 ... ns16, and then dynamically repoint them as we add/remove nameservers?
        // Does this really help?
        // String serverId = dnsServer"ns" + dnsServer.getId();
        String dnsName = dnsServer.dnsName;
        if (dnsName == null) {
            throw new OpsException("DnsName not set on " + dnsServer);
        }
        // TODO: This might not be the right address in complex networks
        for (EndpointInfo dnsServerEndpoint : dnsServerEndpoints) {
            String address = dnsServerEndpoint.publicIp;
            dnsFile.addNS(dnsZone.dnsName, address, dnsName);
        }
    }
    Iterable<DnsRecord> dnsRecords = platformLayer.listItems(DnsRecord.class);
    for (DnsRecord record : dnsRecords) {
        switch(record.getState()) {
            case DELETE_REQUESTED:
            case DELETED:
                log.info("Skipping record (deleted/deleting): " + record);
                continue;
            default:
                break;
        }
        if (!isInZone(record, dnsZone)) {
            continue;
        }
        dnsFile.addAddress(record.dnsName, record.address);
    }
    return dnsFile;
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) DnsRecord(org.platformlayer.dns.model.DnsRecord) DnsServer(org.platformlayer.service.dns.model.DnsServer)

Example 3 with DnsRecord

use of org.platformlayer.dns.model.DnsRecord in project platformlayer by platformlayer.

the class EndpointDnsRecord method handler.

@Handler
public void handler() throws OpsException {
    PublicEndpointBase endpoint = endpointProvider.get();
    if (OpsContext.isConfigure()) {
        // Create a DNS record
        Tag parentTag = Tag.buildParentTag(endpoint.getKey());
        List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(endpoint.getTags(), destinationPort);
        if (endpoints.isEmpty()) {
            throw new OpsException("Cannot find endpoint for port: " + destinationPort);
        }
        DnsRecord record = new DnsRecord();
        record.setDnsName(dnsName);
        for (EndpointInfo endpointInfo : endpoints) {
            record.getAddress().add(endpointInfo.publicIp);
        }
        record.getTags().add(parentTag);
        record.setKey(PlatformLayerKey.fromId(dnsName));
        try {
            platformLayerClient.putItemByTag((ItemBase) record, parentTag);
        } catch (PlatformLayerClientException e) {
            throw new OpsException("Error registering persistent instance", e);
        }
    }
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase) DnsRecord(org.platformlayer.dns.model.DnsRecord) Handler(org.platformlayer.ops.Handler)

Example 4 with DnsRecord

use of org.platformlayer.dns.model.DnsRecord in project platformlayer by platformlayer.

the class DnsService method beforeCreateItem.

@Override
public void beforeCreateItem(ItemBase item) throws OpsException {
    if (item instanceof DnsServer) {
        DnsServer model = (DnsServer) item;
        model.dnsName = normalize(model.dnsName);
        if (Strings.isNullOrEmpty(model.dnsName)) {
            throw new IllegalArgumentException("dnsName must be specified");
        }
    }
    if (item instanceof DnsRecord) {
        DnsRecord model = (DnsRecord) item;
        model.dnsName = normalize(model.dnsName);
        if (Strings.isNullOrEmpty(model.dnsName)) {
            throw new IllegalArgumentException("dnsName must be specified");
        }
        if (Strings.isNullOrEmpty(model.recordType)) {
            model.recordType = "A";
        }
        if (model.recordType.equals("A")) {
        // OK
        } else {
            throw new IllegalArgumentException("Record type not supported: " + model.recordType);
        }
    }
    if (item instanceof DnsZone) {
        DnsZone model = (DnsZone) item;
        model.dnsName = normalize(model.dnsName);
        if (Strings.isNullOrEmpty(model.dnsName)) {
            model.dnsName = normalize(model.getId());
        }
    }
    super.beforeCreateItem(item);
}
Also used : DnsRecord(org.platformlayer.dns.model.DnsRecord) DnsServer(org.platformlayer.service.dns.model.DnsServer) DnsZone(org.platformlayer.service.dns.model.DnsZone)

Aggregations

DnsRecord (org.platformlayer.dns.model.DnsRecord)4 EndpointInfo (org.platformlayer.core.model.EndpointInfo)2 Tag (org.platformlayer.core.model.Tag)2 OpsException (org.platformlayer.ops.OpsException)2 DnsServer (org.platformlayer.service.dns.model.DnsServer)2 NginxFrontend (org.openstack.service.nginx.model.NginxFrontend)1 NginxService (org.openstack.service.nginx.model.NginxService)1 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)1 PublicEndpointBase (org.platformlayer.core.model.PublicEndpointBase)1 Handler (org.platformlayer.ops.Handler)1 Machine (org.platformlayer.ops.Machine)1 UniqueTag (org.platformlayer.ops.UniqueTag)1 DnsZone (org.platformlayer.service.dns.model.DnsZone)1