weizhouapache commented on code in PR #8875: URL: https://github.com/apache/cloudstack/pull/8875#discussion_r1608252968
########## api/src/main/java/org/apache/cloudstack/api/response/VirtualMachineResponse.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.response; + +import org.apache.cloudstack.api.BaseResponse; +import org.apache.cloudstack.api.EntityReference; + +import com.cloud.serializer.Param; +import com.cloud.vm.VirtualMachine; +import com.google.gson.annotations.SerializedName; + +@EntityReference(value = VirtualMachine.class) +public class VirtualMachineResponse extends BaseResponse { + @SerializedName("id") + @Param(description = "the ID of the VM") + private String id; + + @SerializedName("type") + @Param(description = "the type of VM") + private String type; + + @SerializedName("name") + @Param(description = "the name of the VM") + private String name; + + @SerializedName("clusterid") + @Param(description = "the Pod ID for the system VM") Review Comment: ``` @Param(description = "the Cluster ID for the VM instance") ``` ########## api/src/main/java/org/apache/cloudstack/api/command/admin/vm/FindAffectedVmsForStorageScopeChangeCmd.java: ########## @@ -0,0 +1,77 @@ +/* + * 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.vm; + +import org.apache.cloudstack.acl.RoleType; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants; +import org.apache.cloudstack.api.BaseListCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.response.ClusterResponse; +import org.apache.cloudstack.api.response.ListResponse; +import org.apache.cloudstack.api.response.StoragePoolResponse; +import org.apache.cloudstack.api.response.VirtualMachineResponse; + +import com.cloud.vm.VirtualMachine; + +@APICommand(name = "findAffectedVmsForStorageScopeChange", + description = "List user and system VMs that need to be stopped and destroyed respectively for changing the scope of the storage pool from Zone to Cluster.", + responseObject = VirtualMachineResponse.class, + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, since = "4.19.1", + authorized = {RoleType.Admin}) +public class FindAffectedVmsForStorageScopeChangeCmd extends BaseListCmd { Review Comment: normally the APIs to list resources start with `list`, except `FindHostsForMigrationCmd` and `FindStoragePoolsForMigrationCmd` it may be better to use "List" instead of "Find" (most cases `find` returns only 0 or 1 record). it is a trivial issue if use 'find' (ok to me) ########## api/src/main/java/org/apache/cloudstack/api/response/VirtualMachineResponse.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.response; + +import org.apache.cloudstack.api.BaseResponse; +import org.apache.cloudstack.api.EntityReference; + +import com.cloud.serializer.Param; +import com.cloud.vm.VirtualMachine; +import com.google.gson.annotations.SerializedName; + +@EntityReference(value = VirtualMachine.class) +public class VirtualMachineResponse extends BaseResponse { + @SerializedName("id") + @Param(description = "the ID of the VM") + private String id; + + @SerializedName("type") + @Param(description = "the type of VM") + private String type; + + @SerializedName("name") + @Param(description = "the name of the VM") + private String name; + + @SerializedName("clusterid") + @Param(description = "the Pod ID for the system VM") + private String clusterId; + + @SerializedName("clustername") + @Param(description = "the Pod name for the system VM", since = "4.13.2") Review Comment: the description/since need to be updated as well ########## server/src/main/java/com/cloud/storage/StorageManagerImpl.java: ########## @@ -1211,10 +1211,13 @@ private void changeStoragePoolScopeToCluster(StoragePoolVO primaryStorage, Long List<VirtualMachine.State> states = Arrays.asList(State.Starting, State.Running, State.Stopping, State.Migrating, State.Restoring); Long id = primaryStorage.getId(); - List<VolumeVO> volumesInOtherClusters = volumeDao.listByPoolIdVMStatesNotInCluster(clusterId, states, id); - if (volumesInOtherClusters.size() > 0) { - throw new CloudRuntimeException(String.format("Cannot change scope of the pool %s to cluster %s as there are VMs running on other clusters which are using volumes on this storage pool", - primaryStorage.getName(), clusterVO.getName())); + Pair<List<VMInstanceVO>, Integer> vmsNotInClusterUsingPool = _vmInstanceDao.listByVmsNotInClusterUsingPool(clusterId, id); + if (vmsNotInClusterUsingPool.second() != 0) { + throw new CloudRuntimeException(String.format("Cannot change scope of the storage pool [%s] to cluster [%s] %s %s %s", + primaryStorage.getName(), clusterVO.getName(), + "as there are VMs with volumes in this pool that are running on other clusters.", + "All such User VMs must be stopped and System VMs must be destroyed before proceeding.", + "Please use the API findAffectedVmsForStorageScopeChange to get the list.")); } Review Comment: we can add the number of affected VMs in the response ########## server/src/main/java/com/cloud/api/query/QueryManagerImpl.java: ########## @@ -1147,6 +1155,41 @@ public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) { return response; } + @Override + public ListResponse<VirtualMachineResponse> findAffectedVmsForStorageScopeChange(FindAffectedVmsForStorageScopeChangeCmd cmd) { + Long poolId = cmd.getStorageId(); + StoragePoolVO pool = storagePoolDao.findById(poolId); + if (pool == null) { + throw new IllegalArgumentException("Unable to find storage pool with ID: " + poolId); + } + if (!pool.getStatus().equals(StoragePoolStatus.Disabled)) { + throw new InvalidParameterValueException("Scope change of Storage pool is only allowed in Disabled state"); + } + ListResponse<VirtualMachineResponse> response = new ListResponse<>(); + if (pool.getScope() != ScopeType.ZONE) { Review Comment: if storage scope is Zone, throw an exception or add a debug/info message, both are fine to me. ########## api/src/main/java/org/apache/cloudstack/api/response/VirtualMachineResponse.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.response; + +import org.apache.cloudstack.api.BaseResponse; +import org.apache.cloudstack.api.EntityReference; + +import com.cloud.serializer.Param; +import com.cloud.vm.VirtualMachine; +import com.google.gson.annotations.SerializedName; + +@EntityReference(value = VirtualMachine.class) +public class VirtualMachineResponse extends BaseResponse { + @SerializedName("id") + @Param(description = "the ID of the VM") + private String id; + + @SerializedName("type") + @Param(description = "the type of VM") + private String type; + + @SerializedName("name") + @Param(description = "the name of the VM") + private String name; + + @SerializedName("clusterid") + @Param(description = "the Pod ID for the system VM") + private String clusterId; + + @SerializedName("clustername") + @Param(description = "the Pod name for the system VM", since = "4.13.2") + private String clusterName; + + @SerializedName("hostid") + @Param(description = "the host ID for the system VM") + private String hostId; + + @SerializedName("hostname") + @Param(description = "the hostname for the system VM") + private String hostName; + Review Comment: would it be good to add more info the response ? but it could lead to some duplications as the UserVmResponse and SystemVmResponse have lots of information. let's keep it simple like this. If user wants to know more information, use other APIs (listVirtualMachines or listSystemVMs) ########## server/src/main/java/com/cloud/api/query/QueryManagerImpl.java: ########## @@ -1147,6 +1155,41 @@ public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) { return response; } + @Override + public ListResponse<VirtualMachineResponse> findAffectedVmsForStorageScopeChange(FindAffectedVmsForStorageScopeChangeCmd cmd) { + Long poolId = cmd.getStorageId(); + StoragePoolVO pool = storagePoolDao.findById(poolId); + if (pool == null) { + throw new IllegalArgumentException("Unable to find storage pool with ID: " + poolId); + } + if (!pool.getStatus().equals(StoragePoolStatus.Disabled)) { + throw new InvalidParameterValueException("Scope change of Storage pool is only allowed in Disabled state"); + } + ListResponse<VirtualMachineResponse> response = new ListResponse<>(); + if (pool.getScope() != ScopeType.ZONE) { + response.setResponses(null, 0); + return response; + } + + Pair<List<VMInstanceVO>, Integer> vms = _vmInstanceDao.listByVmsNotInClusterUsingPool(cmd.getClusterIdForScopeChange(), poolId); + List<VirtualMachineResponse> responsesList = new ArrayList<>(); + for (VMInstanceVO vm : vms.first()) { + VirtualMachineResponse resp = new VirtualMachineResponse(); + resp.setObjectName(VirtualMachine.class.getSimpleName().toLowerCase()); + resp.setId(vm.getUuid()); + resp.setVmType(vm.getType().toString()); + HostVO host = hostDao.findById(vm.getHostId()); + resp.setHostId(host.getUuid()); Review Comment: add null check on host and cluster ? ########## server/src/main/java/com/cloud/api/query/QueryManagerImpl.java: ########## @@ -1147,6 +1155,41 @@ public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) { return response; } + @Override + public ListResponse<VirtualMachineResponse> findAffectedVmsForStorageScopeChange(FindAffectedVmsForStorageScopeChangeCmd cmd) { + Long poolId = cmd.getStorageId(); + StoragePoolVO pool = storagePoolDao.findById(poolId); + if (pool == null) { + throw new IllegalArgumentException("Unable to find storage pool with ID: " + poolId); + } + if (!pool.getStatus().equals(StoragePoolStatus.Disabled)) { + throw new InvalidParameterValueException("Scope change of Storage pool is only allowed in Disabled state"); + } Review Comment: the check on `pool.getStatus()` may be unnecessary. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
