Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic secondary storage selection #7659

Merged
merged 16 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
397a3aa
Add dynamic secondary storage selection
BryanMLima Jun 20, 2023
a412315
Add missing newlines at the end of files
BryanMLima Jun 21, 2023
55dc636
Add account preset variable with domain details
BryanMLima Aug 2, 2023
266ca31
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Aug 3, 2023
2dee071
Fix unit tests
BryanMLima Aug 3, 2023
ad8a94c
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Aug 15, 2023
54c5b87
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Sep 26, 2023
a746a3f
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Oct 23, 2023
0d5782c
Merge branch 'dynamic-ss-selection' of github.com:scclouds/cloudstack…
BryanMLima Oct 23, 2023
ee6efaa
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Oct 27, 2023
ebb7d37
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Nov 10, 2023
2f45694
Address reviews
BryanMLima Nov 10, 2023
829c070
Address reviews and change heuristic purpose to heuristic type
BryanMLima Nov 13, 2023
f0d734f
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Nov 14, 2023
a77a828
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Nov 17, 2023
70e1705
Merge branch 'main' of github.com:apache/cloudstack into dynamic-ss-s…
BryanMLima Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add account preset variable with domain details
BryanMLima committed Aug 2, 2023
commit 55dc6367489df8981af6f1422de0a6bff9932bc9
Original file line number Diff line number Diff line change
@@ -17,10 +17,14 @@
package org.apache.cloudstack.storage.heuristics;

import com.cloud.api.ApiDBUtils;
import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
import com.cloud.storage.StorageManager;
import com.cloud.storage.StorageStats;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.VolumeVO;
import com.cloud.user.AccountVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
@@ -30,6 +34,8 @@
import org.apache.cloudstack.secstorage.heuristics.HeuristicPurpose;
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
import org.apache.cloudstack.storage.heuristics.presetvariables.Account;
import org.apache.cloudstack.storage.heuristics.presetvariables.Domain;
import org.apache.cloudstack.storage.heuristics.presetvariables.PresetVariables;
import org.apache.cloudstack.storage.heuristics.presetvariables.SecondaryStorage;
import org.apache.cloudstack.storage.heuristics.presetvariables.Snapshot;
@@ -61,6 +67,12 @@ public class HeuristicRuleHelper {
@Inject
private SecondaryStorageHeuristicDao secondaryStorageHeuristicDao;

@Inject
private DomainDao domainDao;

@Inject
private AccountDao accountDao;

/**
* Returns the {@link DataStore} object if the zone, specified by the ID, has an active heuristic rule for the given {@link HeuristicPurpose}.
* It returns null otherwise.
@@ -87,19 +99,24 @@ public DataStore getImageStoreIfThereIsHeuristicRule(Long zoneId, HeuristicPurpo
*/
protected void buildPresetVariables(JsInterpreter jsInterpreter, HeuristicPurpose heuristicPurpose, long zoneId, Object obj) {
PresetVariables presetVariables = new PresetVariables();
Long accountId = null;

switch (heuristicPurpose) {
case TEMPLATE:
case ISO:
presetVariables.setTemplate(setTemplatePresetVariable((VMTemplateVO) obj));
accountId = ((VMTemplateVO) obj).getAccountId();
break;
case SNAPSHOT:
presetVariables.setSnapshot(setSnapshotPresetVariable((SnapshotInfo) obj));
accountId = ((SnapshotInfo) obj).getAccountId();
break;
case VOLUME:
presetVariables.setVolume(setVolumePresetVariable((VolumeVO) obj));
accountId = ((VolumeVO) obj).getAccountId();
break;
}
presetVariables.setAccount(setAccountPresetVariable(accountId));
presetVariables.setSecondaryStorages(setSecondaryStoragesVariable(zoneId));

injectPresetVariables(jsInterpreter, presetVariables);
@@ -131,6 +148,10 @@ protected void injectPresetVariables(JsInterpreter jsInterpreter, PresetVariable
if (presetVariables.getVolume() != null) {
jsInterpreter.injectVariable("volume", presetVariables.getVolume().toString());
}

if (presetVariables.getAccount() != null) {
jsInterpreter.injectVariable("account", presetVariables.getAccount().toString());
}
}

protected List<SecondaryStorage> setSecondaryStoragesVariable(long zoneId) {
@@ -140,7 +161,8 @@ protected List<SecondaryStorage> setSecondaryStoragesVariable(long zoneId) {
for (ImageStoreVO imageStore : imageStoreVOS) {
SecondaryStorage secondaryStorage = new SecondaryStorage();

secondaryStorage.setUuid(imageStore.getUuid());
secondaryStorage.setName(imageStore.getName());
secondaryStorage.setId(imageStore.getUuid());
secondaryStorage.setProtocol(imageStore.getProtocol());
StorageStats storageStats = ApiDBUtils.getSecondaryStorageStatistics(imageStore.getId());

@@ -157,6 +179,7 @@ protected List<SecondaryStorage> setSecondaryStoragesVariable(long zoneId) {
protected Template setTemplatePresetVariable(VMTemplateVO templateVO) {
Template template = new Template();

template.setName(templateVO.getName());
template.setFormat(templateVO.getFormat());
template.setHypervisorType(templateVO.getHypervisorType());

@@ -166,6 +189,7 @@ protected Template setTemplatePresetVariable(VMTemplateVO templateVO) {
protected Volume setVolumePresetVariable(VolumeVO volumeVO) {
Volume volume = new Volume();

volume.setName(volumeVO.getName());
volume.setFormat(volumeVO.getFormat());
volume.setSize(volumeVO.getSize());

@@ -175,12 +199,44 @@ protected Volume setVolumePresetVariable(VolumeVO volumeVO) {
protected Snapshot setSnapshotPresetVariable(SnapshotInfo snapshotInfo) {
Snapshot snapshot = new Snapshot();

snapshot.setName(snapshotInfo.getName());
snapshot.setSize(snapshotInfo.getSize());
snapshot.setHypervisorType(snapshotInfo.getHypervisorType());

return snapshot;
}

protected Account setAccountPresetVariable(Long accountId) {
if (accountId == null) {
return null;
}

AccountVO account = accountDao.findById(accountId);
if (account == null) {
return null;
}

Account accountVariable = new Account();
accountVariable.setName(account.getName());
accountVariable.setId(account.getUuid());

accountVariable.setDomain(setDomainPresetVariable(account.getDomainId()));

return accountVariable;
}

protected Domain setDomainPresetVariable(long domainId) {
DomainVO domain = domainDao.findById(domainId);
if (domain == null) {
return null;
}
Domain domainVariable = new Domain();
domainVariable.setName(domain.getName());
domainVariable.setId(domain.getUuid());

return domainVariable;
}

/**
* This method calls {@link HeuristicRuleHelper#buildPresetVariables(JsInterpreter, HeuristicPurpose, long, Object)} for building the preset variables and
* execute the JS script specified in the <b>rule</b> ({@link String}) parameter. The script is pre-injected with the preset variables, to allow the JS script to reference them
Original file line number Diff line number Diff line change
@@ -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{
private String id;

private Domain domain;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
fieldNamesToIncludeInToString.add("id");
}

public Domain getDomain() {
return domain;
}

public void setDomain(Domain domain) {
this.domain = domain;
fieldNamesToIncludeInToString.add("domain");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 Domain extends GenericHeuristicPresetVariable{
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
fieldNamesToIncludeInToString.add("id");
}
}
Original file line number Diff line number Diff line change
@@ -25,6 +25,17 @@ public class GenericHeuristicPresetVariable {

protected transient Set<String> fieldNamesToIncludeInToString = new HashSet<>();

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
fieldNamesToIncludeInToString.add("name");
}

@Override
public String toString() {
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, fieldNamesToIncludeInToString.toArray(new String[0]));
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@

public class PresetVariables {

private Account account;

private List<SecondaryStorage> secondaryStorages;

private Template template;
@@ -59,4 +61,12 @@ public Volume getVolume() {
public void setVolume(Volume volume) {
this.volume = volume;
}

public Account getAccount() {
return account;
}

public void setAccount(Account account) {
this.account = account;
}
}
Original file line number Diff line number Diff line change
@@ -18,21 +18,21 @@

public class SecondaryStorage extends GenericHeuristicPresetVariable {

private String uuid;
private String id;

private Long usedDiskSize;

private Long totalDiskSize;

private String protocol;

public String getUuid() {
return uuid;
public String getId() {
return id;
}

public void setUuid(String uuid) {
this.uuid = uuid;
fieldNamesToIncludeInToString.add("uuid");
public void setId(String id) {
this.id = id;
fieldNamesToIncludeInToString.add("id");
}

public Long getUsedDiskSize() {
Original file line number Diff line number Diff line change
@@ -25,6 +25,8 @@ public class Template extends GenericHeuristicPresetVariable {

private Storage.ImageFormat format;

private Storage.TemplateType templateType;

public Hypervisor.HypervisorType getHypervisorType() {
return hypervisorType;
}
@@ -42,4 +44,13 @@ public void setFormat(Storage.ImageFormat format) {
this.format = format;
fieldNamesToIncludeInToString.add("format");
}

public Storage.TemplateType getTemplateType() {
return templateType;
}

public void setTemplateType(Storage.TemplateType templateType) {
this.templateType = templateType;
fieldNamesToIncludeInToString.add("templateType");
}
}