Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.cloudstack.dns;

public enum DnsProviderType {
PowerDNS;
PowerDNS,
BunnyDNS;
// Cloudflare
}
5 changes: 5 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,11 @@
<artifactId>cloud-plugin-dns-powerdns</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-plugin-dns-bunny</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>
<build>
Expand Down
30 changes: 30 additions & 0 deletions plugins/dns/bunny/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-plugin-dns-bunny</artifactId>
<name>Apache CloudStack Plugin - Bunny DNS</name>
<parent>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloudstack-plugins</artifactId>
<version>24.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
</project>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.dns.bunny;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.cloudstack.dns.DnsProvider;
import org.apache.cloudstack.dns.DnsProviderType;
import org.apache.cloudstack.dns.DnsRecord;
import org.apache.cloudstack.dns.DnsServer;
import org.apache.cloudstack.dns.DnsZone;
import org.apache.cloudstack.dns.exception.DnsProviderException;

import com.cloud.utils.StringUtils;
import com.cloud.utils.component.AdapterBase;
import com.fasterxml.jackson.databind.JsonNode;

public class BunnyDnsProvider extends AdapterBase implements DnsProvider {
private BunnyDnsClient client;

@Override
public DnsProviderType getProviderType() {
return DnsProviderType.BunnyDNS;
}

@Override
public void validate(DnsServer server) throws Exception {
validateRequiredServerFields(server);
client.validateAccess(server.getUrl(), server.getPort(), server.getDnsApiKey());
}

/** Bunny has no server id to resolve, the AccessKey alone identifies the account. */
@Override
public String validateAndResolveServer(DnsServer server) throws Exception {
validate(server);
return null;
}

@Override
public String provisionZone(DnsServer server, DnsZone zone) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
long zoneId = client.createZone(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
return Long.toString(zoneId);
}

@Override
public void deleteZone(DnsServer server, DnsZone zone) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
client.deleteZone(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
}

/** Nothing in DnsZone maps to a Bunny zone setting, so this only verifies the zone still exists. */
@Override
public void updateZone(DnsServer server, DnsZone zone) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
client.resolveZoneId(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
}

/** The DNS framework expects add to replace all records of this name and type, as a PowerDNS rrset does. */
@Override
public String addRecord(DnsServer server, DnsZone zone, DnsRecord record) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
long zoneId = client.resolveZoneId(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
return client.replaceRecords(server.getUrl(), server.getPort(), server.getDnsApiKey(), zoneId,
zone.getName(), record.getName(), record.getType().name(), record.getTtl(), record.getContents());
}

@Override
public String updateRecord(DnsServer server, DnsZone zone, DnsRecord record) throws DnsProviderException {
return addRecord(server, zone, record);
}

@Override
public String deleteRecord(DnsServer server, DnsZone zone, DnsRecord record) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
long zoneId = client.resolveZoneId(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
return client.deleteRecordsByNameAndType(server.getUrl(), server.getPort(), server.getDnsApiKey(), zoneId,
zone.getName(), record.getName(), record.getType().name());
}

@Override
public List<DnsRecord> listRecords(DnsServer server, DnsZone zone) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
long zoneId = client.resolveZoneId(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
List<JsonNode> rawRecords = client.listRecords(server.getUrl(), server.getPort(), server.getDnsApiKey(), zoneId);

Map<String, DnsRecord> aggregated = new LinkedHashMap<>();
for (JsonNode raw : rawRecords) {
String typeName = BunnyDnsClient.typeName(raw.path("Type").asInt(-1));
if (typeName == null) {
continue;
}
DnsRecord.RecordType type = DnsRecord.RecordType.fromString(typeName);
String fqdn = client.toFqdn(raw.path("Name").asText(""), zone.getName());
String content = BunnyDnsClient.formatContent(typeName, raw);
int ttl = raw.path("Ttl").asInt(0);

String key = fqdn + "|" + type.name();
DnsRecord dnsRecord = aggregated.get(key);
if (dnsRecord == null) {
dnsRecord = new DnsRecord(fqdn, type, new ArrayList<>(), ttl);
aggregated.put(key, dnsRecord);
}
dnsRecord.getContents().add(content);
}
return new ArrayList<>(aggregated.values());
}

@Override
public boolean dnsRecordExists(DnsServer server, DnsZone zone, String recordName, String recordType) throws DnsProviderException {
validateRequiredServerAndZoneFields(server, zone);
long zoneId = client.resolveZoneId(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
return !client.findRecordsByNameAndType(server.getUrl(), server.getPort(), server.getDnsApiKey(), zoneId,
zone.getName(), recordName, recordType).isEmpty();
}

@Override
public boolean dnsZoneExists(DnsServer server, DnsZone zone) {
return client.zoneExists(server.getUrl(), server.getPort(), server.getDnsApiKey(), zone.getName());
}

void validateRequiredServerAndZoneFields(DnsServer server, DnsZone zone) {
validateRequiredServerFields(server);
if (StringUtils.isBlank(zone.getName())) {
throw new IllegalArgumentException("Zone name cannot be empty");
}
}

void validateRequiredServerFields(DnsServer server) {
if (StringUtils.isBlank(server.getUrl())) {
throw new IllegalArgumentException("Bunny DNS API URL cannot be empty");
}
if (StringUtils.isBlank(server.getDnsApiKey())) {
throw new IllegalArgumentException("Bunny DNS API key cannot be empty");
}
}

@Override
public boolean configure(String name, Map<String, Object> params) {
if (client == null) {
client = new BunnyDnsClient();
}
return true;
}

@Override
public boolean stop() {
if (client != null) {
client.close();
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name=bunny
parent=dns
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>

<bean id="bunnyDnsProvider" class="org.apache.cloudstack.dns.bunny.BunnyDnsProvider" />
</beans>
Loading
Loading