|
| 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.backup; |
| 18 | + |
| 19 | +import javax.inject.Inject; |
| 20 | + |
| 21 | +import org.apache.cloudstack.acl.RoleType; |
| 22 | +import org.apache.cloudstack.api.APICommand; |
| 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.BaseCmd; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ServerApiException; |
| 29 | +import org.apache.cloudstack.api.command.offering.DomainAndZoneIdResolver; |
| 30 | +import org.apache.cloudstack.api.response.BackupOfferingResponse; |
| 31 | +import org.apache.cloudstack.api.response.ZoneResponse; |
| 32 | +import org.apache.cloudstack.backup.BackupManager; |
| 33 | +import org.apache.cloudstack.backup.BackupOffering; |
| 34 | +import org.apache.cloudstack.context.CallContext; |
| 35 | + |
| 36 | +import com.cloud.event.EventTypes; |
| 37 | +import com.cloud.exception.ConcurrentOperationException; |
| 38 | +import com.cloud.exception.InsufficientCapacityException; |
| 39 | +import com.cloud.exception.InvalidParameterValueException; |
| 40 | +import com.cloud.exception.NetworkRuleConflictException; |
| 41 | +import com.cloud.exception.ResourceAllocationException; |
| 42 | +import com.cloud.exception.ResourceUnavailableException; |
| 43 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 44 | + |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.List; |
| 47 | +import java.util.function.LongFunction; |
| 48 | + |
| 49 | +@APICommand(name = "cloneBackupOffering", |
| 50 | + description = "Clones a backup offering from an existing offering", |
| 51 | + responseObject = BackupOfferingResponse.class, since = "4.23.0", |
| 52 | + authorized = {RoleType.Admin}) |
| 53 | +public class CloneBackupOfferingCmd extends BaseAsyncCmd implements DomainAndZoneIdResolver { |
| 54 | + |
| 55 | + @Inject |
| 56 | + protected BackupManager backupManager; |
| 57 | + |
| 58 | + ///////////////////////////////////////////////////// |
| 59 | + //////////////// API parameters ///////////////////// |
| 60 | + //////////////////////////////////////////////////// |
| 61 | + |
| 62 | + @Parameter(name = ApiConstants.SOURCE_OFFERING_ID, type = BaseCmd.CommandType.UUID, entityType = BackupOfferingResponse.class, |
| 63 | + required = true, description = "The ID of the source backup offering to clone from") |
| 64 | + private Long sourceOfferingId; |
| 65 | + |
| 66 | + @Parameter(name = ApiConstants.NAME, type = BaseCmd.CommandType.STRING, required = true, |
| 67 | + description = "The name of the cloned offering") |
| 68 | + private String name; |
| 69 | + |
| 70 | + @Parameter(name = ApiConstants.DESCRIPTION, type = BaseCmd.CommandType.STRING, required = false, |
| 71 | + description = "The description of the cloned offering") |
| 72 | + private String description; |
| 73 | + |
| 74 | + @Parameter(name = ApiConstants.EXTERNAL_ID, type = BaseCmd.CommandType.STRING, required = false, |
| 75 | + description = "The backup offering ID (from backup provider side)") |
| 76 | + private String externalId; |
| 77 | + |
| 78 | + @Parameter(name = ApiConstants.ZONE_ID, type = BaseCmd.CommandType.UUID, entityType = ZoneResponse.class, |
| 79 | + description = "The zone ID", required = false) |
| 80 | + private Long zoneId; |
| 81 | + |
| 82 | + @Parameter(name = ApiConstants.DOMAIN_ID, |
| 83 | + type = CommandType.STRING, |
| 84 | + description = "the ID of the containing domain(s) as comma separated string, public for public offerings", |
| 85 | + length = 4096) |
| 86 | + private String domainIds; |
| 87 | + |
| 88 | + @Parameter(name = ApiConstants.ALLOW_USER_DRIVEN_BACKUPS, type = BaseCmd.CommandType.BOOLEAN, |
| 89 | + description = "Whether users are allowed to create adhoc backups and backup schedules", required = false) |
| 90 | + private Boolean userDrivenBackups; |
| 91 | + |
| 92 | + ///////////////////////////////////////////////////// |
| 93 | + /////////////////// Accessors /////////////////////// |
| 94 | + ///////////////////////////////////////////////////// |
| 95 | + |
| 96 | + public Long getSourceOfferingId() { |
| 97 | + return sourceOfferingId; |
| 98 | + } |
| 99 | + |
| 100 | + public String getName() { |
| 101 | + return name; |
| 102 | + } |
| 103 | + |
| 104 | + public String getExternalId() { |
| 105 | + return externalId; |
| 106 | + } |
| 107 | + |
| 108 | + public Long getZoneId() { |
| 109 | + return zoneId; |
| 110 | + } |
| 111 | + |
| 112 | + public String getDescription() { |
| 113 | + return description; |
| 114 | + } |
| 115 | + |
| 116 | + public Boolean getUserDrivenBackups() { |
| 117 | + return userDrivenBackups; |
| 118 | + } |
| 119 | + |
| 120 | + public List<Long> getDomainIds() { |
| 121 | + if (domainIds != null && !domainIds.isEmpty()) { |
| 122 | + return Arrays.asList(Arrays.stream(domainIds.split(",")).map(domainId -> Long.parseLong(domainId.trim())).toArray(Long[]::new)); |
| 123 | + } |
| 124 | + LongFunction<List<Long>> defaultDomainsProvider = null; |
| 125 | + if (backupManager != null) { |
| 126 | + defaultDomainsProvider = backupManager::getBackupOfferingDomains; |
| 127 | + } |
| 128 | + return resolveDomainIds(domainIds, sourceOfferingId, defaultDomainsProvider, "backup offering"); |
| 129 | + } |
| 130 | + |
| 131 | + ///////////////////////////////////////////////////// |
| 132 | + /////////////// API Implementation/////////////////// |
| 133 | + ///////////////////////////////////////////////////// |
| 134 | + |
| 135 | + @Override |
| 136 | + public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { |
| 137 | + try { |
| 138 | + BackupOffering policy = backupManager.cloneBackupOffering(this); |
| 139 | + if (policy == null) { |
| 140 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to clone backup offering"); |
| 141 | + } |
| 142 | + BackupOfferingResponse response = _responseGenerator.createBackupOfferingResponse(policy); |
| 143 | + response.setResponseName(getCommandName()); |
| 144 | + setResponseObject(response); |
| 145 | + } catch (InvalidParameterValueException e) { |
| 146 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.getMessage()); |
| 147 | + } catch (CloudRuntimeException e) { |
| 148 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public long getEntityOwnerId() { |
| 154 | + return CallContext.current().getCallingAccount().getId(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public String getEventType() { |
| 159 | + return EventTypes.EVENT_VM_BACKUP_OFFERING_CLONE; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String getEventDescription() { |
| 164 | + return "Cloning backup offering: " + name + " from source offering: " + (sourceOfferingId == null ? "" : sourceOfferingId.toString()); |
| 165 | + } |
| 166 | +} |
0 commit comments