|
| 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.network; |
| 18 | + |
| 19 | +import org.apache.cloudstack.acl.RoleType; |
| 20 | +import org.apache.cloudstack.api.APICommand; |
| 21 | +import org.apache.cloudstack.api.ApiArgValidator; |
| 22 | +import org.apache.cloudstack.api.ApiConstants; |
| 23 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 24 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 25 | +import org.apache.cloudstack.api.Parameter; |
| 26 | +import org.apache.cloudstack.api.ServerApiException; |
| 27 | +import org.apache.cloudstack.api.response.PodResponse; |
| 28 | +import org.apache.cloudstack.api.response.SuccessResponse; |
| 29 | +import org.apache.log4j.Logger; |
| 30 | + |
| 31 | +import com.cloud.event.EventTypes; |
| 32 | +import com.cloud.exception.ConcurrentOperationException; |
| 33 | +import com.cloud.exception.InvalidParameterValueException; |
| 34 | +import com.cloud.user.Account; |
| 35 | + |
| 36 | +@APICommand(name = UpdatePodManagementNetworkIpRangeCmd.APINAME, |
| 37 | + description = "Updates a management network IP range. Only allowed when no IPs are allocated.", |
| 38 | + responseObject = SuccessResponse.class, |
| 39 | + since = "4.16.0.0", |
| 40 | + requestHasSensitiveInfo = false, |
| 41 | + responseHasSensitiveInfo = false, |
| 42 | + authorized = {RoleType.Admin}) |
| 43 | +public class UpdatePodManagementNetworkIpRangeCmd extends BaseAsyncCmd { |
| 44 | + |
| 45 | + public static final Logger s_logger = Logger.getLogger(UpdatePodManagementNetworkIpRangeCmd.class); |
| 46 | + |
| 47 | + public static final String APINAME = "updatePodManagementNetworkIpRange"; |
| 48 | + |
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + //////////////// API parameters ///////////////////// |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + @Parameter(name = ApiConstants.POD_ID, |
| 53 | + type = CommandType.UUID, |
| 54 | + entityType = PodResponse.class, |
| 55 | + required = true, |
| 56 | + description = "UUID of POD, where the IP range belongs to.", |
| 57 | + validations = {ApiArgValidator.PositiveNumber}) |
| 58 | + private Long podId; |
| 59 | + |
| 60 | + @Parameter(name = ApiConstants.CURRENT_START_IP, |
| 61 | + type = CommandType.STRING, |
| 62 | + entityType = PodResponse.class, |
| 63 | + required = true, |
| 64 | + description = "The current starting IP address.", |
| 65 | + validations = {ApiArgValidator.NotNullOrEmpty}) |
| 66 | + private String currentStartIp; |
| 67 | + |
| 68 | + @Parameter(name = ApiConstants.CURRENT_END_IP, |
| 69 | + type = CommandType.STRING, |
| 70 | + entityType = PodResponse.class, |
| 71 | + required = true, |
| 72 | + description = "The current ending IP address.", |
| 73 | + validations = {ApiArgValidator.NotNullOrEmpty}) |
| 74 | + private String currentEndIp; |
| 75 | + |
| 76 | + @Parameter(name = ApiConstants.NEW_START_IP, |
| 77 | + type = CommandType.STRING, |
| 78 | + description = "The new starting IP address.", |
| 79 | + validations = {ApiArgValidator.NotNullOrEmpty}) |
| 80 | + private String newStartIp; |
| 81 | + |
| 82 | + @Parameter(name = ApiConstants.NEW_END_IP, |
| 83 | + type = CommandType.STRING, |
| 84 | + description = "The new ending IP address.", |
| 85 | + validations = {ApiArgValidator.NotNullOrEmpty}) |
| 86 | + private String newEndIp; |
| 87 | + |
| 88 | + ///////////////////////////////////////////////////// |
| 89 | + /////////////////// Accessors /////////////////////// |
| 90 | + ///////////////////////////////////////////////////// |
| 91 | + |
| 92 | + public Long getPodId() { |
| 93 | + return podId; |
| 94 | + } |
| 95 | + |
| 96 | + public String getCurrentStartIP() { |
| 97 | + return currentStartIp; |
| 98 | + } |
| 99 | + |
| 100 | + public String getCurrentEndIP() { |
| 101 | + return currentEndIp; |
| 102 | + } |
| 103 | + |
| 104 | + public String getNewStartIP() { |
| 105 | + return newStartIp; |
| 106 | + } |
| 107 | + |
| 108 | + public String getNewEndIP() { |
| 109 | + return newEndIp; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getEventType() { |
| 114 | + return EventTypes.EVENT_MANAGEMENT_IP_RANGE_UPDATE; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String getEventDescription() { |
| 119 | + return "Updating pod management IP range " + getNewStartIP() + "-" + getNewEndIP() + " of Pod: " + getPodId(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String getCommandName() { |
| 124 | + return APINAME.toLowerCase() + BaseAsyncCmd.RESPONSE_SUFFIX; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public long getEntityOwnerId() { |
| 129 | + return Account.ACCOUNT_ID_SYSTEM; |
| 130 | + } |
| 131 | + |
| 132 | + ///////////////////////////////////////////////////// |
| 133 | + /////////////// API Implementation/////////////////// |
| 134 | + ///////////////////////////////////////////////////// |
| 135 | + |
| 136 | + @Override |
| 137 | + public void execute() { |
| 138 | + if (getNewStartIP() == null && getNewEndIP() == null) { |
| 139 | + throw new InvalidParameterValueException("Either new starting IP address or new ending IP address must be specified"); |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + _configService.updatePodIpRange(this); |
| 144 | + SuccessResponse response = new SuccessResponse(getCommandName()); |
| 145 | + this.setResponseObject(response); |
| 146 | + } catch (ConcurrentOperationException ex) { |
| 147 | + s_logger.warn("Exception: ", ex); |
| 148 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); |
| 149 | + } catch (Exception e) { |
| 150 | + s_logger.warn("Failed to update pod management IP range " + getNewStartIP() + "-" + getNewEndIP() + " of Pod: " + getPodId(), e); |
| 151 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments