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 support for disabling serivce / system and disk offerings #9546

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,6 @@ List<VMInstanceVO> searchRemovedByRemoveDate(final Date startDate, final Date en
List<Long> skippedVmIds);

Pair<List<VMInstanceVO>, Integer> listByVmsNotInClusterUsingPool(long clusterId, long poolId);

List<VMInstanceVO> listByOfferingId(long offeringId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
protected SearchBuilder<VMInstanceVO> StartingWithNoHostSearch;
protected SearchBuilder<VMInstanceVO> NotMigratingSearch;
protected SearchBuilder<VMInstanceVO> BackupSearch;
protected SearchBuilder<VMInstanceVO> ServiceOfferingSearch;
protected SearchBuilder<VMInstanceVO> LastHostAndStatesSearch;
protected SearchBuilder<VMInstanceVO> VmsNotInClusterUsingPool;

Expand Down Expand Up @@ -325,6 +326,11 @@
VmsNotInClusterUsingPool.join("hostSearch2", hostSearch2, hostSearch2.entity().getId(), VmsNotInClusterUsingPool.entity().getHostId(), JoinType.INNER);
VmsNotInClusterUsingPool.and("vmStates", VmsNotInClusterUsingPool.entity().getState(), Op.IN);
VmsNotInClusterUsingPool.done();

ServiceOfferingSearch = createSearchBuilder();
ServiceOfferingSearch.and("states", ServiceOfferingSearch.entity().getState(), Op.IN);
ServiceOfferingSearch.and("serviceOfferingId", ServiceOfferingSearch.entity().getServiceOfferingId(), Op.EQ);
ServiceOfferingSearch.done();
}

@Override
Expand Down Expand Up @@ -1069,4 +1075,12 @@
List<VMInstanceVO> uniqueVms = vms.stream().distinct().collect(Collectors.toList());
return new Pair<>(uniqueVms, uniqueVms.size());
}

@Override
public List<VMInstanceVO> listByOfferingId(long offeringId) {
SearchCriteria<VMInstanceVO> sc = ServiceOfferingSearch.create();
sc.setParameters("states", State.Starting, State.Running, State.Stopping, State.Stopped, State.Migrating, State.Restoring);
sc.setParameters("serviceOfferingId", offeringId);
return search(sc, null);
}

Check warning on line 1085 in engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java#L1080-L1085

Added lines #L1080 - L1085 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -3431,7 +3431,7 @@
diskOfferingSearch.and("computeOnly", diskOfferingSearch.entity().isComputeOnly(), Op.EQ);

if (state != null) {
diskOfferingSearch.and("state", diskOfferingSearch.entity().getState(), Op.EQ);
diskOfferingSearch.and("state", diskOfferingSearch.entity().getState(), Op.IN);

Check warning on line 3434 in server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java#L3434

Added line #L3434 was not covered by tests
}

// Keeping this logic consistent with domain specific zones
Expand Down Expand Up @@ -3757,7 +3757,7 @@
serviceOfferingSearch.select(null, Func.DISTINCT, serviceOfferingSearch.entity().getId()); // select distinct

if (state != null) {
serviceOfferingSearch.and("state", serviceOfferingSearch.entity().getState(), Op.EQ);
serviceOfferingSearch.and("state", serviceOfferingSearch.entity().getState(), Op.IN);

Check warning on line 3760 in server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java#L3760

Added line #L3760 was not covered by tests
}

if (vmId != null) {
Expand Down Expand Up @@ -3914,8 +3914,7 @@
}

serviceOfferingSearch.join("diskOfferingSearch", diskOfferingSearch, JoinBuilder.JoinType.INNER, JoinBuilder.JoinCondition.AND,
serviceOfferingSearch.entity().getDiskOfferingId(), diskOfferingSearch.entity().getId(),
serviceOfferingSearch.entity().setString("Active"), diskOfferingSearch.entity().getState());
serviceOfferingSearch.entity().getDiskOfferingId(), diskOfferingSearch.entity().getId());

Check warning on line 3917 in server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java#L3917

Added line #L3917 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a query that is executed if vmid != null? if so, should we list inactive offerings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR aims to support disabling service offerings - to make it similar to the way network offerings work, it would make sense to list active and inactive offerings

