|
30 | 30 | import java.util.List; |
31 | 31 |
|
32 | 32 | public class Upgrade42100to42200 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { |
33 | | - private static final String NIMBLE_RESOURCE_TYPES_DIRECTORY = Paths.get("nimble", "resource-types").toString(); |
| 33 | + private static final Path NIMBLE_RESOURCE_TYPES_DIRECTORY = Paths.get("nimble", "resource-types"); |
34 | 34 |
|
35 | 35 | @Override |
36 | 36 | public String[] getUpgradableVersionRange() { |
@@ -110,43 +110,67 @@ protected void updateBackupScheduleOwnership(Connection conn) { |
110 | 110 | } |
111 | 111 |
|
112 | 112 | protected void populateNimbleIacResourceTypes(Connection conn) { |
113 | | - String insertResourceTypeQuery = "INSERT INTO iac_resource_types (uuid, name, element_content) VALUES (UUID(), ?, ?)"; |
114 | | - |
115 | | - List<String> filePaths = FileUtil.getFilesPathsUnderResourceDirectory(NIMBLE_RESOURCE_TYPES_DIRECTORY); |
116 | | - logger.info("Found the following NIMBLE's resource types files: [{}]. " + |
117 | | - "Each one of them will be iterated and its corresponding content will be inserted in the database.", filePaths); |
118 | | - for (String filePath : filePaths) { |
119 | | - try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); |
120 | | - PreparedStatement preparedStatement = conn.prepareStatement(insertResourceTypeQuery)) { |
121 | | - if (inputStream == null) { |
122 | | - throw new Exception(String.format("[%s] file's input stream is [null].", filePath)); |
123 | | - } |
124 | | - |
125 | | - String resourceTypeElementContent = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); |
126 | | - preparedStatement.setString(1, getIacResourceTypeNameFromFilePath(filePath)); |
127 | | - preparedStatement.setString(2, resourceTypeElementContent); |
128 | | - preparedStatement.executeUpdate(); |
129 | | - } catch (SQLException exception) { |
130 | | - logger.warn("Unable to insert resource type [{}] in the database. Skipping it.", filePath, exception); |
131 | | - } catch (Exception exception) { |
132 | | - logger.warn("Unable to read file: [{}]. Skipping it.", filePath, exception); |
133 | | - } |
134 | | - } |
| 113 | + logger.info("Populating NIMBLE IaC resource types of the [CLOUD_ACCESS_MANAGEMENT] category in the database."); |
| 114 | + insertIacResourceType(conn, "account.yaml", "Account", "CLOUD_ACCESS_MANAGEMENT"); |
| 115 | + insertIacResourceType(conn, "domain.yaml", "Domain", "CLOUD_ACCESS_MANAGEMENT"); |
| 116 | + insertIacResourceType(conn, "project.yaml", "Project", "CLOUD_ACCESS_MANAGEMENT"); |
| 117 | + insertIacResourceType(conn, "user.yaml", "User", "CLOUD_ACCESS_MANAGEMENT"); |
| 118 | + |
| 119 | + logger.info("Populating NIMBLE IaC resource types of the [SERVICE_OFFERING] category in the database."); |
| 120 | + insertIacResourceType(conn, "compute-offering.yaml", "ComputeOffering", "SERVICE_OFFERING"); |
| 121 | + insertIacResourceType(conn, "disk-offering.yaml", "DiskOffering", "SERVICE_OFFERING"); |
| 122 | + insertIacResourceType(conn, "network-offering.yaml", "NetworkOffering", "SERVICE_OFFERING"); |
| 123 | + insertIacResourceType(conn, "vpc-offering.yaml", "VpcOffering", "SERVICE_OFFERING"); |
| 124 | + |
| 125 | + logger.info("Populating NIMBLE IaC resource types of the [NETWORK] category in the database."); |
| 126 | + insertIacResourceType(conn, "network.yaml", "Network", "NETWORK"); |
| 127 | + insertIacResourceType(conn, "vpc.yaml", "Vpc", "NETWORK"); |
| 128 | + insertIacResourceType(conn, "egress-firewall-rule.yaml", "EgressFirewallRule", "NETWORK"); |
| 129 | + insertIacResourceType(conn, "firewall-rule.yaml", "FirewallRule", "NETWORK"); |
| 130 | + insertIacResourceType(conn, "port-forwarding-rule.yaml", "PortForwardingRule", "NETWORK"); |
| 131 | + insertIacResourceType(conn, "static-nat.yaml", "StaticNat", "NETWORK"); |
| 132 | + insertIacResourceType(conn, "ip-address.yaml", "IpAddress", "NETWORK"); |
| 133 | + insertIacResourceType(conn, "load-balancer.yaml", "LoadBalancer", "NETWORK"); |
| 134 | + insertIacResourceType(conn, "load-balancer-attachment.yaml", "LoadBalancerAttachment", "NETWORK"); |
| 135 | + insertIacResourceType(conn, "network-acl-list.yaml", "NetworkAclList", "NETWORK"); |
| 136 | + insertIacResourceType(conn, "network-acl-rule.yaml", "NetworkAclRule", "NETWORK"); |
| 137 | + |
| 138 | + logger.info("Populating NIMBLE IaC resource types of the [STORAGE] category in the database."); |
| 139 | + insertIacResourceType(conn, "volume.yaml", "Volume", "STORAGE"); |
| 140 | + insertIacResourceType(conn, "volume-attachment.yaml", "VolumeAttachment", "STORAGE"); |
| 141 | + |
| 142 | + logger.info("Populating NIMBLE IaC resource types of the [COMPUTE] category in the database."); |
| 143 | + insertIacResourceType(conn, "virtual-machine.yaml", "VirtualMachine", "COMPUTE"); |
| 144 | + insertIacResourceType(conn, "kubernetes-cluster.yaml", "KubernetesCluster", "COMPUTE"); |
| 145 | + insertIacResourceType(conn, "instance-group.yaml", "InstanceGroup", "COMPUTE"); |
| 146 | + insertIacResourceType(conn, "affinity-group.yaml", "AffinityGroup", "COMPUTE"); |
| 147 | + insertIacResourceType(conn, "auto-scaling-policy.yaml", "AutoScalingPolicy", "COMPUTE"); |
| 148 | + insertIacResourceType(conn, "auto-scaling-vm-group.yaml", "AutoScalingVmGroup", "COMPUTE"); |
| 149 | + insertIacResourceType(conn, "auto-scaling-vm-profile.yaml", "AutoScalingVmProfile", "COMPUTE"); |
| 150 | + insertIacResourceType(conn, "shared-filesystem.yaml", "SharedFilesystem", "COMPUTE"); |
| 151 | + insertIacResourceType(conn, "ssh-key-pair.yaml", "SshKeyPair", "COMPUTE"); |
| 152 | + insertIacResourceType(conn, "user-data.yaml", "UserData", "COMPUTE"); |
135 | 153 | } |
136 | 154 |
|
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)); |
| 155 | + private void insertIacResourceType(Connection conn, String fileName, String resourceName, String resourceCategory) { |
| 156 | + String insertResourceTypeQuery = "INSERT INTO iac_resource_types (uuid, name, category, element_content) VALUES (UUID(), ?, ?, ?)"; |
| 157 | + String filePath = NIMBLE_RESOURCE_TYPES_DIRECTORY.resolve(fileName).toString(); |
| 158 | + logger.debug("Inserting resource type [name: {}] in the database.", filePath); |
| 159 | + try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); |
| 160 | + PreparedStatement preparedStatement = conn.prepareStatement(insertResourceTypeQuery)) { |
| 161 | + if (inputStream == null) { |
| 162 | + throw new Exception(String.format("[%s] file's input stream is [null].", filePath)); |
147 | 163 | } |
148 | | - } |
149 | 164 |
|
150 | | - return resourceNameInCamelCase.toString(); |
| 165 | + String resourceTypeElementContent = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); |
| 166 | + preparedStatement.setString(1, resourceName); |
| 167 | + preparedStatement.setString(2, resourceCategory); |
| 168 | + preparedStatement.setString(3, resourceTypeElementContent); |
| 169 | + preparedStatement.executeUpdate(); |
| 170 | + } catch (SQLException exception) { |
| 171 | + logger.warn("Unable to insert resource type [{}] in the database. Skipping it.", filePath, exception); |
| 172 | + } catch (Exception exception) { |
| 173 | + logger.warn("Unable to read file: [{}]. Skipping it.", filePath, exception); |
| 174 | + } |
151 | 175 | } |
152 | 176 | } |
0 commit comments