Skip to content

Commit 4972c36

Browse files
committed
introduce import and unmanage option for createDnsZone and deleteDnsZone
1 parent 8d365cb commit 4972c36

19 files changed

Lines changed: 228 additions & 116 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,8 @@ public class ApiConstants {
13781378
public static final String OLD_STATE = "oldState";
13791379
public static final String NEW_STATE = "newState";
13801380
public static final String OLD_HOST_NAME = "oldHostName";
1381-
1381+
public static final String IMPORT = "import";
1382+
public static final String UNMANAGE = "unmanage";
13821383

13831384
public static final String PARAMETER_DESCRIPTION_ACTIVATION_RULE = "Quota tariff's activation rule. It can receive a JS script that results in either " +
13841385
"a boolean or a numeric value: if it results in a boolean value, the tariff value will be applied according to the result; if it results in a numeric value, the " +

api/src/main/java/org/apache/cloudstack/api/command/user/dns/CreateDnsZoneCmd.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public class CreateDnsZoneCmd extends BaseAsyncCreateCmd {
6666
@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "The description of the DNS zone")
6767
private String description;
6868

69+
@Parameter(name = ApiConstants.IMPORT, type = CommandType.BOOLEAN, entityType = DnsZoneResponse.class,
70+
description = "If true, imports an existing DNS zone from the DNS provider into CloudStack. " +
71+
"If false, creates the zone in the DNS provider and registers it in CloudStack.")
72+
private Boolean importDnsZone = false;
73+
6974
/////////////////////////////////////////////////////
7075
/////////////////// Accessors ///////////////////////
7176
/////////////////////////////////////////////////////
@@ -115,7 +120,7 @@ public void create() throws ResourceAllocationException {
115120
@Override
116121
public void execute() {
117122
try {
118-
DnsZone result = dnsProviderManager.provisionDnsZone(getEntityId());
123+
DnsZone result = dnsProviderManager.provisionDnsZone(getEntityId(), isImportDnsZone());
119124
if (result != null) {
120125
DnsZoneResponse response = dnsProviderManager.createDnsZoneResponse(result);
121126
response.setResponseName(getCommandName());
@@ -142,4 +147,8 @@ public String getEventType() {
142147
public String getEventDescription() {
143148
return "creating DNS zone: " + getName();
144149
}
150+
151+
public Boolean isImportDnsZone() {
152+
return Boolean.TRUE.equals(importDnsZone);
153+
}
145154
}

api/src/main/java/org/apache/cloudstack/api/command/user/dns/DeleteDnsZoneCmd.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public class DeleteDnsZoneCmd extends BaseAsyncCmd {
5151
description = "The ID of the DNS zone")
5252
private Long id;
5353

54+
@Parameter(name = ApiConstants.UNMANAGE, type = CommandType.BOOLEAN, entityType = DnsZoneResponse.class,
55+
description = "If true, removes the DNS zone from CloudStack; if false, " +
56+
"removes it from both CloudStack and the DNS provider.")
57+
private Boolean unmanage = false;
58+
5459
/////////////////////////////////////////////////////
5560
/////////////////// Accessors ///////////////////////
5661
/////////////////////////////////////////////////////
@@ -66,7 +71,7 @@ public Long getId() {
6671
@Override
6772
public void execute() {
6873
try {
69-
boolean result = dnsProviderManager.deleteDnsZone(getId());
74+
boolean result = dnsProviderManager.deleteDnsZone(getId(), isUnmanage());
7075
if (result) {
7176
SuccessResponse response = new SuccessResponse(getCommandName());
7277
setResponseObject(response);
@@ -97,4 +102,8 @@ public String getEventType() {
97102
public String getEventDescription() {
98103
return "Deleting DNS Zone ID: " + getId();
99104
}
105+
106+
public Boolean isUnmanage() {
107+
return Boolean.TRUE.equals(unmanage);
108+
}
100109
}

api/src/main/java/org/apache/cloudstack/dns/DnsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ public interface DnsProvider extends Adapter {
4141
String updateRecord(DnsServer server, DnsZone zone, DnsRecord record) throws DnsProviderException;
4242
String deleteRecord(DnsServer server, DnsZone zone, DnsRecord record) throws DnsProviderException;
4343
boolean dnsRecordExists(DnsServer server, DnsZone zone, String recordName, String recordType) throws DnsProviderException;
44+
boolean dnsZoneExists(DnsServer server, DnsZone zone);
4445
}

api/src/main/java/org/apache/cloudstack/dns/DnsProviderManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public interface DnsProviderManager extends Manager, PluggableService {
5252
// Allocates the DB row (State: Inactive)
5353
DnsZone allocateDnsZone(CreateDnsZoneCmd cmd);
5454
// Calls the Plugin (State: Inactive -> Active)
55-
DnsZone provisionDnsZone(long zoneId);
55+
DnsZone provisionDnsZone(long zoneId, boolean isImport);
5656

5757
DnsZone updateDnsZone(UpdateDnsZoneCmd cmd);
58-
boolean deleteDnsZone(Long id);
58+
boolean deleteDnsZone(Long id, boolean isUnmanage);
5959
ListResponse<DnsZoneResponse> listDnsZones(ListDnsZonesCmd cmd);
6060

6161
DnsRecordResponse createDnsRecord(CreateDnsRecordCmd cmd);

api/src/test/java/org/apache/cloudstack/api/command/user/dns/CreateDnsZoneCmdTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void testExecuteSuccess() throws Exception {
127127
DnsZoneResponse mockResponse = new DnsZoneResponse();
128128
mockResponse.setName("example.com");
129129

130-
when(dnsProviderManager.provisionDnsZone(ENTITY_ID)).thenReturn(mockZone);
130+
when(dnsProviderManager.provisionDnsZone(ENTITY_ID, false)).thenReturn(mockZone);
131131
when(dnsProviderManager.createDnsZoneResponse(mockZone)).thenReturn(mockResponse);
132132

133133
cmd.execute();
@@ -142,7 +142,7 @@ public void testExecuteReturnsNull() throws Exception {
142142
CreateDnsZoneCmd cmd = createCmd();
143143
cmd.setEntityId(ENTITY_ID);
144144

145-
when(dnsProviderManager.provisionDnsZone(ENTITY_ID)).thenReturn(null);
145+
when(dnsProviderManager.provisionDnsZone(ENTITY_ID, false)).thenReturn(null);
146146
cmd.execute();
147147
}
148148

@@ -151,7 +151,7 @@ public void testExecuteThrowsException() throws Exception {
151151
CreateDnsZoneCmd cmd = createCmd();
152152
cmd.setEntityId(ENTITY_ID);
153153

154-
when(dnsProviderManager.provisionDnsZone(ENTITY_ID)).thenThrow(new RuntimeException("Provider error"));
154+
when(dnsProviderManager.provisionDnsZone(ENTITY_ID, false)).thenThrow(new RuntimeException("Provider error"));
155155
cmd.execute();
156156
}
157157
}

api/src/test/java/org/apache/cloudstack/api/command/user/dns/DeleteDnsZoneCmdTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,26 @@ public void testEventDescription() throws Exception {
7979
@Test
8080
public void testExecuteSuccess() throws Exception {
8181
DeleteDnsZoneCmd cmd = createCmd();
82-
when(dnsProviderManager.deleteDnsZone(ENTITY_ID)).thenReturn(true);
82+
when(dnsProviderManager.deleteDnsZone(ENTITY_ID, false)).thenReturn(true);
8383

8484
cmd.execute();
8585

8686
SuccessResponse response = (SuccessResponse) cmd.getResponseObject();
8787
assertNotNull(response);
88-
verify(dnsProviderManager).deleteDnsZone(ENTITY_ID);
88+
verify(dnsProviderManager).deleteDnsZone(ENTITY_ID, false);
8989
}
9090

9191
@Test(expected = ServerApiException.class)
9292
public void testExecuteReturnsFalse() throws Exception {
9393
DeleteDnsZoneCmd cmd = createCmd();
94-
when(dnsProviderManager.deleteDnsZone(ENTITY_ID)).thenReturn(false);
94+
when(dnsProviderManager.deleteDnsZone(ENTITY_ID, false)).thenReturn(false);
9595
cmd.execute();
9696
}
9797

9898
@Test(expected = ServerApiException.class)
9999
public void testExecuteThrowsException() throws Exception {
100100
DeleteDnsZoneCmd cmd = createCmd();
101-
when(dnsProviderManager.deleteDnsZone(ENTITY_ID)).thenThrow(new RuntimeException("Error"));
101+
when(dnsProviderManager.deleteDnsZone(ENTITY_ID, false)).thenThrow(new RuntimeException("Error"));
102102
cmd.execute();
103103
}
104104
}

engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.vpc_offerings','conserve_mode', 'tin
118118
--- Disable/enable NICs
119119
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.nics','enabled', 'TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''Indicates whether the NIC is enabled or not'' ');
120120

121+
--- Quota tariff/usage mapping
122+
CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_tariff_usage` (
123+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
124+
`tariff_id` bigint(20) unsigned NOT NULL COMMENT 'ID of the tariff of the Quota usage detail calculated, foreign key to quota_tariff table',
125+
`quota_usage_id` bigint(20) unsigned NOT NULL COMMENT 'ID of the aggregation of Quota usage details, foreign key to quota_usage table',
126+
`quota_used` decimal(20,8) NOT NULL COMMENT 'Amount of quota used',
127+
PRIMARY KEY (`id`),
128+
CONSTRAINT `fk_quota_tariff_usage__tariff_id` FOREIGN KEY (`tariff_id`) REFERENCES `cloud_usage`.`quota_tariff` (`id`),
129+
CONSTRAINT `fk_quota_tariff_usage__quota_usage_id` FOREIGN KEY (`quota_usage_id`) REFERENCES `cloud_usage`.`quota_usage` (`id`));
121130

122131
-- ======================================================================
123132
-- DNS Framework Schema

plugins/dns/powerdns/src/main/java/org/apache/cloudstack/dns/powerdns/PowerDnsClient.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,20 @@ public Iterable<JsonNode> listRecords(String baseUrl, Integer port, String apiKe
240240
return rrsets.isArray() ? rrsets : Collections.emptyList();
241241
}
242242

243-
public boolean dnsRecordExists(String baseUrl, Integer port, String apiKey,
243+
public boolean zoneExists(String baseUrl, Integer port, String apiKey, String externalServerId, String zoneName) {
244+
try {
245+
validateServerId(baseUrl, port, apiKey, externalServerId);
246+
String normalizedZone = normalizeZone(zoneName);
247+
String encodedZone = URLEncoder.encode(normalizedZone, StandardCharsets.UTF_8);
248+
HttpGet request = new HttpGet(buildUrl(baseUrl, port, "/servers/" + externalServerId + "/zones/" + encodedZone));
249+
execute(request, apiKey, 200);
250+
return true;
251+
} catch (DnsProviderException | IllegalArgumentException e) {
252+
return false;
253+
}
254+
}
255+
256+
public boolean recordExists(String baseUrl, Integer port, String apiKey,
244257
String externalServerId, String zoneName,
245258
String recordName, String type) throws DnsProviderException {
246259

plugins/dns/powerdns/src/main/java/org/apache/cloudstack/dns/powerdns/PowerDnsProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,15 @@ public List<DnsRecord> listRecords(DnsServer server, DnsZone zone) throws DnsPro
159159
}
160160

161161
public boolean dnsRecordExists(DnsServer server, DnsZone zone, String recordName, String recordType) throws DnsProviderException {
162-
return client.dnsRecordExists(server.getUrl(), server.getPort(), server.getDnsApiKey(),
162+
return client.recordExists(server.getUrl(), server.getPort(), server.getDnsApiKey(),
163163
server.getDetail(PDNS_SERVER_ID), zone.getName(), recordName, recordType);
164164
}
165165

166+
@Override
167+
public boolean dnsZoneExists(DnsServer server, DnsZone zone) {
168+
return client.zoneExists(server.getUrl(), server.getPort(), server.getDnsApiKey(), server.getDetail(PDNS_SERVER_ID), zone.getName());
169+
}
170+
166171
void validateRequiredServerAndZoneFields(DnsServer server, DnsZone zone) {
167172
validateRequiredServerFields(server);
168173
if (StringUtils.isBlank(zone.getName())) {

0 commit comments

Comments
 (0)