|
| 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.region; |
| 19 | + |
| 20 | +import javax.inject.Inject; |
| 21 | + |
| 22 | +import com.cloud.async.AsyncJob; |
| 23 | +import com.cloud.dc.Vlan; |
| 24 | +import com.cloud.event.EventTypes; |
| 25 | +import com.cloud.exception.ConcurrentOperationException; |
| 26 | +import com.cloud.exception.InsufficientCapacityException; |
| 27 | +import com.cloud.exception.ResourceAllocationException; |
| 28 | +import org.apache.cloudstack.api.APICommand; |
| 29 | +import org.apache.cloudstack.api.ApiConstants; |
| 30 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 31 | +import org.apache.cloudstack.api.BaseAsyncCreateCmd; |
| 32 | +import org.apache.cloudstack.api.Parameter; |
| 33 | +import org.apache.cloudstack.api.ServerApiException; |
| 34 | +import org.apache.cloudstack.api.response.PortableIpRangeResponse; |
| 35 | +import org.apache.cloudstack.api.response.RegionResponse; |
| 36 | +import org.apache.cloudstack.api.response.VlanIpRangeResponse; |
| 37 | +import org.apache.cloudstack.region.PortableIpRange; |
| 38 | +import org.apache.cloudstack.region.Region; |
| 39 | +import org.apache.cloudstack.region.RegionService; |
| 40 | +import org.apache.log4j.Logger; |
| 41 | + |
| 42 | +import com.cloud.user.Account; |
| 43 | + |
| 44 | +@APICommand(name = "createPortableIpRange", responseObject=PortableIpRangeResponse.class, description="adds a range of portable public IP's to a region", since="4.2.0") |
| 45 | +public class CreatePortableIpRangeCmd extends BaseAsyncCreateCmd { |
| 46 | + |
| 47 | + public static final Logger s_logger = Logger.getLogger(CreatePortableIpRangeCmd.class.getName()); |
| 48 | + |
| 49 | + private static final String s_name = "createportableiprangeresponse"; |
| 50 | + |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + //////////////// API parameters ///////////////////// |
| 53 | + ///////////////////////////////////////////////////// |
| 54 | + |
| 55 | + @Parameter(name=ApiConstants.REGION_ID, type=CommandType.INTEGER, entityType = RegionResponse.class, required=true, description="Id of the Region") |
| 56 | + private Integer regionId; |
| 57 | + |
| 58 | + @Parameter(name=ApiConstants.START_IP, type=CommandType.STRING, required=true, description="the beginning IP address in the portable IP range") |
| 59 | + private String startIp; |
| 60 | + |
| 61 | + @Parameter(name=ApiConstants.END_IP, type=CommandType.STRING, required=true, description="the ending IP address in the portable IP range") |
| 62 | + private String endIp; |
| 63 | + |
| 64 | + @Parameter(name=ApiConstants.GATEWAY, type=CommandType.STRING, required=true, description="the gateway for the portable IP range") |
| 65 | + private String gateway; |
| 66 | + |
| 67 | + @Parameter(name=ApiConstants.NETMASK, type=CommandType.STRING, required=true, description="the netmask of the portable IP range") |
| 68 | + private String netmask; |
| 69 | + |
| 70 | + @Parameter(name=ApiConstants.VLAN, type=CommandType.STRING, description="VLAN id, if not specified defaulted to untagged") |
| 71 | + private String vlan; |
| 72 | + |
| 73 | + ///////////////////////////////////////////////////// |
| 74 | + /////////////////// Accessors /////////////////////// |
| 75 | + ///////////////////////////////////////////////////// |
| 76 | + |
| 77 | + public Integer getRegionId() { |
| 78 | + return regionId; |
| 79 | + } |
| 80 | + |
| 81 | + public String getStartIp() { |
| 82 | + return startIp; |
| 83 | + } |
| 84 | + |
| 85 | + public String getEndIp() { |
| 86 | + return endIp; |
| 87 | + } |
| 88 | + |
| 89 | + public String getVlan() { |
| 90 | + return vlan; |
| 91 | + } |
| 92 | + |
| 93 | + public String getGateway() { |
| 94 | + return gateway; |
| 95 | + } |
| 96 | + |
| 97 | + public String getNetmask() { |
| 98 | + return netmask; |
| 99 | + } |
| 100 | + |
| 101 | + ///////////////////////////////////////////////////// |
| 102 | + /////////////// API Implementation/////////////////// |
| 103 | + ///////////////////////////////////////////////////// |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getCommandName() { |
| 107 | + return s_name; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public long getEntityOwnerId() { |
| 112 | + return Account.ACCOUNT_ID_SYSTEM; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void execute(){ |
| 117 | + PortableIpRange portableIpRange = _entityMgr.findById(PortableIpRange.class, getEntityId()); |
| 118 | + PortableIpRangeResponse response = null; |
| 119 | + if (portableIpRange != null) { |
| 120 | + response = _responseGenerator.createPortableIPRangeResponse(portableIpRange); |
| 121 | + } |
| 122 | + response.setResponseName(getCommandName()); |
| 123 | + this.setResponseObject(response); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void create() throws ResourceAllocationException { |
| 128 | + try { |
| 129 | + PortableIpRange portableIpRange = _configService.createPortableIpRange(this); |
| 130 | + if (portableIpRange != null) { |
| 131 | + this.setEntityId(portableIpRange.getId()); |
| 132 | + this.setEntityUuid(portableIpRange.getUuid()); |
| 133 | + } else { |
| 134 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create portable public IP range"); |
| 135 | + } |
| 136 | + } catch (ConcurrentOperationException ex) { |
| 137 | + s_logger.warn("Exception: ", ex); |
| 138 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public String getEventType() { |
| 144 | + return EventTypes.EVENT_PORTABLE_IP_RANGE_CREATE; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String getEventDescription() { |
| 149 | + return "creating a portable public ip range in region: " + getRegionId(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public AsyncJob.Type getInstanceType() { |
| 154 | + return AsyncJob.Type.PortableIpAddress; |
| 155 | + } |
| 156 | +} |
0 commit comments