andrijapanicsb commented on code in PR #14256:
URL: https://github.com/apache/cloudstack/pull/14256#discussion_r4199062073


##########
plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorImportHelper.java:
##########
@@ -0,0 +1,201 @@
+// 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.storage.datastore.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.cloud.hypervisor.Hypervisor;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.linbit.linstor.api.ApiException;
+import com.linbit.linstor.api.DevelopersApi;
+import com.linbit.linstor.api.model.Node;
+import com.linbit.linstor.api.model.ResourceDefinition;
+import com.linbit.linstor.api.model.ResourceWithVolumes;
+import com.linbit.linstor.api.model.VolumeDefinition;
+import org.apache.cloudstack.storage.volume.VolumeOnStorageTO;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Import validation must not change resource placement or live-migration 
properties.
+ * Both single-volume inspection and listing use controller metadata, never 
local DRBD devices.
+ */
+public final class LinstorImportHelper {
+    static final int PAGE_SIZE = 1000;
+
+    private LinstorImportHelper() {
+    }
+
+    private static String resourceName(String path) {
+        // CloudStack volume paths are the resource name without the cs- 
prefix, not device paths.
+        if (StringUtils.isBlank(path) || !path.matches("[a-zA-Z0-9_-]+")) {
+            throw new CloudRuntimeException("Invalid LINSTOR volume path: 
expected a resource name without a device path");
+        }
+        return LinstorUtil.RSC_PREFIX + path;
+    }
+
+    private static void requireGroup(String group) {
+        if (StringUtils.isBlank(group)) {
+            throw new CloudRuntimeException("Cannot validate LINSTOR import: 
the storage pool has no resource group");
+        }
+    }
+
+    private static List<ResourceDefinition> definitions(DevelopersApi api, 
String path) throws ApiException {
+        List<String> names = path == null ? Collections.emptyList() : 
Collections.singletonList(resourceName(path));
+        List<ResourceDefinition> result = new ArrayList<>();
+        for (int offset = 0; ; offset += PAGE_SIZE) {
+            List<ResourceDefinition> page = api.resourceDefinitionList(names, 
true, null, PAGE_SIZE, offset);
+            if (page == null) {
+                throw new CloudRuntimeException("Cannot read LINSTOR resource 
definitions");
+            }
+            result.addAll(page);
+            if (page.size() < PAGE_SIZE) {
+                return result;
+            }
+        }
+    }
+
+    private static ResourceDefinition findDefinition(List<ResourceDefinition> 
definitions, String path) {
+        String name = resourceName(path);
+        return definitions.stream().filter(rd -> 
name.equals(rd.getName())).findFirst()
+                .orElseThrow(() -> new CloudRuntimeException("LINSTOR resource 
not found: " + name));
+    }
+
+    private static void checkGroup(ResourceDefinition definition, String 
group) {
+        if (!group.equalsIgnoreCase(definition.getResourceGroupName())) {
+            throw new CloudRuntimeException(String.format(
+                    "Cannot import resource %s: it belongs to resource group 
%s, but the selected storage pool uses %s",
+                    definition.getName(), definition.getResourceGroupName(), 
group));
+        }
+    }
+
+    public static void validateResourceGroup(DevelopersApi api, String group, 
String path) {
+        requireGroup(group);
+        try {
+            checkGroup(findDefinition(definitions(api, path), path), group);
+        } catch (ApiException e) {
+            throw new CloudRuntimeException("Cannot verify LINSTOR resource 
ownership: " + e.getBestMessage(), e);
+        }
+    }
+
+    private static List<ResourceWithVolumes> resources(DevelopersApi api, 
String path) throws ApiException {
+        List<String> names = path == null ? Collections.emptyList() : 
Collections.singletonList(resourceName(path));
+        List<ResourceWithVolumes> result = new ArrayList<>();
+        for (int offset = 0; ; offset += PAGE_SIZE) {
+            List<ResourceWithVolumes> page = 
api.viewResources(Collections.emptyList(), names,
+                    Collections.emptyList(), null, PAGE_SIZE, offset);
+            if (page == null) {
+                throw new CloudRuntimeException("Cannot read LINSTOR resource 
usage");
+            }
+            result.addAll(page);
+            if (page.size() < PAGE_SIZE) {
+                return result;
+            }
+        }
+    }
+
+    private static boolean unavailable(List<String> flags) {
+        return flags != null && flags.stream().anyMatch(flag ->
+                "DELETE".equals(flag) || "DELETING".equals(flag) || 
"INACTIVE".equals(flag));
+    }
+
+    private static Set<String> onlineNodes(DevelopersApi api) throws 
ApiException {
+        Set<String> result = new HashSet<>();
+        for (int offset = 0; ; offset += PAGE_SIZE) {
+            List<Node> page = api.nodeList(Collections.emptyList(), 
Collections.emptyList(), PAGE_SIZE, offset);

Review Comment:
   fixed in ce349d813e



##########
plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorImportHelper.java:
##########
@@ -0,0 +1,201 @@
+// 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.storage.datastore.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.cloud.hypervisor.Hypervisor;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.linbit.linstor.api.ApiException;
+import com.linbit.linstor.api.DevelopersApi;
+import com.linbit.linstor.api.model.Node;
+import com.linbit.linstor.api.model.ResourceDefinition;
+import com.linbit.linstor.api.model.ResourceWithVolumes;
+import com.linbit.linstor.api.model.VolumeDefinition;
+import org.apache.cloudstack.storage.volume.VolumeOnStorageTO;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Import validation must not change resource placement or live-migration 
properties.
+ * Both single-volume inspection and listing use controller metadata, never 
local DRBD devices.
+ */
+public final class LinstorImportHelper {
+    static final int PAGE_SIZE = 1000;
+
+    private LinstorImportHelper() {
+    }
+
+    private static String resourceName(String path) {
+        // CloudStack volume paths are the resource name without the cs- 
prefix, not device paths.
+        if (StringUtils.isBlank(path) || !path.matches("[a-zA-Z0-9_-]+")) {
+            throw new CloudRuntimeException("Invalid LINSTOR volume path: 
expected a resource name without a device path");
+        }
+        return LinstorUtil.RSC_PREFIX + path;
+    }
+
+    private static void requireGroup(String group) {
+        if (StringUtils.isBlank(group)) {
+            throw new CloudRuntimeException("Cannot validate LINSTOR import: 
the storage pool has no resource group");
+        }
+    }
+
+    private static List<ResourceDefinition> definitions(DevelopersApi api, 
String path) throws ApiException {
+        List<String> names = path == null ? Collections.emptyList() : 
Collections.singletonList(resourceName(path));
+        List<ResourceDefinition> result = new ArrayList<>();
+        for (int offset = 0; ; offset += PAGE_SIZE) {
+            List<ResourceDefinition> page = api.resourceDefinitionList(names, 
true, null, PAGE_SIZE, offset);
+            if (page == null) {
+                throw new CloudRuntimeException("Cannot read LINSTOR resource 
definitions");
+            }
+            result.addAll(page);
+            if (page.size() < PAGE_SIZE) {
+                return result;
+            }
+        }
+    }
+
+    private static ResourceDefinition findDefinition(List<ResourceDefinition> 
definitions, String path) {
+        String name = resourceName(path);
+        return definitions.stream().filter(rd -> 
name.equals(rd.getName())).findFirst()
+                .orElseThrow(() -> new CloudRuntimeException("LINSTOR resource 
not found: " + name));
+    }
+
+    private static void checkGroup(ResourceDefinition definition, String 
group) {
+        if (!group.equalsIgnoreCase(definition.getResourceGroupName())) {
+            throw new CloudRuntimeException(String.format(
+                    "Cannot import resource %s: it belongs to resource group 
%s, but the selected storage pool uses %s",
+                    definition.getName(), definition.getResourceGroupName(), 
group));
+        }
+    }
+
+    public static void validateResourceGroup(DevelopersApi api, String group, 
String path) {
+        requireGroup(group);
+        try {
+            checkGroup(findDefinition(definitions(api, path), path), group);
+        } catch (ApiException e) {
+            throw new CloudRuntimeException("Cannot verify LINSTOR resource 
ownership: " + e.getBestMessage(), e);
+        }
+    }
+
+    private static List<ResourceWithVolumes> resources(DevelopersApi api, 
String path) throws ApiException {
+        List<String> names = path == null ? Collections.emptyList() : 
Collections.singletonList(resourceName(path));
+        List<ResourceWithVolumes> result = new ArrayList<>();
+        for (int offset = 0; ; offset += PAGE_SIZE) {
+            List<ResourceWithVolumes> page = 
api.viewResources(Collections.emptyList(), names,
+                    Collections.emptyList(), null, PAGE_SIZE, offset);

Review Comment:
   fixed in ce349d813e



-- 
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]

Reply via email to