Skip to content

Commit 6a26a63

Browse files
committed
update vm state api
1 parent e2d18c0 commit 6a26a63

6 files changed

Lines changed: 257 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
21+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<artifactId>cloud-plugin-api-vm-state-update</artifactId>
24+
<name>Apache CloudStack Plugin - VM State Update API</name>
25+
<parent>
26+
<groupId>org.apache.cloudstack</groupId>
27+
<artifactId>cloudstack-plugins</artifactId>
28+
<version>4.23.0.0-SNAPSHOT</version>
29+
<relativePath>../../pom.xml</relativePath>
30+
</parent>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.apache.cloudstack</groupId>
34+
<artifactId>cloud-api</artifactId>
35+
<version>${project.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.apache.cloudstack</groupId>
39+
<artifactId>cloud-utils</artifactId>
40+
<version>${project.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.apache.cloudstack</groupId>
44+
<artifactId>cloud-engine-schema</artifactId>
45+
<version>${project.version}</version>
46+
</dependency>
47+
</dependencies>
48+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.vm;
18+
19+
import javax.inject.Inject;
20+
21+
import org.apache.cloudstack.acl.RoleType;
22+
import org.apache.cloudstack.api.ACL;
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.BaseCmd;
27+
import org.apache.cloudstack.api.Parameter;
28+
import org.apache.cloudstack.api.ServerApiException;
29+
import org.apache.cloudstack.api.response.SuccessResponse;
30+
import org.apache.cloudstack.api.response.UserVmResponse;
31+
import org.apache.cloudstack.vmstateupdate.UpdateVmStateService;
32+
33+
import com.cloud.user.Account;
34+
import com.cloud.vm.VirtualMachine;
35+
36+
@APICommand(name = "updateVirtualMachineState",
37+
description = "Updates the state of a virtual machine. This is an admin-only operation.",
38+
responseObject = SuccessResponse.class,
39+
entityType = {VirtualMachine.class},
40+
requestHasSensitiveInfo = false,
41+
responseHasSensitiveInfo = false,
42+
authorized = {RoleType.Admin})
43+
public class UpdateVmStateCmd extends BaseCmd {
44+
45+
@ACL
46+
@Parameter(name = ApiConstants.ID,
47+
type = CommandType.UUID,
48+
entityType = UserVmResponse.class,
49+
required = true,
50+
description = "The ID of the virtual machine")
51+
private Long id;
52+
53+
@Parameter(name = ApiConstants.STATE,
54+
type = CommandType.STRING,
55+
required = true,
56+
description = "The state to set for the virtual machine. Valid values are: Starting, Running, Stopping, Stopped, Destroyed, Expunging, Migrating, Error, Unknown, Shutdown, Restoring")
57+
private String state;
58+
59+
@Inject
60+
UpdateVmStateService _updateVmStateService;
61+
62+
public Long getId() {
63+
return id;
64+
}
65+
66+
public String getState() {
67+
return state;
68+
}
69+
70+
@Override
71+
public void execute() throws ServerApiException {
72+
try {
73+
boolean result = _updateVmStateService.updateVmState(getId(), getState());
74+
if (result) {
75+
SuccessResponse response = new SuccessResponse(getCommandName());
76+
response.setDisplayText("Virtual machine state updated successfully to " + getState());
77+
setResponseObject(response);
78+
} else {
79+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update virtual machine state");
80+
}
81+
} catch (IllegalArgumentException e) {
82+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.getMessage());
83+
}
84+
}
85+
86+
@Override
87+
public long getEntityOwnerId() {
88+
return Account.ACCOUNT_ID_SYSTEM;
89+
}
90+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.vmstateupdate;
18+
19+
import com.cloud.utils.component.PluggableService;
20+
21+
public interface UpdateVmStateService extends PluggableService {
22+
boolean updateVmState(Long vmId, String state) throws IllegalArgumentException;
23+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.vmstateupdate;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import javax.inject.Inject;
23+
24+
import org.apache.cloudstack.api.command.admin.vm.UpdateVmStateCmd;
25+
import org.springframework.stereotype.Component;
26+
27+
import com.cloud.utils.component.ComponentLifecycleBase;
28+
import com.cloud.vm.VMInstanceVO;
29+
import com.cloud.vm.VirtualMachine;
30+
import com.cloud.vm.dao.VMInstanceDao;
31+
32+
@Component
33+
public class UpdateVmStateServiceImpl extends ComponentLifecycleBase implements UpdateVmStateService {
34+
35+
@Inject
36+
private VMInstanceDao _vmInstanceDao;
37+
38+
@Override
39+
public boolean updateVmState(Long vmId, String state) throws IllegalArgumentException {
40+
VirtualMachine.State newState;
41+
try {
42+
newState = VirtualMachine.State.valueOf(state);
43+
} catch (IllegalArgumentException e) {
44+
throw new IllegalArgumentException("Invalid state: " + state +
45+
". Valid values are: Starting, Running, Stopping, Stopped, Destroyed, Expunging, Migrating, Error, Unknown, Shutdown, Restoring");
46+
}
47+
48+
VMInstanceVO vm = _vmInstanceDao.findById(vmId);
49+
if (vm == null) {
50+
throw new IllegalArgumentException("Virtual machine with ID " + vmId + " not found");
51+
}
52+
53+
vm.setState(newState);
54+
return _vmInstanceDao.update(vmId, vm);
55+
}
56+
57+
@Override
58+
public List<Class<?>> getCommands() {
59+
List<Class<?>> cmdList = new ArrayList<>();
60+
cmdList.add(UpdateVmStateCmd.class);
61+
return cmdList;
62+
}
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<beans xmlns="http://www.springframework.org/schema/beans"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xmlns:context="http://www.springframework.org/schema/context"
23+
xmlns:aop="http://www.springframework.org/schema/aop"
24+
xsi:schemaLocation="http://www.springframework.org/schema/beans
25+
http://www.springframework.org/schema/beans/spring-beans.xsd
26+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
27+
http://www.springframework.org/schema/context
28+
http://www.springframework.org/schema/context/spring-context.xsd">
29+
30+
<bean id="updateVmStateServiceImpl" class="org.apache.cloudstack.vmstateupdate.UpdateVmStateServiceImpl"/>
31+
32+
</beans>

plugins/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<module>api/discovery</module>
6060
<module>api/rate-limit</module>
6161
<module>api/solidfire-intg-test</module>
62+
<module>api/vm-state-update</module>
6263

6364
<module>backup/dummy</module>
6465
<module>backup/networker</module>

0 commit comments

Comments
 (0)