content: // 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.resource;
  
  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.ResourceLimitResponse;
  import org.apache.cloudstack.context.CallContext;
  
  import com.cloud.configuration.ResourceLimit;
  
  @APICommand(name = "updateResourceLimit", description = "Updates resource limits for an account or domain.", responseObject = ResourceLimitResponse.class,
          requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
  public class UpdateResourceLimitCmd extends BaseCmd {
  
  
      /////////////////////////////////////////////////////
      //////////////// API parameters /////////////////////
      /////////////////////////////////////////////////////
  
      @Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "Update resource for a specified account. Must be used with the domainId parameter.")
      private String accountName;
  
      @Parameter(name = ApiConstants.DOMAIN_ID,
                 type = CommandType.UUID,
                 entityType = DomainResponse.class,
                 description = "Update resource limits for all accounts in specified domain. If used with the account parameter, updates resource limits for a specified account in specified domain.")
      private Long domainId;
  
      @Parameter(name = ApiConstants.PROJECT_ID, type = CommandType.UUID, entityType = ProjectResponse.class, description = "Update resource limits for project")
      private Long projectId;
  
      @Parameter(name = ApiConstants.MAX, type = CommandType.LONG, description = "  Maximum resource limit.")
      private Long max;
  
      @Parameter(name = ApiConstants.RESOURCE_TYPE,
                 type = CommandType.INTEGER,
                 required = true,
                 description = "Type of resource to update. Values are 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 and 11. "
                     + "0 - Instance. Number of instances a user can create. "
                     + "1 - IP. Number of public IP addresses a user can own. "
                     + "2 - Volume. Number of disk volumes a user can create. "
                     + "3 - Snapshot. Number of snapshots a user can create. "
                     + "4 - Template. Number of templates that a user can register/create. "
                     + "5 - Project. Number of projects a user can create. "
                     + "6 - Network. Number of guest network a user can create. "
                     + "7 - VPC. Number of VPC a user can create. "
                     + "8 - CPU. Total number of CPU cores a user can use. "
                     + "9 - Memory. Total Memory (in MB) a user can use. "
                     + "10 - PrimaryStorage. Total primary storage space (in GiB) a user can use. "
                     + "11 - SecondaryStorage. Total secondary storage space (in GiB) a user can use. ")
      private Integer resourceType;
  
      @Parameter(name = ApiConstants.TAG, type = CommandType.STRING, description = "Tag for the resource type", since = "4.20.0")
      private String tag;
  
      /////////////////////////////////////////////////////
      /////////////////// Accessors ///////////////////////
      /////////////////////////////////////////////////////
  
      public Long getMax() {
          return max;
      }
  
      public Long getDomainId() {
          return domainId;
      }
  
      public String getTag() {
          return tag;
      }
  
      public Integer getResourceType() {
          return resourceType;
      }
  
      /////////////////////////////////////////////////////
      /////////////// API Implementation///////////////////
      /////////////////////////////////////////////////////
  
      @Override
      public long getEntityOwnerId() {
          Long accountId = _accountService.finalyzeAccountId(accountName, domainId, projectId, true);
          if (accountId == null) {
              return CallContext.current().getCallingAccount().getId();
          }
  
          return accountId;
      }
  
      @Override
      public void execute() {
          ResourceLimit result = _resourceLimitService.updateResourceLimit(_accountService.finalyzeAccountId(accountName, domainId, projectId, true), getDomainId(), resourceType, max, getTag());
          if (result != null || (result == null && max != null && max.longValue() == -1L)) {
              ResourceLimitResponse response = _responseGenerator.createResourceLimitResponse(result);
              response.setResponseName(getCommandName());
              this.setResponseObject(response);
          } else {
              throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update resource limit");
          }
      }
  }
  