|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.api.command.user.dns; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.apache.cloudstack.acl.RoleType; |
| 26 | +import org.apache.cloudstack.api.APICommand; |
| 27 | +import org.apache.cloudstack.api.ApiConstants; |
| 28 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 29 | +import org.apache.cloudstack.api.BaseCmd; |
| 30 | +import org.apache.cloudstack.api.Parameter; |
| 31 | +import org.apache.cloudstack.api.ServerApiException; |
| 32 | +import org.apache.cloudstack.api.response.DnsServerResponse; |
| 33 | +import org.apache.cloudstack.context.CallContext; |
| 34 | +import org.apache.cloudstack.dns.DnsProviderType; |
| 35 | +import org.apache.cloudstack.dns.DnsServer; |
| 36 | +import org.apache.commons.collections.MapUtils; |
| 37 | +import org.apache.commons.lang3.BooleanUtils; |
| 38 | + |
| 39 | +import com.cloud.exception.InvalidParameterValueException; |
| 40 | +import com.cloud.utils.EnumUtils; |
| 41 | + |
| 42 | +@APICommand(name = "addDnsServer", |
| 43 | + description = "Adds a new external DNS server", |
| 44 | + responseObject = DnsServerResponse.class, |
| 45 | + entityType = {DnsServer.class}, |
| 46 | + requestHasSensitiveInfo = true, |
| 47 | + responseHasSensitiveInfo = false, |
| 48 | + since = "4.23.0", |
| 49 | + authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User}) |
| 50 | +public class AddDnsServerCmd extends BaseCmd { |
| 51 | + |
| 52 | + ///////////////////////////////////////////////////// |
| 53 | + //////////////// API parameters ///////////////////// |
| 54 | + ///////////////////////////////////////////////////// |
| 55 | + /// |
| 56 | + @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "Name of the DNS server") |
| 57 | + private String name; |
| 58 | + |
| 59 | + @Parameter(name = ApiConstants.URL, type = CommandType.STRING, required = true, description = "API URL of the provider") |
| 60 | + private String url; |
| 61 | + |
| 62 | + @Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING, required = true, description = "Provider type (e.g., PowerDNS)") |
| 63 | + private String provider; |
| 64 | + |
| 65 | + @Parameter(name = ApiConstants.DNS_USER_NAME, type = CommandType.STRING, |
| 66 | + description = "Username or email associated with the DNS provider account (used for authentication)") |
| 67 | + private String dnsUserName; |
| 68 | + |
| 69 | + @Parameter(name = ApiConstants.DNS_API_KEY, required = true, type = CommandType.STRING, description = "API key or token for the DNS provider") |
| 70 | + private String dnsApiKey; |
| 71 | + |
| 72 | + @Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "Port number of the external DNS server") |
| 73 | + private Integer port; |
| 74 | + |
| 75 | + @Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN, |
| 76 | + description = "Whether this DNS server can be used by accounts other than the owner to create and manage DNS zones") |
| 77 | + private Boolean isPublic; |
| 78 | + |
| 79 | + @Parameter(name = ApiConstants.PUBLIC_DOMAIN_SUFFIX, type = CommandType.STRING, |
| 80 | + description = "Domain suffix that restricts DNS zones created by non-owner accounts to subdomains of this " + |
| 81 | + "suffix (for example, sub.example.com under example.com)") |
| 82 | + private String publicDomainSuffix; |
| 83 | + |
| 84 | + @Parameter(name = ApiConstants.NAME_SERVERS, type = CommandType.LIST, collectionType = CommandType.STRING, |
| 85 | + required = true, |
| 86 | + description = "Comma separated list of name servers; used to create NS records for the DNS Zone (for example, ns1.example.com, ns2.example.com)") |
| 87 | + private List<String> nameServers; |
| 88 | + |
| 89 | + @Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, description = "Details in key/value pairs using " + |
| 90 | + "format details[i].keyname=keyvalue. Example: details[0].pdnsServerId=localhost") |
| 91 | + protected Map details; |
| 92 | + |
| 93 | + ///////////////////////////////////////////////////// |
| 94 | + /////////////////// Accessors /////////////////////// |
| 95 | + ///////////////////////////////////////////////////// |
| 96 | + |
| 97 | + public String getName() { return name; } |
| 98 | + |
| 99 | + public String getUrl() { return url; } |
| 100 | + |
| 101 | + public String getDnsApiKey() { |
| 102 | + return dnsApiKey; |
| 103 | + } |
| 104 | + |
| 105 | + public Integer getPort() { |
| 106 | + return port; |
| 107 | + } |
| 108 | + |
| 109 | + public Boolean isPublic() { |
| 110 | + return BooleanUtils.isTrue(isPublic); |
| 111 | + } |
| 112 | + |
| 113 | + public String getPublicDomainSuffix() { |
| 114 | + return publicDomainSuffix; |
| 115 | + } |
| 116 | + |
| 117 | + public List<String> getNameServers() { |
| 118 | + return nameServers; |
| 119 | + } |
| 120 | + |
| 121 | + public DnsProviderType getProvider() { |
| 122 | + DnsProviderType dnsProviderType = EnumUtils.getEnumIgnoreCase(DnsProviderType.class, provider, DnsProviderType.PowerDNS); |
| 123 | + if (dnsProviderType == null) { |
| 124 | + throw new InvalidParameterValueException(String.format("Invalid value passed for provider type, valid values are: %s", |
| 125 | + EnumUtils.listValues(DnsProviderType.values()))); |
| 126 | + } |
| 127 | + return dnsProviderType; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public long getEntityOwnerId() { |
| 132 | + return CallContext.current().getCallingAccount().getId(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void execute() { |
| 137 | + try { |
| 138 | + DnsServer server = dnsProviderManager.addDnsServer(this); |
| 139 | + if (server != null) { |
| 140 | + DnsServerResponse response = dnsProviderManager.createDnsServerResponse(server); |
| 141 | + response.setResponseName(getCommandName()); |
| 142 | + setResponseObject(response); |
| 143 | + } else { |
| 144 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add DNS server"); |
| 145 | + } |
| 146 | + } catch (Exception ex) { |
| 147 | + logger.error("Failed to add DNS server", ex); |
| 148 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public String getDnsUserName() { |
| 153 | + return dnsUserName; |
| 154 | + } |
| 155 | + |
| 156 | + public Map<String, String> getDetails() { |
| 157 | + Map<String, String> detailsMap = new HashMap<>(); |
| 158 | + if (MapUtils.isNotEmpty(details)) { |
| 159 | + Collection<?> props = details.values(); |
| 160 | + for (Object prop : props) { |
| 161 | + HashMap<String, String> detail = (HashMap<String, String>) prop; |
| 162 | + for (Map.Entry<String, String> entry: detail.entrySet()) { |
| 163 | + detailsMap.put(entry.getKey(),entry.getValue()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + return detailsMap; |
| 168 | + } |
| 169 | +} |
0 commit comments