|
| 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.api.command.admin.ha; |
| 19 | + |
| 20 | +import com.cloud.event.EventTypes; |
| 21 | +import com.cloud.exception.ConcurrentOperationException; |
| 22 | +import com.cloud.exception.InsufficientCapacityException; |
| 23 | +import com.cloud.exception.NetworkRuleConflictException; |
| 24 | +import com.cloud.exception.ResourceAllocationException; |
| 25 | +import com.cloud.exception.ResourceUnavailableException; |
| 26 | +import com.cloud.host.Host; |
| 27 | +import com.cloud.user.Account; |
| 28 | +import org.apache.cloudstack.acl.RoleType; |
| 29 | +import org.apache.cloudstack.api.APICommand; |
| 30 | +import org.apache.cloudstack.api.ApiArgValidator; |
| 31 | +import org.apache.cloudstack.api.ApiConstants; |
| 32 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 33 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 34 | +import org.apache.cloudstack.api.BaseCmd; |
| 35 | +import org.apache.cloudstack.api.Parameter; |
| 36 | +import org.apache.cloudstack.api.ServerApiException; |
| 37 | +import org.apache.cloudstack.api.response.HostHAResponse; |
| 38 | +import org.apache.cloudstack.api.response.HostResponse; |
| 39 | +import org.apache.cloudstack.context.CallContext; |
| 40 | +import org.apache.cloudstack.ha.HAConfigManager; |
| 41 | +import org.apache.cloudstack.ha.HAResource; |
| 42 | + |
| 43 | +import javax.inject.Inject; |
| 44 | + |
| 45 | +@APICommand(name = ConfigureHAForHostCmd.APINAME, description = "Configures HA for a host", |
| 46 | + responseObject = HostHAResponse.class, |
| 47 | + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, |
| 48 | + since = "4.11", authorized = {RoleType.Admin}) |
| 49 | +public final class ConfigureHAForHostCmd extends BaseAsyncCmd { |
| 50 | + public static final String APINAME = "configureHAForHost"; |
| 51 | + |
| 52 | + @Inject |
| 53 | + private HAConfigManager haConfigManager; |
| 54 | + |
| 55 | + ///////////////////////////////////////////////////// |
| 56 | + //////////////// API parameters ///////////////////// |
| 57 | + ///////////////////////////////////////////////////// |
| 58 | + |
| 59 | + @Parameter(name = ApiConstants.HOST_ID, type = CommandType.UUID, entityType = HostResponse.class, |
| 60 | + description = "ID of the host", required = true, validations = {ApiArgValidator.PositiveNumber}) |
| 61 | + private Long hostId; |
| 62 | + |
| 63 | + @Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING, |
| 64 | + description = "HA provider", required = true, validations = {ApiArgValidator.NotNullOrEmpty}) |
| 65 | + private String haProvider; |
| 66 | + |
| 67 | + ///////////////////////////////////////////////////// |
| 68 | + /////////////////// Accessors /////////////////////// |
| 69 | + ///////////////////////////////////////////////////// |
| 70 | + |
| 71 | + public Long getHostId() { |
| 72 | + return hostId; |
| 73 | + } |
| 74 | + |
| 75 | + public String getHaProvider() { |
| 76 | + return haProvider; |
| 77 | + } |
| 78 | + |
| 79 | + ///////////////////////////////////////////////////// |
| 80 | + /////////////// API Implementation/////////////////// |
| 81 | + ///////////////////////////////////////////////////// |
| 82 | + |
| 83 | + @Override |
| 84 | + public String getCommandName() { |
| 85 | + return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public long getEntityOwnerId() { |
| 90 | + return Account.ACCOUNT_ID_SYSTEM; |
| 91 | + } |
| 92 | + |
| 93 | + private void setupResponse(final boolean result, final String resourceUuid) { |
| 94 | + final HostHAResponse response = new HostHAResponse(); |
| 95 | + response.setId(resourceUuid); |
| 96 | + response.setProvider(getHaProvider().toLowerCase()); |
| 97 | + response.setResponseName(getCommandName()); |
| 98 | + setResponseObject(response); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { |
| 103 | + final Host host = _resourceService.getHost(getHostId()); |
| 104 | + if (host == null) { |
| 105 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to find host by ID: " + getHostId()); |
| 106 | + } |
| 107 | + |
| 108 | + final boolean result = haConfigManager.configureHA(host.getId(), HAResource.ResourceType.Host, getHaProvider()); |
| 109 | + if (!result) { |
| 110 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to configure HA provider for the host"); |
| 111 | + } |
| 112 | + CallContext.current().setEventDetails("Host Id:" + host.getId() + " HA configured with provider: " + getHaProvider()); |
| 113 | + CallContext.current().putContextParameter(Host.class, host.getUuid()); |
| 114 | + |
| 115 | + setupResponse(result, host.getUuid()); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getEventType() { |
| 120 | + return EventTypes.EVENT_HA_RESOURCE_DISABLE; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String getEventDescription() { |
| 125 | + return "configure HA for host: " + getHostId(); |
| 126 | + } |
| 127 | +} |
0 commit comments