1+ //
2+ // Licensed to the Apache Software Foundation (ASF) under one
3+ // or more contributor license agreements. See the NOTICE file
4+ // distributed with this work for additional information
5+ // regarding copyright ownership. The ASF licenses this file
6+ // to you under the Apache License, Version 2.0 (the
7+ // "License"); you may not use this file except in compliance
8+ // with the License. You may obtain a copy of the License at
9+ //
10+ // http://www.apache.org/licenses/LICENSE-2.0
11+ //
12+ // Unless required by applicable law or agreed to in writing,
13+ // software distributed under the License is distributed on an
14+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ // KIND, either express or implied. See the License for the
16+ // specific language governing permissions and limitations
17+ // under the License.
18+ //
19+
20+ package org .apache .cloudstack .api .command .admin .vm ;
21+
22+ import com .cloud .event .EventTypes ;
23+ import com .cloud .exception .ConcurrentOperationException ;
24+ import com .cloud .exception .InsufficientCapacityException ;
25+ import com .cloud .exception .NetworkRuleConflictException ;
26+ import com .cloud .exception .ResourceAllocationException ;
27+ import com .cloud .exception .ResourceUnavailableException ;
28+ import com .cloud .user .Account ;
29+ import com .cloud .uservm .UserVm ;
30+ import org .apache .cloudstack .acl .RoleType ;
31+ import org .apache .cloudstack .api .APICommand ;
32+ import org .apache .cloudstack .api .ApiConstants ;
33+ import org .apache .cloudstack .api .BaseAsyncCmd ;
34+ import org .apache .cloudstack .api .Parameter ;
35+ import org .apache .cloudstack .api .ResponseObject ;
36+ import org .apache .cloudstack .api .ServerApiException ;
37+ import org .apache .cloudstack .api .response .UnmanageVMInstanceResponse ;
38+ import org .apache .cloudstack .api .response .UserVmResponse ;
39+ import org .apache .cloudstack .context .CallContext ;
40+ import org .apache .cloudstack .vm .UnmanageVMManager ;
41+ import org .apache .log4j .Logger ;
42+
43+ import javax .inject .Inject ;
44+
45+ @ APICommand (name = UnmanageVMInstanceCmd .API_NAME ,
46+ description = "Unmanage a virtual machine from a given cluster." ,
47+ responseObject = UnmanageVMInstanceResponse .class ,
48+ responseView = ResponseObject .ResponseView .Full ,
49+ requestHasSensitiveInfo = false ,
50+ authorized = {RoleType .Admin },
51+ since = "4.14.0" )
52+ public class UnmanageVMInstanceCmd extends BaseAsyncCmd {
53+
54+ public static final Logger LOGGER = Logger .getLogger (UnmanageVMInstanceCmd .class );
55+ public static final String API_NAME = "unmanageVirtualMachine" ;
56+
57+ @ Inject
58+ private UnmanageVMManager unmanageVMManager ;
59+
60+ /////////////////////////////////////////////////////
61+ //////////////// API parameters /////////////////////
62+ /////////////////////////////////////////////////////
63+
64+ @ Parameter (name = ApiConstants .ID , type = CommandType .UUID ,
65+ entityType = UserVmResponse .class , required = true ,
66+ description = "The ID of the virtual machine to unmanage" )
67+ private Long vmId ;
68+
69+ /////////////////////////////////////////////////////
70+ /////////////////// Accessors ///////////////////////
71+ /////////////////////////////////////////////////////
72+
73+ public Long getVmId () {
74+ return vmId ;
75+ }
76+
77+ @ Override
78+ public String getEventType () {
79+ return EventTypes .EVENT_VM_UNMANAGE ;
80+ }
81+
82+ @ Override
83+ public String getEventDescription () {
84+ return "unmanaging VM. VM ID = " + vmId ;
85+ }
86+
87+ /////////////////////////////////////////////////////
88+ /////////////// API Implementation///////////////////
89+ /////////////////////////////////////////////////////
90+
91+ @ Override
92+ public void execute () throws ResourceUnavailableException , InsufficientCapacityException , ServerApiException ,
93+ ConcurrentOperationException , ResourceAllocationException , NetworkRuleConflictException {
94+ UnmanageVMInstanceResponse response = new UnmanageVMInstanceResponse ();
95+ try {
96+ CallContext .current ().setEventDetails ("VM ID = " + vmId );
97+ unmanageVMManager .unmanageVMInstance (vmId );
98+ response .setSuccess (true );
99+ } catch (Exception e ) {
100+ response .setSuccess (false );
101+ response .setDetails (e .getMessage ());
102+ }
103+ response .setResponseName (getCommandName ());
104+ setResponseObject (response );
105+ }
106+
107+ @ Override
108+ public String getCommandName () {
109+ return API_NAME .toLowerCase () + BaseAsyncCmd .RESPONSE_SUFFIX ;
110+ }
111+
112+ @ Override
113+ public long getEntityOwnerId () {
114+ UserVm vm = _responseGenerator .findUserVmById (vmId );
115+ if (vm != null ) {
116+ return vm .getAccountId ();
117+ }
118+ return Account .ACCOUNT_ID_SYSTEM ;
119+ }
120+ }
0 commit comments