Skip to content

Commit

Permalink
New feature: Import volume from storage pool
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Mar 20, 2024
1 parent 33dc746 commit 8b4b915
Show file tree
Hide file tree
Showing 22 changed files with 1,737 additions and 8 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ public class EventTypes {
public static final String EVENT_VOLUME_UPDATE = "VOLUME.UPDATE";
public static final String EVENT_VOLUME_DESTROY = "VOLUME.DESTROY";
public static final String EVENT_VOLUME_RECOVER = "VOLUME.RECOVER";
public static final String EVENT_VOLUME_IMPORT = "VOLUME.IMPORT";
public static final String EVENT_VOLUME_UNMANAGE = "VOLUME.UNMANAGE";
public static final String EVENT_VOLUME_CHANGE_DISK_OFFERING = "VOLUME.CHANGE.DISK.OFFERING";

// Domains
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public class ApiConstants {
public static final String CURRENT_START_IP = "currentstartip";
public static final String CURRENT_END_IP = "currentendip";
public static final String ENCRYPT = "encrypt";
public static final String ENCRYPT_FORMAT = "encryptformat";
public static final String ENCRYPT_ROOT = "encryptroot";
public static final String ENCRYPTION_SUPPORTED = "encryptionsupported";
public static final String MIN_IOPS = "miniops";
Expand Down Expand Up @@ -191,6 +192,7 @@ public class ApiConstants {
public static final String FORMAT = "format";
public static final String FOR_VIRTUAL_NETWORK = "forvirtualnetwork";
public static final String FOR_SYSTEM_VMS = "forsystemvms";
public static final String FULL_PATH = "fullpath";
public static final String GATEWAY = "gateway";
public static final String IP6_GATEWAY = "ip6gateway";
public static final String GROUP = "group";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// 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.admin.volume;

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.user.Account;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DiskOfferingResponse;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ProjectResponse;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import org.apache.cloudstack.api.response.VolumeResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.storage.volume.VolumeImportUnmanageService;

import javax.inject.Inject;

@APICommand(name = "importVolume",
description = "Import an unmanaged volume from a storage pool on a host into CloudStack",
responseObject = VolumeResponse.class,
responseView = ResponseObject.ResponseView.Full,
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = true,
authorized = {RoleType.Admin},
since = "4.19.1")
public class ImportVolumeCmd extends BaseAsyncCmd {

@Inject
public VolumeImportUnmanageService volumeImportService;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////


@Parameter(name = ApiConstants.PATH,
type = BaseCmd.CommandType.STRING,
required = true,
description = "the path of the volume")
private String path;

@Parameter(name = ApiConstants.STORAGE_ID,
type = BaseCmd.CommandType.UUID,
required = true,
entityType = StoragePoolResponse.class,
description = "the ID of the storage pool")
private Long storageId;

@Parameter(name = ApiConstants.DISK_OFFERING_ID,
type = BaseCmd.CommandType.UUID,
entityType = DiskOfferingResponse.class,
description = "the ID of the disk offering linked to the volume")
private Long diskOfferingId;

@Parameter(name = ApiConstants.ACCOUNT,
type = BaseCmd.CommandType.STRING,
description = "an optional account for the virtual machine. Must be used with domainId.")
private String accountName;

@Parameter(name = ApiConstants.DOMAIN_ID,
type = BaseCmd.CommandType.UUID,
entityType = DomainResponse.class,
description = "import instance to the domain specified")
private Long domainId;

@Parameter(name = ApiConstants.PROJECT_ID,
type = BaseCmd.CommandType.UUID,
entityType = ProjectResponse.class,
description = "import instance for the project")
private Long projectId;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Long getStorageId() {
return storageId;
}

public void setStorageId(Long storageId) {
this.storageId = storageId;
}

public Long getDiskOfferingId() {
return diskOfferingId;
}

public void setDiskOfferingId(Long diskOfferingId) {
this.diskOfferingId = diskOfferingId;
}

public String getAccountName() {
return accountName;
}

public void setAccountName(String accountName) {
this.accountName = accountName;
}

public Long getDomainId() {
return domainId;
}

public void setDomainId(Long domainId) {
this.domainId = domainId;
}

public Long getProjectId() {
return projectId;
}

public void setProjectId(Long projectId) {
this.projectId = projectId;
}

@Override
public String getEventType() {
return EventTypes.EVENT_VOLUME_IMPORT;
}

@Override
public String getEventDescription() {
return String.format("Importing unmanaged Volume with path: %s", path);
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
VolumeResponse response = volumeImportService.importVolume(this);
response.setResponseName(getCommandName());
setResponseObject(response);
}

@Override
public long getEntityOwnerId() {
Account caller = CallContext.current().getCallingAccount();
return caller.getAccountId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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.admin.volume;

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.user.Account;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import org.apache.cloudstack.api.response.VolumeForImportResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.storage.volume.VolumeImportUnmanageService;
import org.apache.cloudstack.storage.volume.VolumeOnStorageTO;

import javax.inject.Inject;

@APICommand(name = "listVolumesForImport",
description = "Lists unmanaged volumes on a storage pool",
responseObject = VolumeForImportResponse.class,
responseView = ResponseObject.ResponseView.Full,
entityType = {VolumeOnStorageTO.class},
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = true,
authorized = {RoleType.Admin},
since = "4.19.1")
public class ListVolumesForImportCmd extends BaseListCmd {

@Inject
public VolumeImportUnmanageService volumeImportService;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////

@Parameter(name = ApiConstants.STORAGE_ID,
type = BaseCmd.CommandType.UUID,
required = true,
entityType = StoragePoolResponse.class,
description = "the ID of the storage pool")
private Long storageId;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public Long getStorageId() {
return storageId;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
ListResponse<VolumeForImportResponse> response = volumeImportService.listVolumesForImport(this);
response.setResponseName(getCommandName());
setResponseObject(response);
}

@Override
public long getEntityOwnerId() {
Account account = CallContext.current().getCallingAccount();
if (account != null) {
return account.getId();
}
return Account.ACCOUNT_ID_SYSTEM;
}
}
Loading

0 comments on commit 8b4b915

Please sign in to comment.