From 066cc1f4b27db853acf3447916fb3715a23be2b5 Mon Sep 17 00:00:00 2001 From: Hanarion Date: Thu, 18 Jun 2026 20:15:31 +0200 Subject: [PATCH] Apply suggestions from copilot code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../admin/offering/CreateServiceOfferingCategoryCmd.java | 2 +- .../command/admin/offering/CreateServiceOfferingCmd.java | 2 +- .../admin/offering/DeleteServiceOfferingCategoryCmd.java | 2 +- .../command/admin/offering/UpdateServiceOfferingCmd.java | 2 +- .../api/command/user/offering/ListServiceOfferingsCmd.java | 2 +- .../cloudstack/api/response/ServiceOfferingResponse.java | 4 ++-- .../src/main/resources/META-INF/db/schema-42210to42300.sql | 2 +- .../com/cloud/configuration/ConfigurationManagerImpl.java | 6 ++++++ 8 files changed, 14 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCategoryCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCategoryCmd.java index 3e28bffcbfd..62a3df40a94 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCategoryCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCategoryCmd.java @@ -80,6 +80,6 @@ public class CreateServiceOfferingCategoryCmd extends BaseCmd { @Override public long getEntityOwnerId() { - return Account.Type.ADMIN.ordinal(); + return Account.ACCOUNT_ID_SYSTEM; } } diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java index 6580283757e..7226893a673 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java @@ -294,7 +294,7 @@ public class CreateServiceOfferingCmd extends BaseCmd { entityType = ServiceOfferingCategoryResponse.class, required = false, description = "the ID of the service offering category to associate with this offering", - since = "4.23") + since = "4.23.0") private Long categoryId; ///////////////////////////////////////////////////// diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/DeleteServiceOfferingCategoryCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/DeleteServiceOfferingCategoryCmd.java index 3357351872f..3a9e251b097 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/DeleteServiceOfferingCategoryCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/DeleteServiceOfferingCategoryCmd.java @@ -71,6 +71,6 @@ public class DeleteServiceOfferingCategoryCmd extends BaseCmd { @Override public long getEntityOwnerId() { - return Account.Type.ADMIN.ordinal(); + return Account.ACCOUNT_ID_SYSTEM; } } diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/UpdateServiceOfferingCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/UpdateServiceOfferingCmd.java index deeae2c95d9..44ffc1ae726 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/UpdateServiceOfferingCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/UpdateServiceOfferingCmd.java @@ -114,7 +114,7 @@ public class UpdateServiceOfferingCmd extends BaseCmd implements DomainAndZoneId entityType = ServiceOfferingCategoryResponse.class, required = false, description = "the ID of the service offering category to associate", - since = "4.23") + since = "4.23.0") private Long categoryId; ///////////////////////////////////////////////////// diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/offering/ListServiceOfferingsCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/offering/ListServiceOfferingsCmd.java index 5d149f065df..f369a1e5d0d 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/offering/ListServiceOfferingsCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/offering/ListServiceOfferingsCmd.java @@ -127,7 +127,7 @@ public class ListServiceOfferingsCmd extends BaseListProjectAndAccountResourcesC @Parameter(name = ApiConstants.SERVICE_OFFERING_CATEGORY_ID, type = CommandType.UUID, - entityType = ServiceOfferingCategoryResponse .class, + entityType = ServiceOfferingCategoryResponse.class, description = "the ID of the service offering category", since = "4.23.0") private Long categoryId; diff --git a/api/src/main/java/org/apache/cloudstack/api/response/ServiceOfferingResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/ServiceOfferingResponse.java index 30fbd180934..86e152f20f0 100644 --- a/api/src/main/java/org/apache/cloudstack/api/response/ServiceOfferingResponse.java +++ b/api/src/main/java/org/apache/cloudstack/api/response/ServiceOfferingResponse.java @@ -287,11 +287,11 @@ public class ServiceOfferingResponse extends BaseResponseWithAnnotations { private String leaseExpiryAction; @SerializedName("categoryid") - @Param(description = "the ID of the service offering category", since = "4.23") + @Param(description = "the ID of the service offering category", since = "4.23.0") private String categoryId; @SerializedName("category") - @Param(description = "the name of the service offering category", since = "4.23") + @Param(description = "the name of the service offering category", since = "4.23.0") private String categoryName; public ServiceOfferingResponse() { diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql b/engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql index 5663794400c..3e456815b9c 100644 --- a/engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql +++ b/engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql @@ -28,10 +28,10 @@ CREATE TABLE IF NOT EXISTS `cloud`.`service_offering_category` ( CONSTRAINT `uc_service_offering_category__uuid` UNIQUE (`uuid`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; +INSERT INTO `cloud`.`service_offering_category` (id, name, uuid) VALUES (1, 'Default', UUID()); ALTER TABLE `cloud`.`service_offering` ADD COLUMN `category_id` bigint unsigned NOT NULL DEFAULT 1; ALTER TABLE `cloud`.`service_offering` ADD CONSTRAINT `fk_service_offering__category_id` FOREIGN KEY (`category_id`) REFERENCES `cloud`.`service_offering_category` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE; -INSERT INTO `cloud`.`service_offering_category` (id, name, uuid) VALUES (1, 'Default', UUID()); CREATE TABLE `cloud`.`backup_offering_details` ( `id` bigint unsigned NOT NULL auto_increment, diff --git a/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java index 38076b9f9d7..bb66797cefc 100644 --- a/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java @@ -9613,6 +9613,12 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati throw new InvalidParameterValueException("Cannot delete the default service offering category"); } + // Check if any service offering is using this category + List offeringsUsingCategory = _serviceOfferingDao.listByCategoryId(categoryId); + if (offeringsUsingCategory != null && !offeringsUsingCategory.isEmpty()) { + throw new InvalidParameterValueException("Cannot delete service offering category with id " + categoryId + + " because it is in use by one or more service offerings."); + } boolean result = _serviceOfferingCategoryDao.remove(categoryId); if (result) { CallContext.current().setEventDetails("Service offering category id=" + categoryId);