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 .vm ;
18+
19+ import com .cloud .exception .*;
20+ import com .cloud .user .Account ;
21+ import com .cloud .user .UserContext ;
22+ import com .cloud .uservm .UserVm ;
23+ import org .apache .cloudstack .api .*;
24+ import org .apache .cloudstack .api .response .ServiceOfferingResponse ;
25+ import org .apache .cloudstack .api .response .UserVmResponse ;
26+ import org .apache .log4j .Logger ;
27+
28+
29+ @ APICommand (name = "scaleVirtualMachine" , description ="Scales the virtual machine to a new service offering." , responseObject =UserVmResponse .class )
30+ public class ScaleVMCmd extends BaseCmd {
31+ public static final Logger s_logger = Logger .getLogger (ScaleVMCmd .class .getName ());
32+ private static final String s_name = "scalevirtualmachineresponse" ;
33+
34+ /////////////////////////////////////////////////////
35+ //////////////// API parameters /////////////////////
36+ /////////////////////////////////////////////////////
37+
38+ @ ACL
39+ @ Parameter (name =ApiConstants .ID , type =CommandType .UUID , entityType =UserVmResponse .class ,
40+ required =true , description ="The ID of the virtual machine" )
41+ private Long id ;
42+
43+ @ ACL
44+ @ Parameter (name =ApiConstants .SERVICE_OFFERING_ID , type =CommandType .UUID , entityType =ServiceOfferingResponse .class ,
45+ required =true , description ="the ID of the service offering for the virtual machine" )
46+ private Long serviceOfferingId ;
47+
48+ /////////////////////////////////////////////////////
49+ /////////////////// Accessors ///////////////////////
50+ /////////////////////////////////////////////////////
51+
52+ public Long getId () {
53+ return id ;
54+ }
55+
56+ public Long getServiceOfferingId () {
57+ return serviceOfferingId ;
58+ }
59+
60+ /////////////////////////////////////////////////////
61+ /////////////// API Implementation///////////////////
62+ /////////////////////////////////////////////////////
63+
64+ @ Override
65+ public String getCommandName () {
66+ return s_name ;
67+ }
68+
69+ public static String getResultObjectName () {
70+ return "virtualmachine" ;
71+ }
72+
73+ @ Override
74+ public long getEntityOwnerId () {
75+ UserVm userVm = _entityMgr .findById (UserVm .class , getId ());
76+ if (userVm != null ) {
77+ return userVm .getAccountId ();
78+ }
79+
80+ return Account .ACCOUNT_ID_SYSTEM ; // no account info given, parent this command to SYSTEM so ERROR events are tracked
81+ }
82+
83+ @ Override
84+ public void execute (){
85+ //UserContext.current().setEventDetails("Vm Id: "+getId());
86+ UserVm result = null ;
87+ try {
88+ result = _userVmService .upgradeVirtualMachine (this );
89+ } catch (ResourceUnavailableException ex ) {
90+ s_logger .warn ("Exception: " , ex );
91+ throw new ServerApiException (ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , ex .getMessage ());
92+ } catch (ConcurrentOperationException ex ) {
93+ s_logger .warn ("Exception: " , ex );
94+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , ex .getMessage ());
95+ } catch (ManagementServerException ex ) {
96+ s_logger .warn ("Exception: " , ex );
97+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , ex .getMessage ());
98+ } catch (VirtualMachineMigrationException ex ) {
99+ s_logger .warn ("Exception: " , ex );
100+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , ex .getMessage ());
101+ }
102+ if (result != null ){
103+ UserVmResponse response = _responseGenerator .createUserVmResponse ("virtualmachine" , result ).get (0 );
104+ response .setResponseName (getCommandName ());
105+ this .setResponseObject (response );
106+ } else {
107+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR , "Failed to scale vm" );
108+ }
109+ }
110+ }
0 commit comments