Skip to content

Commit d6ee6ad

Browse files
author
Daniel Vega
committed
Merging load balancer operations in GloboDNS
1 parent aae03c1 commit d6ee6ad

4 files changed

Lines changed: 146 additions & 1 deletion

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* 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, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.globo.globodns.cloudstack.commands;
18+
19+
import com.cloud.agent.api.Command;
20+
21+
public class CreateLbRecordAndReverseCommand extends Command {
22+
23+
private String lbRecordName;
24+
25+
private String lbRecordIp;
26+
27+
private String lbDomain;
28+
29+
private Long reverseTemplateId;
30+
31+
private boolean override;
32+
33+
public CreateLbRecordAndReverseCommand(String lbRecordName, String lbRecordIp, String lbDomain, Long reverseTemplateId, boolean override) {
34+
this.lbRecordName = lbRecordName;
35+
this.lbRecordIp = lbRecordIp;
36+
this.lbDomain = lbDomain;
37+
this.reverseTemplateId = reverseTemplateId;
38+
this.override = override;
39+
}
40+
41+
@Override
42+
public boolean executeInSequence() {
43+
return false;
44+
}
45+
46+
public String getLbRecordName() {
47+
return this.lbRecordName;
48+
}
49+
50+
public String getLbRecordIp() {
51+
return this.lbRecordIp;
52+
}
53+
54+
public String getLbDomain() {
55+
return this.lbDomain;
56+
}
57+
58+
public Long getReverseTemplateId() {
59+
return reverseTemplateId;
60+
}
61+
62+
public boolean isOverride() {
63+
return override;
64+
}
65+
66+
}

plugins/network-elements/globodns/src/com/globo/globodns/cloudstack/element/GloboDnsElement.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.cloud.vm.VirtualMachine;
7272
import com.cloud.vm.VirtualMachineProfile;
7373
import com.globo.globodns.cloudstack.api.AddGloboDnsHostCmd;
74+
import com.globo.globodns.cloudstack.commands.CreateLbRecordAndReverseCommand;
7475
import com.globo.globodns.cloudstack.commands.CreateOrUpdateDomainCommand;
7576
import com.globo.globodns.cloudstack.commands.CreateOrUpdateRecordAndReverseCommand;
7677
import com.globo.globodns.cloudstack.commands.RemoveDomainCommand;
@@ -90,6 +91,8 @@ public class GloboDnsElement extends AdapterBase implements ResourceStateAdapter
9091
"Template id to be used when creating domains in GloboDNS", true, ConfigKey.Scope.Global);
9192
private static final ConfigKey<Boolean> GloboDNSOverride = new ConfigKey<Boolean>("Advanced", Boolean.class, "globodns.override.entries", "true",
9293
"Allow GloboDns to override entries that already exist", true, ConfigKey.Scope.Global);
94+
private static final ConfigKey<Boolean> GloboDNSLbOverride = new ConfigKey<Boolean>("Advanced", Boolean.class, "globodns.override.lb.entries", "false",
95+
"Allow GloboDns to override entries for load balancer names that already exist", true, ConfigKey.Scope.Global);
9396

9497
// DAOs
9598
@Inject
@@ -268,7 +271,7 @@ public String getConfigComponentName() {
268271

269272
@Override
270273
public ConfigKey<?>[] getConfigKeys() {
271-
return new ConfigKey<?>[] {GloboDNSTemplateId, GloboDNSOverride};
274+
return new ConfigKey<?>[] {GloboDNSTemplateId, GloboDNSOverride, GloboDNSLbOverride};
272275
}
273276

274277
////////// Resource/Host methods ////////////
@@ -385,4 +388,31 @@ public Host doInTransaction(TransactionStatus status) throws CloudRuntimeExcepti
385388

386389
return host;
387390
}
391+
392+
// Load Balancing methods
393+
@Override
394+
public boolean createDnsRecordForLoadBalancer(String lbDomain, String lbRecord, String lbIpAddress, Long zoneId) {
395+
s_logger.debug("Creating LB DNS record " + lbRecord + " in domain " + lbDomain);
396+
DataCenter zone = _dcDao.findById(zoneId);
397+
if (zone == null) {
398+
throw new CloudRuntimeException("Could not find zone with ID " + zoneId);
399+
}
400+
401+
CreateLbRecordAndReverseCommand cmd = new CreateLbRecordAndReverseCommand(lbRecord, lbIpAddress, lbDomain, GloboDNSTemplateId.value(), GloboDNSLbOverride.value());
402+
callCommand(cmd, zoneId);
403+
return true;
404+
}
405+
406+
@Override
407+
public boolean removeDnsRecordForLoadBalancer(String lbDomain, String lbRecord, String lbIpAddress, Long zoneId) {
408+
s_logger.debug("Removing LB DNS record " + lbRecord + " from domain " + lbDomain);
409+
DataCenter zone = _dcDao.findById(zoneId);
410+
if (zone == null) {
411+
throw new CloudRuntimeException("Could not find zone with ID " + zoneId);
412+
}
413+
414+
RemoveRecordCommand cmd = new RemoveRecordCommand(lbRecord, lbIpAddress, lbDomain, true); // Remove record no matter what
415+
callCommand(cmd, zoneId);
416+
return true;
417+
}
388418
}

