-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add new command to update security group name #3739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package org.apache.cloudstack.api.command.user.securitygroup; | ||
|
|
||
| import org.apache.log4j.Logger; | ||
|
|
||
| import org.apache.cloudstack.acl.RoleType; | ||
| import org.apache.cloudstack.acl.SecurityChecker.AccessType; | ||
| import org.apache.cloudstack.api.ACL; | ||
| import org.apache.cloudstack.api.APICommand; | ||
| import org.apache.cloudstack.api.ApiConstants; | ||
| import org.apache.cloudstack.api.ApiErrorCode; | ||
| import org.apache.cloudstack.api.BaseCustomIdCmd; | ||
| import org.apache.cloudstack.api.Parameter; | ||
| import org.apache.cloudstack.api.ServerApiException; | ||
| import org.apache.cloudstack.api.response.SecurityGroupResponse; | ||
|
|
||
| import com.cloud.network.security.SecurityGroup; | ||
| import com.cloud.user.Account; | ||
|
|
||
| @APICommand(name = UpdateSecurityGroupCmd.APINAME, description = "Updates a security group", responseObject = SecurityGroupResponse.class, entityType = {SecurityGroup.class}, | ||
| requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, | ||
| since = "4.14.0.0", | ||
| authorized = {RoleType.Admin}) | ||
| public class UpdateSecurityGroupCmd extends BaseCustomIdCmd { | ||
| public static final String APINAME = "updateSecurityGroup"; | ||
| public static final Logger s_logger = Logger.getLogger(UpdateSecurityGroupCmd.class.getName()); | ||
| private static final String s_name = "updatesecuritygroupresponse"; | ||
|
|
||
| ///////////////////////////////////////////////////// | ||
| //////////////// API parameters ///////////////////// | ||
| ///////////////////////////////////////////////////// | ||
|
|
||
| @ACL(accessType = AccessType.OperateEntry) | ||
| @Parameter(name = ApiConstants.ID, type = CommandType.UUID, required=true, description="The ID of the security group.", entityType=SecurityGroupResponse.class) | ||
| private Long id; | ||
|
|
||
| @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "The new name of the security group.") | ||
| private String name; | ||
|
|
||
| ///////////////////////////////////////////////////// | ||
| /////////////////// Accessors /////////////////////// | ||
| ///////////////////////////////////////////////////// | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| ///////////////////////////////////////////////////// | ||
| /////////////// API Implementation/////////////////// | ||
| ///////////////////////////////////////////////////// | ||
|
|
||
| @Override | ||
| public String getCommandName() { | ||
| return s_name; | ||
| } | ||
|
|
||
| @Override | ||
| public long getEntityOwnerId() { | ||
| SecurityGroup securityGroup = _entityMgr.findById(SecurityGroup.class, getId()); | ||
| if (securityGroup != null) { | ||
| return securityGroup.getAccountId(); | ||
| } | ||
|
|
||
| return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this an exceptional situation? wouldn't there always be at least root admin responsible for creating a sec-group? |
||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| SecurityGroup result = _securityGroupService.updateSecurityGroup(this); | ||
| if (result != null) { | ||
| SecurityGroupResponse response = _responseGenerator.createSecurityGroupResponse(result); | ||
| response.setResponseName(getCommandName()); | ||
| setResponseObject(response); | ||
| } else { | ||
| throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update security group"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void checkUuid() { | ||
| if (getCustomId() != null) { | ||
| _uuidMgr.checkUuid(getCustomId(), SecurityGroup.class); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| import org.apache.cloudstack.api.command.user.securitygroup.DeleteSecurityGroupCmd; | ||
| import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupEgressCmd; | ||
| import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupIngressCmd; | ||
| import org.apache.cloudstack.api.command.user.securitygroup.UpdateSecurityGroupCmd; | ||
| import org.apache.cloudstack.context.CallContext; | ||
| import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; | ||
| import org.apache.cloudstack.framework.config.dao.ConfigurationDao; | ||
|
|
@@ -879,6 +880,10 @@ public SecurityGroupVO createSecurityGroup(CreateSecurityGroupCmd cmd) throws Pe | |
| Account caller = CallContext.current().getCallingAccount(); | ||
| Account owner = _accountMgr.finalizeOwner(caller, cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId()); | ||
|
|
||
| if (StringUtils.isBlank(name)) { | ||
| throw new InvalidParameterValueException("Security group name cannot be empty"); | ||
| } | ||
|
|
||
| if (_securityGroupDao.isNameInUse(owner.getId(), owner.getDomainId(), cmd.getSecurityGroupName())) { | ||
| throw new InvalidParameterValueException("Unable to create security group, a group with name " + name + " already exists."); | ||
| } | ||
|
|
@@ -1117,6 +1122,60 @@ public void doInTransactionWithoutResult(TransactionStatus status) { | |
| s_logger.debug("Security group mappings are removed successfully for vm id=" + userVmId); | ||
| } | ||
|
|
||
| @DB | ||
| @Override | ||
| @ActionEvent(eventType = EventTypes.EVENT_SECURITY_GROUP_UPDATE, eventDescription = "updating security group") | ||
| public SecurityGroup updateSecurityGroup(UpdateSecurityGroupCmd cmd) { | ||
| final Long groupId = cmd.getId(); | ||
| final String newName = cmd.getName(); | ||
| Account caller = CallContext.current().getCallingAccount(); | ||
|
|
||
| SecurityGroupVO group = _securityGroupDao.findById(groupId); | ||
| if (group == null) { | ||
| throw new InvalidParameterValueException("Unable to find security group: " + groupId + "; failed to update security group."); | ||
| } | ||
|
|
||
| if (newName == null) { | ||
| s_logger.debug("security group name is not changed. id=" + groupId); | ||
| return group; | ||
| } | ||
|
|
||
| if (StringUtils.isBlank(newName)) { | ||
| throw new InvalidParameterValueException("Security group name cannot be empty"); | ||
| } | ||
|
|
||
| // check permissions | ||
| _accountMgr.checkAccess(caller, null, true, group); | ||
|
|
||
| return Transaction.execute(new TransactionCallback<SecurityGroupVO>() { | ||
| @Override | ||
| public SecurityGroupVO doInTransaction(TransactionStatus status) { | ||
| SecurityGroupVO group = _securityGroupDao.lockRow(groupId, true); | ||
| if (group == null) { | ||
| throw new InvalidParameterValueException("Unable to find security group by id " + groupId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the message here could better be "unable to acquire lock for security group with id " + groupId |
||
| } | ||
|
|
||
| if (newName.equals(group.getName())) { | ||
| s_logger.debug("security group name is not changed. id=" + groupId); | ||
| return group; | ||
| } else if (newName.equalsIgnoreCase(SecurityGroupManager.DEFAULT_GROUP_NAME)) { | ||
| throw new InvalidParameterValueException("The security group name " + SecurityGroupManager.DEFAULT_GROUP_NAME + " is reserved"); | ||
| } | ||
|
|
||
| if (group.getName().equalsIgnoreCase(SecurityGroupManager.DEFAULT_GROUP_NAME)) { | ||
| throw new InvalidParameterValueException("The default security group cannot be renamed"); | ||
| } | ||
|
|
||
| group.setName(newName); | ||
| _securityGroupDao.update(groupId, group); | ||
|
|
||
| s_logger.debug("Updated security group id=" + groupId); | ||
|
|
||
| return group; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @DB | ||
| @Override | ||
| @ActionEvent(eventType = EventTypes.EVENT_SECURITY_GROUP_DELETE, eventDescription = "deleting security group") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather see it constructed form the API name .toLower()+"response". no biggy