|
| 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.kms; |
| 18 | + |
| 19 | +import com.cloud.user.Account; |
| 20 | +import org.apache.cloudstack.acl.RoleType; |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 22 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 23 | +import org.apache.cloudstack.api.ApiConstants; |
| 24 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 25 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 26 | +import org.apache.cloudstack.api.Parameter; |
| 27 | +import org.apache.cloudstack.api.ServerApiException; |
| 28 | +import org.apache.cloudstack.api.response.AsyncJobResponse; |
| 29 | +import org.apache.cloudstack.api.response.DomainResponse; |
| 30 | +import org.apache.cloudstack.api.response.ZoneResponse; |
| 31 | +import org.apache.cloudstack.framework.kms.KMSException; |
| 32 | +import org.apache.cloudstack.kms.KMSManager; |
| 33 | + |
| 34 | +import javax.inject.Inject; |
| 35 | + |
| 36 | +@APICommand(name = "migrateVolumesToKMS", |
| 37 | + description = "Migrates passphrase-based volumes to KMS (admin only)", |
| 38 | + responseObject = AsyncJobResponse.class, |
| 39 | + since = "4.23.0", |
| 40 | + authorized = {RoleType.Admin}, |
| 41 | + requestHasSensitiveInfo = false, |
| 42 | + responseHasSensitiveInfo = false) |
| 43 | +public class MigrateVolumesToKMSCmd extends BaseAsyncCmd { |
| 44 | + private static final String s_name = "migratevolumestokmsresponse"; |
| 45 | + |
| 46 | + @Inject |
| 47 | + private KMSManager kmsManager; |
| 48 | + |
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + //////////////// API parameters ///////////////////// |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + |
| 53 | + @Parameter(name = ApiConstants.ZONE_ID, |
| 54 | + required = true, |
| 55 | + type = CommandType.UUID, |
| 56 | + entityType = ZoneResponse.class, |
| 57 | + description = "Zone ID") |
| 58 | + private Long zoneId; |
| 59 | + |
| 60 | + @Parameter(name = ApiConstants.ACCOUNT, |
| 61 | + type = CommandType.STRING, |
| 62 | + description = "Migrate volumes for specific account") |
| 63 | + private String accountName; |
| 64 | + |
| 65 | + @Parameter(name = ApiConstants.DOMAIN_ID, |
| 66 | + type = CommandType.UUID, |
| 67 | + entityType = DomainResponse.class, |
| 68 | + description = "Domain ID") |
| 69 | + private Long domainId; |
| 70 | + |
| 71 | + ///////////////////////////////////////////////////// |
| 72 | + /////////////////// Accessors /////////////////////// |
| 73 | + ///////////////////////////////////////////////////// |
| 74 | + |
| 75 | + public Long getZoneId() { |
| 76 | + return zoneId; |
| 77 | + } |
| 78 | + |
| 79 | + public String getAccountName() { |
| 80 | + return accountName; |
| 81 | + } |
| 82 | + |
| 83 | + public Long getDomainId() { |
| 84 | + return domainId; |
| 85 | + } |
| 86 | + |
| 87 | + ///////////////////////////////////////////////////// |
| 88 | + /////////////// API Implementation/////////////////// |
| 89 | + ///////////////////////////////////////////////////// |
| 90 | + |
| 91 | + @Override |
| 92 | + public void execute() { |
| 93 | + try { |
| 94 | + kmsManager.migrateVolumesToKMS(this); |
| 95 | + } catch (KMSException e) { |
| 96 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, |
| 97 | + "Failed to migrate volumes to KMS: " + e.getMessage()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getCommandName() { |
| 103 | + return s_name; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public long getEntityOwnerId() { |
| 108 | + return Account.ACCOUNT_ID_SYSTEM; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getEventType() { |
| 113 | + return com.cloud.event.EventTypes.EVENT_VOLUME_MIGRATE_TO_KMS; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getEventDescription() { |
| 118 | + return "Migrating volumes to KMS for zone: " + _uuidMgr.getUuid(ZoneResponse.class, zoneId); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public ApiCommandResourceType getApiResourceType() { |
| 123 | + return ApiCommandResourceType.Zone; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Long getApiResourceId() { |
| 128 | + return zoneId; |
| 129 | + } |
| 130 | +} |
0 commit comments