Copy link
Contributor

@JoaoJandre JoaoJandre Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is too long (500+ lines...) for me to judge what it is exactly doing and where it is being done.
I just want to make sure that when creating VMs we don't list inactive offerings. If this change does not cause that, then I'm ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Al, that's a fair point.. Probably reverting to it listing Active by default would be a better option. Thanks.

}

if (cpuNumber != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.dc.VlanDetailsVO;
import com.cloud.dc.dao.VlanDetailsDao;
import com.cloud.hypervisor.HypervisorGuru;
import com.cloud.network.dao.NsxProviderDao;
import com.cloud.network.element.NsxProviderVO;
import com.cloud.utils.crypt.DBEncryptionUtil;
import com.cloud.host.HostTagVO;
import com.cloud.storage.StoragePoolTagVO;
import com.cloud.storage.VolumeApiServiceImpl;
import com.cloud.vm.VMInstanceVO;
import com.googlecode.ipv6.IPv6Address;
import org.apache.cloudstack.acl.SecurityChecker;
import org.apache.cloudstack.affinity.AffinityGroup;
import org.apache.cloudstack.affinity.AffinityGroupService;
Expand Down Expand Up @@ -161,7 +172,6 @@
import com.cloud.dc.PodVlanMapVO;
import com.cloud.dc.Vlan;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanDetailsVO;
import com.cloud.dc.VlanVO;
import com.cloud.dc.dao.AccountVlanMapDao;
import com.cloud.dc.dao.ClusterDao;
Expand All @@ -175,7 +185,6 @@
import com.cloud.dc.dao.HostPodDao;
import com.cloud.dc.dao.PodVlanMapDao;
import com.cloud.dc.dao.VlanDao;
import com.cloud.dc.dao.VlanDetailsDao;
import com.cloud.dc.dao.VsphereStoragePolicyDao;
import com.cloud.deploy.DataCenterDeployment;
import com.cloud.deploy.DeploymentClusterPlanner;
Expand All @@ -194,12 +203,10 @@
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.gpu.GPU;
import com.cloud.host.HostTagVO;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.host.dao.HostTagsDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.hypervisor.HypervisorGuru;
import com.cloud.hypervisor.kvm.dpdk.DpdkHelper;
import com.cloud.network.IpAddress;
import com.cloud.network.IpAddressManager;
Expand All @@ -222,13 +229,11 @@
import com.cloud.network.dao.Ipv6GuestPrefixSubnetNetworkMapDao;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.dao.NsxProviderDao;
import com.cloud.network.dao.PhysicalNetworkDao;
import com.cloud.network.dao.PhysicalNetworkTrafficTypeDao;
import com.cloud.network.dao.PhysicalNetworkTrafficTypeVO;
import com.cloud.network.dao.PhysicalNetworkVO;
import com.cloud.network.dao.UserIpv6AddressDao;
import com.cloud.network.element.NsxProviderVO;
import com.cloud.network.rules.LoadBalancerContainer.Scheme;
import com.cloud.network.vpc.VpcManager;
import com.cloud.offering.DiskOffering;
Expand Down Expand Up @@ -256,9 +261,7 @@
import com.cloud.storage.Storage;
import com.cloud.storage.Storage.ProvisioningType;
import com.cloud.storage.StorageManager;
import com.cloud.storage.StoragePoolTagVO;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeApiServiceImpl;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.storage.dao.StoragePoolTagsDao;
Expand All @@ -278,7 +281,6 @@
import com.cloud.utils.Pair;
import com.cloud.utils.UriUtils;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.crypt.DBEncryptionUtil;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.db.Filter;
Expand All @@ -303,7 +305,6 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import com.googlecode.ipv6.IPv6Address;
import com.googlecode.ipv6.IPv6Network;

import static com.cloud.configuration.Config.SecStorageAllowedInternalDownloadSites;
Expand Down Expand Up @@ -4367,8 +4368,11 @@
}

