|
| 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 | +package org.apache.cloudstack.api.command.admin.guest; |
| 18 | + |
| 19 | +import org.apache.log4j.Logger; |
| 20 | + |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 22 | +import org.apache.cloudstack.api.ApiCommandJobType; |
| 23 | +import org.apache.cloudstack.api.ApiConstants; |
| 24 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 25 | +import org.apache.cloudstack.api.BaseAsyncCreateCmd; |
| 26 | +import org.apache.cloudstack.api.Parameter; |
| 27 | +import org.apache.cloudstack.api.ServerApiException; |
| 28 | +import org.apache.cloudstack.api.response.GuestOSCategoryResponse; |
| 29 | +import org.apache.cloudstack.api.response.GuestOSResponse; |
| 30 | +import org.apache.cloudstack.context.CallContext; |
| 31 | + |
| 32 | +import com.cloud.event.EventTypes; |
| 33 | +import com.cloud.storage.GuestOS; |
| 34 | +import com.cloud.user.Account; |
| 35 | + |
| 36 | +@APICommand(name = "addGuestOs", description = "Add a new guest OS type", responseObject = GuestOSResponse.class, |
| 37 | + since = "4.4.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) |
| 38 | +public class AddGuestOsCmd extends BaseAsyncCreateCmd { |
| 39 | + public static final Logger s_logger = Logger.getLogger(AddGuestOsCmd.class.getName()); |
| 40 | + |
| 41 | + private static final String s_name = "addguestosresponse"; |
| 42 | + |
| 43 | + ///////////////////////////////////////////////////// |
| 44 | + //////////////// API parameters ///////////////////// |
| 45 | + ///////////////////////////////////////////////////// |
| 46 | + |
| 47 | + @Parameter(name = ApiConstants.OS_CATEGORY_ID, type = CommandType.UUID, entityType = GuestOSCategoryResponse.class, required = true, description = "ID of Guest OS category") |
| 48 | + private Long osCategoryId; |
| 49 | + |
| 50 | + @Parameter(name = ApiConstants.OS_DISPLAY_NAME, type = CommandType.STRING, required = true, description = "Unique display name for Guest OS") |
| 51 | + private String osDisplayName; |
| 52 | + |
| 53 | + @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = false, description = "Optional name for Guest OS") |
| 54 | + private String osName; |
| 55 | + |
| 56 | + |
| 57 | +///////////////////////////////////////////////////// |
| 58 | + /////////////////// Accessors /////////////////////// |
| 59 | + ///////////////////////////////////////////////////// |
| 60 | + |
| 61 | + public Long getOsCategoryId() { |
| 62 | + return osCategoryId; |
| 63 | + } |
| 64 | + |
| 65 | + public String getOsDisplayName() { |
| 66 | + return osDisplayName; |
| 67 | + } |
| 68 | + |
| 69 | + public String getOsName() { |
| 70 | + return osName; |
| 71 | + } |
| 72 | + |
| 73 | + ///////////////////////////////////////////////////// |
| 74 | + /////////////// API Implementation/////////////////// |
| 75 | + ///////////////////////////////////////////////////// |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getCommandName() { |
| 79 | + return s_name; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public long getEntityOwnerId() { |
| 84 | + return Account.ACCOUNT_ID_SYSTEM; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void create() { |
| 89 | + GuestOS guestOs = _mgr.addGuestOs(this); |
| 90 | + if (guestOs != null) { |
| 91 | + setEntityId(guestOs.getId()); |
| 92 | + setEntityUuid(guestOs.getUuid()); |
| 93 | + } else { |
| 94 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add new guest OS type entity"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void execute() { |
| 100 | + CallContext.current().setEventDetails("Guest OS Id: " + getEntityId()); |
| 101 | + GuestOS guestOs = _mgr.getAddedGuestOs(getEntityId()); |
| 102 | + if (guestOs != null) { |
| 103 | + GuestOSResponse response = _responseGenerator.createGuestOSResponse(guestOs); |
| 104 | + response.setResponseName(getCommandName()); |
| 105 | + setResponseObject(response); |
| 106 | + } else { |
| 107 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add new guest OS type"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getEventType() { |
| 113 | + return EventTypes.EVENT_GUEST_OS_ADD; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getEventDescription() { |
| 118 | + return "adding a new guest OS type Id: " + getEntityId(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public ApiCommandJobType getInstanceType() { |
| 123 | + return ApiCommandJobType.GuestOs; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String getCreateEventType() { |
| 128 | + return EventTypes.EVENT_GUEST_OS_ADD; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String getCreateEventDescription() { |
| 133 | + return "adding new guest OS type"; |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments