|
| 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.user.network; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.cloudstack.api.APICommand; |
| 24 | +import org.apache.cloudstack.api.ApiConstants; |
| 25 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 26 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ServerApiException; |
| 29 | +import org.apache.cloudstack.api.response.ListResponse; |
| 30 | +import org.apache.cloudstack.api.response.NetworkACLItemResponse; |
| 31 | +import org.apache.cloudstack.api.response.NetworkACLResponse; |
| 32 | +import org.apache.cloudstack.context.CallContext; |
| 33 | +import org.apache.commons.collections.MapUtils; |
| 34 | + |
| 35 | +import com.cloud.event.EventTypes; |
| 36 | +import com.cloud.exception.ResourceUnavailableException; |
| 37 | +import com.cloud.network.vpc.NetworkACLItem; |
| 38 | +import com.cloud.user.Account; |
| 39 | +import com.cloud.utils.Pair; |
| 40 | + |
| 41 | +@APICommand(name = "importNetworkACL", description = "Imports network ACL rules.", |
| 42 | + responseObject = NetworkACLItemResponse.class, |
| 43 | + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) |
| 44 | +public class ImportNetworkACLCmd extends BaseAsyncCmd { |
| 45 | + |
| 46 | + // /////////////////////////////////////////////////// |
| 47 | + // ////////////// API parameters ///////////////////// |
| 48 | + // /////////////////////////////////////////////////// |
| 49 | + |
| 50 | + @Parameter( |
| 51 | + name = ApiConstants.ACL_ID, |
| 52 | + type = CommandType.UUID, |
| 53 | + entityType = NetworkACLResponse.class, |
| 54 | + required = true, |
| 55 | + description = "The ID of the network ACL to which the rules will be imported", |
| 56 | + since = "4.22.0" |
| 57 | + ) |
| 58 | + private Long aclId; |
| 59 | + |
| 60 | + @Parameter(name = ApiConstants.RULES, type = CommandType.MAP, required = true, |
| 61 | + description = "Rules param list, id and protocol are must. Example: " + |
| 62 | + "rules[0].id=101&rules[0].protocol=tcp&rules[0].traffictype=ingress&rules[0].state=active&rules[0].cidrlist=192.168.1.0/24" + |
| 63 | + "&rules[0].tags=web&rules[0].aclid=acl-001&rules[0].aclname=web-acl&rules[0].number=1&rules[0].action=allow&rules[0].fordisplay=true" + |
| 64 | + "&rules[0].description=allow%20web%20traffic&rules[1].id=102&rules[1].protocol=udp&rules[1].traffictype=egress&rules[1].state=enabled" + |
| 65 | + "&rules[1].cidrlist=10.0.0.0/8&rules[1].tags=db&rules[1].aclid=acl-002&rules[1].aclname=db-acl&rules[1].number=2&rules[1].action=deny" + |
| 66 | + "&rules[1].fordisplay=false&rules[1].description=deny%20database%20traffic", |
| 67 | + since = "4.22.0") |
| 68 | + private Map rules; |
| 69 | + |
| 70 | + |
| 71 | + // /////////////////////////////////////////////////// |
| 72 | + // ///////////////// Accessors /////////////////////// |
| 73 | + // /////////////////////////////////////////////////// |
| 74 | + |
| 75 | + // Returns map, corresponds to a rule with the details in the keys: |
| 76 | + // id, protocol, startport, endport, traffictype, state, cidrlist, tags, aclid, aclname, number, action, fordisplay, description |
| 77 | + public Map getRules() { |
| 78 | + return rules; |
| 79 | + } |
| 80 | + |
| 81 | + public Long getAclId() { |
| 82 | + return aclId; |
| 83 | + } |
| 84 | + |
| 85 | + // /////////////////////////////////////////////////// |
| 86 | + // ///////////// API Implementation/////////////////// |
| 87 | + // /////////////////////////////////////////////////// |
| 88 | + |
| 89 | + |
| 90 | + @Override |
| 91 | + public void execute() throws ResourceUnavailableException { |
| 92 | + validateParams(); |
| 93 | + List<NetworkACLItem> importedRules = _networkACLService.importNetworkACLRules(this); |
| 94 | + ListResponse<NetworkACLItemResponse> response = new ListResponse<>(); |
| 95 | + List<NetworkACLItemResponse> aclResponse = new ArrayList<>(); |
| 96 | + for (NetworkACLItem acl : importedRules) { |
| 97 | + NetworkACLItemResponse ruleData = _responseGenerator.createNetworkACLItemResponse(acl); |
| 98 | + aclResponse.add(ruleData); |
| 99 | + } |
| 100 | + response.setResponses(aclResponse, importedRules.size()); |
| 101 | + response.setResponseName(getCommandName()); |
| 102 | + setResponseObject(response); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public long getEntityOwnerId() { |
| 107 | + Account account = CallContext.current().getCallingAccount(); |
| 108 | + if (account != null) { |
| 109 | + return account.getId(); |
| 110 | + } |
| 111 | + return Account.ACCOUNT_ID_SYSTEM; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String getEventType() { |
| 116 | + return EventTypes.EVENT_NETWORK_ACL_CREATE; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public String getEventDescription() { |
| 121 | + return "Importing ACL rules for ACL ID: " + getAclId(); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + private void validateParams() { |
| 126 | + if(MapUtils.isEmpty(rules)) { |
| 127 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Rules parameter is empty or null"); |
| 128 | + } |
| 129 | + |
| 130 | + if (getAclId() == null || _networkACLService.getNetworkACL(getAclId()) == null) { |
| 131 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to find network ACL with provided aclid"); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments