|
| 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.framework.extensions.api; |
| 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.BaseAsyncCmd; |
| 28 | +import org.apache.cloudstack.api.Parameter; |
| 29 | +import org.apache.cloudstack.api.ServerApiException; |
| 30 | +import org.apache.cloudstack.api.response.ExtensionResponse; |
| 31 | +import org.apache.cloudstack.api.response.ManagementServerResponse; |
| 32 | +import org.apache.cloudstack.api.response.SuccessResponse; |
| 33 | +import org.apache.cloudstack.extension.Extension; |
| 34 | +import org.apache.cloudstack.framework.extensions.api.response.DownloadExtensionResponse; |
| 35 | +import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager; |
| 36 | + |
| 37 | +import com.cloud.event.EventTypes; |
| 38 | +import com.cloud.exception.ConcurrentOperationException; |
| 39 | +import com.cloud.user.Account; |
| 40 | + |
| 41 | +@APICommand(name = "downloadExtension", |
| 42 | + description = "To download the extension files as an archive", |
| 43 | + responseObject = SuccessResponse.class, |
| 44 | + responseHasSensitiveInfo = false, |
| 45 | + entityType = {Extension.class}, |
| 46 | + authorized = {RoleType.Admin}, |
| 47 | + since = "4.23.0") |
| 48 | +public class DownloadExtensionCmd extends BaseAsyncCmd { |
| 49 | + |
| 50 | + @Inject |
| 51 | + ExtensionsManager extensionsManager; |
| 52 | + |
| 53 | + ///////////////////////////////////////////////////// |
| 54 | + //////////////// API parameters ///////////////////// |
| 55 | + ///////////////////////////////////////////////////// |
| 56 | + |
| 57 | + @Parameter(name = ApiConstants.ID, type = CommandType.UUID, required = true, |
| 58 | + entityType = ExtensionResponse.class, description = "ID of the extension") |
| 59 | + private Long id; |
| 60 | + |
| 61 | + @Parameter(name = ApiConstants.MANAGEMENT_SERVER_ID, type = CommandType.UUID, |
| 62 | + entityType = ManagementServerResponse.class, |
| 63 | + description = "ID of the management server from which files are to be downloaded") |
| 64 | + private Long managementServerId; |
| 65 | + |
| 66 | + ///////////////////////////////////////////////////// |
| 67 | + /////////////////// Accessors /////////////////////// |
| 68 | + ///////////////////////////////////////////////////// |
| 69 | + |
| 70 | + public Long getId() { |
| 71 | + return id; |
| 72 | + } |
| 73 | + |
| 74 | + public Long getManagementServerId() { |
| 75 | + return managementServerId; |
| 76 | + } |
| 77 | + |
| 78 | + ///////////////////////////////////////////////////// |
| 79 | + /////////////// API Implementation/////////////////// |
| 80 | + ///////////////////////////////////////////////////// |
| 81 | + |
| 82 | + @Override |
| 83 | + public void execute() throws ServerApiException, ConcurrentOperationException { |
| 84 | + DownloadExtensionResponse response = extensionsManager.downloadExtension(this); |
| 85 | + if (response != null) { |
| 86 | + setResponseObject(response); |
| 87 | + } else { |
| 88 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to download extension"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public long getEntityOwnerId() { |
| 94 | + return Account.ACCOUNT_ID_SYSTEM; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public ApiCommandResourceType getApiResourceType() { |
| 99 | + return ApiCommandResourceType.Extension; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Long getApiResourceId() { |
| 104 | + return getId(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getEventType() { |
| 109 | + return EventTypes.EVENT_EXTENSION_DOWNLOAD; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getEventDescription() { |
| 114 | + return "Download extension: " + getId(); |
| 115 | + } |
| 116 | +} |
0 commit comments