annotationDao.removeByEntityType(AnnotationService.EntityType.DISK_OFFERING.name(), offering.getUuid());
offering.setState(DiskOffering.State.Inactive);
if (_diskOfferingDao.update(offering.getId(), offering)) {
List<VolumeVO> volumesUsingOffering = _volumeDao.findByDiskOfferingId(diskOfferingId);

Check warning on line 4371 in server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java#L4371

Added line #L4371 was not covered by tests
if (!volumesUsingOffering.isEmpty()) {
throw new InvalidParameterValueException(String.format("Unable to delete disk offering: %s [%s] because there are volumes using it", offering.getUuid(), offering.getName()));

Check warning on line 4373 in server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java#L4373

Added line #L4373 was not covered by tests
}
if (_diskOfferingDao.remove(offering.getId())) {
CallContext.current().setEventDetails("Disk offering id=" + diskOfferingId);
return true;
} else {
Expand Down Expand Up @@ -4440,15 +4444,17 @@
throw new InvalidParameterValueException(String.format("Unable to delete service offering: %s by user: %s because it is not root-admin or domain-admin", offering.getUuid(), user.getUuid()));
}

List<VMInstanceVO> vmsUsingOffering = _vmInstanceDao.listByOfferingId(offeringId);

Check warning on line 4447 in server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java#L4447

Added line #L4447 was not covered by tests
if (!vmsUsingOffering.isEmpty()) {
throw new CloudRuntimeException(String.format("Unable to delete service offering %s as it is in use", offering.getUuid()));

Check warning on line 4449 in server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java#L4449

Added line #L4449 was not covered by tests
}
annotationDao.removeByEntityType(AnnotationService.EntityType.SERVICE_OFFERING.name(), offering.getUuid());
if (diskOffering.isComputeOnly()) {
diskOffering.setState(DiskOffering.State.Inactive);
if (!_diskOfferingDao.update(diskOffering.getId(), diskOffering)) {
if (!_diskOfferingDao.remove(diskOffering.getId())) {
throw new CloudRuntimeException(String.format("Unable to delete disk offering %s mapped to the service offering %s", diskOffering.getUuid(), offering.getUuid()));
}
}
offering.setState(ServiceOffering.State.Inactive);
if (_serviceOfferingDao.update(offeringId, offering)) {
if (_serviceOfferingDao.remove(offeringId)) {
CallContext.current().setEventDetails("Service offering id=" + offeringId);
return true;
} else {
Expand Down
11 changes: 11 additions & 0 deletions test/integration/component/test_acl_listvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d1)
cls.vm_d1_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d1.id)

cls.apiclient.connection.apiKey = cls.user_d1a_apikey
Expand All @@ -238,6 +239,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d1a)
cls.vm_d1a_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d1a.id)

cls.apiclient.connection.apiKey = cls.user_d1b_apikey
Expand All @@ -249,6 +251,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d1b)
cls.vm_d1b_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d1b.id)

cls.apiclient.connection.apiKey = cls.user_d11_apikey
Expand All @@ -260,6 +263,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d11)
cls.vm_d11_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d11.id)

cls.apiclient.connection.apiKey = cls.user_d11a_apikey
Expand All @@ -271,6 +275,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d11a)
cls.vm_d11a_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d11a.id)

cls.apiclient.connection.apiKey = cls.user_d11b_apikey
Expand All @@ -282,6 +287,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d11b)
cls.vm_d11b_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d11b.id)

cls.apiclient.connection.apiKey = cls.user_d111a_apikey
Expand All @@ -293,6 +299,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d111a)
cls.vm_d111a_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d111a.id)

cls.apiclient.connection.apiKey = cls.user_d12a_apikey
Expand All @@ -304,6 +311,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d12a)
cls.vm_d12a_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d12a.id)

cls.apiclient.connection.apiKey = cls.user_d12b_apikey
Expand All @@ -315,6 +323,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d12b)
cls.vm_d12b_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d12b.id)

cls.apiclient.connection.apiKey = cls.user_d2a_apikey
Expand All @@ -326,6 +335,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_d2)
cls.vm_d2_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_d2.id)

cls.apiclient.connection.apiKey = cls.user_a_apikey
Expand All @@ -337,6 +347,7 @@ def setUpClass(cls):
serviceofferingid=cls.service_offering.id,
templateid=cls.template.id
)
cls.cleanup.append(cls.vm_a)
cls.vm_a_volume = Volume.list(cls.apiclient, virtualmachineid=cls.vm_a.id)

