Skip to content

Commit 6978c18

Browse files
Stephen Hoogendijkustcweizhou
authored andcommitted
CLOUDSTACK-7308 - Adds tagging support for security group rules
1 parent 3bcd22b commit 6978c18

4 files changed

Lines changed: 44 additions & 0 deletions

File tree

api/src/com/cloud/server/ResourceTag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public enum ResourceObjectType {
3535
PortForwardingRule(true, true),
3636
FirewallRule(true, true),
3737
SecurityGroup(true, false),
38+
SecurityGroupRule(true, false),
3839
PublicIpAddress(true, true),
3940
Project(true, false),
4041
Vpc(true, true),

api/src/org/apache/cloudstack/api/response/SecurityGroupRuleResponse.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.cloud.network.security.SecurityRule;
2626
import com.cloud.serializer.Param;
2727

28+
import java.util.Set;
29+
2830
@EntityReference(value = SecurityRule.class)
2931
public class SecurityGroupRuleResponse extends BaseResponse {
3032
@SerializedName("ruleid")
@@ -63,6 +65,10 @@ public class SecurityGroupRuleResponse extends BaseResponse {
6365
@Param(description = "the CIDR notation for the base IP address of the security group rule")
6466
private String cidr;
6567

68+
@SerializedName(ApiConstants.TAGS)
69+
@Param(description = "the list of resource tags associated with the rule", responseObject = ResourceTagResponse.class)
70+
private java.util.Set<ResourceTagResponse> tags;
71+
6672
public String getRuleId() {
6773
return ruleId;
6874
}
@@ -161,4 +167,12 @@ public boolean equals(Object obj) {
161167
return false;
162168
return true;
163169
}
170+
171+
public void setTags(Set<ResourceTagResponse> tags) {
172+
this.tags = tags;
173+
}
174+
175+
public void addTag(ResourceTagResponse tag) {
176+
this.tags.add(tag);
177+
}
164178
}

server/src/com/cloud/api/query/dao/SecurityGroupJoinDaoImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
package com.cloud.api.query.dao;
1818

1919
import java.util.ArrayList;
20+
import java.util.HashSet;
2021
import java.util.List;
22+
import java.util.Set;
2123

2224
import javax.ejb.Local;
2325
import javax.inject.Inject;
2426

27+
import com.cloud.server.ResourceTag;
28+
import org.apache.cloudstack.api.response.ResourceTagResponse;
2529
import org.apache.log4j.Logger;
2630
import org.springframework.stereotype.Component;
2731

@@ -48,6 +52,9 @@ public class SecurityGroupJoinDaoImpl extends GenericDaoBase<SecurityGroupJoinVO
4852
@Inject
4953
private ConfigurationDao _configDao;
5054

55+
@Inject
56+
private ResourceTagJoinDao _resourceTagJoinDao;
57+
5158
private final SearchBuilder<SecurityGroupJoinVO> sgSearch;
5259

5360
private final SearchBuilder<SecurityGroupJoinVO> sgIdSearch;
@@ -99,6 +106,16 @@ public SecurityGroupResponse newSecurityGroupResponse(SecurityGroupJoinVO vsg, A
99106
ruleData.setCidr(vsg.getRuleAllowedSourceIpCidr());
100107
}
101108

109+
// list the tags by rule uuid
110+
List<ResourceTagJoinVO> tags = _resourceTagJoinDao.listBy(vsg.getRuleUuid(), ResourceTag.ResourceObjectType.SecurityGroupRule);
111+
Set<ResourceTagResponse> tagResponse = new HashSet<ResourceTagResponse>();
112+
for (ResourceTagJoinVO tag: tags) {
113+
tagResponse.add(ApiDBUtils.newResourceTagResponse(tag, false));
114+
}
115+
116+
// add the tags to the rule data
117+
ruleData.setTags(tagResponse);
118+
102119
if (vsg.getRuleType() == SecurityRuleType.IngressRule) {
103120
ruleData.setObjectName("ingressrule");
104121
sgResponse.addSecurityGroupIngressRule(ruleData);

server/src/com/cloud/tags/TaggedResourceManagerImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.cloud.network.rules.FirewallRuleVO;
5656
import com.cloud.network.rules.PortForwardingRuleVO;
5757
import com.cloud.network.security.SecurityGroupVO;
58+
import com.cloud.network.security.SecurityGroupRuleVO;
5859
import com.cloud.network.vpc.NetworkACLItemVO;
5960
import com.cloud.network.vpc.NetworkACLVO;
6061
import com.cloud.network.vpc.StaticRouteVO;
@@ -103,6 +104,7 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso
103104
s_typeMap.put(ResourceObjectType.PortForwardingRule, PortForwardingRuleVO.class);
104105
s_typeMap.put(ResourceObjectType.FirewallRule, FirewallRuleVO.class);
105106
s_typeMap.put(ResourceObjectType.SecurityGroup, SecurityGroupVO.class);
107+
s_typeMap.put(ResourceObjectType.SecurityGroupRule, SecurityGroupRuleVO.class);
106108
s_typeMap.put(ResourceObjectType.PublicIpAddress, IPAddressVO.class);
107109
s_typeMap.put(ResourceObjectType.Project, ProjectVO.class);
108110
s_typeMap.put(ResourceObjectType.Vpc, VpcVO.class);
@@ -178,6 +180,16 @@ private Pair<Long, Long> getAccountDomain(long resourceId, ResourceObjectType re
178180
Object entity = _entityMgr.findById(clazz, resourceId);
179181
Long accountId = null;
180182
Long domainId = null;
183+
184+
// if the resource type is a security group rule, get the accountId and domainId from the security group itself
185+
if (resourceType == ResourceObjectType.SecurityGroupRule) {
186+
SecurityGroupRuleVO rule = (SecurityGroupRuleVO)entity;
187+
Object SecurityGroup = _entityMgr.findById(s_typeMap.get(ResourceObjectType.SecurityGroup), rule.getSecurityGroupId());
188+
189+
accountId = ((SecurityGroupVO)SecurityGroup).getAccountId();
190+
domainId = ((SecurityGroupVO)SecurityGroup).getDomainId();
191+
}
192+
181193
if (entity instanceof OwnedBy) {
182194
accountId = ((OwnedBy)entity).getAccountId();
183195
}

0 commit comments

Comments
 (0)