You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by bh...@apache.org on 2012/12/04 20:32:06 UTC

[8/18] api_refactor: refactor volume apis

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/api/src/org/apache/cloudstack/api/user/volume/command/MigrateVolumeCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/user/volume/command/MigrateVolumeCmd.java b/api/src/org/apache/cloudstack/api/user/volume/command/MigrateVolumeCmd.java
new file mode 100644
index 0000000..947e89a
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/user/volume/command/MigrateVolumeCmd.java
@@ -0,0 +1,106 @@
+// 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.user.volume.command;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.IdentityMapper;
+import org.apache.cloudstack.api.Implementation;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import com.cloud.api.response.VolumeResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.storage.Volume;
+import com.cloud.user.Account;
+
+
+@Implementation(description="Migrate volume", responseObject=VolumeResponse.class, since="3.0.0")
+public class MigrateVolumeCmd extends BaseAsyncCmd {
+    private static final String s_name = "migratevolumeresponse";
+
+     /////////////////////////////////////////////////////
+    //////////////// API parameters /////////////////////
+    /////////////////////////////////////////////////////
+
+    @IdentityMapper(entityTableName="volumes")
+    @Parameter(name=ApiConstants.VOLUME_ID, type=CommandType.LONG, required=true, description="the ID of the volume")
+    private Long volumeId;
+
+    @IdentityMapper(entityTableName="storage_pool")
+    @Parameter(name=ApiConstants.STORAGE_ID, type=CommandType.LONG, required=true, description="destination storage pool ID to migrate the volume to")
+    private Long storageId;
+
+    /////////////////////////////////////////////////////
+    /////////////////// Accessors ///////////////////////
+    /////////////////////////////////////////////////////
+
+    public Long getVolumeId() {
+        return volumeId;
+    }
+
+    public Long getStoragePoolId() {
+        return storageId;
+    }
+    /////////////////////////////////////////////////////
+    /////////////// API Implementation///////////////////
+    /////////////////////////////////////////////////////
+
+    @Override
+    public String getCommandName() {
+        return s_name;
+    }
+
+    @Override
+    public long getEntityOwnerId() {
+          Volume volume = _entityMgr.findById(Volume.class, getVolumeId());
+          if (volume != null) {
+              return volume.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_VOLUME_MIGRATE;
+    }
+
+    @Override
+    public String getEventDescription() {
+        return  "Attempting to migrate volume Id: " + getVolumeId() + " to storage pool Id: "+ getStoragePoolId();
+    }
+
+
+    @Override
+    public void execute(){
+        Volume result;
+        try {
+            result = _storageService.migrateVolume(getVolumeId(), getStoragePoolId());
+             if (result != null) {
+                 VolumeResponse response = _responseGenerator.createVolumeResponse(result);
+                 response.setResponseName(getCommandName());
+                 this.setResponseObject(response);
+             }
+        } catch (ConcurrentOperationException e) {
+            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to migrate volume: ");
+        }
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/api/src/org/apache/cloudstack/api/user/volume/command/UploadVolumeCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/user/volume/command/UploadVolumeCmd.java b/api/src/org/apache/cloudstack/api/user/volume/command/UploadVolumeCmd.java
new file mode 100755
index 0000000..164a4bb
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/user/volume/command/UploadVolumeCmd.java
@@ -0,0 +1,147 @@
+// 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.user.volume.command;
+
+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.IdentityMapper;
+import org.apache.cloudstack.api.Implementation;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import com.cloud.api.response.VolumeResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.storage.Volume;
+import com.cloud.user.UserContext;
+
+@Implementation(description="Uploads a data disk.", responseObject=VolumeResponse.class)
+public class UploadVolumeCmd extends BaseAsyncCmd {
+    public static final Logger s_logger = Logger.getLogger(UploadVolumeCmd.class.getName());
+    private static final String s_name = "uploadvolumeresponse";
+
+    /////////////////////////////////////////////////////
+    //////////////// API parameters /////////////////////
+    /////////////////////////////////////////////////////
+
+    @Parameter(name=ApiConstants.FORMAT, type=CommandType.STRING, required=true, description="the format for the volume. Possible values include QCOW2, OVA, and VHD.")
+    private String format;
+
+    @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, required=true, description="the name of the volume")
+    private String volumeName;
+
+    @Parameter(name=ApiConstants.URL, type=CommandType.STRING, required=true, description="the URL of where the volume is hosted. Possible URL include http:// and https://")
+    private String url;
+
+    @IdentityMapper(entityTableName="data_center")
+    @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, required=true, description="the ID of the zone the volume is to be hosted on")
+    private Long zoneId;
+
+    @IdentityMapper(entityTableName="domain")
+    @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, 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 accountName. Must be used with domainId.")
+    private String accountName;
+
+    @Parameter(name=ApiConstants.CHECKSUM, type=CommandType.STRING, description="the MD5 checksum value of this volume")
+    private String checksum;
+
+    /////////////////////////////////////////////////////
+    /////////////////// Accessors ///////////////////////
+    /////////////////////////////////////////////////////
+
+    public String getFormat() {
+        return format;
+    }
+
+    public String getVolumeName() {
+        return volumeName;
+    }
+
+    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 void execute() throws ResourceUnavailableException,
+            InsufficientCapacityException, ServerApiException,
+            ConcurrentOperationException, ResourceAllocationException,
+            NetworkRuleConflictException {
+
+            Volume volume = _storageService.uploadVolume(this);
+            if (volume != null){
+                VolumeResponse response = _responseGenerator.createVolumeResponse(volume);
+                response.setResponseName(getCommandName());
+                this.setResponseObject(response);
+            } else {
+                throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to upload volume");
+            }
+    }
+
+    @Override
+    public String getCommandName() {
+           return s_name;
+    }
+
+    @Override
+    public long getEntityOwnerId() {
+        Long accountId = finalyzeAccountId(accountName, domainId, null, true);
+        if (accountId == null) {
+            return UserContext.current().getCaller().getId();
+        }
+
+        return accountId;
+    }
+
+    @Override
+    public String getEventDescription() {
+        return  "uploading volume: " + getVolumeName() + " in the zone " + getZoneId();
+    }
+
+    @Override
+    public String getEventType() {
+        return EventTypes.EVENT_VOLUME_UPLOAD;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/client/tomcatconf/commands.properties.in
----------------------------------------------------------------------
diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in
index 9dbad2b..14c79db 100755
--- a/client/tomcatconf/commands.properties.in
+++ b/client/tomcatconf/commands.properties.in
@@ -242,14 +242,14 @@ addSecondaryStorage=com.cloud.api.commands.AddSecondaryStorageCmd;1
 updateHostPassword=com.cloud.api.commands.UpdateHostPasswordCmd;1
 
 #### volume commands
-attachVolume=com.cloud.api.commands.AttachVolumeCmd;15
-uploadVolume=com.cloud.api.commands.UploadVolumeCmd;15
-detachVolume=com.cloud.api.commands.DetachVolumeCmd;15
-createVolume=com.cloud.api.commands.CreateVolumeCmd;15
-deleteVolume=com.cloud.api.commands.DeleteVolumeCmd;15
-listVolumes=com.cloud.api.commands.ListVolumesCmd;15
-extractVolume=com.cloud.api.commands.ExtractVolumeCmd;15
-migrateVolume=com.cloud.api.commands.MigrateVolumeCmd;15
+attachVolume=org.apache.cloudstack.api.user.volume.command.AttachVolumeCmd;15
+uploadVolume=org.apache.cloudstack.api.user.volume.command.UploadVolumeCmd;15
+detachVolume=org.apache.cloudstack.api.user.volume.command.DetachVolumeCmd;15
+createVolume=org.apache.cloudstack.api.user.volume.command.CreateVolumeCmd;15
+deleteVolume=org.apache.cloudstack.api.user.volume.command.DeleteVolumeCmd;15
+listVolumes=org.apache.cloudstack.api.user.volume.command.ListVolumesCmd;15
+extractVolume=org.apache.cloudstack.api.user.volume.command.ExtractVolumeCmd;15
+migrateVolume=org.apache.cloudstack.api.user.volume.command.MigrateVolumeCmd;15
 
 #### registration command:  FIXME -- this really should be something in management server that
 ####                                 generates a new key for the user and they just have to

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java b/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java
index e0ce531..b5abd82 100755
--- a/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java
+++ b/server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java
@@ -26,6 +26,8 @@ import javax.ejb.Local;
 import javax.naming.ConfigurationException;
 
 import org.apache.cloudstack.api.user.template.command.CreateTemplateCmd;
+import org.apache.cloudstack.api.user.volume.command.AttachVolumeCmd;
+import org.apache.cloudstack.api.user.volume.command.DetachVolumeCmd;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.api.Answer;
@@ -33,9 +35,7 @@ import com.cloud.agent.api.StopAnswer;
 import com.cloud.agent.api.baremetal.IpmISetBootDevCommand;
 import com.cloud.agent.api.baremetal.IpmiBootorResetCommand;
 import com.cloud.agent.manager.Commands;
-import com.cloud.api.commands.AttachVolumeCmd;
 import org.apache.cloudstack.api.user.vm.command.DeployVMCmd;
-import com.cloud.api.commands.DetachVolumeCmd;
 import org.apache.cloudstack.api.user.vm.command.StartVMCmd;
 import org.apache.cloudstack.api.user.vm.command.UpgradeVMCmd;
 import com.cloud.baremetal.PxeServerManager.PxeServerType;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/server/src/com/cloud/server/ManagementServerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java
index 6b3dece..f6e9207 100755
--- a/server/src/com/cloud/server/ManagementServerImpl.java
+++ b/server/src/com/cloud/server/ManagementServerImpl.java
@@ -72,7 +72,7 @@ import org.apache.cloudstack.api.ApiConstants;
 import com.cloud.api.ApiDBUtils;
 import org.apache.cloudstack.api.user.ssh.command.CreateSSHKeyPairCmd;
 import com.cloud.api.commands.DestroySystemVmCmd;
-import com.cloud.api.commands.ExtractVolumeCmd;
+import org.apache.cloudstack.api.user.volume.command.ExtractVolumeCmd;
 import org.apache.cloudstack.api.user.vm.command.GetVMPasswordCmd;
 import com.cloud.api.commands.ListAlertsCmd;
 import com.cloud.api.commands.ListCapacityCmd;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/server/src/com/cloud/storage/StorageManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java
index e252633..882e798 100755
--- a/server/src/com/cloud/storage/StorageManagerImpl.java
+++ b/server/src/com/cloud/storage/StorageManagerImpl.java
@@ -44,6 +44,7 @@ import java.util.concurrent.TimeUnit;
 import javax.ejb.Local;
 import javax.naming.ConfigurationException;
 
+import org.apache.cloudstack.api.user.volume.command.CreateVolumeCmd;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.AgentManager;
@@ -73,11 +74,10 @@ import com.cloud.alert.AlertManager;
 import com.cloud.api.ApiDBUtils;
 import com.cloud.api.commands.CancelPrimaryStorageMaintenanceCmd;
 import com.cloud.api.commands.CreateStoragePoolCmd;
-import com.cloud.api.commands.CreateVolumeCmd;
 import com.cloud.api.commands.DeletePoolCmd;
-import com.cloud.api.commands.ListVolumesCmd;
+import org.apache.cloudstack.api.user.volume.command.ListVolumesCmd;
 import com.cloud.api.commands.UpdateStoragePoolCmd;
-import com.cloud.api.commands.UploadVolumeCmd;
+import org.apache.cloudstack.api.user.volume.command.UploadVolumeCmd;
 import com.cloud.async.AsyncJobManager;
 import com.cloud.capacity.Capacity;
 import com.cloud.capacity.CapacityManager;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/server/src/com/cloud/storage/upload/UploadListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/upload/UploadListener.java b/server/src/com/cloud/storage/upload/UploadListener.java
index aab798b..35bf732 100755
--- a/server/src/com/cloud/storage/upload/UploadListener.java
+++ b/server/src/com/cloud/storage/upload/UploadListener.java
@@ -25,6 +25,7 @@ import java.util.Timer;
 import java.util.TimerTask;
 
 import org.apache.cloudstack.api.user.iso.command.ExtractIsoCmd;
+import org.apache.cloudstack.api.user.volume.command.ExtractVolumeCmd;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 
@@ -40,7 +41,6 @@ import com.cloud.agent.api.storage.UploadCommand;
 import com.cloud.agent.api.storage.UploadProgressCommand;
 import com.cloud.agent.api.storage.UploadProgressCommand.RequestType;
 import org.apache.cloudstack.api.user.template.command.ExtractTemplateCmd;
-import com.cloud.api.commands.ExtractVolumeCmd;
 import com.cloud.api.response.ExtractResponse;
 import com.cloud.async.AsyncJobManager;
 import com.cloud.async.AsyncJobResult;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/server/src/com/cloud/vm/UserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index c8c6b51..eacf7f4 100755
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -33,6 +33,8 @@ import javax.naming.ConfigurationException;
 
 import org.apache.cloudstack.api.user.template.command.CreateTemplateCmd;
 import org.apache.cloudstack.api.user.vmgroup.command.DeleteVMGroupCmd;
+import org.apache.cloudstack.api.user.volume.command.AttachVolumeCmd;
+import org.apache.cloudstack.api.user.volume.command.DetachVolumeCmd;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.log4j.Logger;
 
@@ -61,11 +63,9 @@ import com.cloud.alert.AlertManager;
 import com.cloud.api.ApiDBUtils;
 import org.apache.cloudstack.api.BaseCmd;
 import org.apache.cloudstack.api.admin.vm.command.AssignVMCmd;
-import com.cloud.api.commands.AttachVolumeCmd;
 import org.apache.cloudstack.api.user.vmgroup.command.CreateVMGroupCmd;
 import org.apache.cloudstack.api.user.vm.command.DeployVMCmd;
 import org.apache.cloudstack.api.user.vm.command.DestroyVMCmd;
-import com.cloud.api.commands.DetachVolumeCmd;
 import org.apache.cloudstack.api.user.vm.command.ListVMsCmd;
 import org.apache.cloudstack.api.user.vm.command.RebootVMCmd;
 import org.apache.cloudstack.api.admin.vm.command.RecoverVMCmd;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/8773fc2a/server/test/com/cloud/vm/MockUserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vm/MockUserVmManagerImpl.java b/server/test/com/cloud/vm/MockUserVmManagerImpl.java
index 4573948..71e3871 100644
--- a/server/test/com/cloud/vm/MockUserVmManagerImpl.java
+++ b/server/test/com/cloud/vm/MockUserVmManagerImpl.java
@@ -29,13 +29,13 @@ import com.cloud.agent.api.to.NicTO;
 import com.cloud.agent.api.to.VirtualMachineTO;
 import com.cloud.agent.manager.Commands;
 import org.apache.cloudstack.api.admin.vm.command.AssignVMCmd;
-import com.cloud.api.commands.AttachVolumeCmd;
+import org.apache.cloudstack.api.user.volume.command.AttachVolumeCmd;
 import org.apache.cloudstack.api.user.template.command.CreateTemplateCmd;
 import org.apache.cloudstack.api.user.vmgroup.command.CreateVMGroupCmd;
 import org.apache.cloudstack.api.user.vmgroup.command.DeleteVMGroupCmd;
 import org.apache.cloudstack.api.user.vm.command.DeployVMCmd;
 import org.apache.cloudstack.api.user.vm.command.DestroyVMCmd;
-import com.cloud.api.commands.DetachVolumeCmd;
+import org.apache.cloudstack.api.user.volume.command.DetachVolumeCmd;
 import org.apache.cloudstack.api.user.vm.command.ListVMsCmd;
 import org.apache.cloudstack.api.user.vm.command.RebootVMCmd;
 import org.apache.cloudstack.api.admin.vm.command.RecoverVMCmd;