|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.api.command.user.bootgroup; |
| 19 | + |
| 20 | +import javax.inject.Inject; |
| 21 | + |
| 22 | +import org.apache.cloudstack.acl.RoleType; |
| 23 | +import org.apache.cloudstack.api.APICommand; |
| 24 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 25 | +import org.apache.cloudstack.api.ApiConstants; |
| 26 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 27 | +import org.apache.cloudstack.api.BaseCmd; |
| 28 | +import org.apache.cloudstack.api.Parameter; |
| 29 | +import org.apache.cloudstack.api.ServerApiException; |
| 30 | +import org.apache.cloudstack.api.command.user.UserCmd; |
| 31 | +import org.apache.cloudstack.api.response.DomainResponse; |
| 32 | +import org.apache.cloudstack.api.response.InstanceBootGroupResponse; |
| 33 | +import org.apache.cloudstack.api.response.ProjectResponse; |
| 34 | +import org.apache.cloudstack.context.CallContext; |
| 35 | +import org.apache.cloudstack.vm.bootgroup.InstanceBootGroup; |
| 36 | +import org.apache.cloudstack.vm.bootgroup.InstanceBootGroupService; |
| 37 | + |
| 38 | +@APICommand(name = "createInstanceBootGroup", |
| 39 | + description = "Creates an instance boot group", |
| 40 | + responseObject = InstanceBootGroupResponse.class, |
| 41 | + entityType = {InstanceBootGroup.class}, |
| 42 | + requestHasSensitiveInfo = false, |
| 43 | + responseHasSensitiveInfo = false, |
| 44 | + authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User}) |
| 45 | +public class CreateInstanceBootGroupCmd extends BaseCmd implements UserCmd { |
| 46 | + |
| 47 | + @Inject |
| 48 | + InstanceBootGroupService instanceBootGroupService; |
| 49 | + |
| 50 | + @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "The name of the instance boot group") |
| 51 | + private String name; |
| 52 | + |
| 53 | + @Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "The description of the instance boot group") |
| 54 | + private String description; |
| 55 | + |
| 56 | + @Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "The account of the instance boot group. Must be used with domainId.") |
| 57 | + private String accountName; |
| 58 | + |
| 59 | + @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "The domain ID of the account owning the instance boot group") |
| 60 | + private Long domainId; |
| 61 | + |
| 62 | + @Parameter(name = ApiConstants.PROJECT_ID, type = CommandType.UUID, entityType = ProjectResponse.class, description = "The project of the instance boot group") |
| 63 | + private Long projectId; |
| 64 | + |
| 65 | + public String getName() { |
| 66 | + return name; |
| 67 | + } |
| 68 | + |
| 69 | + public String getDescription() { |
| 70 | + return description; |
| 71 | + } |
| 72 | + |
| 73 | + public String getAccountName() { |
| 74 | + return accountName; |
| 75 | + } |
| 76 | + |
| 77 | + public Long getDomainId() { |
| 78 | + return domainId; |
| 79 | + } |
| 80 | + |
| 81 | + public Long getProjectId() { |
| 82 | + return projectId; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public long getEntityOwnerId() { |
| 87 | + Long accountId = _accountService.finalizeAccountId(accountName, domainId, projectId, true); |
| 88 | + if (accountId == null) { |
| 89 | + return CallContext.current().getCallingAccount().getId(); |
| 90 | + } |
| 91 | + return accountId; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public ApiCommandResourceType getApiResourceType() { |
| 96 | + return ApiCommandResourceType.InstanceBootGroup; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void execute() { |
| 101 | + InstanceBootGroup result = instanceBootGroupService.createInstanceBootGroup(this); |
| 102 | + if (result == null) { |
| 103 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create instance boot group"); |
| 104 | + } |
| 105 | + InstanceBootGroupResponse response = instanceBootGroupService.createInstanceBootGroupResponse(result.getId()); |
| 106 | + response.setResponseName(getCommandName()); |
| 107 | + setResponseObject(response); |
| 108 | + } |
| 109 | +} |
0 commit comments