You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ml...@apache.org on 2013/01/10 01:48:37 UTC
[9/52] [partial] Summary: Fixes for api_refactoring
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/iso/ListIsoPermissionsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/ListIsoPermissionsCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/ListIsoPermissionsCmd.java
new file mode 100644
index 0000000..0ca711f
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/iso/ListIsoPermissionsCmd.java
@@ -0,0 +1,46 @@
+// Licensedname = "listIsoPermissions", to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.iso;
+
+import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.TemplatePermissionsResponse;
+import org.apache.log4j.Logger;
+
+import com.cloud.storage.Storage.ImageFormat;
+import com.cloud.template.VirtualMachineTemplate;
+
+@APICommand(name="listIsoPermissions", description="List iso visibility and all accounts that have permissions to view this iso.", responseObject=TemplatePermissionsResponse.class)
+public class ListIsoPermissionsCmd extends BaseListTemplateOrIsoPermissionsCmd {
+ protected String getResponseName() {
+ return "listisopermissionsresponse";
+ }
+
+ @Override
+ public String getMediaType() {
+ return "iso";
+ }
+
+ @Override
+ protected Logger getLogger() {
+ return Logger.getLogger(ListIsoPermissionsCmd.class.getName());
+ }
+
+ protected boolean templateIsCorrectType(VirtualMachineTemplate template) {
+ return template.getFormat().equals(ImageFormat.ISO);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java
new file mode 100644
index 0000000..e6ccfff
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java
@@ -0,0 +1,161 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.iso;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListTaggedResourcesCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.TemplateResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.template.VirtualMachineTemplate.TemplateFilter;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+import com.cloud.utils.Pair;
+
+@APICommand(name = "listIsos", description="Lists all available ISO files.", responseObject=TemplateResponse.class)
+public class ListIsosCmd extends BaseListTaggedResourcesCmd {
+ public static final Logger s_logger = Logger.getLogger(ListIsosCmd.class.getName());
+
+ private static final String s_name = "listisosresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.BOOTABLE, type=CommandType.BOOLEAN, description="true if the ISO is bootable, false otherwise")
+ private Boolean bootable;
+
+ @Parameter(name=ApiConstants.HYPERVISOR, type=CommandType.STRING, description="the hypervisor for which to restrict the search")
+ private String hypervisor;
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = TemplateResponse.class,
+ description="list all isos by id")
+ private Long id;
+
+ @Parameter(name=ApiConstants.IS_PUBLIC, type=CommandType.BOOLEAN, description="true if the ISO is publicly available to all users, false otherwise.")
+ private Boolean publicIso;
+
+ @Parameter(name=ApiConstants.IS_READY, type=CommandType.BOOLEAN, description="true if this ISO is ready to be deployed")
+ private Boolean ready;
+
+ @Parameter(name=ApiConstants.ISO_FILTER, type=CommandType.STRING, description="possible values are \"featured\", \"self\", \"self-executable\",\"executable\", and \"community\". " +
+ "* featured-ISOs that are featured and are publicself-ISOs that have been registered/created by the owner. " +
+ "* selfexecutable-ISOs that have been registered/created by the owner that can be used to deploy a new VM. " +
+ "* executable-all ISOs that can be used to deploy a new VM " +
+ "* community-ISOs that are public.")
+ private String isoFilter = TemplateFilter.selfexecutable.toString();
+
+ @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="list all isos by name")
+ private String isoName;
+
+ @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
+ description="the ID of the zone")
+ private Long zoneId;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+
+ public Boolean isBootable() {
+ return bootable;
+ }
+
+ public String getHypervisor() {
+ return hypervisor;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public Boolean isPublic() {
+ return publicIso;
+ }
+
+ public Boolean isReady() {
+ return ready;
+ }
+
+ public String getIsoFilter() {
+ return isoFilter;
+ }
+
+ public String getIsoName() {
+ return isoName;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+
+ public boolean listInReadyState() {
+ Account account = UserContext.current().getCaller();
+ // It is account specific if account is admin type and domainId and accountName are not null
+ boolean isAccountSpecific = (account == null || isAdmin(account.getType())) && (getAccountName() != null) && (getDomainId() != null);
+ // Show only those that are downloaded.
+ TemplateFilter templateFilter = TemplateFilter.valueOf(getIsoFilter());
+ boolean onlyReady = (templateFilter == TemplateFilter.featured) || (templateFilter == TemplateFilter.selfexecutable) || (templateFilter == TemplateFilter.sharedexecutable)
+ || (templateFilter == TemplateFilter.executable && isAccountSpecific) || (templateFilter == TemplateFilter.community);
+
+ if (!onlyReady) {
+ if (isReady() != null && isReady().booleanValue() != onlyReady) {
+ onlyReady = isReady().booleanValue();
+ }
+ }
+
+ return onlyReady;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.Iso;
+ }
+
+ @Override
+ public void execute(){
+ Set<Pair<Long, Long>> isoZonePairSet = _mgr.listIsos(this);
+ ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
+ List<TemplateResponse> templateResponses = new ArrayList<TemplateResponse>();
+
+ for (Pair<Long, Long> iso : isoZonePairSet) {
+ List<TemplateResponse> responses = new ArrayList<TemplateResponse>();
+ responses = _responseGenerator.createIsoResponses(iso.first(), iso.second(), listInReadyState());
+ templateResponses.addAll(responses);
+ }
+ response.setResponses(templateResponses);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/iso/RegisterIsoCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/RegisterIsoCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/RegisterIsoCmd.java
new file mode 100644
index 0000000..214d18d
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/iso/RegisterIsoCmd.java
@@ -0,0 +1,173 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.iso;
+
+import java.util.List;
+
+import org.apache.cloudstack.api.*;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.DomainResponse;
+import org.apache.cloudstack.api.response.GuestOSResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.ProjectResponse;
+import org.apache.cloudstack.api.response.TemplateResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.template.VirtualMachineTemplate;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "registerIso", responseObject=TemplateResponse.class, description="Registers an existing ISO into the CloudStack Cloud.")
+public class RegisterIsoCmd extends BaseCmd {
+ public static final Logger s_logger = Logger.getLogger(RegisterIsoCmd.class.getName());
+
+ private static final String s_name = "registerisoresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.BOOTABLE, type=CommandType.BOOLEAN, description="true if this ISO is bootable. If not passed explicitly its assumed to be true")
+ private Boolean bootable;
+
+ @Parameter(name=ApiConstants.DISPLAY_TEXT, type=CommandType.STRING, required=true, description="the display text of the ISO. This is usually used for display purposes.", length=4096)
+ private String displayText;
+
+ @Parameter(name=ApiConstants.IS_FEATURED, type=CommandType.BOOLEAN, description="true if you want this ISO to be featured")
+ private Boolean featured;
+
+ @Parameter(name=ApiConstants.IS_PUBLIC, type=CommandType.BOOLEAN, description="true if you want to register the ISO to be publicly available to all users, false otherwise.")
+ private Boolean publicIso;
+
+ @Parameter(name=ApiConstants.IS_EXTRACTABLE, type=CommandType.BOOLEAN, description="true if the iso or its derivatives are extractable; default is false")
+ private Boolean extractable;
+
+ @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, required=true, description="the name of the ISO")
+ private String isoName;
+
+ @Parameter(name=ApiConstants.OS_TYPE_ID, type=CommandType.UUID, entityType = GuestOSResponse.class,
+ description="the ID of the OS Type that best represents the OS of this ISO. If the iso is bootable this parameter needs to be passed")
+ private Long osTypeId;
+
+ @Parameter(name=ApiConstants.URL, type=CommandType.STRING, required=true, description="the URL to where the ISO is currently being hosted")
+ private String url;
+
+ @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
+ required=true, description="the ID of the zone you wish to register the ISO to.")
+ private Long zoneId;
+
+ @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.UUID, entityType = DomainResponse.class,
+ description="an optional domainId. If the account parameter is used, domainId must also be used.")
+ private Long domainId;
+
+ @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="an optional account name. Must be used with domainId.")
+ private String accountName;
+
+ @Parameter(name=ApiConstants.CHECKSUM, type=CommandType.STRING, description="the MD5 checksum value of this ISO")
+ private String checksum;
+
+ @Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.UUID, entityType = ProjectResponse.class,
+ description="Register iso for the project")
+ private Long projectId;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Boolean isBootable() {
+ return bootable;
+ }
+
+ public String getDisplayText() {
+ return displayText;
+ }
+
+ public Boolean isFeatured() {
+ return featured;
+ }
+
+ public Boolean isPublic() {
+ return publicIso;
+ }
+
+ public Boolean isExtractable() {
+ return extractable;
+ }
+
+ public String getIsoName() {
+ return isoName;
+ }
+
+ public Long getOsTypeId() {
+ return osTypeId;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+
+ public Long getDomainId() {
+ return domainId;
+ }
+
+ public String getAccountName() {
+ return accountName;
+ }
+
+ public String getChecksum() {
+ return checksum;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Long accountId = finalyzeAccountId(accountName, domainId, projectId, true);
+ if (accountId == null) {
+ return UserContext.current().getCaller().getId();
+ }
+
+ return accountId;
+ }
+
+ @Override
+ public void execute() throws ResourceAllocationException{
+ VirtualMachineTemplate template = _templateService.registerIso(this);
+ if (template != null) {
+ ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
+ List<TemplateResponse> templateResponses = _responseGenerator.createIsoResponses(template.getId(), zoneId, false);
+ response.setResponses(templateResponses);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to register iso");
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoCmd.java
new file mode 100644
index 0000000..f54f8a6
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoCmd.java
@@ -0,0 +1,78 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.iso;
+
+import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoCmd;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.TemplateResponse;
+import com.cloud.template.VirtualMachineTemplate;
+import com.cloud.user.Account;
+
+@APICommand(name = "updateIso", description="Updates an ISO file.", responseObject=TemplateResponse.class)
+public class UpdateIsoCmd extends BaseUpdateTemplateOrIsoCmd {
+ public static final Logger s_logger = Logger.getLogger(UpdateIsoCmd.class.getName());
+ private static final String s_name = "updateisoresponse";
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public Boolean isPasswordEnabled() {
+ return null;
+ }
+
+ @Override
+ public String getFormat() {
+ return null;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ VirtualMachineTemplate template = _entityMgr.findById(VirtualMachineTemplate.class, getId());
+ if (template != null) {
+ return template.getAccountId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+
+ @Override
+ public void execute(){
+ VirtualMachineTemplate result = _mgr.updateTemplate(this);
+ if (result != null) {
+ TemplateResponse response = _responseGenerator.createIsoResponse(result);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update iso");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoPermissionsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoPermissionsCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoPermissionsCmd.java
new file mode 100644
index 0000000..b7a2c56
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoPermissionsCmd.java
@@ -0,0 +1,46 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.iso;
+
+import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoPermissionsCmd;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.template.VirtualMachineTemplate;
+import com.cloud.user.Account;
+
+@APICommand(name = "updateIsoPermissions", description="Updates iso permissions", responseObject=SuccessResponse.class)
+public class UpdateIsoPermissionsCmd extends BaseUpdateTemplateOrIsoPermissionsCmd {
+ protected String getResponseName() {
+ return "updateisopermissionsresponse";
+ }
+
+ protected Logger getLogger() {
+ return Logger.getLogger(UpdateIsoPermissionsCmd.class.getName());
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ VirtualMachineTemplate template = _entityMgr.findById(VirtualMachineTemplate.class, getId());
+ if (template != null) {
+ return template.getAccountId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/job/ListAsyncJobsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/job/ListAsyncJobsCmd.java b/api/src/org/apache/cloudstack/api/command/user/job/ListAsyncJobsCmd.java
new file mode 100644
index 0000000..50ac52e
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/job/ListAsyncJobsCmd.java
@@ -0,0 +1,69 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.job;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListAccountResourcesCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.AccountResponse;
+import org.apache.cloudstack.api.response.AsyncJobResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.utils.Pair;
+
+@APICommand(name = "listAsyncJobs", description="Lists all pending asynchronous jobs for the account.", responseObject=AsyncJobResponse.class)
+public class ListAsyncJobsCmd extends BaseListAccountResourcesCmd {
+ private static final String s_name = "listasyncjobsresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.START_DATE, type=CommandType.TZDATE, description="the start date of the async job")
+ private Date startDate;
+
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Date getStartDate() {
+ return startDate;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute(){
+
+ ListResponse<AsyncJobResponse> response = _queryService.searchForAsyncJobs(this);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/job/QueryAsyncJobResultCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/job/QueryAsyncJobResultCmd.java b/api/src/org/apache/cloudstack/api/command/user/job/QueryAsyncJobResultCmd.java
new file mode 100644
index 0000000..256d4ff
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/job/QueryAsyncJobResultCmd.java
@@ -0,0 +1,70 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.job;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.AsyncJobResponse;
+import com.cloud.user.Account;
+
+@APICommand(name = "queryAsyncJobResult", description="Retrieves the current status of asynchronous job.", responseObject=AsyncJobResponse.class)
+public class QueryAsyncJobResultCmd extends BaseCmd {
+ public static final Logger s_logger = Logger.getLogger(QueryAsyncJobResultCmd.class.getName());
+
+ private static final String s_name = "queryasyncjobresultresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.JOB_ID, type=CommandType.UUID, entityType=AsyncJobResponse.class,
+ required=true, description="the ID of the asychronous job")
+ private Long id;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ return Account.ACCOUNT_ID_SYSTEM;
+ }
+
+ @Override
+ public void execute(){
+ AsyncJobResponse response = _responseGenerator.queryJobResult(this);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/AssignToLoadBalancerRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/AssignToLoadBalancerRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/AssignToLoadBalancerRuleCmd.java
new file mode 100644
index 0000000..972673f
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/AssignToLoadBalancerRuleCmd.java
@@ -0,0 +1,122 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import java.util.List;
+
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.cloudstack.api.response.UserVmResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+import com.cloud.utils.StringUtils;
+
+@APICommand(name = "assignToLoadBalancerRule", description="Assigns virtual machine or a list of virtual machines to a load balancer rule.", responseObject=SuccessResponse.class)
+public class AssignToLoadBalancerRuleCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(AssignToLoadBalancerRuleCmd.class.getName());
+
+ private static final String s_name = "assigntoloadbalancerruleresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="the ID of the load balancer rule")
+ private Long id;
+
+ @Parameter(name=ApiConstants.VIRTUAL_MACHINE_IDS, type=CommandType.LIST, collectionType=CommandType.UUID, entityType = UserVmResponse.class,
+ required=true, description="the list of IDs of the virtual machine that are being assigned to the load balancer rule(i.e. virtualMachineIds=1,2,3)")
+ private List<Long> virtualMachineIds;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getLoadBalancerId() {
+ return id;
+ }
+
+ public List<Long> getVirtualMachineIds() {
+ return virtualMachineIds;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, getLoadBalancerId());
+ if (lb == null) {
+ return Account.ACCOUNT_ID_SYSTEM; // bad id given, parent this command to SYSTEM so ERROR events are tracked
+ }
+ return lb.getAccountId();
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_ASSIGN_TO_LOAD_BALANCER_RULE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "applying instances for load balancer: " + getLoadBalancerId() + " (ids: " + StringUtils.join(getVirtualMachineIds(), ",") + ")";
+ }
+
+ @Override
+ public void execute(){
+ UserContext.current().setEventDetails("Load balancer Id: "+getLoadBalancerId()+" VmIds: "+StringUtils.join(getVirtualMachineIds(), ","));
+ boolean result = _lbService.assignToLoadBalancer(getLoadBalancerId(), virtualMachineIds);
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to assign load balancer rule");
+ }
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ LoadBalancer lb = _lbService.findById(id);
+ if(lb == null){
+ throw new InvalidParameterValueException("Unable to find load balancer rule: " + id);
+ }
+ return lb.getNetworkId();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLBStickinessPolicyCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLBStickinessPolicyCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLBStickinessPolicyCmd.java
new file mode 100644
index 0000000..dc80d31
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLBStickinessPolicyCmd.java
@@ -0,0 +1,161 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+
+import java.util.Map;
+
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCreateCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.rules.StickinessPolicy;
+import org.apache.cloudstack.api.response.LBStickinessResponse;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+
+
+@APICommand(name = "createLBStickinessPolicy", description = "Creates a Load Balancer stickiness policy ", responseObject = LBStickinessResponse.class, since="3.0.0")
+@SuppressWarnings("rawtypes")
+public class CreateLBStickinessPolicyCmd extends BaseAsyncCreateCmd {
+ public static final Logger s_logger = Logger
+ .getLogger(CreateLBStickinessPolicyCmd.class.getName());
+
+ private static final String s_name = "createLBStickinessPolicy";
+
+ // ///////////////////////////////////////////////////
+ // ////////////// API parameters /////////////////////
+ // ///////////////////////////////////////////////////
+
+ @Parameter(name = ApiConstants.LBID, type = CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required = true, description = "the ID of the load balancer rule")
+ private Long lbRuleId;
+
+ @Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "the description of the LB Stickiness policy")
+ private String description;
+
+ @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "name of the LB Stickiness policy")
+ private String lbStickinessPolicyName;
+
+ @Parameter(name = ApiConstants.METHOD_NAME, type = CommandType.STRING, required = true, description = "name of the LB Stickiness policy method, possible values can be obtained from ListNetworks API ")
+ private String stickinessMethodName;
+
+ @Parameter(name = ApiConstants.PARAM_LIST, type = CommandType.MAP, description = "param list. Example: param[0].name=cookiename¶m[0].value=LBCookie ")
+ private Map paramList;
+
+ // ///////////////////////////////////////////////////
+ // ///////////////// Accessors ///////////////////////
+ // ///////////////////////////////////////////////////
+
+ public Long getLbRuleId() {
+ return lbRuleId;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public String getLBStickinessPolicyName() {
+ return lbStickinessPolicyName;
+ }
+
+ public String getStickinessMethodName() {
+ return stickinessMethodName;
+ }
+
+ public Map getparamList() {
+ return paramList;
+ }
+
+ public String getEntityTable() {
+ return "firewall_rules";
+ }
+ // ///////////////////////////////////////////////////
+ // ///////////// API Implementation///////////////////
+ // ///////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Account account = UserContext.current().getCaller();
+ if (account != null) {
+ return account.getId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+
+ @Override
+ public void execute() throws ResourceAllocationException, ResourceUnavailableException {
+ StickinessPolicy policy = null;
+ boolean success = false;
+
+ try {
+ UserContext.current().setEventDetails("Rule Id: " + getEntityId());
+ success = _lbService.applyLBStickinessPolicy(this);
+ if (success) {
+ // State might be different after the rule is applied, so get new object here
+ policy = _entityMgr.findById(StickinessPolicy.class, getEntityId());
+ LoadBalancer lb = _lbService.findById(policy.getLoadBalancerId());
+ LBStickinessResponse spResponse = _responseGenerator.createLBStickinessPolicyResponse(policy, lb);
+ setResponseObject(spResponse);
+ spResponse.setResponseName(getCommandName());
+ }
+ } finally {
+ if (!success || (policy == null)) {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create stickiness policy ");
+ }
+ }
+ }
+
+ @Override
+ public void create() {
+ try {
+ StickinessPolicy result = _lbService.createLBStickinessPolicy(this);
+ this.setEntityId(result.getId());
+ } catch (NetworkRuleConflictException e) {
+ s_logger.warn("Exception: ", e);
+ throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, e.getMessage());
+ }
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "creating a Load Balancer Stickiness policy: " + getLBStickinessPolicyName();
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
new file mode 100644
index 0000000..4aacc8e
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
@@ -0,0 +1,384 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCreateCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.DomainResponse;
+import org.apache.cloudstack.api.response.IPAddressResponse;
+import org.apache.cloudstack.api.response.LoadBalancerResponse;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.dc.DataCenter;
+import com.cloud.dc.DataCenter.NetworkType;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InsufficientAddressCapacityException;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.IpAddress;
+import com.cloud.network.Network;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+import com.cloud.utils.net.NetUtils;
+
+@APICommand(name = "createLoadBalancerRule", description="Creates a load balancer rule", responseObject=LoadBalancerResponse.class)
+public class CreateLoadBalancerRuleCmd extends BaseAsyncCreateCmd /*implements LoadBalancer */{
+ public static final Logger s_logger = Logger.getLogger(CreateLoadBalancerRuleCmd.class.getName());
+
+ private static final String s_name = "createloadbalancerruleresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ALGORITHM, type=CommandType.STRING, required=true, description="load balancer algorithm (source, roundrobin, leastconn)")
+ private String algorithm;
+
+ @Parameter(name=ApiConstants.DESCRIPTION, type=CommandType.STRING, description="the description of the load balancer rule", length=4096)
+ private String description;
+
+ @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, required=true, description="name of the load balancer rule")
+ private String loadBalancerRuleName;
+
+ @Parameter(name=ApiConstants.PRIVATE_PORT, type=CommandType.INTEGER, required=true, description="the private port of the private ip address/virtual machine where the network traffic will be load balanced to")
+ private Integer privatePort;
+
+ @Parameter(name=ApiConstants.PUBLIC_IP_ID, type=CommandType.UUID, entityType = IPAddressResponse.class,
+ description="public ip address id from where the network traffic will be load balanced from")
+ private Long publicIpId;
+
+ @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
+ required=false, description="zone where the load balancer is going to be created. This parameter is required when LB service provider is ElasticLoadBalancerVm")
+ private Long zoneId;
+
+ @Parameter(name=ApiConstants.PUBLIC_PORT, type=CommandType.INTEGER, required=true, description="the public port from where the network traffic will be load balanced from")
+ private Integer publicPort;
+
+ @Parameter(name = ApiConstants.OPEN_FIREWALL, type = CommandType.BOOLEAN, description = "if true, firewall rule for" +
+ " source/end pubic port is automatically created; if false - firewall rule has to be created explicitely. If not specified 1) defaulted to false when LB" +
+ " rule is being created for VPC guest network 2) in all other cases defaulted to true")
+ private Boolean openFirewall;
+
+ @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="the account associated with the load balancer. Must be used with the domainId parameter.")
+ private String accountName;
+
+ @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.UUID, entityType = DomainResponse.class,
+ description="the domain ID associated with the load balancer")
+ private Long domainId;
+
+ @Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list to forward traffic from")
+ private List<String> cidrlist;
+
+ @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ description="The guest network this " +
+ "rule will be created for. Required when public Ip address is not associated with any Guest network yet (VPC case)")
+ private Long networkId;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public String getAlgorithm() {
+ return algorithm;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public String getLoadBalancerRuleName() {
+ return loadBalancerRuleName;
+ }
+
+ public Integer getPrivatePort() {
+ return privatePort;
+ }
+
+ public String getEntityTable() {
+ return "firewall_rules";
+ }
+
+ public Long getSourceIpAddressId() {
+ if (publicIpId != null) {
+ IpAddress ipAddr = _networkService.getIp(publicIpId);
+ if (ipAddr == null || !ipAddr.readyToUse()) {
+ throw new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id " + ipAddr.getId());
+ }
+ } else if (getEntityId() != null) {
+ LoadBalancer rule = _entityMgr.findById(LoadBalancer.class, getEntityId());
+ return rule.getSourceIpAddressId();
+ }
+
+ return publicIpId;
+ }
+
+ private Long getVpcId() {
+ if (publicIpId != null) {
+ IpAddress ipAddr = _networkService.getIp(publicIpId);
+ if (ipAddr == null || !ipAddr.readyToUse()) {
+ throw new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id " + ipAddr.getId());
+ } else {
+ return ipAddr.getVpcId();
+ }
+ }
+ return null;
+ }
+
+
+ public Long getNetworkId() {
+ if (networkId != null) {
+ return networkId;
+ }
+ Long zoneId = getZoneId();
+
+ if (zoneId == null) {
+ Long ipId = getSourceIpAddressId();
+ if (ipId == null) {
+ throw new InvalidParameterValueException("Either networkId or zoneId or publicIpId has to be specified");
+ }
+ }
+
+ if (zoneId != null) {
+ DataCenter zone = _configService.getZone(zoneId);
+ if (zone.getNetworkType() == NetworkType.Advanced) {
+ List<? extends Network> networks = _networkService.getIsolatedNetworksOwnedByAccountInZone(getZoneId(), _accountService.getAccount(getEntityOwnerId()));
+ if (networks.size() == 0) {
+ String domain = _domainService.getDomain(getDomainId()).getName();
+ throw new InvalidParameterValueException("Account name=" + getAccountName() + " domain=" + domain + " doesn't have virtual networks in zone=" + zone.getName());
+ }
+
+ if (networks.size() < 1) {
+ throw new InvalidParameterValueException("Account doesn't have any Isolated networks in the zone");
+ } else if (networks.size() > 1) {
+ throw new InvalidParameterValueException("Account has more than one Isolated network in the zone");
+ }
+
+ return networks.get(0).getId();
+ } else {
+ Network defaultGuestNetwork = _networkService.getExclusiveGuestNetwork(zoneId);
+ if (defaultGuestNetwork == null) {
+ throw new InvalidParameterValueException("Unable to find a default Guest network for account " + getAccountName() + " in domain id=" + getDomainId());
+ } else {
+ return defaultGuestNetwork.getId();
+ }
+ }
+ } else {
+ IpAddress ipAddr = _networkService.getIp(publicIpId);
+ if (ipAddr.getAssociatedWithNetworkId() != null) {
+ return ipAddr.getAssociatedWithNetworkId();
+ } else {
+ throw new InvalidParameterValueException("Ip address id=" + publicIpId + " is not associated with any network");
+ }
+ }
+ }
+
+ public Integer getPublicPort() {
+ return publicPort;
+ }
+
+ public String getName() {
+ return loadBalancerRuleName;
+ }
+
+ public Boolean getOpenFirewall() {
+ boolean isVpc = getVpcId() == null ? false : true;
+ if (openFirewall != null) {
+ if (isVpc && openFirewall) {
+ throw new InvalidParameterValueException("Can't have openFirewall=true when IP address belongs to VPC");
+ }
+ return openFirewall;
+ } else {
+ if (isVpc) {
+ return false;
+ }
+ return true;
+ }
+ }
+
+ public List<String> getSourceCidrList() {
+ if (cidrlist != null) {
+ throw new InvalidParameterValueException("Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command");
+ }
+ return null;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute() throws ResourceAllocationException, ResourceUnavailableException {
+
+ UserContext callerContext = UserContext.current();
+ boolean success = true;
+ LoadBalancer rule = null;
+ try {
+ UserContext.current().setEventDetails("Rule Id: " + getEntityId());
+
+ if (getOpenFirewall()) {
+ success = success && _firewallService.applyFirewallRules(getSourceIpAddressId(), callerContext.getCaller());
+ }
+
+ // State might be different after the rule is applied, so get new object here
+ rule = _entityMgr.findById(LoadBalancer.class, getEntityId());
+ LoadBalancerResponse lbResponse = new LoadBalancerResponse();
+ if (rule != null) {
+ lbResponse = _responseGenerator.createLoadBalancerResponse(rule);
+ setResponseObject(lbResponse);
+ }
+ lbResponse.setResponseName(getCommandName());
+ } catch (Exception ex) {
+ s_logger.warn("Failed to create LB rule due to exception ", ex);
+ }finally {
+ if (!success || rule == null) {
+
+ if (getOpenFirewall()) {
+ _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
+ }
+ // no need to apply the rule on the backend as it exists in the db only
+ _lbService.deleteLoadBalancerRule(getEntityId(), false);
+
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create load balancer rule");
+ }
+ }
+ }
+
+ @Override
+ public void create() {
+ //cidr list parameter is deprecated
+ if (cidrlist != null) {
+ throw new InvalidParameterValueException("Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command");
+ }
+ try {
+ LoadBalancer result = _lbService.createLoadBalancerRule(this, getOpenFirewall());
+ this.setEntityId(result.getId());
+ } catch (NetworkRuleConflictException e) {
+ s_logger.warn("Exception: ", e);
+ throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, e.getMessage());
+ } catch (InsufficientAddressCapacityException e) {
+ s_logger.warn("Exception: ", e);
+ throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, e.getMessage());
+ }
+ }
+
+ public Integer getSourcePortStart() {
+ return publicPort.intValue();
+ }
+
+ public Integer getSourcePortEnd() {
+ return publicPort.intValue();
+ }
+
+ public String getProtocol() {
+ return NetUtils.TCP_PROTO;
+ }
+
+ public long getAccountId() {
+ if (publicIpId != null)
+ return _networkService.getIp(getSourceIpAddressId()).getAccountId();
+
+ Account account = null;
+ if ((domainId != null) && (accountName != null)) {
+ account = _responseGenerator.findAccountByNameDomain(accountName, domainId);
+ if (account != null) {
+ return account.getId();
+ } else {
+ throw new InvalidParameterValueException("Unable to find account " + account + " in domain id=" + domainId);
+ }
+ } else {
+ throw new InvalidParameterValueException("Can't define IP owner. Either specify account/domainId or publicIpId");
+ }
+ }
+
+ public long getDomainId() {
+ if (publicIpId != null)
+ return _networkService.getIp(getSourceIpAddressId()).getDomainId();
+ if (domainId != null) {
+ return domainId;
+ }
+ return UserContext.current().getCaller().getDomainId();
+ }
+
+ public int getDefaultPortStart() {
+ return privatePort.intValue();
+ }
+
+ public int getDefaultPortEnd() {
+ return privatePort.intValue();
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ return getAccountId();
+ }
+
+ public String getAccountName() {
+ return accountName;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+
+ public void setPublicIpId(Long publicIpId) {
+ this.publicIpId = publicIpId;
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_LOAD_BALANCER_CREATE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "creating load balancer: " + getName() + " account: " + getAccountName();
+
+ }
+
+ public String getXid() {
+ /*FIXME*/
+ return null;
+ }
+
+ public void setSourceIpAddressId(Long ipId) {
+ this.publicIpId = ipId;
+ }
+
+ @Override
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.FirewallRule;
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLBStickinessPolicyCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLBStickinessPolicyCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLBStickinessPolicyCmd.java
new file mode 100644
index 0000000..9329bd3
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLBStickinessPolicyCmd.java
@@ -0,0 +1,116 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import org.apache.cloudstack.api.response.LBStickinessResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.rules.StickinessPolicy;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "deleteLBStickinessPolicy", description = "Deletes a LB stickiness policy.", responseObject = SuccessResponse.class, since="3.0.0")
+public class DeleteLBStickinessPolicyCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(DeleteLBStickinessPolicyCmd.class.getName());
+ private static final String s_name = "deleteLBstickinessrruleresponse";
+ // ///////////////////////////////////////////////////
+ // ////////////// API parameters /////////////////////
+ // ///////////////////////////////////////////////////
+
+ @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = LBStickinessResponse.class,
+ required = true, description = "the ID of the LB stickiness policy")
+ private Long id;
+
+ // ///////////////////////////////////////////////////
+ // ///////////////// Accessors ///////////////////////
+ // ///////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ // ///////////////////////////////////////////////////
+ // ///////////// API Implementation///////////////////
+ // ///////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Account account = UserContext.current().getCaller();
+ if (account != null) {
+ return account.getId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_LB_STICKINESSPOLICY_DELETE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "deleting load balancer stickiness policy: " + getId();
+ }
+
+ @Override
+ public void execute() {
+ UserContext.current().setEventDetails("Load balancer stickiness policy Id: " + getId());
+ boolean result = _lbService.deleteLBStickinessPolicy(getId(), true);
+
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete load balancer stickiness policy");
+ }
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ StickinessPolicy policy = _entityMgr.findById(StickinessPolicy.class,
+ getId());
+ if (policy == null) {
+ throw new InvalidParameterValueException("Unable to find LB stickiness rule: " + id);
+ }
+ LoadBalancer lb = _lbService.findById(policy.getLoadBalancerId());
+ if (lb == null) {
+ throw new InvalidParameterValueException("Unable to find load balancer rule for stickiness rule: " + id);
+ }
+ return lb.getNetworkId();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLoadBalancerRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLoadBalancerRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLoadBalancerRuleCmd.java
new file mode 100644
index 0000000..d53155f
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLoadBalancerRuleCmd.java
@@ -0,0 +1,114 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import org.apache.cloudstack.api.*;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "deleteLoadBalancerRule", description="Deletes a load balancer rule.", responseObject=SuccessResponse.class)
+public class DeleteLoadBalancerRuleCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(DeleteLoadBalancerRuleCmd.class.getName());
+ private static final String s_name = "deleteloadbalancerruleresponse";
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="the ID of the load balancer rule")
+ private Long id;
+
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, getId());
+ if (lb != null) {
+ return lb.getAccountId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_LOAD_BALANCER_DELETE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "deleting load balancer: " + getId();
+ }
+
+ @Override
+ public void execute(){
+ UserContext.current().setEventDetails("Load balancer Id: "+getId());
+ boolean result = _firewallService.revokeRelatedFirewallRule(id, true);
+ result = result && _lbService.deleteLoadBalancerRule(id, true);
+
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete load balancer");
+ }
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ LoadBalancer lb = _lbService.findById(id);
+ if(lb == null){
+ throw new InvalidParameterValueException("Unable to find load balancer rule: " + id);
+ }
+ return lb.getNetworkId();
+ }
+
+ @Override
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.FirewallRule;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLBStickinessPoliciesCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLBStickinessPoliciesCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLBStickinessPoliciesCmd.java
new file mode 100644
index 0000000..90708c0
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLBStickinessPoliciesCmd.java
@@ -0,0 +1,86 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.LBStickinessResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.network.rules.StickinessPolicy;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "listLBStickinessPolicies", description = "Lists LBStickiness policies.", responseObject = LBStickinessResponse.class, since="3.0.0")
+public class ListLBStickinessPoliciesCmd extends BaseListCmd {
+ public static final Logger s_logger = Logger
+ .getLogger(ListLBStickinessPoliciesCmd.class.getName());
+
+ private static final String s_name = "listlbstickinesspoliciesresponse";
+
+ // ///////////////////////////////////////////////////
+ // ////////////// API parameters /////////////////////
+ // ///////////////////////////////////////////////////
+ @Parameter(name = ApiConstants.LBID, type = CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required = true, description = "the ID of the load balancer rule")
+ private Long lbRuleId;
+
+ // ///////////////////////////////////////////////////
+ // ///////////////// Accessors ///////////////////////
+ // ///////////////////////////////////////////////////
+ public Long getLbRuleId() {
+ return lbRuleId;
+ }
+
+ // ///////////////////////////////////////////////////
+ // ///////////// API Implementation///////////////////
+ // ///////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute() {
+ List<LBStickinessResponse> spResponses = new ArrayList<LBStickinessResponse>();
+ LoadBalancer lb = _lbService.findById(getLbRuleId());
+ ListResponse<LBStickinessResponse> response = new ListResponse<LBStickinessResponse>();
+
+ if (lb != null) {
+ //check permissions
+ Account caller = UserContext.current().getCaller();
+ _accountService.checkAccess(caller, null, true, lb);
+ List<? extends StickinessPolicy> stickinessPolicies = _lbService.searchForLBStickinessPolicies(this);
+ LBStickinessResponse spResponse = _responseGenerator.createLBStickinessPolicyResponse(stickinessPolicies, lb);
+ spResponses.add(spResponse);
+ response.setResponses(spResponses);
+ }
+
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java
new file mode 100644
index 0000000..374c6fb
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java
@@ -0,0 +1,83 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.UserVmResponse;
+import com.cloud.uservm.UserVm;
+
+@APICommand(name = "listLoadBalancerRuleInstances", description="List all virtual machine instances that are assigned to a load balancer rule.", responseObject=UserVmResponse.class)
+public class ListLoadBalancerRuleInstancesCmd extends BaseListCmd {
+ public static final Logger s_logger = Logger.getLogger (ListLoadBalancerRuleInstancesCmd.class.getName());
+
+ private static final String s_name = "listloadbalancerruleinstancesresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.APPLIED, type=CommandType.BOOLEAN, description="true if listing all virtual machines currently applied to the load balancer rule; default is true")
+ private Boolean applied;
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="the ID of the load balancer rule")
+ private Long id;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Boolean isApplied() {
+ return applied;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute(){
+ List<? extends UserVm> result = _lbService.listLoadBalancerInstances(this);
+ ListResponse<UserVmResponse> response = new ListResponse<UserVmResponse>();
+ List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
+ if (result != null) {
+ vmResponses = _responseGenerator.createUserVmResponse("loadbalancerruleinstance", result.toArray(new UserVm[result.size()]));
+ }
+ response.setResponses(vmResponses);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRulesCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRulesCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRulesCmd.java
new file mode 100644
index 0000000..d0d4e8d
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRulesCmd.java
@@ -0,0 +1,116 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListTaggedResourcesCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.cloudstack.api.response.IPAddressResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.LoadBalancerResponse;
+import org.apache.cloudstack.api.response.UserVmResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.utils.Pair;
+
+@APICommand(name = "listLoadBalancerRules", description = "Lists load balancer rules.", responseObject = LoadBalancerResponse.class)
+public class ListLoadBalancerRulesCmd extends BaseListTaggedResourcesCmd {
+ public static final Logger s_logger = Logger.getLogger(ListLoadBalancerRulesCmd.class.getName());
+
+ private static final String s_name = "listloadbalancerrulesresponse";
+
+ // ///////////////////////////////////////////////////
+ // ////////////// API parameters /////////////////////
+ // ///////////////////////////////////////////////////
+
+ @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = FirewallRuleResponse.class,
+ description = "the ID of the load balancer rule")
+ private Long id;
+
+ @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "the name of the load balancer rule")
+ private String loadBalancerRuleName;
+
+ @Parameter(name = ApiConstants.PUBLIC_IP_ID, type = CommandType.UUID, entityType = IPAddressResponse.class,
+ description = "the public IP address id of the load balancer rule ")
+ private Long publicIpId;
+
+ @Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID, type = CommandType.UUID, entityType = UserVmResponse.class,
+ description = "the ID of the virtual machine of the load balancer rule")
+ private Long virtualMachineId;
+
+ @Parameter(name = ApiConstants.ZONE_ID, type = CommandType.UUID, entityType = ZoneResponse.class,
+ description = "the availability zone ID")
+ private Long zoneId;
+
+ // ///////////////////////////////////////////////////
+ // ///////////////// Accessors ///////////////////////
+ // ///////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getLoadBalancerRuleName() {
+ return loadBalancerRuleName;
+ }
+
+ public Long getPublicIpId() {
+ return publicIpId;
+ }
+
+ public Long getVirtualMachineId() {
+ return virtualMachineId;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+
+ // ///////////////////////////////////////////////////
+ // ///////////// API Implementation///////////////////
+ // ///////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute() {
+ Pair<List<? extends LoadBalancer>, Integer> loadBalancers = _lbService.searchForLoadBalancers(this);
+ ListResponse<LoadBalancerResponse> response = new ListResponse<LoadBalancerResponse>();
+ List<LoadBalancerResponse> lbResponses = new ArrayList<LoadBalancerResponse>();
+ if (loadBalancers != null) {
+ for (LoadBalancer loadBalancer : loadBalancers.first()) {
+ LoadBalancerResponse lbResponse = _responseGenerator.createLoadBalancerResponse(loadBalancer);
+ lbResponse.setObjectName("loadbalancerrule");
+ lbResponses.add(lbResponse);
+ }
+ }
+ response.setResponses(lbResponses, loadBalancers.second());
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/RemoveFromLoadBalancerRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/RemoveFromLoadBalancerRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/RemoveFromLoadBalancerRuleCmd.java
new file mode 100644
index 0000000..f56f06d
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/RemoveFromLoadBalancerRuleCmd.java
@@ -0,0 +1,118 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import java.util.List;
+
+import org.apache.cloudstack.api.*;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.cloudstack.api.response.UserVmResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+import com.cloud.utils.StringUtils;
+
+@APICommand(name = "removeFromLoadBalancerRule", description="Removes a virtual machine or a list of virtual machines from a load balancer rule.", responseObject=SuccessResponse.class)
+public class RemoveFromLoadBalancerRuleCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(RemoveFromLoadBalancerRuleCmd.class.getName());
+
+ private static final String s_name = "removefromloadbalancerruleresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="The ID of the load balancer rule")
+ private Long id;
+
+ @Parameter(name=ApiConstants.VIRTUAL_MACHINE_IDS, type=CommandType.LIST, required = true, collectionType=CommandType.UUID, entityType = UserVmResponse.class,
+ description="the list of IDs of the virtual machines that are being removed from the load balancer rule (i.e. virtualMachineIds=1,2,3)")
+ private List<Long> virtualMachineIds;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ public List<Long> getVirtualMachineIds() {
+ return virtualMachineIds;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, getId());
+ if (lb == null) {
+ return Account.ACCOUNT_ID_SYSTEM; // bad id given, parent this command to SYSTEM so ERROR events are tracked
+ }
+ return lb.getAccountId();
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_REMOVE_FROM_LOAD_BALANCER_RULE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "removing instances from load balancer: " + getId() + " (ids: " + StringUtils.join(getVirtualMachineIds(), ",") + ")";
+ }
+
+ @Override
+ public void execute(){
+ UserContext.current().setEventDetails("Load balancer Id: "+getId()+" VmIds: "+StringUtils.join(getVirtualMachineIds(), ","));
+ boolean result = _lbService.removeFromLoadBalancer(id, virtualMachineIds);
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to remove instance from load balancer rule");
+ }
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ LoadBalancer lb = _lbService.findById(id);
+ if(lb == null){
+ throw new InvalidParameterValueException("Unable to find load balancer rule: " + id);
+ }
+ return lb.getNetworkId();
+ }
+}