|
| 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.nic; |
| 18 | + |
| 19 | +import javax.inject.Inject; |
| 20 | + |
| 21 | +import org.apache.cloudstack.acl.RoleType; |
| 22 | +import org.apache.cloudstack.api.APICommand; |
| 23 | +import org.apache.cloudstack.api.ApiConstants; |
| 24 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 25 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 26 | +import org.apache.cloudstack.api.Parameter; |
| 27 | +import org.apache.cloudstack.api.ServerApiException; |
| 28 | +import org.apache.cloudstack.api.response.NicResponse; |
| 29 | +import org.apache.cloudstack.api.response.SuccessResponse; |
| 30 | +import org.apache.cloudstack.context.CallContext; |
| 31 | +import org.apache.cloudstack.network.packetcapture.PacketCaptureService; |
| 32 | + |
| 33 | +import com.cloud.event.EventTypes; |
| 34 | +import com.cloud.exception.ResourceUnavailableException; |
| 35 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 36 | +import com.cloud.vm.Nic; |
| 37 | + |
| 38 | +@APICommand(name = "enablePacketCapture", responseObject = SuccessResponse.class, entityType = {Nic.class}, |
| 39 | + responseHasSensitiveInfo = false, |
| 40 | + requestHasSensitiveInfo = false, |
| 41 | + description = "Enables packet capture on an Instance NIC. The capture is started on the host the Instance " + |
| 42 | + "is running on and follows the Instance across stop/start and migration. Only supported on KVM.", |
| 43 | + authorized = {RoleType.Admin}, |
| 44 | + since = "4.23.0") |
| 45 | +public class EnablePacketCaptureCmd extends BaseAsyncCmd { |
| 46 | + |
| 47 | + @Inject |
| 48 | + private PacketCaptureService packetCaptureService; |
| 49 | + |
| 50 | + ///////////////////////////////////////////////////// |
| 51 | + //////////////// API parameters ///////////////////// |
| 52 | + ///////////////////////////////////////////////////// |
| 53 | + @Parameter(name = ApiConstants.NIC_ID, type = CommandType.UUID, entityType = NicResponse.class, required = true, |
| 54 | + description = "The ID of the NIC to capture packets on") |
| 55 | + private Long nicId; |
| 56 | + |
| 57 | + ///////////////////////////////////////////////////// |
| 58 | + /////////////////// Accessors /////////////////////// |
| 59 | + ///////////////////////////////////////////////////// |
| 60 | + public Long getNicId() { |
| 61 | + return nicId; |
| 62 | + } |
| 63 | + |
| 64 | + ///////////////////////////////////////////////////// |
| 65 | + /////////////////// Implementation ////////////////// |
| 66 | + ///////////////////////////////////////////////////// |
| 67 | + @Override |
| 68 | + public long getEntityOwnerId() { |
| 69 | + return CallContext.current().getCallingAccountId(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String getEventType() { |
| 74 | + return EventTypes.EVENT_NIC_PACKET_CAPTURE_ENABLE; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getEventDescription() { |
| 79 | + return "Enabling packet capture on NIC with ID: " + getResourceUuid(ApiConstants.NIC_ID); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void execute() throws ResourceUnavailableException, ServerApiException { |
| 84 | + try { |
| 85 | + packetCaptureService.enablePacketCapture(getNicId()); |
| 86 | + SuccessResponse response = new SuccessResponse(getCommandName()); |
| 87 | + setResponseObject(response); |
| 88 | + } catch (CloudRuntimeException e) { |
| 89 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments