| // 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.cloudstack.api.APICommand; |
| import org.apache.cloudstack.api.ApiConstants; |
| import org.apache.cloudstack.api.ApiErrorCode; |
| import org.apache.cloudstack.api.BaseCmd; |
| import org.apache.cloudstack.api.Parameter; |
| import org.apache.cloudstack.api.ServerApiException; |
| import org.apache.cloudstack.api.response.DomainResponse; |
| import org.apache.cloudstack.api.response.ProjectResponse; |
| import org.apache.cloudstack.api.response.SecurityGroupResponse; |
| import org.apache.cloudstack.context.CallContext; |
| |
| import com.cloud.network.security.SecurityGroup; |
| import com.cloud.user.Account; |
| |
| @APICommand(name = "createSecurityGroup", responseObject = SecurityGroupResponse.class, description = "Creates a security group", entityType = {SecurityGroup.class}, |
| requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) |
| public class CreateSecurityGroupCmd extends BaseCmd { |
| |
| |
| // /////////////////////////////////////////////////// |
| // ////////////// API parameters ///////////////////// |
| // /////////////////////////////////////////////////// |
| |
| @Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "an optional account for the security group. Must be used with domainId.") |
| private String accountName; |
| |
| @Parameter(name = ApiConstants.DOMAIN_ID, |
| type = CommandType.UUID, |
| description = "an optional domainId for the security group. If the account parameter is used, domainId must also be used.", |
| entityType = DomainResponse.class) |
| private Long domainId; |
| |
| @Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "the description of the security group") |
| private String description; |
| |
| @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "name of the security group") |
| private String securityGroupName; |
| |
| @Parameter(name = ApiConstants.PROJECT_ID, type = CommandType.UUID, description = "Create security group for project", entityType = ProjectResponse.class) |
| private Long projectId; |
| |
| // /////////////////////////////////////////////////// |
| // ///////////////// Accessors /////////////////////// |
| // /////////////////////////////////////////////////// |
| |
| public String getAccountName() { |
| return accountName; |
| } |
| |
| public String getDescription() { |
| return description; |
| } |
| |
| public Long getDomainId() { |
| return domainId; |
| } |
| |
| public String getSecurityGroupName() { |
| return securityGroupName; |
| } |
| |
| public Long getProjectId() { |
| return projectId; |
| } |
| |
| // /////////////////////////////////////////////////// |
| // ///////////// API Implementation/////////////////// |
| // /////////////////////////////////////////////////// |
| |
| @Override |
| public long getEntityOwnerId() { |
| Account account = CallContext.current().getCallingAccount(); |
| if ((account == null) || _accountService.isAdmin(account.getId())) { |
| if ((domainId != null) && (accountName != null)) { |
| Account userAccount = _responseGenerator.findAccountByNameDomain(accountName, domainId); |
| if (userAccount != null) { |
| return userAccount.getId(); |
| } |
| } |
| } |
| |
| if (account != null) { |
| return account.getId(); |
| } |
| |
| return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are |
| // tracked |
| } |
| |
| @Override |
| public void execute() { |
| SecurityGroup group = _securityGroupService.createSecurityGroup(this); |
| if (group != null) { |
| SecurityGroupResponse response = _responseGenerator.createSecurityGroupResponse(group); |
| response.setResponseName(getCommandName()); |
| setResponseObject(response); |
| } else { |
| throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create security group"); |
| } |
| } |
| } |