Skip to content

Commit c754263

Browse files
committed
Create DHCP relay command and execute request
1 parent be9e06f commit c754263

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
package org.apache.cloudstack.agent.api;
18+
19+
import java.util.List;
20+
21+
public class CreateDhcpRelayCommand extends NsxCommand {
22+
23+
private String dhcpRelayName;
24+
private List<String> serverAddresses;
25+
26+
public CreateDhcpRelayCommand(String zoneName, Long zoneId, String accountName, Long accountId,
27+
String dhcpRelayName, List<String> serverAddresses) {
28+
super(zoneName, zoneId, accountName, accountId);
29+
this.dhcpRelayName = dhcpRelayName;
30+
this.serverAddresses = serverAddresses;
31+
}
32+
33+
public String getDhcpRelayName() {
34+
return dhcpRelayName;
35+
}
36+
37+
public List<String> getServerAddresses() {
38+
return serverAddresses;
39+
}
40+
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/resource/NsxResource.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131

3232
import com.vmware.nsx.model.TransportZone;
3333
import com.vmware.nsx.model.TransportZoneListResult;
34+
import com.vmware.nsx_policy.infra.DhcpRelayConfigs;
3435
import com.vmware.nsx_policy.infra.Segments;
3536
import com.vmware.nsx_policy.infra.Sites;
3637
import com.vmware.nsx_policy.infra.Tier1s;
3738
import com.vmware.nsx_policy.infra.sites.EnforcementPoints;
3839
import com.vmware.nsx_policy.infra.tier_0s.LocaleServices;
3940
import com.vmware.nsx_policy.model.ApiError;
41+
import com.vmware.nsx_policy.model.DhcpRelayConfig;
4042
import com.vmware.nsx_policy.model.EnforcementPointListResult;
4143
import com.vmware.nsx_policy.model.LocaleServicesListResult;
4244
import com.vmware.nsx_policy.model.Segment;
@@ -47,6 +49,7 @@
4749
import com.vmware.vapi.std.errors.Error;
4850
import org.apache.cloudstack.NsxAnswer;
4951
import org.apache.cloudstack.StartupNsxCommand;
52+
import org.apache.cloudstack.agent.api.CreateDhcpRelayCommand;
5053
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
5154
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
5255
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
@@ -128,6 +131,8 @@ public Answer executeRequest(Command cmd) {
128131
return executeRequest((CreateNsxSegmentCommand) cmd);
129132
} else if (cmd instanceof CreateNsxTier1GatewayCommand) {
130133
return executeRequest((CreateNsxTier1GatewayCommand) cmd);
134+
} else if (cmd instanceof CreateDhcpRelayCommand) {
135+
return executeRequest((CreateDhcpRelayCommand) cmd);
131136
} else {
132137
return Answer.createUnsupportedCommandAnswer(cmd);
133138
}
@@ -235,6 +240,29 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
235240
return true;
236241
}
237242

243+
private Answer executeRequest(CreateDhcpRelayCommand cmd) {
244+
String dhcpRelayName = cmd.getDhcpRelayName();
245+
List<String> addresses = cmd.getServerAddresses();
246+
String msg = String.format("Creating DHCP relay with name %s for addresses %s", dhcpRelayName, addresses);
247+
LOGGER.debug(msg);
248+
249+
try {
250+
DhcpRelayConfigs service = (DhcpRelayConfigs) nsxService.apply(DhcpRelayConfigs.class);
251+
DhcpRelayConfig config = new DhcpRelayConfig.Builder()
252+
.setServerAddresses(addresses)
253+
.setId(dhcpRelayName)
254+
.setDisplayName(dhcpRelayName)
255+
.build();
256+
service.patch(dhcpRelayName, config);
257+
} catch (Error error) {
258+
ApiError ae = error.getData()._convertTo(ApiError.class);
259+
msg = String.format("Error creating the DHCP relay with name %s: %s", dhcpRelayName, ae.getErrorMessage());
260+
LOGGER.error(msg);
261+
return new NsxAnswer(cmd, new CloudRuntimeException(ae.getErrorMessage()));
262+
}
263+
return new NsxAnswer(cmd, true, "");
264+
}
265+
238266
private Answer executeRequest(ReadyCommand cmd) {
239267
return new ReadyAnswer(cmd);
240268
}

0 commit comments

Comments
 (0)