weizhouapache commented on code in PR #12737:
URL: https://github.com/apache/cloudstack/pull/12737#discussion_r3153000158
##########
api/src/main/java/com/cloud/configuration/Resource.java:
##########
@@ -38,7 +38,8 @@ enum ResourceType { // All storage type resources are
allocated_storage and not
backup_storage("backup_storage", 13),
bucket("bucket", 14),
object_storage("object_storage", 15),
- gpu("gpu", 16);
+ gpu("gpu", 16),
+ dns_zone("dns_zone", 17);
Review Comment:
resource limitation on dns_zone is not implemented, isn't it ?
##########
api/src/main/java/org/apache/cloudstack/api/command/user/dns/AddDnsServerCmd.java:
##########
@@ -0,0 +1,150 @@
+// 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.api.command.user.dns;
+
+import java.util.List;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.DnsServerResponse;
+import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.dns.DnsProviderType;
+import org.apache.cloudstack.dns.DnsServer;
+import org.apache.commons.lang3.BooleanUtils;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.utils.EnumUtils;
+
+@APICommand(name = "addDnsServer",
+ description = "Adds a new external DNS server",
+ responseObject = DnsServerResponse.class,
+ entityType = {DnsServer.class},
+ requestHasSensitiveInfo = false,
+ responseHasSensitiveInfo = false,
+ since = "4.23.0",
+ authorized = {RoleType.Admin, RoleType.ResourceAdmin,
RoleType.DomainAdmin, RoleType.User})
+public class AddDnsServerCmd extends BaseCmd {
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+ ///
+ @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required =
true, description = "Name of the DNS server")
+ private String name;
+
+ @Parameter(name = ApiConstants.URL, type = CommandType.STRING, required =
true, description = "API URL of the provider")
+ private String url;
+
+ @Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING,
required = true, description = "Provider type (e.g., PowerDNS)")
+ private String provider;
+
+ @Parameter(name = ApiConstants.DNS_USER_NAME, type = CommandType.STRING,
+ description = "Username or email associated with the DNS provider
account (used for authentication)")
+ private String dnsUserName;
+
+ @Parameter(name = ApiConstants.DNS_API_KEY, required = true, type =
CommandType.STRING, description = "API key or token for the DNS provider")
+ private String dnsApiKey;
+
+ @Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER,
description = "Port number of the external DNS server")
+ private Integer port;
+
+ @Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN,
description = "Whether the DNS server is publicly accessible by other accounts")
+ private Boolean isPublic;
+
+ @Parameter(name = ApiConstants.PUBLIC_DOMAIN_SUFFIX, type =
CommandType.STRING, description = "The domain suffix used for public access
(e.g. public.example.com)")
Review Comment:
@sudo87
can you add more in the description ? for example, `If set, user can only
create dns zones with this domain suffix ......"
##########
api/src/main/java/org/apache/cloudstack/api/command/user/dns/DisassociateDnsZoneFromNetworkCmd.java:
##########
@@ -0,0 +1,80 @@
+// 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.api.command.user.dns;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.acl.SecurityChecker;
+import org.apache.cloudstack.api.ACL;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.Network;
+import com.cloud.user.Account;
+
+@APICommand(name = "disassociateDnsZoneFromNetwork",
+ description = "Removes the association between a DNS Zone and a
Network",
+ responseObject = SuccessResponse.class,
+ requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
+ since = "4.23.0",
+ authorized = {RoleType.Admin, RoleType.ResourceAdmin,
RoleType.DomainAdmin, RoleType.User})
+public class DisassociateDnsZoneFromNetworkCmd extends BaseCmd {
+
+ @ACL(accessType = SecurityChecker.AccessType.OperateEntry)
+ @Parameter(name = ApiConstants.NETWORK_ID, type = CommandType.UUID,
entityType = NetworkResponse.class,
+ required = true, description = "The ID of the Network")
+ private Long networkId;
+
+ @Override
+ public void execute() throws ResourceUnavailableException,
InsufficientCapacityException, ServerApiException,
ConcurrentOperationException, ResourceAllocationException,
NetworkRuleConflictException {
+ try {
+ boolean result =
dnsProviderManager.disassociateZoneFromNetwork(this);
Review Comment:
will DNS records be kept ?
##########
engine/schema/src/main/java/com/cloud/vm/dao/NicDetailsDaoImpl.java:
##########
@@ -17,17 +17,40 @@
package com.cloud.vm.dao;
+import java.util.List;
+
+import org.apache.cloudstack.api.ApiConstants;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.resourcedetail.ResourceDetailsDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
import com.cloud.vm.NicDetailVO;
@Component
public class NicDetailsDaoImpl extends ResourceDetailsDaoBase<NicDetailVO>
implements NicDetailsDao {
+ private final SearchBuilder<NicDetailVO> NameValuesSearch;
+
+ public NicDetailsDaoImpl() {
+ super();
+ NameValuesSearch = createSearchBuilder();
+ NameValuesSearch.and(ApiConstants.NAME,
NameValuesSearch.entity().getName(), SearchCriteria.Op.EQ);
+ NameValuesSearch.and(ApiConstants.VALUE,
NameValuesSearch.entity().getValue(), SearchCriteria.Op.IN);
+ NameValuesSearch.done();
+ }
+
@Override
public void addDetail(long resourceId, String key, String value, boolean
display) {
super.addDetail(new NicDetailVO(resourceId, key, value, display));
}
+
+ @Override
+ public void removeDetailsForValuesIn(String resourceName, List<String>
values) {
+ SearchCriteria<NicDetailVO> sc = NameValuesSearch.create();
+ sc.setParameters(ApiConstants.NAME, resourceName);
+ sc.setParameters(ApiConstants.VALUE, values.toArray());
Review Comment:
to be safer, check if `values` is null or empty ? do nothing if so.
##########
api/src/main/java/org/apache/cloudstack/api/command/user/dns/AddDnsServerCmd.java:
##########
@@ -0,0 +1,150 @@
+// 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.api.command.user.dns;
+
+import java.util.List;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.DnsServerResponse;
+import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.dns.DnsProviderType;
+import org.apache.cloudstack.dns.DnsServer;
+import org.apache.commons.lang3.BooleanUtils;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.utils.EnumUtils;
+
+@APICommand(name = "addDnsServer",
+ description = "Adds a new external DNS server",
+ responseObject = DnsServerResponse.class,
+ entityType = {DnsServer.class},
+ requestHasSensitiveInfo = false,
+ responseHasSensitiveInfo = false,
+ since = "4.23.0",
+ authorized = {RoleType.Admin, RoleType.ResourceAdmin,
RoleType.DomainAdmin, RoleType.User})
+public class AddDnsServerCmd extends BaseCmd {
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+ ///
+ @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required =
true, description = "Name of the DNS server")
+ private String name;
+
+ @Parameter(name = ApiConstants.URL, type = CommandType.STRING, required =
true, description = "API URL of the provider")
+ private String url;
+
+ @Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING,
required = true, description = "Provider type (e.g., PowerDNS)")
+ private String provider;
+
+ @Parameter(name = ApiConstants.DNS_USER_NAME, type = CommandType.STRING,
+ description = "Username or email associated with the DNS provider
account (used for authentication)")
+ private String dnsUserName;
+
+ @Parameter(name = ApiConstants.DNS_API_KEY, required = true, type =
CommandType.STRING, description = "API key or token for the DNS provider")
+ private String dnsApiKey;
+
+ @Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER,
description = "Port number of the external DNS server")
+ private Integer port;
+
+ @Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN,
description = "Whether the DNS server is publicly accessible by other accounts")
+ private Boolean isPublic;
+
+ @Parameter(name = ApiConstants.PUBLIC_DOMAIN_SUFFIX, type =
CommandType.STRING, description = "The domain suffix used for public access
(e.g. public.example.com)")
+ private String publicDomainSuffix;
+
+ @Parameter(name = ApiConstants.NAME_SERVERS, type = CommandType.LIST,
collectionType = CommandType.STRING,
+ required = true, description = "Comma separated list of name
servers")
+ private List<String> nameServers;
+
+ @Parameter(name = "externalserverid", type = CommandType.STRING,
description = "External server id or hostname for the DNS server, e.g.,
'localhost' for PowerDNS")
+ private String externalServerId;
Review Comment:
is it applicable for PowerDNS only ?
I think we'd better avoid provider-specific parameters
##########
api/src/main/java/org/apache/cloudstack/api/command/user/dns/AddDnsServerCmd.java:
##########
@@ -0,0 +1,150 @@
+// 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.api.command.user.dns;
+
+import java.util.List;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.DnsServerResponse;
+import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.dns.DnsProviderType;
+import org.apache.cloudstack.dns.DnsServer;
+import org.apache.commons.lang3.BooleanUtils;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.utils.EnumUtils;
+
+@APICommand(name = "addDnsServer",
+ description = "Adds a new external DNS server",
+ responseObject = DnsServerResponse.class,
+ entityType = {DnsServer.class},
+ requestHasSensitiveInfo = false,
+ responseHasSensitiveInfo = false,
+ since = "4.23.0",
+ authorized = {RoleType.Admin, RoleType.ResourceAdmin,
RoleType.DomainAdmin, RoleType.User})
+public class AddDnsServerCmd extends BaseCmd {
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+ ///
+ @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required =
true, description = "Name of the DNS server")
+ private String name;
+
+ @Parameter(name = ApiConstants.URL, type = CommandType.STRING, required =
true, description = "API URL of the provider")
+ private String url;
+
+ @Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING,
required = true, description = "Provider type (e.g., PowerDNS)")
+ private String provider;
+
+ @Parameter(name = ApiConstants.DNS_USER_NAME, type = CommandType.STRING,
+ description = "Username or email associated with the DNS provider
account (used for authentication)")
+ private String dnsUserName;
+
+ @Parameter(name = ApiConstants.DNS_API_KEY, required = true, type =
CommandType.STRING, description = "API key or token for the DNS provider")
+ private String dnsApiKey;
+
+ @Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER,
description = "Port number of the external DNS server")
+ private Integer port;
+
+ @Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN,
description = "Whether the DNS server is publicly accessible by other accounts")
+ private Boolean isPublic;
+
+ @Parameter(name = ApiConstants.PUBLIC_DOMAIN_SUFFIX, type =
CommandType.STRING, description = "The domain suffix used for public access
(e.g. public.example.com)")
+ private String publicDomainSuffix;
+
+ @Parameter(name = ApiConstants.NAME_SERVERS, type = CommandType.LIST,
collectionType = CommandType.STRING,
+ required = true, description = "Comma separated list of name
servers")
Review Comment:
add more details in the description like below ?
`NS records will be created for the DNS zone ......"
##########
api/src/main/java/org/apache/cloudstack/api/response/NicResponse.java:
##########
@@ -150,6 +150,10 @@ public class NicResponse extends BaseResponse {
@Param(description = "whether the NIC is enabled or not")
private Boolean isEnabled;
+ @SerializedName(ApiConstants.NIC_DNS_NAME)
+ @Param(description = "Public IP address associated with this NIC via
Static NAT rule")
Review Comment:
the description seems wrong
##########
api/src/main/java/org/apache/cloudstack/api/command/user/dns/DeleteDnsZoneCmd.java:
##########
@@ -0,0 +1,100 @@
+// 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.api.command.user.dns;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.acl.SecurityChecker;
+import org.apache.cloudstack.api.ACL;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.DnsZoneResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.dns.DnsZone;
+
+import com.cloud.event.EventTypes;
+import com.cloud.user.Account;
+
+@APICommand(name = "deleteDnsZone",
+ description = "Removes a DNS Zone from CloudStack and the external
provider",
+ responseObject = SuccessResponse.class,
+ entityType = {DnsZone.class},
+ requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
+ since = "4.23.0",
+ authorized = {RoleType.Admin, RoleType.ResourceAdmin,
RoleType.DomainAdmin, RoleType.User})
+public class DeleteDnsZoneCmd extends BaseAsyncCmd {
+
+ /////////////////////////////////////////////////////
+ //////////////// API Parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @ACL(accessType = SecurityChecker.AccessType.OperateEntry)
+ @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType =
DnsZoneResponse.class, required = true,
+ description = "The ID of the DNS zone")
+ private Long id;
Review Comment:
when delete a DNS zone, is it possible to keep the DNS records on the
external provider ?
##########
api/src/main/java/org/apache/cloudstack/dns/DnsProviderType.java:
##########
@@ -0,0 +1,23 @@
+// 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;
+
+public enum DnsProviderType {
+ PowerDNS;
+// Cloudflare
+}
Review Comment:
there is an API to list all available DNS provider types:
`ListDnsProvidersCmd`, which calls `listProviderNames`
this enum may be not needed
##########
api/src/main/java/org/apache/cloudstack/api/command/user/dns/ListDnsServersCmd.java:
##########
@@ -0,0 +1,82 @@
+// 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.api.command.user.dns;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListAccountResourcesCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.DnsServerResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.dns.DnsProviderType;
+import org.apache.cloudstack.dns.DnsServer;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.utils.EnumUtils;
+
+@APICommand(name = "listDnsServers",
+ description = "Lists DNS servers owned by the account.",
+ responseObject = DnsServerResponse.class,
+ entityType = {DnsServer.class},
+ requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
+ since = "4.23.0",
+ authorized = {RoleType.Admin, RoleType.ResourceAdmin,
RoleType.DomainAdmin, RoleType.User})
+public class ListDnsServersCmd extends BaseListAccountResourcesCmd {
+
+ /////////////////////////////////////////////////////
+ //////////////// API Parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType =
DnsServerResponse.class,
+ description = "the ID of the DNS server")
+ private Long id;
+
+ @Parameter(name = ApiConstants.PROVIDER_TYPE, type = CommandType.STRING,
+ description = "filter by provider type (e.g. PowerDNS,
Cloudflare)")
+ private String providerType;
Review Comment:
is this optional or required ?
will line 65 throw an exception if not set ?
##########
plugins/dns/powerdns/src/main/java/org/apache/cloudstack/dns/powerdns/PowerDnsClient.java:
##########
@@ -0,0 +1,377 @@
+// 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.powerdns;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.dns.exception.DnsAuthenticationException;
+import org.apache.cloudstack.dns.exception.DnsConflictException;
+import org.apache.cloudstack.dns.exception.DnsNotFoundException;
+import org.apache.cloudstack.dns.exception.DnsOperationException;
+import org.apache.cloudstack.dns.exception.DnsProviderException;
+import org.apache.cloudstack.dns.exception.DnsTransportException;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPatch;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.cloud.utils.StringUtils;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class PowerDnsClient implements AutoCloseable {
+ public static final Logger logger =
LoggerFactory.getLogger(PowerDnsClient.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private static final int CONNECT_TIMEOUT_MS = 5_000;
+ private static final int SOCKET_TIMEOUT_MS = 10_000;
+ private static final int MAX_CONNECTIONS_TOTAL = 50;
+ private static final int MAX_CONNECTIONS_PER_ROUTE = 10;
+ private static final String API_PREFIX = "/api/v1";
+ public static final String DEFAULT_SERVER_NAME = "localhost";
+
+ private final CloseableHttpClient httpClient;
+
+ public PowerDnsClient() {
+ PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager();
+ connectionManager.setMaxTotal(MAX_CONNECTIONS_TOTAL);
+ connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
+
+ RequestConfig requestConfig = RequestConfig.custom()
+ .setConnectTimeout(CONNECT_TIMEOUT_MS)
+ .setConnectionRequestTimeout(CONNECT_TIMEOUT_MS)
+ .setSocketTimeout(SOCKET_TIMEOUT_MS)
+ .build();
+
+ this.httpClient = HttpClientBuilder.create()
+ .setConnectionManager(connectionManager)
+ .setDefaultRequestConfig(requestConfig)
+ .evictIdleConnections(30, TimeUnit.SECONDS)
+ .disableCookieManagement()
+ .build();
+ }
+
+ public String resolveServerId(String baseUrl, Integer port, String apiKey,
String externalServerId) throws DnsProviderException {
+ if (StringUtils.isNotBlank(externalServerId)) {
+ return validateServerId(baseUrl, port, apiKey, externalServerId);
+ }
+ return discoverAuthoritativeServerId(baseUrl, port, apiKey);
+ }
+
+ public String validateServerId(String baseUrl, Integer port, String
apiKey, String externalServerId) throws DnsProviderException {
+ String encodedServer = URLEncoder.encode(externalServerId,
StandardCharsets.UTF_8);
+ HttpGet request = new HttpGet(buildUrl(baseUrl, port, "/servers/" +
encodedServer));
+ JsonNode server = execute(request, apiKey, 200);
+ if
(!ApiConstants.AUTHORITATIVE.equalsIgnoreCase(server.path("daemon_type").asText(null)))
{
+ throw new DnsOperationException(String.format("Server %s is not
authoritative type=%s", externalServerId,
+ server.path("daemon_type").asText(null)));
+ }
+ return externalServerId;
+ }
+
+ public String discoverAuthoritativeServerId(String baseUrl, Integer port,
String apiKey) throws DnsProviderException {
+ String url = buildUrl(baseUrl, port , "/servers");
+ HttpGet request = new HttpGet(url);
+ JsonNode servers = execute(request, apiKey, 200);
+ if (servers == null || !servers.isArray() || servers.isEmpty()) {
+ throw new DnsOperationException("No servers returned by PowerDNS
API");
+ }
+ String fallbackId = null;
+ for (JsonNode server : servers) {
+ String daemonType = server.path("daemon_type").asText(null);
+ if (!ApiConstants.AUTHORITATIVE.equalsIgnoreCase(daemonType)) {
+ continue;
+ }
+ String serverId = server.path(ApiConstants.ID).asText(null);
+ if (StringUtils.isBlank(serverId)) {
+ continue;
+ }
+ // Prefer localhost if present
+ if (DEFAULT_SERVER_NAME.equals(serverId)) {
+ return serverId;
+ }
+ if (fallbackId == null) {
+ fallbackId = serverId;
+ }
+ }
+ if (fallbackId != null) {
+ return fallbackId;
+ }
+ throw new DnsOperationException("No authoritative PowerDNS server
found");
+ }
+
+ public String createZone(String baseUrl, Integer port, String apiKey,
String externalServerId, String zoneName,
+ String zoneKind, boolean dnsSecFlag, List<String>
nameServers) throws DnsProviderException {
+
+ validateServerId(baseUrl, port, apiKey, externalServerId);
+ String normalizedZone = normalizeZone(zoneName);
+ ObjectNode json = MAPPER.createObjectNode();
+ json.put(ApiConstants.NAME, normalizedZone);
+ json.put(ApiConstants.KIND, zoneKind);
+ json.put(ApiConstants.DNS_SEC, dnsSecFlag);
+ if (!CollectionUtils.isEmpty(nameServers)) {
+ ArrayNode nsArray = json.putArray(ApiConstants.NAME_SERVERS);
+ for (String ns : nameServers) {
+ nsArray.add(ns.endsWith(".") ? ns : ns + ".");
+ }
+ }
+ HttpPost request = new HttpPost(buildUrl(baseUrl, port, "/servers/" +
externalServerId + "/zones"));
+ request.setEntity(new StringEntity(json.toString(),
StandardCharsets.UTF_8));
+ JsonNode response = execute(request, apiKey, 201);
Review Comment:
if the DNS zone already exists in powerdns, what will happen ?
##########
plugins/dns/powerdns/src/main/java/org/apache/cloudstack/dns/powerdns/PowerDnsClient.java:
##########
@@ -0,0 +1,377 @@
+// 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.powerdns;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.dns.exception.DnsAuthenticationException;
+import org.apache.cloudstack.dns.exception.DnsConflictException;
+import org.apache.cloudstack.dns.exception.DnsNotFoundException;
+import org.apache.cloudstack.dns.exception.DnsOperationException;
+import org.apache.cloudstack.dns.exception.DnsProviderException;
+import org.apache.cloudstack.dns.exception.DnsTransportException;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPatch;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.cloud.utils.StringUtils;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class PowerDnsClient implements AutoCloseable {
+ public static final Logger logger =
LoggerFactory.getLogger(PowerDnsClient.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private static final int CONNECT_TIMEOUT_MS = 5_000;
+ private static final int SOCKET_TIMEOUT_MS = 10_000;
+ private static final int MAX_CONNECTIONS_TOTAL = 50;
+ private static final int MAX_CONNECTIONS_PER_ROUTE = 10;
+ private static final String API_PREFIX = "/api/v1";
+ public static final String DEFAULT_SERVER_NAME = "localhost";
+
+ private final CloseableHttpClient httpClient;
+
+ public PowerDnsClient() {
+ PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager();
+ connectionManager.setMaxTotal(MAX_CONNECTIONS_TOTAL);
+ connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
+
+ RequestConfig requestConfig = RequestConfig.custom()
+ .setConnectTimeout(CONNECT_TIMEOUT_MS)
+ .setConnectionRequestTimeout(CONNECT_TIMEOUT_MS)
+ .setSocketTimeout(SOCKET_TIMEOUT_MS)
+ .build();
+
+ this.httpClient = HttpClientBuilder.create()
+ .setConnectionManager(connectionManager)
+ .setDefaultRequestConfig(requestConfig)
+ .evictIdleConnections(30, TimeUnit.SECONDS)
+ .disableCookieManagement()
+ .build();
+ }
+
+ public String resolveServerId(String baseUrl, Integer port, String apiKey,
String externalServerId) throws DnsProviderException {
+ if (StringUtils.isNotBlank(externalServerId)) {
+ return validateServerId(baseUrl, port, apiKey, externalServerId);
+ }
+ return discoverAuthoritativeServerId(baseUrl, port, apiKey);
+ }
+
+ public String validateServerId(String baseUrl, Integer port, String
apiKey, String externalServerId) throws DnsProviderException {
+ String encodedServer = URLEncoder.encode(externalServerId,
StandardCharsets.UTF_8);
+ HttpGet request = new HttpGet(buildUrl(baseUrl, port, "/servers/" +
encodedServer));
+ JsonNode server = execute(request, apiKey, 200);
+ if
(!ApiConstants.AUTHORITATIVE.equalsIgnoreCase(server.path("daemon_type").asText(null)))
{
+ throw new DnsOperationException(String.format("Server %s is not
authoritative type=%s", externalServerId,
+ server.path("daemon_type").asText(null)));
+ }
+ return externalServerId;
+ }
+
+ public String discoverAuthoritativeServerId(String baseUrl, Integer port,
String apiKey) throws DnsProviderException {
+ String url = buildUrl(baseUrl, port , "/servers");
+ HttpGet request = new HttpGet(url);
+ JsonNode servers = execute(request, apiKey, 200);
+ if (servers == null || !servers.isArray() || servers.isEmpty()) {
+ throw new DnsOperationException("No servers returned by PowerDNS
API");
+ }
+ String fallbackId = null;
+ for (JsonNode server : servers) {
+ String daemonType = server.path("daemon_type").asText(null);
+ if (!ApiConstants.AUTHORITATIVE.equalsIgnoreCase(daemonType)) {
+ continue;
+ }
+ String serverId = server.path(ApiConstants.ID).asText(null);
+ if (StringUtils.isBlank(serverId)) {
+ continue;
+ }
+ // Prefer localhost if present
+ if (DEFAULT_SERVER_NAME.equals(serverId)) {
+ return serverId;
+ }
+ if (fallbackId == null) {
+ fallbackId = serverId;
+ }
+ }
+ if (fallbackId != null) {
+ return fallbackId;
+ }
+ throw new DnsOperationException("No authoritative PowerDNS server
found");
+ }
+
+ public String createZone(String baseUrl, Integer port, String apiKey,
String externalServerId, String zoneName,
+ String zoneKind, boolean dnsSecFlag, List<String>
nameServers) throws DnsProviderException {
+
+ validateServerId(baseUrl, port, apiKey, externalServerId);
+ String normalizedZone = normalizeZone(zoneName);
+ ObjectNode json = MAPPER.createObjectNode();
+ json.put(ApiConstants.NAME, normalizedZone);
+ json.put(ApiConstants.KIND, zoneKind);
+ json.put(ApiConstants.DNS_SEC, dnsSecFlag);
+ if (!CollectionUtils.isEmpty(nameServers)) {
+ ArrayNode nsArray = json.putArray(ApiConstants.NAME_SERVERS);
+ for (String ns : nameServers) {
+ nsArray.add(ns.endsWith(".") ? ns : ns + ".");
+ }
+ }
+ HttpPost request = new HttpPost(buildUrl(baseUrl, port, "/servers/" +
externalServerId + "/zones"));
+ request.setEntity(new StringEntity(json.toString(),
StandardCharsets.UTF_8));
+ JsonNode response = execute(request, apiKey, 201);
Review Comment:
I wonder if DNS zone existence need to be checked before each DNS zone
operation (create, update, list, delete), it will lead to 1 more API and cause
longer response time. need to consider the trade-offs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]