Skip to content

Commit add277a

Browse files
committed
Separate network and VPC related cmd to use two different views.
1 parent 7c6f1c1 commit add277a

19 files changed

Lines changed: 423 additions & 65 deletions

File tree

api/src/org/apache/cloudstack/api/ResponseGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ LBHealthCheckResponse createLBHealthCheckPolicyResponse(List<? extends HealthChe
313313

314314
NetworkOfferingResponse createNetworkOfferingResponse(NetworkOffering offering);
315315

316-
NetworkResponse createNetworkResponse(Network network);
316+
NetworkResponse createNetworkResponse(ResponseView view, Network network);
317317

318318
UserResponse createUserResponse(User user);
319319

@@ -374,7 +374,7 @@ LBHealthCheckResponse createLBHealthCheckPolicyResponse(List<? extends HealthChe
374374
* @param vpc
375375
* @return
376376
*/
377-
VpcResponse createVpcResponse(Vpc vpc);
377+
VpcResponse createVpcResponse(ResponseView view, Vpc vpc);
378378

379379
/**
380380
* @param networkACLItem
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.api.command.admin.network;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.Parameter;
25+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
26+
import org.apache.cloudstack.api.ServerApiException;
27+
import org.apache.cloudstack.api.command.user.network.CreateNetworkCmd;
28+
import org.apache.cloudstack.api.response.NetworkResponse;
29+
30+
import com.cloud.exception.ConcurrentOperationException;
31+
import com.cloud.exception.InsufficientCapacityException;
32+
import com.cloud.exception.ResourceAllocationException;
33+
import com.cloud.network.Network;
34+
35+
@APICommand(name = "createNetwork", description = "Creates a network", responseObject = NetworkResponse.class, responseView = ResponseView.Full)
36+
public class CreateNetworkCmdByAdmin extends CreateNetworkCmd {
37+
public static final Logger s_logger = Logger.getLogger(CreateNetworkCmdByAdmin.class.getName());
38+
39+
@Parameter(name=ApiConstants.VLAN, type=CommandType.STRING, description="the ID or VID of the network")
40+
private String vlan;
41+
42+
/////////////////////////////////////////////////////
43+
/////////////////// Accessors ///////////////////////
44+
/////////////////////////////////////////////////////
45+
46+
public String getVlan() {
47+
return vlan;
48+
}
49+
50+
/////////////////////////////////////////////////////
51+
/////////////// API Implementation///////////////////
52+
/////////////////////////////////////////////////////
53+
54+
@Override
55+
// an exception thrown by createNetwork() will be caught by the dispatcher.
56+
public void execute() throws InsufficientCapacityException, ConcurrentOperationException, ResourceAllocationException{
57+
Network result = _networkService.createGuestNetwork(this);
58+
if (result != null) {
59+
NetworkResponse response = _responseGenerator.createNetworkResponse(ResponseView.Full, result);
60+
response.setResponseName(getCommandName());
61+
setResponseObject(response);
62+
}else {
63+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create network");
64+
}
65+
}
66+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.api.command.admin.network;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.apache.log4j.Logger;
23+
24+
import org.apache.cloudstack.api.APICommand;
25+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
26+
import org.apache.cloudstack.api.command.user.network.ListNetworksCmd;
27+
import org.apache.cloudstack.api.response.ListResponse;
28+
import org.apache.cloudstack.api.response.NetworkResponse;
29+
30+
import com.cloud.network.Network;
31+
32+
@APICommand(name = "listNetworks", description = "Lists all available networks.", responseObject = NetworkResponse.class, responseView = ResponseView.Full)
33+
public class ListNetworksCmdByAdmin extends ListNetworksCmd {
34+
public static final Logger s_logger = Logger.getLogger(ListNetworksCmdByAdmin.class.getName());
35+
36+
@Override
37+
public void execute(){
38+
List<? extends Network> networks = _networkService.searchForNetworks(this);
39+
ListResponse<NetworkResponse> response = new ListResponse<NetworkResponse>();
40+
List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
41+
for (Network network : networks) {
42+
NetworkResponse networkResponse = _responseGenerator.createNetworkResponse(ResponseView.Full, network);
43+
networkResponses.add(networkResponse);
44+
}
45+
46+
response.setResponses(networkResponses);
47+
response.setResponseName(getCommandName());
48+
setResponseObject(response);
49+
}
50+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.api.command.admin.network;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiErrorCode;
23+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
24+
import org.apache.cloudstack.api.ServerApiException;
25+
import org.apache.cloudstack.api.command.user.network.UpdateNetworkCmd;
26+
import org.apache.cloudstack.api.response.NetworkResponse;
27+
import org.apache.cloudstack.context.CallContext;
28+
29+
import com.cloud.exception.ConcurrentOperationException;
30+
import com.cloud.exception.InsufficientCapacityException;
31+
import com.cloud.exception.InvalidParameterValueException;
32+
import com.cloud.network.Network;
33+
import com.cloud.user.Account;
34+
import com.cloud.user.User;
35+
36+
@APICommand(name = "updateNetwork", description = "Updates a network", responseObject = NetworkResponse.class, responseView = ResponseView.Full)
37+
public class UpdateNetworkCmdByAdmin extends UpdateNetworkCmd {
38+
public static final Logger s_logger = Logger.getLogger(UpdateNetworkCmdByAdmin.class.getName());
39+
40+
41+
@Override
42+
public void execute() throws InsufficientCapacityException, ConcurrentOperationException{
43+
User callerUser = _accountService.getActiveUser(CallContext.current().getCallingUserId());
44+
Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId());
45+
Network network = _networkService.getNetwork(id);
46+
if (network == null) {
47+
throw new InvalidParameterValueException("Couldn't find network by id");
48+
}
49+
50+
Network result = _networkService.updateGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
51+
callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr(), getGuestVmCidr(), getDisplayNetwork());
52+
53+
54+
if (result != null) {
55+
NetworkResponse response = _responseGenerator.createNetworkResponse(ResponseView.Full, result);
56+
response.setResponseName(getCommandName());
57+
setResponseObject(response);
58+
} else {
59+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update network");
60+
}
61+
}
62+
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.api.command.admin.vpc;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiErrorCode;
23+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
24+
import org.apache.cloudstack.api.ServerApiException;
25+
import org.apache.cloudstack.api.command.user.vpc.CreateVPCCmd;
26+
import org.apache.cloudstack.api.response.VpcResponse;
27+
28+
import com.cloud.exception.ConcurrentOperationException;
29+
import com.cloud.exception.InsufficientCapacityException;
30+
import com.cloud.exception.ResourceUnavailableException;
31+
import com.cloud.network.vpc.Vpc;
32+
33+
@APICommand(name = "createVPC", description = "Creates a VPC", responseObject = VpcResponse.class, responseView = ResponseView.Full)
34+
public class CreateVPCCmdByAdmin extends CreateVPCCmd {
35+
public static final Logger s_logger = Logger.getLogger(CreateVPCCmdByAdmin.class.getName());
36+
37+
@Override
38+
public void execute() {
39+
Vpc vpc = null;
40+
try {
41+
if (_vpcService.startVpc(getEntityId(), true)) {
42+
vpc = _entityMgr.findById(Vpc.class, getEntityId());
43+
}
44+
} catch (ResourceUnavailableException ex) {
45+
s_logger.warn("Exception: ", ex);
46+
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
47+
} catch (ConcurrentOperationException ex) {
48+
s_logger.warn("Exception: ", ex);
49+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
50+
} catch (InsufficientCapacityException ex) {
51+
s_logger.info(ex);
52+
s_logger.trace(ex);
53+
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
54+
}
55+
56+
if (vpc != null) {
57+
VpcResponse response = _responseGenerator.createVpcResponse(ResponseView.Full, vpc);
58+
response.setResponseName(getCommandName());
59+
setResponseObject(response);
60+
} else {
61+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create VPC");
62+
}
63+
}
64+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.api.command.admin.vpc;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.apache.log4j.Logger;
23+
24+
import org.apache.cloudstack.api.APICommand;
25+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
26+
import org.apache.cloudstack.api.command.user.vpc.ListVPCsCmd;
27+
import org.apache.cloudstack.api.response.ListResponse;
28+
import org.apache.cloudstack.api.response.VpcResponse;
29+
30+
import com.cloud.network.vpc.Vpc;
31+
32+
33+
@APICommand(name = "listVPCs", description = "Lists VPCs", responseObject = VpcResponse.class, responseView = ResponseView.Full)
34+
public class ListVPCsCmdByAdmin extends ListVPCsCmd {
35+
public static final Logger s_logger = Logger.getLogger(ListVPCsCmdByAdmin.class.getName());
36+
37+
@Override
38+
public void execute() {
39+
List<? extends Vpc> vpcs = _vpcService.listVpcs(getId(), getVpcName(), getDisplayText(),
40+
getSupportedServices(), getCidr(), getVpcOffId(), getState(), getAccountName(), getDomainId(),
41+
getKeyword(), getStartIndex(), getPageSizeVal(), getZoneId(), isRecursive(),
42+
listAll(), getRestartRequired(), getTags(), getProjectId());
43+
ListResponse<VpcResponse> response = new ListResponse<VpcResponse>();
44+
List<VpcResponse> offeringResponses = new ArrayList<VpcResponse>();
45+
for (Vpc vpc : vpcs) {
46+
VpcResponse offeringResponse = _responseGenerator.createVpcResponse(ResponseView.Full, vpc);
47+
offeringResponses.add(offeringResponse);
48+
}
49+
50+
response.setResponses(offeringResponses);
51+
response.setResponseName(getCommandName());
52+
setResponseObject(response);
53+
}
54+
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.api.command.admin.vpc;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiErrorCode;
23+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
24+
import org.apache.cloudstack.api.ServerApiException;
25+
import org.apache.cloudstack.api.command.user.vpc.UpdateVPCCmd;
26+
import org.apache.cloudstack.api.response.VpcResponse;
27+
28+
import com.cloud.network.vpc.Vpc;
29+
30+
@APICommand(name = "updateVPC", description = "Updates a VPC", responseObject = VpcResponse.class, responseView = ResponseView.Full)
31+
public class UpdateVPCCmdByAdmin extends UpdateVPCCmd {
32+
public static final Logger s_logger = Logger.getLogger(UpdateVPCCmdByAdmin.class.getName());
33+
34+
@Override
35+
public void execute(){
36+
Vpc result = _vpcService.updateVpc(getId(), getVpcName(), getDisplayText());
37+
if (result != null) {
38+
VpcResponse response = _responseGenerator.createVpcResponse(ResponseView.Full, result);
39+
response.setResponseName(getCommandName());
40+
setResponseObject(response);
41+
} else {
42+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update VPC");
43+
}
44+
}
45+
46+
47+
}

0 commit comments

Comments
 (0)