cls.cleanup = [
Expand Down
9 changes: 4 additions & 5 deletions test/integration/component/test_cpu_max_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def createInstance(self, service_off, account=None,
projectid=project.id,
networkids=networks,
serviceofferingid=service_off.id)
self.cleanup.append(vm)
vms = VirtualMachine.list(api_client, id=vm.id, listall=True)
self.assertIsInstance(vms,
list,
Expand Down Expand Up @@ -229,7 +230,6 @@ def test_02_deploy_vm_account_limit_reached(self):
self.apiclient,
self.testdata["service_offering_multiple_cores"]
)
# Adding to cleanup list after execution
self.cleanup.append(self.service_offering)

self.debug("Setting up account and domain hierarchy")
Expand All @@ -242,7 +242,7 @@ def test_02_deploy_vm_account_limit_reached(self):
self.debug("Deploying instance with account: %s" %
self.child_do_admin.name)

self.createInstance(account=self.child_do_admin,
self.vm1 = self.createInstance(account=self.child_do_admin,
service_off=self.service_offering, api_client=api_client_admin)

self.debug("Deploying instance when CPU limit is reached in account")
Expand Down Expand Up @@ -305,7 +305,6 @@ def test_04_deployVm__account_limit_reached(self):
self.apiclient,
self.testdata["service_offering_multiple_cores"]
)
# Adding to cleanup list after execution
self.cleanup.append(self.service_offering)

self.debug("Setting up account and domain hierarchy")
Expand All @@ -317,9 +316,9 @@ def test_04_deployVm__account_limit_reached(self):

self.debug("Deploying instance with account: %s" %
self.child_do_admin.name)
self.createInstance(account=self.child_do_admin,
self.vm2 = self.createInstance(account=self.child_do_admin,
service_off=self.service_offering, api_client=api_client_admin)

# Adding to cleanup list after execution
self.debug("Deploying instance in project when CPU limit is reached in account")

with self.assertRaises(Exception):
Expand Down
17 changes: 9 additions & 8 deletions test/integration/component/test_network_offering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,19 +1388,17 @@ def setUpClass(cls):
cls.services["service_offering"]
)

cls.cleanup = [
cls._cleanup = [
cls.service_offering,
]
return

@classmethod
def tearDownClass(cls):
try:
#Cleanup resources used
cleanup_resources(cls.apiclient, cls.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
super(TestNOWithOnlySourceNAT, cls).tearDownClass()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pearl1594 , the class level cleanup array should be called _cleanup, it is now called cleanup and will be cleaned up at the instance level tearDown() method I think. Better rename it.

class scope methods should use _cleanup and instance scope methods (i.e. the test methods) should use cleanup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @DaanHoogland , not sure I understand what's the problem here? As suggested, I've updated the tearDownClass methods to invoke super()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is in the names of the variables _cleanup and cleanup @Pearl1594 !


def tearDown(self):
super(TestNOWithOnlySourceNAT, self).tearDown()

@attr(tags=["advanced", "advancedns"], required_hardware="false")
def test_create_network_with_snat(self):
Expand Down Expand Up @@ -1446,17 +1444,20 @@ def test_create_network_with_snat(self):
zoneid=self.zone.id
)
self.debug("Created guest network with ID: %s within account %s" % (self.network.id, self.account.name))
self.cleanup.append(self.network)

self.debug("Deploying VM in account: %s on the network %s" % (self.account.name, self.network.id))
# Spawn an instance in that network
VirtualMachine.create(
self.vm1 = VirtualMachine.create(
self.apiclient,
self.services["virtual_machine"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
networkids=[str(self.network.id)]
)
self.cleanup.append(self.vm1)

self.debug("Deployed VM in network: %s" % self.network.id)

src_nat_list = PublicIPAddress.list(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/component/test_stopped_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ def test_vm_per_account(self):
serviceofferingid=self.service_offering.id,
startvm=False
)

self.cleanup.append(virtual_machine)
# Verify VM state
self.assertEqual(
virtual_machine.state,
Expand Down
Loading
Loading