Skip to content

Commit 2d6be0c

Browse files
add category and projection
1 parent cc63b78 commit 2d6be0c

13 files changed

Lines changed: 204 additions & 73 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ public class ApiConstants {
13301330

13311331
// NIMBLE related
13321332
public static final String IAC_RESOURCE_TYPE_CONTENT = "iacresourcetypecontent";
1333+
public static final String SHOW_IAC_RESOURCE_TYPE_CONTENT = "showiacresourcetypecontent";
13331334

13341335
public static final String PARAMETER_DESCRIPTION_ACTIVATION_RULE = "Quota tariff's activation rule. It can receive a JS script that results in either " +
13351336
"a boolean or a numeric value: if it results in a boolean value, the tariff value will be applied according to the result; if it results in a numeric value, the " +

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

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.List;
3131

3232
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");
3434

3535
@Override
3636
public String[] getUpgradableVersionRange() {
@@ -110,43 +110,67 @@ protected void updateBackupScheduleOwnership(Connection conn) {
110110
}
111111

112112
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");
135153
}
136154

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));
147163
}
148-
}
149164

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+
}
151175
}
152176
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `cloud`.`iac_resource_types` (
2525
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
2626
`uuid` VARCHAR(40) UNIQUE,
2727
`name` VARCHAR(100) NOT NULL COMMENT 'Profile''s element name.',
28+
`category` VARCHAR(100) NOT NULL COMMENT 'Profile''s element category.',
2829
`element_content` TEXT NOT NULL COMMENT 'Profile''s element content.',
2930
PRIMARY KEY (`id`)
3031
);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command;
1818

19+
import com.cloud.exception.InvalidParameterValueException;
1920
import org.apache.cloudstack.acl.RoleType;
2021
import org.apache.cloudstack.api.APICommand;
2122
import org.apache.cloudstack.api.ApiConstants;
@@ -25,6 +26,7 @@
2526
import org.apache.cloudstack.api.response.ListResponse;
2627
import org.apache.cloudstack.persistence.iactemplatesprofile.IacResourceType;
2728
import org.apache.cloudstack.service.NimbleService;
29+
import org.apache.commons.lang3.EnumUtils;
2830

2931
import javax.inject.Inject;
3032

@@ -42,6 +44,13 @@ public class ListIacResourceTypesCmd extends BaseListCmd {
4244
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "The name of the IaC resource type.")
4345
private String name;
4446

47+
@Parameter(name = ApiConstants.CATEGORY, type = CommandType.STRING, description = "The category of the IaC resource type. " +
48+
"Valid values are: compute, storage, network, service_offering and cloud_access_management.")
49+
private String category;
50+
51+
@Parameter(name = ApiConstants.SHOW_IAC_RESOURCE_TYPE_CONTENT, type = CommandType.BOOLEAN, description = "Whether to return the IaC resource type content. Defaults to false.")
52+
private boolean showIacResourceTypeContent = false;
53+
4554
public Long getId() {
4655
return id;
4756
}
@@ -50,6 +59,24 @@ public String getName() {
5059
return name;
5160
}
5261

