3737import com .cloud .async .AsyncJob ;
3838import com .cloud .event .EventTypes ;
3939import com .cloud .network .security .IngressRule ;
40+ import com .cloud .network .security .SecurityGroup ;
4041import com .cloud .user .Account ;
41- import com .cloud .user .UserContext ;
4242import com .cloud .utils .StringUtils ;
4343
44- @ Implementation (responseObject =IngressRuleResponse .class , description ="Authorizes a particular ingress rule for this security group" ) @ SuppressWarnings ("rawtypes" )
44+ @ Implementation (responseObject = IngressRuleResponse .class , description = "Authorizes a particular ingress rule for this security group" )
45+ @ SuppressWarnings ("rawtypes" )
4546public class AuthorizeSecurityGroupIngressCmd extends BaseAsyncCmd {
46- public static final Logger s_logger = Logger .getLogger (AuthorizeSecurityGroupIngressCmd .class .getName ());
47+ public static final Logger s_logger = Logger .getLogger (AuthorizeSecurityGroupIngressCmd .class .getName ());
4748
4849 private static final String s_name = "authorizesecuritygroupingress" ;
4950
50- /////////////////////////////////////////////////////
51- //////////////// API parameters /////////////////////
52- /////////////////////////////////////////////////////
51+ // ///////////////////////////////////////////////////
52+ // ////////////// API parameters /////////////////////
53+ // ///////////////////////////////////////////////////
5354
54- @ Parameter (name = ApiConstants .PROTOCOL , type = CommandType .STRING , description = "TCP is default. UDP is the other supported protocol" )
55+ @ Parameter (name = ApiConstants .PROTOCOL , type = CommandType .STRING , description = "TCP is default. UDP is the other supported protocol" )
5556 private String protocol ;
5657
57- @ Parameter (name = ApiConstants .START_PORT , type = CommandType .INTEGER , description = "start port for this ingress rule" )
58+ @ Parameter (name = ApiConstants .START_PORT , type = CommandType .INTEGER , description = "start port for this ingress rule" )
5859 private Integer startPort ;
5960
60- @ Parameter (name = ApiConstants .END_PORT , type = CommandType .INTEGER , description = "end port for this ingress rule" )
61+ @ Parameter (name = ApiConstants .END_PORT , type = CommandType .INTEGER , description = "end port for this ingress rule" )
6162 private Integer endPort ;
6263
63- @ Parameter (name = ApiConstants .ICMP_TYPE , type = CommandType .INTEGER , description = "type of the icmp message being sent" )
64+ @ Parameter (name = ApiConstants .ICMP_TYPE , type = CommandType .INTEGER , description = "type of the icmp message being sent" )
6465 private Integer icmpType ;
6566
66- @ Parameter (name = ApiConstants .ICMP_CODE , type = CommandType .INTEGER , description = "error code for this icmp message" )
67+ @ Parameter (name = ApiConstants .ICMP_CODE , type = CommandType .INTEGER , description = "error code for this icmp message" )
6768 private Integer icmpCode ;
6869
69- @ Parameter (name = ApiConstants .SECURITY_GROUP_ID , type = CommandType .LONG , required = true , description = "The ID of the security group" )
70+ @ Parameter (name = ApiConstants .SECURITY_GROUP_ID , type = CommandType .LONG , required = true , description = "The ID of the security group" )
7071 private Long securityGroupId ;
7172
72- @ Parameter (name = ApiConstants .CIDR_LIST , type = CommandType .LIST , collectionType = CommandType .STRING , description = "the cidr list associated" )
73- private List cidrList ;
73+ @ Parameter (name = ApiConstants .CIDR_LIST , type = CommandType .LIST , collectionType = CommandType .STRING , description = "the cidr list associated" )
74+ private List < String > cidrList ;
7475
75- @ Parameter (name = ApiConstants .USER_SECURITY_GROUP_LIST , type = CommandType .MAP , description = "user to security group mapping" )
76+ @ Parameter (name = ApiConstants .USER_SECURITY_GROUP_LIST , type = CommandType .MAP , description = "user to security group mapping" )
7677 private Map userSecurityGroupList ;
7778
78- @ Parameter (name =ApiConstants .ACCOUNT , type =CommandType .STRING , description ="an optional account for the security group. Must be used with domainId." )
79- private String accountName ;
79+ // ///////////////////////////////////////////////////
80+ // ///////////////// Accessors ///////////////////////
81+ // ///////////////////////////////////////////////////
8082
81- @ Parameter (name =ApiConstants .DOMAIN_ID , type =CommandType .LONG , description ="an optional domainId for the security group. If the account parameter is used, domainId must also be used." )
82- private Long domainId ;
83-
84-
85- /////////////////////////////////////////////////////
86- /////////////////// Accessors ///////////////////////
87- /////////////////////////////////////////////////////
88-
89- public String getAccountName () {
90- return accountName ;
91- }
92-
93- public List getCidrList () {
83+ public List <String > getCidrList () {
9484 return cidrList ;
9585 }
9686
97- public Long getDomainId () {
98- return domainId ;
99- }
100-
10187 public Integer getEndPort () {
10288 return endPort ;
10389 }
@@ -129,34 +115,24 @@ public Map getUserSecurityGroupList() {
129115 return userSecurityGroupList ;
130116 }
131117
132-
133- /////////////////////////////////////////////////////
134- /////////////// API Implementation///////////////////
135- /////////////////////////////////////////////////////
118+ // ///////////////////////////////////////////////////
119+ // ///////////// API Implementation///////////////////
120+ // ///////////////////////////////////////////////////
136121
137122 @ Override
138123 public String getCommandName () {
139124 return s_name ;
140125 }
141126
142127 public static String getResultObjectName () {
143- return "securitygroup" ;
128+ return "securitygroup" ;
144129 }
145130
146131 @ Override
147132 public long getEntityOwnerId () {
148- Account account = UserContext .current ().getCaller ();
149- if ((account == null ) || isAdmin (account .getType ())) {
150- if ((domainId != null ) && (accountName != null )) {
151- Account userAccount = _responseGenerator .findAccountByNameDomain (accountName , domainId );
152- if (userAccount != null ) {
153- return userAccount .getId ();
154- }
155- }
156- }
157-
158- if (account != null ) {
159- return account .getId ();
133+ SecurityGroup group = _entityMgr .findById (SecurityGroup .class , getSecurityGroupId ());
134+ if (group != null ) {
135+ return group .getAccountId ();
160136 }
161137
162138 return Account .ACCOUNT_ID_SYSTEM ; // no account info given, parent this command to SYSTEM so ERROR events are tracked
@@ -175,15 +151,15 @@ public String getEventDescription() {
175151 Collection userGroupCollection = getUserSecurityGroupList ().values ();
176152 Iterator iter = userGroupCollection .iterator ();
177153
178- HashMap userGroup = (HashMap )iter .next ();
179- String group = (String )userGroup .get ("group" );
180- String authorizedAccountName = (String )userGroup .get ("account" );
154+ HashMap userGroup = (HashMap ) iter .next ();
155+ String group = (String ) userGroup .get ("group" );
156+ String authorizedAccountName = (String ) userGroup .get ("account" );
181157 sb .append (group + "/" + authorizedAccountName );
182158
183159 while (iter .hasNext ()) {
184- userGroup = (HashMap )iter .next ();
185- group = (String )userGroup .get ("group" );
186- authorizedAccountName = (String )userGroup .get ("account" );
160+ userGroup = (HashMap ) iter .next ();
161+ group = (String ) userGroup .get ("group" );
162+ authorizedAccountName = (String ) userGroup .get ("account" );
187163 sb .append (", " + group + "/" + authorizedAccountName );
188164 }
189165 } else if (getCidrList () != null ) {
@@ -193,26 +169,26 @@ public String getEventDescription() {
193169 sb .append ("<error: no ingress parameters>" );
194170 }
195171
196- return "authorizing ingress to group: " + getSecurityGroupId () + " to " + sb .toString ();
172+ return "authorizing ingress to group: " + getSecurityGroupId () + " to " + sb .toString ();
197173 }
198-
174+
199175 @ Override
200- public void execute (){
176+ public void execute () {
201177 List <? extends IngressRule > ingressRules = _securityGroupService .authorizeSecurityGroupIngress (this );
202- if (ingressRules != null && ! ingressRules .isEmpty ()) {
203- SecurityGroupResponse response = _responseGenerator .createSecurityGroupResponseFromIngressRule (ingressRules );
178+ if (ingressRules != null && !ingressRules .isEmpty ()) {
179+ SecurityGroupResponse response = _responseGenerator .createSecurityGroupResponseFromIngressRule (ingressRules );
204180 this .setResponseObject (response );
205181 } else {
206182 throw new ServerApiException (BaseCmd .INTERNAL_ERROR , "Failed to authorize security group ingress rule(s)" );
207183 }
208-
184+
209185 }
210-
186+
211187 @ Override
212188 public AsyncJob .Type getInstanceType () {
213189 return AsyncJob .Type .SecurityGroup ;
214190 }
215-
191+
216192 @ Override
217193 public Long getInstanceId () {
218194 return getSecurityGroupId ();
0 commit comments