Skip to content

Commit b630cf3

Browse files
refactoring loading to persist YAML content in the DB and start implementing the listIacResourceTypes API
1 parent 971f1da commit b630cf3

17 files changed

Lines changed: 122 additions & 146 deletions

File tree

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.cloud.utils.FileUtil;
2020
import com.cloud.utils.exception.CloudRuntimeException;
2121

22-
import java.io.IOException;
2322
import java.io.InputStream;
2423
import java.nio.charset.StandardCharsets;
2524
import java.nio.file.Path;
@@ -31,7 +30,7 @@
3130
import java.util.List;
3231

3332
public class Upgrade42100to42200 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
34-
private static final Path NIMBLE_RESOURCE_TYPES_DIRECTORY = Paths.get("nimble", "resource-types");
33+
private static final String NIMBLE_RESOURCE_TYPES_DIRECTORY = Paths.get("nimble", "resource-types").toString();
3534

3635
@Override
3736
public String[] getUpgradableVersionRange() {
@@ -56,8 +55,8 @@ public InputStream[] getPrepareScripts() {
5655

5756
@Override
5857
public void performDataMigration(Connection conn) {
59-
updateSnapshotPolicyOwnership(conn);
60-
updateBackupScheduleOwnership(conn);
58+
// updateSnapshotPolicyOwnership(conn);
59+
// updateBackupScheduleOwnership(conn);
6160
populateNimbleIacResourceTypes(conn);
6261
}
6362

@@ -111,9 +110,9 @@ protected void updateBackupScheduleOwnership(Connection conn) {
111110
}
112111

113112
protected void populateNimbleIacResourceTypes(Connection conn) {
114-
String insertResourceTypeQuery = "INSERT INTO iac_templates_profile (uuid, name, element_content) VALUES (UUID(), ?, ?)";
113+
String insertResourceTypeQuery = "INSERT INTO iac_resource_types (uuid, name, element_content) VALUES (UUID(), ?, ?)";
115114

116-
List<String> filePaths = FileUtil.getFilesPathsUnderResourceDirectory(NIMBLE_RESOURCE_TYPES_DIRECTORY.toString());
115+
List<String> filePaths = FileUtil.getFilesPathsUnderResourceDirectory(NIMBLE_RESOURCE_TYPES_DIRECTORY);
117116
logger.info("Found the following NIMBLE's resource types files: [{}]. " +
118117
"Each one of them will be iterated and its corresponding content will be inserted in the database.", filePaths);
119118
for (String filePath : filePaths) {
@@ -124,7 +123,7 @@ protected void populateNimbleIacResourceTypes(Connection conn) {
124123
}
125124

126125
String resourceTypeElementContent = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
127-
preparedStatement.setString(1, filePath);
126+
preparedStatement.setString(1, getIacResourceTypeNameFromFilePath(filePath));
128127
preparedStatement.setString(2, resourceTypeElementContent);
129128
preparedStatement.executeUpdate();
130129
} catch (SQLException exception) {
@@ -134,4 +133,20 @@ protected void populateNimbleIacResourceTypes(Connection conn) {
134133
}
135134
}
136135
}
136+
137+
protected String getIacResourceTypeNameFromFilePath(String filePath) {
138+
String fileName = Path.of(filePath).getFileName().toString();
139+
int fileExtensionDelimiterPosition = fileName.lastIndexOf('.');
140+
String resourceNameInKebabCase = fileExtensionDelimiterPosition == -1 ?
141+
fileName : fileName.substring(0, fileExtensionDelimiterPosition);
142+
StringBuilder resourceNameInCamelCase = new StringBuilder();
143+
for (String resourceNamePart : resourceNameInKebabCase.split("-")) {
144+
if (!resourceNamePart.isEmpty()) {
145+
resourceNameInCamelCase.append(Character.toUpperCase(resourceNamePart.charAt(0)))
146+
.append(resourceNamePart.substring(1));
147+
}
148+
}
149+
150+
return resourceNameInCamelCase.toString();
151+
}
137152
}

engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,80 +20,11 @@
2020
--;
2121

2222

23-
-- health check status as enum
24-
CALL `cloud`.`IDEMPOTENT_CHANGE_COLUMN`('router_health_check', 'check_result', 'check_result', 'varchar(16) NOT NULL COMMENT "check executions result: SUCCESS, FAILURE, WARNING, UNKNOWN"');
25-
26-
-- Increase length of scripts_version column to 128 due to md5sum to sha512sum change
27-
CALL `cloud`.`IDEMPOTENT_CHANGE_COLUMN`('cloud.domain_router', 'scripts_version', 'scripts_version', 'VARCHAR(128)');
28-
29-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.snapshot_policy','domain_id', 'BIGINT(20) DEFAULT NULL');
30-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.snapshot_policy','account_id', 'BIGINT(20) DEFAULT NULL');
31-
32-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.backup_schedule','domain_id', 'BIGINT(20) DEFAULT NULL');
33-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.backup_schedule','account_id', 'BIGINT(20) DEFAULT NULL');
34-
35-
-- Increase the cache_mode column size from cloud.disk_offering table
36-
CALL `cloud`.`IDEMPOTENT_CHANGE_COLUMN`('cloud.disk_offering', 'cache_mode', 'cache_mode', 'varchar(18) DEFAULT "none" COMMENT "The disk cache mode to use for disks created with this offering"');
37-
38-
-- Add uuid column to ldap_configuration table
39-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.ldap_configuration', 'uuid', 'VARCHAR(40) NOT NULL');
40-
41-
-- Populate uuid for existing rows where uuid is NULL or empty
42-
UPDATE `cloud`.`ldap_configuration` SET uuid = UUID() WHERE uuid IS NULL OR uuid = '';
43-
44-
-- Add the column cross_zone_instance_creation to cloud.backup_repository. if enabled it means that new Instance can be created on all Zones from Backups on this Repository.
45-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.backup_repository', 'cross_zone_instance_creation', 'TINYINT(1) DEFAULT NULL COMMENT ''Backup Repository can be used for disaster recovery on another zone''');
46-
47-
-- Updated display to false for password/token detail of the storage pool details
48-
UPDATE `cloud`.`storage_pool_details` SET display = 0 WHERE name LIKE '%password%';
49-
UPDATE `cloud`.`storage_pool_details` SET display = 0 WHERE name LIKE '%token%';
50-
51-
-- Add csi_enabled column to kubernetes_cluster table to indicate if the cluster is using csi or not
52-
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster', 'csi_enabled', 'TINYINT(1) unsigned NOT NULL DEFAULT 0 COMMENT "true if kubernetes cluster is using csi, false otherwise" ');
53-
54-
-- VMware to KVM migration improvements
55-
CREATE TABLE IF NOT EXISTS `cloud`.`import_vm_task`(
56-
`id` bigint unsigned NOT NULL auto_increment COMMENT 'id',
57-
`uuid` varchar(40),
58-
`zone_id` bigint unsigned NOT NULL COMMENT 'Zone ID',
59-
`account_id` bigint unsigned NOT NULL COMMENT 'Account ID',
60-
`user_id` bigint unsigned NOT NULL COMMENT 'User ID',
61-
`vm_id` bigint unsigned COMMENT 'VM ID',
62-
`display_name` varchar(255) COMMENT 'Display VM Name',
63-
`vcenter` varchar(255) COMMENT 'VCenter',
64-
`datacenter` varchar(255) COMMENT 'VCenter Datacenter name',
65-
`source_vm_name` varchar(255) COMMENT 'Source VM name on vCenter',
66-
`convert_host_id` bigint unsigned COMMENT 'Convert Host ID',
67-
`import_host_id` bigint unsigned COMMENT 'Import Host ID',
68-
`step` varchar(20) COMMENT 'Importing VM Task Step',
69-
`state` varchar(20) COMMENT 'Importing VM Task State',
70-
`description` varchar(255) COMMENT 'Importing VM Task Description',
71-
`duration` bigint unsigned COMMENT 'Duration in milliseconds for the completed tasks',
72-
`created` datetime NOT NULL COMMENT 'date created',
73-
`updated` datetime COMMENT 'date updated if not null',
74-
`removed` datetime COMMENT 'date removed if not null',
75-
PRIMARY KEY (`id`),
76-
CONSTRAINT `fk_import_vm_task__zone_id` FOREIGN KEY `fk_import_vm_task__zone_id` (`zone_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE,
77-
CONSTRAINT `fk_import_vm_task__account_id` FOREIGN KEY `fk_import_vm_task__account_id` (`account_id`) REFERENCES `account`(`id`) ON DELETE CASCADE,
78-
CONSTRAINT `fk_import_vm_task__user_id` FOREIGN KEY `fk_import_vm_task__user_id` (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
79-
CONSTRAINT `fk_import_vm_task__vm_id` FOREIGN KEY `fk_import_vm_task__vm_id` (`vm_id`) REFERENCES `vm_instance`(`id`) ON DELETE CASCADE,
80-
CONSTRAINT `fk_import_vm_task__convert_host_id` FOREIGN KEY `fk_import_vm_task__convert_host_id` (`convert_host_id`) REFERENCES `host`(`id`) ON DELETE CASCADE,
81-
CONSTRAINT `fk_import_vm_task__import_host_id` FOREIGN KEY `fk_import_vm_task__import_host_id` (`import_host_id`) REFERENCES `host`(`id`) ON DELETE CASCADE,
82-
INDEX `i_import_vm_task__zone_id`(`zone_id`)
83-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
84-
85-
CALL `cloud`.`INSERT_EXTENSION_IF_NOT_EXISTS`('MaaS', 'Baremetal Extension for Canonical MaaS written in Python', 'MaaS/maas.py');
86-
CALL `cloud`.`INSERT_EXTENSION_DETAIL_IF_NOT_EXISTS`('MaaS', 'orchestratorrequirespreparevm', 'true', 0);
87-
88-
CALL `cloud`.`IDEMPOTENT_DROP_UNIQUE_KEY`('counter', 'uc_counter__provider__source__value');
89-
CALL `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY`('cloud.counter', 'uc_counter__provider__source__value__removed', '(provider, source, value, removed)');
90-
91-
-- NIMBLE - mudar para resource types talvez? iac_resource_types
92-
CREATE TABLE IF NOT EXISTS `cloud`.`iac_templates_profile` (
23+
-- NIMBLE
24+
CREATE TABLE IF NOT EXISTS `cloud`.`iac_resource_types` (
9325
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
9426
`uuid` VARCHAR(40) UNIQUE,
9527
`name` VARCHAR(100) NOT NULL COMMENT 'Profile''s element name.',
96-
-- `type` VARCHAR(100) NOT NULL COMMENT 'Profile''s element type. Valid values are: `CAPABILITY`, `RELATIONSHIP` and `NODE`.',
9728
`element_content` TEXT NOT NULL COMMENT 'Profile''s element content.',
9829
PRIMARY KEY (`id`)
9930
);

engine/schema/src/main/resources/nimble/resource_types/kubernetes-cluster.yaml renamed to engine/schema/src/main/resources/nimble/resource-types/kubernetes-cluster.yaml

File renamed without changes.

engine/schema/src/main/resources/nimble/resource_types/volume.yaml renamed to engine/schema/src/main/resources/nimble/resource-types/volume.yaml

File renamed without changes.

plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/command/ListIacResourceTypesCmd.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,42 @@
1818

1919
import org.apache.cloudstack.acl.RoleType;
2020
import org.apache.cloudstack.api.APICommand;
21+
import org.apache.cloudstack.api.ApiConstants;
2122
import org.apache.cloudstack.api.BaseListCmd;
22-
import org.apache.cloudstack.api.response.IacResourceTypesResponse;
23+
import org.apache.cloudstack.api.Parameter;
24+
import org.apache.cloudstack.api.response.IacResourceTypeResponse;
25+
import org.apache.cloudstack.api.response.ListResponse;
26+
import org.apache.cloudstack.persistence.iactemplatesprofile.IacResourceType;
2327
import org.apache.cloudstack.service.NimbleService;
2428

2529
import javax.inject.Inject;
2630

2731
@APICommand(name = "listIacResourceTypes",
28-
description = "Lists available IaC resource types, such as TOSCA's node, relationship and capacity types.",
29-
responseObject = IacResourceTypesResponse.class, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
30-
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
32+
description = "Lists all available IaC resource types, such as TOSCA node types.",
33+
responseObject = IacResourceTypeResponse.class, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
34+
entityType = {IacResourceType.class}, authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
3135
public class ListIacResourceTypesCmd extends BaseListCmd {
3236
@Inject
3337
private NimbleService nimbleService;
3438

39+
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = IacResourceTypeResponse.class, description = "The ID of the IaC resource type.")
40+
private Long id;
41+
42+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "The name of the IaC resource type.")
43+
private String name;
44+
45+
public Long getId() {
46+
return id;
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
3553
@Override
3654
public void execute() {
37-
nimbleService.listIacResourceTypes();
55+
ListResponse<IacResourceTypeResponse> response = nimbleService.listIacResourceTypes(this);
56+
response.setResponseName(getCommandName());
57+
setResponseObject(response);
3858
}
3959
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/response/IacResourceTypesResponse.java renamed to plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/response/IacResourceTypeResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
import org.apache.cloudstack.api.EntityReference;
2121

2222
@EntityReference(value = {})
23-
public class IacResourceTypesResponse extends BaseResponseWithAnnotations {
23+
public class IacResourceTypeResponse extends BaseResponseWithAnnotations {
2424
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacTemplatesProfileDao.java renamed to plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/response/NimbleResponseBuilder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
package org.apache.cloudstack.persistence.iactemplatesprofile;
18-
19-
import com.cloud.utils.db.GenericDao;
20-
21-
public interface IacTemplatesProfileDao extends GenericDao<IacTemplatesProfileVO, Long> {
17+
package org.apache.cloudstack.api.response;
2218

19+
public class NimbleResponseBuilder {
2320
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacTemplatesProfile.java renamed to plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacResourceType.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919
import org.apache.cloudstack.api.Identity;
2020
import org.apache.cloudstack.api.InternalIdentity;
2121

22-
public interface IacTemplatesProfile extends Identity, InternalIdentity {
23-
enum Type {
24-
CAPABILITY, RELATIONSHIP, NODE
25-
}
26-
27-
long getId();
22+
public interface IacResourceType extends Identity, InternalIdentity {
2823
String getName();
29-
Type getType();
30-
String getElementContentFilePath();
24+
String getElementContent();
3125
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.persistence.iactemplatesprofile;
18+
19+
import com.cloud.utils.Pair;
20+
import com.cloud.utils.db.GenericDao;
21+
22+
import java.util.List;
23+
24+
public interface IacResourceTypeDao extends GenericDao<IacResourceTypeVO, Long> {
25+
Pair<List<IacResourceTypeVO>, Integer> listIacResourceTypes(Long id, String name, String keyword, Long pageSizeVal, Long startIndex);
26+
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacTemplatesProfileDaoImpl.java renamed to plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacResourceTypeDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
import org.springframework.stereotype.Component;
2121

2222
@Component
23-
public class IacTemplatesProfileDaoImpl extends GenericDaoBase<IacTemplatesProfileVO, Long> implements IacTemplatesProfileDao {
23+
public class IacResourceTypeDaoImpl extends GenericDaoBase<IacResourceTypeVO, Long> implements IacResourceTypeDao {
2424
}

0 commit comments

Comments
 (0)