Skip to content

Commit 971f1da

Browse files
refactoring loading to persist YAML content in the DB
1 parent b63e5f4 commit 971f1da

12 files changed

Lines changed: 169 additions & 25 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616
// under the License.
1717
package com.cloud.upgrade.dao;
1818

19+
import com.cloud.utils.FileUtil;
1920
import com.cloud.utils.exception.CloudRuntimeException;
2021

22+
import java.io.IOException;
2123
import java.io.InputStream;
24+
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
2227
import java.sql.Connection;
2328
import java.sql.PreparedStatement;
2429
import java.sql.ResultSet;
2530
import java.sql.SQLException;
31+
import java.util.List;
2632

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

2936
@Override
3037
public String[] getUpgradableVersionRange() {
@@ -51,6 +58,7 @@ public InputStream[] getPrepareScripts() {
5158
public void performDataMigration(Connection conn) {
5259
updateSnapshotPolicyOwnership(conn);
5360
updateBackupScheduleOwnership(conn);
61+
populateNimbleIacResourceTypes(conn);
5462
}
5563

5664
protected void updateSnapshotPolicyOwnership(Connection conn) {
@@ -99,5 +107,31 @@ protected void updateBackupScheduleOwnership(Connection conn) {
99107
} catch (SQLException e) {
100108
throw new CloudRuntimeException("Unable to update backup_schedule table with account_id and domain_id", e);
101109
}
110+
111+
}
112+
113+
protected void populateNimbleIacResourceTypes(Connection conn) {
114+
String insertResourceTypeQuery = "INSERT INTO iac_templates_profile (uuid, name, element_content) VALUES (UUID(), ?, ?)";
115+
116+
List<String> filePaths = FileUtil.getFilesPathsUnderResourceDirectory(NIMBLE_RESOURCE_TYPES_DIRECTORY.toString());
117+
logger.info("Found the following NIMBLE's resource types files: [{}]. " +
118+
"Each one of them will be iterated and its corresponding content will be inserted in the database.", filePaths);
119+
for (String filePath : filePaths) {
120+
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
121+
PreparedStatement preparedStatement = conn.prepareStatement(insertResourceTypeQuery)) {
122+
if (inputStream == null) {
123+
throw new Exception(String.format("[%s] file's input stream is [null].", filePath));
124+
}
125+
126+
String resourceTypeElementContent = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
127+
preparedStatement.setString(1, filePath);
128+
preparedStatement.setString(2, resourceTypeElementContent);
129+
preparedStatement.executeUpdate();
130+
} catch (SQLException exception) {
131+
logger.warn("Unable to insert resource type [{}] in the database. Skipping it.", filePath, exception);
132+
} catch (Exception exception) {
133+
logger.warn("Unable to read file: [{}]. Skipping it.", filePath, exception);
134+
}
135+
}
102136
}
103137
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ CALL `cloud`.`INSERT_EXTENSION_DETAIL_IF_NOT_EXISTS`('MaaS', 'orchestratorrequir
8888
CALL `cloud`.`IDEMPOTENT_DROP_UNIQUE_KEY`('counter', 'uc_counter__provider__source__value');
8989
CALL `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY`('cloud.counter', 'uc_counter__provider__source__value__removed', '(provider, source, value, removed)');
9090

91-
-- NIMBLE
91+
-- NIMBLE - mudar para resource types talvez? iac_resource_types
9292
CREATE TABLE IF NOT EXISTS `cloud`.`iac_templates_profile` (
9393
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
9494
`uuid` VARCHAR(40) UNIQUE,
9595
`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`.',
97-
`element_content_file_path` VARCHAR(255) NOT NULL COMMENT 'Profile''s element content file path.',
96+
-- `type` VARCHAR(100) NOT NULL COMMENT 'Profile''s element type. Valid values are: `CAPABILITY`, `RELATIONSHIP` and `NODE`.',
97+
`element_content` TEXT NOT NULL COMMENT 'Profile''s element content.',
9898
PRIMARY KEY (`id`)
9999
);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
tosca_definitions_version: tosca_2_0
2+
3+
description: >
4+
KubernetesCluster node type definition.
5+
6+
node_types:
7+
KubernetesCluster:
8+
derived_from: Root
9+
properties:
10+
name:
11+
type: string
12+
description: The name of the Kubernetes cluster.
13+
required: true
14+
zone-id:
15+
type: string
16+
description: The ID of the availability zone.
17+
required: true
18+
description:
19+
type: string
20+
description: The description of the cluster.
21+
required: false
22+
account:
23+
type: string
24+
description: The account associated with the VPC.
25+
required: false
26+
domain-id:
27+
type: string
28+
description: The domain ID associated with the VPC.
29+
required: false
30+
project-id:
31+
type: string
32+
description: The project ID associated with the VPC.
33+
required: false
34+
kubernetes-version-id:
35+
type: string
36+
description: Kubernetes version ID used to launch the cluster.
37+
required: false
38+
network-id:
39+
type: string
40+
description: The ID of the network in which the cluster will be launched.
41+
required: false
42+
node-root-disk-size:
43+
type: integer
44+
description: Root disk size (GB) for each node.
45+
required: false
46+
service-offering-id:
47+
type: string
48+
description: Service offering ID for the VMs in the cluster.
49+
required: false
50+
size:
51+
type: integer
52+
description: Number of worker nodes in the cluster.
53+
required: false
54+
control-nodes:
55+
type: integer
56+
description: Number of control nodes in the cluster.
57+
required: false
58+
key-pair:
59+
type: string
60+
description: SSH key pair name for logging into the VMs.
61+
required: false
62+
cluster-type:
63+
type: string
64+
description: Kubernetes cluster type (CloudManaged or ExternalManaged).
65+
required: false
66+
validation: { $valid_values: [ $value, [CloudManaged, ExternalManaged] ] }
67+
docker-registry-password:
68+
type: string
69+
description: Password for the Docker image private registry.
70+
required: false
71+
docker-registry-url:
72+
type: string
73+
description: URL for the Docker image private registry.
74+
required: false
75+
docker-registry-username:
76+
type: string
77+
description: Username for the Docker image private registry.
78+
required: false
79+
external-load-balancer-ip-address:
80+
type: string
81+
description: External load balancer IP address for HA clusters.
82+
required: false
83+
attributes:
84+
id:
85+
type: string
86+
description: The ID of the Kubernetes cluster.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
tosca_definitions_version: tosca_2_0
2+
3+
description: >
4+
Apache CloudStack TOSCA profile Volume node type definition.
5+
6+
node_types:
7+
Volume:
8+
description: >
9+
Apache CloudStack Volume node type.
10+
derived_from: Root
11+
attributes:
12+
id:
13+
type: string
14+
description: ID of the volume.
15+
name:
16+
type: string
17+
description: The name of the volume.
18+
disk-offering-id:
19+
type: string
20+
description: The ID of the disk offering used by the volume.
21+
properties:
22+
zone-id:
23+
type: string
24+
description: The ID of the zone in which the volume will be created.
25+
required: true
26+
disk-offering-id:
27+
type: string
28+
description: The ID of the disk offering for the volume.
29+
required: true
30+
name:
31+
type: string
32+
description: The name for the volume.
33+
required: true

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacTemplatesProfileDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
import com.cloud.utils.db.GenericDao;
2020

21-
public interface IacTemplatesProfileDao extends GenericDao<IacTemplatesProfile, Long> {
21+
public interface IacTemplatesProfileDao extends GenericDao<IacTemplatesProfileVO, Long> {
2222

2323
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplatesprofile/IacTemplatesProfileDaoImpl.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<IacTemplatesProfile, Long> implements IacTemplatesProfileDao {
23+
public class IacTemplatesProfileDaoImpl extends GenericDaoBase<IacTemplatesProfileVO, Long> implements IacTemplatesProfileDao {
2424
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/service/NimbleManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.cloud.utils.component.ManagerBase;
2020
import org.apache.cloudstack.api.command.ListIacResourceTypesCmd;
2121
import org.apache.cloudstack.framework.config.ConfigKey;
22-
import org.apache.cloudstack.persistence.iactemplatesprofile.IacTemplatesProfile;
2322
import org.apache.cloudstack.persistence.iactemplatesprofile.IacTemplatesProfileDao;
23+
import org.apache.cloudstack.persistence.iactemplatesprofile.IacTemplatesProfileVO;
2424
import org.apache.cloudstack.tosca.model.ToscaNodeType;
2525
import org.apache.cloudstack.tosca.parser.ToscaParser;
2626

@@ -63,7 +63,7 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
6363

6464
protected Map<String, ToscaNodeType> loadToscaProfile() {
6565
logger.info("Loading NIMBLE's TOSCA profile.");
66-
List<IacTemplatesProfile> profileElements = iacTemplatesProfileDao.listAll();
66+
List<IacTemplatesProfileVO> profileElements = iacTemplatesProfileDao.listAll();
6767

6868
return profileElements.stream()
6969
.map(element -> toscaParser.parseNodeType(Paths.get(String.format("%s/%s", NIMBLE_PROFILE_FOLDER_PATH, element.getElementContentFilePath()))))

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/model/ToscaAttributeDefinition.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@ public class ToscaAttributeDefinition extends ToscaFieldDefinition {
44
public ToscaAttributeDefinition(String name, String description, ToscaPrimitiveType type) {
55
super(name, description, type);
66
}
7-
8-
@Override
9-
public String toString() {
10-
return String.format("%s: %s", this.getClass().getName(), super.toString());
11-
}
127
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/model/ToscaFieldDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public ToscaPrimitiveType getType() {
2727

2828
@Override
2929
public String toString() {
30-
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "name", "description", "type");
30+
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "name", "type");
3131
}
3232
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/model/ToscaNodeType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public Map<String, ToscaAttributeDefinition> getAttributes() {
2929

3030
@Override
3131
public String toString() {
32-
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "name", "properties", "attributes");
32+
return String.format("%s: %s", name,
33+
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "properties", "attributes"));
3334
}
3435
}

0 commit comments

Comments
 (0)