|
| 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.user.volume; |
| 18 | + |
| 19 | +import com.cloud.event.EventTypes; |
| 20 | +import com.cloud.exception.InvalidParameterValueException; |
| 21 | +import org.apache.cloudstack.acl.RoleType; |
| 22 | +import org.apache.cloudstack.api.APICommand; |
| 23 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 24 | +import org.apache.cloudstack.api.ApiConstants; |
| 25 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 26 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ResponseObject.ResponseView; |
| 29 | +import org.apache.cloudstack.api.ServerApiException; |
| 30 | +import org.apache.cloudstack.api.response.VolumeResponse; |
| 31 | +import org.apache.cloudstack.context.CallContext; |
| 32 | +import org.apache.log4j.Logger; |
| 33 | + |
| 34 | +import com.cloud.exception.ResourceAllocationException; |
| 35 | +import com.cloud.storage.Volume; |
| 36 | +import com.cloud.user.Account; |
| 37 | +import com.cloud.utils.Pair; |
| 38 | +import com.cloud.utils.StringUtils; |
| 39 | + |
| 40 | +import java.util.Arrays; |
| 41 | + |
| 42 | +@APICommand(name = "checkVolume", description = "Check the volume for any errors or leaks and also repairs when repair parameter is passed, this is currently supported for KVM only", responseObject = VolumeResponse.class, entityType = {Volume.class}, |
| 43 | + since = "4.19.1", |
| 44 | + authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User}) |
| 45 | +public class CheckAndRepairVolumeCmd extends BaseAsyncCmd { |
| 46 | + public static final Logger s_logger = Logger.getLogger(CheckAndRepairVolumeCmd.class.getName()); |
| 47 | + |
| 48 | + private static final String s_name = "checkandrepairvolumeresponse"; |
| 49 | + |
| 50 | + ///////////////////////////////////////////////////// |
| 51 | + //////////////// API parameters ///////////////////// |
| 52 | + ///////////////////////////////////////////////////// |
| 53 | + |
| 54 | + @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = VolumeResponse.class, required = true, description = "The ID of the volume") |
| 55 | + private Long id; |
| 56 | + |
| 57 | + @Parameter(name = ApiConstants.REPAIR, type = CommandType.STRING, required = false, description = "parameter to repair the volume, leaks or all are the possible values") |
| 58 | + private String repair; |
| 59 | + |
| 60 | + ///////////////////////////////////////////////////// |
| 61 | + /////////////////// Accessors /////////////////////// |
| 62 | + ///////////////////////////////////////////////////// |
| 63 | + |
| 64 | + public enum RepairValues { |
| 65 | + LEAKS, ALL |
| 66 | + } |
| 67 | + |
| 68 | + public Long getId() { |
| 69 | + return id; |
| 70 | + } |
| 71 | + |
| 72 | + public String getRepair() { |
| 73 | + if (org.apache.commons.lang3.StringUtils.isNotEmpty(repair)) { |
| 74 | + RepairValues repairType = Enum.valueOf(RepairValues.class, repair.toUpperCase()); |
| 75 | + if (repairType == null) { |
| 76 | + throw new InvalidParameterValueException(String.format("Repair parameter can only take the following values: %s" + Arrays.toString(RepairValues.values()))); |
| 77 | + } |
| 78 | + return repair.toLowerCase(); |
| 79 | + } |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + ///////////////////////////////////////////////////// |
| 84 | + /////////////// API Implementation/////////////////// |
| 85 | + ///////////////////////////////////////////////////// |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getCommandName() { |
| 89 | + return s_name; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public long getEntityOwnerId() { |
| 94 | + Volume volume = _entityMgr.findById(Volume.class, getId()); |
| 95 | + if (volume != null) { |
| 96 | + return volume.getAccountId(); |
| 97 | + } |
| 98 | + |
| 99 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String getEventType() { |
| 104 | + return EventTypes.EVENT_VOLUME_CHECK; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getEventDescription() { |
| 109 | + return String.format("check and repair operation on volume: %s", this._uuidMgr.getUuid(Volume.class, getId())); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Long getApiResourceId() { |
| 114 | + return id; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public ApiCommandResourceType getApiResourceType() { |
| 119 | + return ApiCommandResourceType.Volume; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void execute() throws ResourceAllocationException { |
| 124 | + CallContext.current().setEventDetails("Volume Id: " + getId()); |
| 125 | + Pair<String, String> result = _volumeService.checkAndRepairVolume(this); |
| 126 | + Volume volume = _responseGenerator.findVolumeById(getId()); |
| 127 | + if (result != null) { |
| 128 | + VolumeResponse response = _responseGenerator.createVolumeResponse(ResponseView.Full, volume); |
| 129 | + response.setVolumeCheckResult(StringUtils.parseJsonToMap(result.first())); |
| 130 | + if (getRepair() != null) { |
| 131 | + response.setVolumeRepairResult(StringUtils.parseJsonToMap(result.second())); |
| 132 | + } |
| 133 | + response.setResponseName(getCommandName()); |
| 134 | + setResponseObject(response); |
| 135 | + } else { |
| 136 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to check volume and repair"); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments