vishesh92 commented on code in PR #7659:
URL: https://github.com/apache/cloudstack/pull/7659#discussion_r1375769078


##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/heuristics/CreateSecondaryStorageSelectorCmd.java:
##########
@@ -0,0 +1,92 @@
+// 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.storage.heuristics;
+
+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.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.SecondaryStorageHeuristicsResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.secstorage.heuristics.Heuristic;
+
+import static 
org.apache.cloudstack.api.ApiConstants.HEURISTIC_PURPOSE_VALID_OPTIONS;
+
+@APICommand(name = "createSecondaryStorageSelector", description = "Creates a 
secondary storage selector, described by the heuristic rule.", responseObject =
+        SecondaryStorageHeuristicsResponse.class, entityType = 
{Heuristic.class}, requestHasSensitiveInfo = false, responseHasSensitiveInfo = 
false, authorized = {RoleType.Admin})
+public class CreateSecondaryStorageSelectorCmd extends BaseCmd{
+
+    @Parameter(name = ApiConstants.NAME, required = true, type = 
CommandType.STRING, description = "The name identifying the heuristic rule.")
+    private String name;
+
+    @Parameter(name = ApiConstants.DESCRIPTION, required = true, type = 
BaseCmd.CommandType.STRING, description = "The description of the heuristic 
rule.")
+    private String description;
+
+    @Parameter(name = ApiConstants.ZONE_ID, required = true, entityType = 
ZoneResponse.class, type = BaseCmd.CommandType.UUID, description = "The zone in 
which the heuristic " +
+            "rule will be applied.")
+    private Long zoneId;
+
+    @Parameter(name = ApiConstants.PURPOSE, required = true, type = 
BaseCmd.CommandType.STRING, description =

Review Comment:
   IMO, type would be a better parameter name here.



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/heuristics/RemoveSecondaryStorageSelectorCmd.java:
##########
@@ -0,0 +1,54 @@
+// 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.storage.heuristics;
+
+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.Parameter;
+import org.apache.cloudstack.api.response.SecondaryStorageHeuristicsResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.secstorage.heuristics.Heuristic;
+
+@APICommand(name = "removeSecondaryStorageSelector", description = "Removes an 
existing secondary storage selector.", responseObject = 
SecondaryStorageHeuristicsResponse.class,

Review Comment:
   Add
   ```java
   since = "4.19.0",
   ```



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/heuristics/ListSecondaryStorageSelectorsCmd.java:
##########
@@ -0,0 +1,63 @@
+// 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.storage.heuristics;
+
+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.ListResponse;
+import org.apache.cloudstack.api.response.SecondaryStorageHeuristicsResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import org.apache.cloudstack.secstorage.heuristics.Heuristic;
+
+import static 
org.apache.cloudstack.api.ApiConstants.HEURISTIC_PURPOSE_VALID_OPTIONS;
+
+@APICommand(name = "listSecondaryStorageSelectors", description = "Lists the 
secondary storage selectors and their rules.", responseObject = 
SecondaryStorageHeuristicsResponse.class,

Review Comment:
   Add
   ```java
   since = "4.19.0",
   ```



##########
engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/ImageStoreDaoImpl.java:
##########
@@ -191,4 +198,12 @@ public ImageStoreVO findOneByZoneAndProtocol(long 
dataCenterId, String protocol)
         List<ImageStoreVO> results =  listBy(sc, filter);
         return results.size() == 0 ? null : results.get(0);
     }
+
+
+    @Override
+    public List<ImageStoreVO> listImageStoresByZoneIds(Long... zoneIds) {
+        SearchCriteria<ImageStoreVO> sc = zonesInSearch.create();
+        sc.setParametersIfNotNull("zonesIn" ,zoneIds);

Review Comment:
   ```suggestion
           sc.setParametersIfNotNull("zonesIn", zoneIds);
   ```



##########
engine/schema/src/main/java/org/apache/cloudstack/secstorage/HeuristicVO.java:
##########
@@ -0,0 +1,127 @@
+// 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.secstorage;
+
+import com.cloud.utils.db.GenericDao;
+import org.apache.cloudstack.secstorage.heuristics.Heuristic;
+import 
org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import java.util.Date;
+import java.util.UUID;
+
+@Entity
+@Table(name = "heuristics")
+public class HeuristicVO implements Heuristic {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Long id;
+
+    @Column(name = "uuid", nullable = false)
+    private String uuid = UUID.randomUUID().toString();
+
+    @Column(name = "name")
+    private String name;
+
+    @Column(name = "description")
+    private String description;
+
+    @Column(name = "zone_id", nullable = false)
+    private Long zoneId;
+
+    @Column(name = "purpose", nullable = false)
+    private String purpose;
+
+    @Column(name = "heuristic_rule", nullable = false, length = 65535)
+    private String heuristicRule;
+
+    @Column(name = GenericDao.CREATED_COLUMN, nullable = false)
+    @Temporal(value = TemporalType.TIMESTAMP)
+    private Date created;
+
+    @Column(name = GenericDao.REMOVED_COLUMN)
+    @Temporal(value = TemporalType.TIMESTAMP)
+    private Date removed = null;
+
+    public HeuristicVO() {
+    }
+
+    public HeuristicVO(String name, String description, Long zoneId, String 
purpose, String heuristicRule, Date created, Date removed) {
+        this.name = name;
+        this.description = description;
+        this.zoneId = zoneId;
+        this.purpose = purpose;
+        this.heuristicRule = heuristicRule;
+        this.created = created;

Review Comment:
   Do we need `created` and `removed` in the constructor? AFAIK, `created` is 
populated by default on resource creation. And I don't think we will ever be 
creating an object which is `removed` from the beginning.



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/heuristics/CreateSecondaryStorageSelectorCmd.java:
##########
@@ -0,0 +1,92 @@
+// 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.storage.heuristics;
+
+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.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.SecondaryStorageHeuristicsResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.secstorage.heuristics.Heuristic;
+
+import static 
org.apache.cloudstack.api.ApiConstants.HEURISTIC_PURPOSE_VALID_OPTIONS;
+
+@APICommand(name = "createSecondaryStorageSelector", description = "Creates a 
secondary storage selector, described by the heuristic rule.", responseObject =

Review Comment:
   Add
   ```java
   since = "4.19.0",
   ```



##########
server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Account.java:
##########
@@ -0,0 +1,41 @@
+// 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.heuristics.presetvariables;
+
+public class Account extends GenericHeuristicPresetVariable{

Review Comment:
   ```suggestion
   public class Account extends GenericHeuristicPresetVariable {
   ```



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/heuristics/UpdateSecondaryStorageSelectorCmd.java:
##########
@@ -0,0 +1,66 @@
+// 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.storage.heuristics;
+
+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.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SecondaryStorageHeuristicsResponse;
+import org.apache.cloudstack.secstorage.heuristics.Heuristic;
+
+@APICommand(name = "updateSecondaryStorageSelector", description = "Updates an 
existing secondary storage selector.", responseObject = 
SecondaryStorageHeuristicsResponse.class,

Review Comment:
   Add
   ```java
   since = "4.19.0",
   ```



##########
server/src/main/java/com/cloud/api/ApiResponseHelper.java:
##########
@@ -5088,4 +5090,22 @@ public FirewallResponse 
createIpv6FirewallRuleResponse(FirewallRule fwRule) {
         response.setObjectName("firewallrule");
         return response;
     }
+
+    @Override
+    public SecondaryStorageHeuristicsResponse 
createSecondaryStorageSelectorResponse(Heuristic heuristic) {
+        SecondaryStorageHeuristicsResponse secondaryStorageHeuristicsResponse 
= new SecondaryStorageHeuristicsResponse();
+        String zoneUuid = 
ApiDBUtils.findZoneById(heuristic.getZoneId()).getUuid();
+
+        secondaryStorageHeuristicsResponse.setId(heuristic.getUuid());

Review Comment:
   Directly use the constructor here to create the response object.



##########
engine/schema/src/main/java/org/apache/cloudstack/secstorage/dao/SecondaryStorageHeuristicDaoImpl.java:
##########
@@ -0,0 +1,50 @@
+// 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.secstorage.dao;
+
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import org.apache.cloudstack.secstorage.HeuristicVO;
+import org.apache.cloudstack.secstorage.heuristics.HeuristicPurpose;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+@Component
+public class SecondaryStorageHeuristicDaoImpl extends 
GenericDaoBase<HeuristicVO, Long> implements SecondaryStorageHeuristicDao {
+    private SearchBuilder<HeuristicVO> zoneAndPurposeSearch;
+
+    @PostConstruct
+    public void init() {
+        zoneAndPurposeSearch = createSearchBuilder();
+        zoneAndPurposeSearch.and("zoneId", 
zoneAndPurposeSearch.entity().getZoneId(), SearchCriteria.Op.EQ);
+        zoneAndPurposeSearch.and("purpose", 
zoneAndPurposeSearch.entity().getPurpose(), SearchCriteria.Op.IN);
+        zoneAndPurposeSearch.done();
+    }
+
+    @Override
+    public HeuristicVO findByZoneIdAndPurpose(long zoneId, HeuristicPurpose 
purpose) {
+        SearchCriteria<HeuristicVO> searchCriteria = 
zoneAndPurposeSearch.create();
+        searchCriteria.setParameters("zoneId", zoneId);
+        searchCriteria.setParameters("purpose", purpose.toString());
+        final Filter filter = new Filter(HeuristicVO.class, "created", false);
+
+        return findOneIncludingRemovedBy(searchCriteria, filter);

Review Comment:
   Why do we need to get removed entries here?



##########
server/src/main/java/com/cloud/storage/StorageManagerImpl.java:
##########
@@ -1882,6 +1894,65 @@ public StoragePool syncStoragePool(SyncStoragePoolCmd 
cmd) {
         return (PrimaryDataStoreInfo) _dataStoreMgr.getDataStore(pool.getId(), 
DataStoreRole.Primary);
     }
 
+
+    @Override
+    public Heuristic 
createSecondaryStorageHeuristic(CreateSecondaryStorageSelectorCmd cmd) {
+        String name = cmd.getName();
+        String description = cmd.getDescription();
+        long zoneId = cmd.getZoneId();
+        String heuristicRule = cmd.getHeuristicRule();
+        String purpose = cmd.getPurpose();
+        HeuristicPurpose formattedPurpose = 
EnumUtils.getEnumIgnoreCase(HeuristicPurpose.class, purpose);
+
+        if (formattedPurpose == null) {
+            throw new IllegalArgumentException(String.format("The given 
heuristic purpose [%s] is not valid for creating a new secondary storage 
selector." +
+                    " The valid options are %s.", purpose, 
Arrays.asList(HeuristicPurpose.values())));
+        }
+
+        HeuristicVO heuristic = 
secondaryStorageHeuristicDao.findByZoneIdAndPurpose(zoneId, formattedPurpose);

Review Comment:
   This is getting removed entries as well. Which means if I already created an 
entry then removed it, I will never be able to create another one.



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