1+ package org .apache .cloudstack .api .command .user .vm ;
2+
3+ import com .cloud .exception .*;
4+ import com .cloud .user .Account ;
5+ import com .cloud .user .UserContext ;
6+ import com .cloud .uservm .UserVm ;
7+ import org .apache .cloudstack .api .*;
8+ import org .apache .cloudstack .api .response .ServiceOfferingResponse ;
9+ import org .apache .cloudstack .api .response .UserVmResponse ;
10+ import org .apache .log4j .Logger ;
11+
12+ // Licensed to the Apache Software Foundation (ASF) under one
13+ // or more contributor license agreements. See the NOTICE file
14+ // distributed with this work for additional information
15+ // regarding copyright ownership. The ASF licenses this file
16+ // to you under the Apache License, Version 2.0 (the
17+ // "License"); you may not use this file except in compliance
18+ // with the License. You may obtain a copy of the License at
19+ //
20+ // http://www.apache.org/licenses/LICENSE-2.0
21+ //
22+ // Unless required by applicable law or agreed to in writing,
23+ // software distributed under the License is distributed on an
24+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25+ // KIND, either express or implied. See the License for the
26+ // specific language governing permissions and limitations
27+ // under the License.
28+ @ APICommand (name = "scaleVirtualMachine" , description ="Scales the virtual machine to a new service offering." , responseObject =UserVmResponse .class )
29+ public class ScaleVMCmd extends BaseCmd {
30+ public static final Logger s_logger = Logger .getLogger (ScaleVMCmd .class .getName ());
31+ private static final String s_name = "scalevirtualmachineresponse" ;
32+
33+ /////////////////////////////////////////////////////
34+ //////////////// API parameters /////////////////////
35+ /////////////////////////////////////////////////////
36+
37+ @ ACL
38+ @ Parameter (name =ApiConstants .ID , type =CommandType .UUID , entityType =UserVmResponse .class ,
39+ required =true , description ="The ID of the virtual machine" )
40+ private Long id ;
41+
42+ @ ACL
43+ @ Parameter (name =ApiConstants .SERVICE_OFFERING_ID , type =CommandType .UUID , entityType =ServiceOfferingResponse .class ,
44+ required =true , description ="the ID of the service offering for the virtual machine" )
45+ private Long serviceOfferingId ;
46+
47+ /////////////////////////////////////////////////////
48+ /////////////////// Accessors ///////////////////////
49+ /////////////////////////////////////////////////////
50+
51+ public Long getId () {
52+ return id ;
53+ }
54+
55+ public Long getServiceOfferingId () {
56+ return serviceOfferingId ;
57+ }
58+
59+ /////////////////////////////////////////////////////
60+ /////////////// API Implementation///////////////////
61+ /////////////////////////////////////////////////////
62+
63+ @ Override
64+ public String getCommandName () {
65+ return s_name ;
66+ }
67+
68+ public static String getResultObjectName () {
69+ return "virtualmachine" ;
70+ }
71+
72+ @ Override
73+ public long getEntityOwnerId () {
74+ UserVm userVm = _entityMgr .findById (UserVm .class , getId ());
75+ if (userVm != null ) {
76+ return userVm .getAccountId ();
77+ }
78+
79+ return Account .ACCOUNT_ID_SYSTEM ; // no account info given, parent this command to SYSTEM so ERROR events are tracked
80+ }
81+
82+ @ Override
83+ public void execute (){
84+ UserContext .current ().setEventDetails ("Vm Id: " +getId ());
85+ UserVm result = null ;
86+ try {
87+ result = _userVmService .upgradeVirtualMachine (this );
88+ } catch (ResourceUnavailableException ex ) {
89+ s_logger .warn ("Exception: " , ex );
90+ throw new ServerApiException (ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , ex .getMessage ());
91+ } catch (ConcurrentOperationException ex ) {
92+ s_logger .warn ("Exception: " , ex );
93+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , ex .getMessage ());
94+ } catch (ManagementServerException ex ) {
95+ s_logger .warn ("Exception: " , ex );
96+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , ex .getMessage ());
97+ } catch (VirtualMachineMigrationException ex ) {
98+ s_logger .warn ("Exception: " , ex );
99+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , ex .getMessage ());
100+ }
101+ if (result != null ){
102+ UserVmResponse response = _responseGenerator .createUserVmResponse ("virtualmachine" , result ).get (0 );
103+ response .setResponseName (getCommandName ());
104+ this .setResponseObject (response );
105+ } else {
106+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , "Failed to upgrade vm" );
107+ }
108+ }
109+ }
0 commit comments