62+
public IacResourceType.Category getCategory() {
63+
if (category == null) {
64+
return null;
65+
}
66+
67+
IacResourceType.Category iacResourceTypeCategory = EnumUtils.getEnumIgnoreCase(IacResourceType.Category.class, category);
68+
if (iacResourceTypeCategory == null) {
69+
throw new InvalidParameterValueException(
70+
String.format("The IaC resource type category [%s] is invalid. Valid values are: compute, storage, network, service_offering and cloud_access_management.", category));
71+
}
72+
73+
return iacResourceTypeCategory;
74+
}
75+
76+
public boolean showIacResourceTypeContent() {
77+
return showIacResourceTypeContent;
78+
}
79+
5380
@Override
5481
public void execute() {
5582
ListResponse<IacResourceTypeResponse> response = nimbleService.listIacResourceTypes(this);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class IacResourceTypeResponse extends BaseResponse {
3333
@Param(description = "The name of the IaC resource type.")
3434
private String name;
3535

36+
@SerializedName(ApiConstants.CATEGORY)
37+
@Param(description = "The category of the IaC resource type.")
38+
private IacResourceType.Category category;
39+
3640
@SerializedName(ApiConstants.IAC_RESOURCE_TYPE_CONTENT)
3741
@Param(description = "The content of the IaC resource type.")
3842
private String iacResourceTypeContent;
@@ -53,6 +57,14 @@ public void setName(String name) {
5357
this.name = name;
5458
}
5559

60+
public IacResourceType.Category getCategory() {
61+
return category;
62+
}
63+
64+
public void setCategory(IacResourceType.Category category) {
65+
this.category = category;
66+
}
67+
5668
public String getIacResourceTypeContent() {
5769
return iacResourceTypeContent;
5870
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/response/NimbleResponseBuilder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919
import org.apache.cloudstack.persistence.iactemplatesprofile.IacResourceType;
2020

2121
public class NimbleResponseBuilder {
22-
public IacResourceTypeResponse createIacResourceTypeResponse(IacResourceType iacResourceType) {
22+
public IacResourceTypeResponse createIacResourceTypeResponse(IacResourceType iacResourceType, boolean showIacResourceTypeContent) {
2323
IacResourceTypeResponse response = new IacResourceTypeResponse();
2424
response.setId(iacResourceType.getUuid());
2525
response.setName(iacResourceType.getName());
26-
response.setIacResourceTypeContent(iacResourceType.getContent());
26+
response.setCategory(iacResourceType.getCategory());
27+
28+
if (showIacResourceTypeContent) {
29+
response.setIacResourceTypeContent(iacResourceType.getContent());
30+
}
31+
2732
return response;
2833
}
2934
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121

2222
public interface IacResourceType extends Identity, InternalIdentity {
2323
enum Category {
24-
CLOUD_ACCESS_MANAGEMENT, COMPUTE, SERVICE_OFFERINGS, NETWORK, STORAGE
24+
CLOUD_ACCESS_MANAGEMENT, COMPUTE, SERVICE_OFFERING, NETWORK, STORAGE
2525
}
2626

2727
String getName();
2828
String getContent();
29+
Category getCategory();
2930
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
import java.util.List;
2323

2424
public interface IacResourceTypeDao extends GenericDao<IacResourceTypeVO, Long> {
25-
Pair<List<IacResourceTypeVO>, Integer> listIacResourceTypes(Long id, String name, String keyword, Long pageSizeVal, Long startIndex);
25+
Pair<List<IacResourceTypeVO>, Integer> listIacResourceTypes(Long id, String name, IacResourceType.Category category, String keyword, Long pageSizeVal, Long startIndex);
2626
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class IacResourceTypeDaoImpl extends GenericDaoBase<IacResourceTypeVO, Long> implements IacResourceTypeDao {
3030
private static final String ID = "id";
3131
private static final String NAME = "name";
32+
private static final String CATEGORY = "category";
3233
private static final String NAME_LIKE_KEYWORD = "nameLikeKeyword";
3334

3435
private final SearchBuilder<IacResourceTypeVO> listIacResourceTypesSearchBuilder;
@@ -42,10 +43,11 @@ public IacResourceTypeDaoImpl() {
4243
}
4344

4445
@Override
45-
public Pair<List<IacResourceTypeVO>, Integer> listIacResourceTypes(Long id, String name, String keyword, Long pageSizeVal, Long startIndex) {
46+
public Pair<List<IacResourceTypeVO>, Integer> listIacResourceTypes(Long id, String name, IacResourceType.Category category, String keyword, Long pageSizeVal, Long startIndex) {
4647
SearchCriteria<IacResourceTypeVO> searchCriteria = listIacResourceTypesSearchBuilder.create();
4748
searchCriteria.setParametersIfNotNull(ID, id);
4849
searchCriteria.setParametersIfNotNull(NAME, name);
50+
searchCriteria.setParametersIfNotNull(CATEGORY, category);
4951
if (keyword != null) {
5052
searchCriteria.setParameters(NAME_LIKE_KEYWORD, "%" + keyword + "%");
5153
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import javax.persistence.Column;
2020
import javax.persistence.Entity;
21+
import javax.persistence.EnumType;
22+
import javax.persistence.Enumerated;
2123
import javax.persistence.GeneratedValue;
2224
import javax.persistence.GenerationType;
2325
import javax.persistence.Id;
@@ -41,6 +43,10 @@ public class IacResourceTypeVO implements IacResourceType {
4143
@Column(name = "content", nullable = false, length = 65535)
4244
private String content;
4345

46+
@Column(name = "category", nullable = false, length = 100)
47+
@Enumerated(value = EnumType.STRING)
48+
private IacResourceType.Category category;
49+
4450
@Override
4551
public long getId() {
4652
return id;
@@ -60,4 +66,9 @@ public String getName() {
6066
public String getContent() {
6167
return content;
6268
}
69+
70+
@Override
71+
public Category getCategory() {
72+
return category;
73+
}
6374
}

0 commit comments

Comments
 (0)