|
| 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.storage.heuristics; |
| 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.ApiConstants; |
| 23 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 24 | +import org.apache.cloudstack.api.BaseCmd; |
| 25 | +import org.apache.cloudstack.api.Parameter; |
| 26 | +import org.apache.cloudstack.api.response.SecondaryStorageHeuristicsResponse; |
| 27 | +import org.apache.cloudstack.api.response.ZoneResponse; |
| 28 | +import org.apache.cloudstack.api.ServerApiException; |
| 29 | +import org.apache.cloudstack.secstorage.heuristics.Heuristic; |
| 30 | + |
| 31 | +import static org.apache.cloudstack.api.ApiConstants.HEURISTIC_TYPE_VALID_OPTIONS; |
| 32 | + |
| 33 | +@APICommand(name = "createSecondaryStorageSelector", description = "Creates a secondary storage selector, described by the heuristic rule.", since = "4.19.0", responseObject = |
| 34 | + SecondaryStorageHeuristicsResponse.class, entityType = {Heuristic.class}, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, authorized = {RoleType.Admin}) |
| 35 | +public class CreateSecondaryStorageSelectorCmd extends BaseCmd{ |
| 36 | + |
| 37 | + @Parameter(name = ApiConstants.NAME, required = true, type = CommandType.STRING, description = "The name identifying the heuristic rule.") |
| 38 | + private String name; |
| 39 | + |
| 40 | + @Parameter(name = ApiConstants.DESCRIPTION, required = true, type = BaseCmd.CommandType.STRING, description = "The description of the heuristic rule.") |
| 41 | + private String description; |
| 42 | + |
| 43 | + @Parameter(name = ApiConstants.ZONE_ID, required = true, entityType = ZoneResponse.class, type = BaseCmd.CommandType.UUID, description = "The zone in which the heuristic " + |
| 44 | + "rule will be applied.") |
| 45 | + private Long zoneId; |
| 46 | + |
| 47 | + @Parameter(name = ApiConstants.TYPE, required = true, type = BaseCmd.CommandType.STRING, description = |
| 48 | + "The resource type directed to a specific secondary storage by the selector. " + HEURISTIC_TYPE_VALID_OPTIONS) |
| 49 | + private String type; |
| 50 | + |
| 51 | + @Parameter(name = ApiConstants.HEURISTIC_RULE, required = true, type = BaseCmd.CommandType.STRING, description = "The heuristic rule, in JavaScript language. It is required " + |
| 52 | + "that it returns the UUID of a secondary storage pool. An example of a rule is `if (snapshot.hypervisorType === 'KVM') { '7832f261-c602-4e8e-8580-2496ffbbc45d'; " + |
| 53 | + "}` would allocate all snapshots with the KVM hypervisor to the specified secondary storage UUID.", length = 65535) |
| 54 | + private String heuristicRule; |
| 55 | + |
| 56 | + public String getName() { |
| 57 | + return name; |
| 58 | + } |
| 59 | + |
| 60 | + public String getDescription() { |
| 61 | + return description; |
| 62 | + } |
| 63 | + |
| 64 | + public Long getZoneId() { |
| 65 | + return zoneId; |
| 66 | + } |
| 67 | + |
| 68 | + public String getType() { |
| 69 | + return type; |
| 70 | + } |
| 71 | + |
| 72 | + public String getHeuristicRule() { |
| 73 | + return heuristicRule; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void execute() { |
| 78 | + Heuristic heuristic = _storageService.createSecondaryStorageHeuristic(this); |
| 79 | + |
| 80 | + if (heuristic == null) { |
| 81 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a secondary storage selector."); |
| 82 | + } |
| 83 | + |
| 84 | + SecondaryStorageHeuristicsResponse response = _responseGenerator.createSecondaryStorageSelectorResponse(heuristic); |
| 85 | + response.setResponseName(getCommandName()); |
| 86 | + this.setResponseObject(response); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public long getEntityOwnerId() { |
| 91 | + return Account.ACCOUNT_ID_SYSTEM; |
| 92 | + } |
| 93 | +} |
0 commit comments