plugins/network-elements/globodns/src/com/globo/globodns/cloudstack/element/GloboDnsElementService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
public interface GloboDnsElementService extends PluggableService {
2323

2424
public Host addGloboDnsHost(Long pNtwkId, String username, String password, String url);
25+
26+
public boolean createDnsRecordForLoadBalancer(String lbDomain, String lbRecord, String lbIpAddress, Long zoneId);
27+
28+
public boolean removeDnsRecordForLoadBalancer(String lbDomain, String lbRecord, String lbIpAddress, Long zoneId);
2529
}

plugins/network-elements/globodns/src/com/globo/globodns/cloudstack/resource/GloboDnsResource.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.globo.globodns.client.model.Domain;
4343
import com.globo.globodns.client.model.Export;
4444
import com.globo.globodns.client.model.Record;
45+
import com.globo.globodns.cloudstack.commands.CreateLbRecordAndReverseCommand;
4546
import com.globo.globodns.cloudstack.commands.CreateOrUpdateDomainCommand;
4647
import com.globo.globodns.cloudstack.commands.CreateOrUpdateRecordAndReverseCommand;
4748
import com.globo.globodns.cloudstack.commands.RemoveDomainCommand;
@@ -173,6 +174,8 @@ public Answer executeRequest(Command cmd) {
173174
return execute((CreateOrUpdateDomainCommand)cmd);
174175
} else if (cmd instanceof CreateOrUpdateRecordAndReverseCommand) {
175176
return execute((CreateOrUpdateRecordAndReverseCommand)cmd);
177+
} else if (cmd instanceof CreateLbRecordAndReverseCommand) {
178+
return execute((CreateLbRecordAndReverseCommand) cmd);
176179
}
177180
return Answer.createUnsupportedCommandAnswer(cmd);
178181
}
@@ -282,6 +285,48 @@ public Answer execute(CreateOrUpdateRecordAndReverseCommand cmd) {
282285
}
283286
}
284287

288+
public Answer execute(CreateLbRecordAndReverseCommand cmd) {
289+
boolean needsExport = false;
290+
try {
291+
Domain domain = searchDomain(cmd.getLbDomain(), false);
292+
if (domain == null) {
293+
String msg = "Domain " + cmd.getLbDomain() + " doesn't exist.";
294+
s_logger.debug(msg);
295+
return new Answer(cmd, false, msg);
296+
}
297+
298+
boolean created = createOrUpdateRecord(domain.getId(), cmd.getLbRecordName(), cmd.getLbRecordIp(), IPV4_RECORD_TYPE, cmd.isOverride());
299+
if (!created) {
300+
String msg = "Unable to create LB record " + cmd.getLbRecordName() + " at " + cmd.getLbDomain();
301+
if (!cmd.isOverride()) {
302+
msg += ". Override LB record option is false, maybe record already exists.";
303+
}
304+
return new Answer(cmd, false, msg);
305+
} else {
306+
needsExport = true;
307+
}
308+
309+
String reverseRecordContent = cmd.getLbRecordName() + '.' + cmd.getLbDomain() + '.';
310+
if (createOrUpdateReverse(cmd.getLbRecordIp(), reverseRecordContent, cmd.getReverseTemplateId(), cmd.isOverride())) {
311+
needsExport = true;
312+
} else {
313+
if (!cmd.isOverride()) {
314+
String msg = "Unable to create LB reverse record " + cmd.getLbRecordName() + " for ip " + cmd.getLbRecordIp();
315+
msg += ". Override record option is false, maybe record already exists.";
316+
return new Answer(cmd, false, msg);
317+
}
318+
}
319+
320+
return new Answer(cmd);
321+
} catch (GloboDnsException e) {
322+
return new Answer(cmd, false, e.getMessage());
323+
} finally {
324+
if (needsExport) {
325+
scheduleExportChangesToBind();
326+
}
327+
}
328+
}
329+
285330
protected boolean createOrUpdateReverse(String networkIp, String reverseRecordContent, Long templateId, boolean override) {
286331
String reverseDomainName = generateReverseDomainNameFromNetworkIp(networkIp);
287332
Domain reverseDomain = searchDomain(reverseDomainName, true);

0 commit comments

Comments
 (0)