Skip to content

Commit 0b857de

Browse files
New feature: Import/Unamange DATA volume from storage pool (#8808)
1 parent 80adf5e commit 0b857de

File tree

32 files changed

+4440
-10
lines changed

32 files changed

+4440
-10
lines changed

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ public class EventTypes {
316316
public static final String EVENT_VOLUME_UPDATE = "VOLUME.UPDATE";
317317
public static final String EVENT_VOLUME_DESTROY = "VOLUME.DESTROY";
318318
public static final String EVENT_VOLUME_RECOVER = "VOLUME.RECOVER";
319+
public static final String EVENT_VOLUME_IMPORT = "VOLUME.IMPORT";
320+
public static final String EVENT_VOLUME_UNMANAGE = "VOLUME.UNMANAGE";
319321
public static final String EVENT_VOLUME_CHANGE_DISK_OFFERING = "VOLUME.CHANGE.DISK.OFFERING";
320322

321323
// Domains

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public class ApiConstants {
117117
public static final String CURRENT_START_IP = "currentstartip";
118118
public static final String CURRENT_END_IP = "currentendip";
119119
public static final String ENCRYPT = "encrypt";
120+
public static final String ENCRYPT_FORMAT = "encryptformat";
120121
public static final String ENCRYPT_ROOT = "encryptroot";
121122
public static final String ENCRYPTION_SUPPORTED = "encryptionsupported";
122123
public static final String MIN_IOPS = "miniops";
@@ -191,6 +192,7 @@ public class ApiConstants {
191192
public static final String FORMAT = "format";
192193
public static final String FOR_VIRTUAL_NETWORK = "forvirtualnetwork";
193194
public static final String FOR_SYSTEM_VMS = "forsystemvms";
195+
public static final String FULL_PATH = "fullpath";
194196
public static final String GATEWAY = "gateway";
195197
public static final String IP6_GATEWAY = "ip6gateway";
196198
public static final String GROUP = "group";
@@ -550,6 +552,7 @@ public class ApiConstants {
550552
public static final String ALLOCATION_STATE = "allocationstate";
551553
public static final String MANAGED_STATE = "managedstate";
552554
public static final String MANAGEMENT_SERVER_ID = "managementserverid";
555+
public static final String STORAGE = "storage";
553556
public static final String STORAGE_ID = "storageid";
554557
public static final String PING_STORAGE_SERVER_IP = "pingstorageserverip";
555558
public static final String PING_DIR = "pingdir";
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.api.command.admin.volume;
19+
20+
import com.cloud.event.EventTypes;
21+
import com.cloud.exception.ConcurrentOperationException;
22+
import com.cloud.exception.InsufficientCapacityException;
23+
import com.cloud.exception.NetworkRuleConflictException;
24+
import com.cloud.exception.ResourceAllocationException;
25+
import com.cloud.exception.ResourceUnavailableException;
26+
27+
import org.apache.cloudstack.acl.RoleType;
28+
import org.apache.cloudstack.api.APICommand;
29+
import org.apache.cloudstack.api.ApiConstants;
30+
import org.apache.cloudstack.api.BaseAsyncCmd;
31+
import org.apache.cloudstack.api.BaseCmd;
32+
import org.apache.cloudstack.api.Parameter;
33+
import org.apache.cloudstack.api.ResponseObject;
34+
import org.apache.cloudstack.api.ServerApiException;
35+
import org.apache.cloudstack.api.response.DiskOfferingResponse;
36+
import org.apache.cloudstack.api.response.DomainResponse;
37+
import org.apache.cloudstack.api.response.ProjectResponse;
38+
import org.apache.cloudstack.api.response.StoragePoolResponse;
39+
import org.apache.cloudstack.api.response.VolumeResponse;
40+
import org.apache.cloudstack.context.CallContext;
41+
import org.apache.cloudstack.storage.volume.VolumeImportUnmanageService;
42+
43+
import javax.inject.Inject;
44+
45+
@APICommand(name = "importVolume",
46+
description = "Import an unmanaged volume from a storage pool on a host into CloudStack",
47+
responseObject = VolumeResponse.class,
48+
responseView = ResponseObject.ResponseView.Full,
49+
requestHasSensitiveInfo = false,
50+
responseHasSensitiveInfo = true,
51+
authorized = {RoleType.Admin},
52+
since = "4.19.1")
53+
public class ImportVolumeCmd extends BaseAsyncCmd {
54+
55+
@Inject
56+
public VolumeImportUnmanageService volumeImportService;
57+
58+
/////////////////////////////////////////////////////
59+
//////////////// API parameters /////////////////////
60+
/////////////////////////////////////////////////////
61+
62+
63+
@Parameter(name = ApiConstants.PATH,
64+
type = BaseCmd.CommandType.STRING,
65+
required = true,
66+
description = "the path of the volume")
67+
private String path;
68+
69+
@Parameter(name = ApiConstants.NAME,
70+
type = BaseCmd.CommandType.STRING,
71+
description = "the name of the volume. If not set, it will be set to the path of the volume.")
72+
private String name;
73+
74+
@Parameter(name = ApiConstants.STORAGE_ID,
75+
type = BaseCmd.CommandType.UUID,
76+
required = true,
77+
entityType = StoragePoolResponse.class,
78+
description = "the ID of the storage pool")
79+
private Long storageId;
80+
81+
@Parameter(name = ApiConstants.DISK_OFFERING_ID,
82+
type = BaseCmd.CommandType.UUID,
83+
entityType = DiskOfferingResponse.class,
84+
description = "the ID of the disk offering linked to the volume")
85+
private Long diskOfferingId;
86+
87+
@Parameter(name = ApiConstants.ACCOUNT,
88+
type = BaseCmd.CommandType.STRING,
89+
description = "an optional account for the volume. Must be used with domainId.")
90+
private String accountName;
91+
92+
@Parameter(name = ApiConstants.DOMAIN_ID,
93+
type = BaseCmd.CommandType.UUID,
94+
entityType = DomainResponse.class,
95+
description = "import volume to the domain specified")
96+
private Long domainId;
97+
98+
@Parameter(name = ApiConstants.PROJECT_ID,
99+
type = BaseCmd.CommandType.UUID,
100+
entityType = ProjectResponse.class,
101+
description = "import volume for the project")
102+
private Long projectId;
103+
104+
/////////////////////////////////////////////////////
105+
/////////////////// Accessors ///////////////////////
106+
/////////////////////////////////////////////////////
107+
108+
public String getPath() {
109+
return path;
110+
}
111+
112+
public String getName() {
113+
return name;
114+
}
115+
116+
public Long getStorageId() {
117+
return storageId;
118+
}
119+
120+
public Long getDiskOfferingId() {
121+
return diskOfferingId;
122+
}
123+
124+
public String getAccountName() {
125+
return accountName;
126+
}
127+
128+
public Long getDomainId() {
129+
return domainId;
130+
}
131+
132+
public Long getProjectId() {
133+
return projectId;
134+
}
135+
136+
@Override
137+
public String getEventType() {
138+
return EventTypes.EVENT_VOLUME_IMPORT;
139+
}
140+
141+
@Override
142+
public String getEventDescription() {
143+
return String.format("Importing unmanaged Volume with path: %s", path);
144+
}
145+
146+
/////////////////////////////////////////////////////
147+
/////////////// API Implementation///////////////////
148+
/////////////////////////////////////////////////////
149+
150+
@Override
151+
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
152+
VolumeResponse response = volumeImportService.importVolume(this);
153+
response.setResponseName(getCommandName());
154+
setResponseObject(response);
155+
}
156+
157+
@Override
158+
public long getEntityOwnerId() {
159+
Long accountId = _accountService.finalyzeAccountId(accountName, domainId, projectId, true);
160+
if (accountId == null) {
161+
return CallContext.current().getCallingAccount().getId();
162+
}
163+
return accountId;
164+
}
165+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.api.command.admin.volume;
19+
20+
import com.cloud.exception.ConcurrentOperationException;
21+
import com.cloud.exception.InsufficientCapacityException;
22+
import com.cloud.exception.NetworkRuleConflictException;
23+
import com.cloud.exception.ResourceAllocationException;
24+
import com.cloud.exception.ResourceUnavailableException;
25+
import org.apache.cloudstack.acl.RoleType;
26+
import org.apache.cloudstack.api.APICommand;
27+
import org.apache.cloudstack.api.ApiConstants;
28+
import org.apache.cloudstack.api.BaseCmd;
29+
import org.apache.cloudstack.api.BaseListCmd;
30+
import org.apache.cloudstack.api.Parameter;
31+
import org.apache.cloudstack.api.ResponseObject;
32+
import org.apache.cloudstack.api.ServerApiException;
33+
import org.apache.cloudstack.api.response.ListResponse;
34+
import org.apache.cloudstack.api.response.StoragePoolResponse;
35+
import org.apache.cloudstack.api.response.VolumeForImportResponse;
36+
import org.apache.cloudstack.storage.volume.VolumeImportUnmanageService;
37+
import org.apache.cloudstack.storage.volume.VolumeOnStorageTO;
38+
39+
import javax.inject.Inject;
40+
41+
@APICommand(name = "listVolumesForImport",
42+
description = "Lists unmanaged volumes on a storage pool",
43+
responseObject = VolumeForImportResponse.class,
44+
responseView = ResponseObject.ResponseView.Full,
45+
entityType = {VolumeOnStorageTO.class},
46+
requestHasSensitiveInfo = false,
47+
responseHasSensitiveInfo = true,
48+
authorized = {RoleType.Admin},
49+
since = "4.19.1")
50+
public class ListVolumesForImportCmd extends BaseListCmd {
51+
52+
@Inject
53+
public VolumeImportUnmanageService volumeImportService;
54+
55+
/////////////////////////////////////////////////////
56+
//////////////// API parameters /////////////////////
57+
/////////////////////////////////////////////////////
58+
59+
@Parameter(name = ApiConstants.STORAGE_ID,
60+
type = BaseCmd.CommandType.UUID,
61+
required = true,
62+
entityType = StoragePoolResponse.class,
63+
description = "the ID of the storage pool")
64+
private Long storageId;
65+
66+
@Parameter(name = ApiConstants.PATH,
67+
type = BaseCmd.CommandType.STRING,
68+
description = "the path of the volume on the storage pool")
69+
private String path;
70+
71+
/////////////////////////////////////////////////////
72+
/////////////////// Accessors ///////////////////////
73+
/////////////////////////////////////////////////////
74+
75+
public Long getStorageId() {
76+
return storageId;
77+
}
78+
79+
public String getPath() {
80+
return path;
81+
}
82+
83+
/////////////////////////////////////////////////////
84+
/////////////// API Implementation///////////////////
85+
/////////////////////////////////////////////////////
86+
87+
@Override
88+
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
89+
ListResponse<VolumeForImportResponse> response = volumeImportService.listVolumesForImport(this);
90+
response.setResponseName(getCommandName());
91+
setResponseObject(response);
92+
}
93+
}

0 commit comments

Comments
 (0)