Skip to content

Commit 1380c60

Browse files
server: add Host Control Plane State to uservm and systemvm response (#6946)
Co-authored-by: dahn <daan.hoogland@gmail.com>
1 parent d0b34b7 commit 1380c60

File tree

20 files changed

+509
-6
lines changed

20 files changed

+509
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 com.cloud.host;
18+
19+
import com.cloud.resource.ResourceState;
20+
21+
public enum ControlState {
22+
Enabled,
23+
Disabled,
24+
Offline,
25+
Maintenance,
26+
Unknown;
27+
28+
public static ControlState getControlState(Status hostStatus, ResourceState hostResourceState) {
29+
if (hostStatus == null || Status.Unknown.equals(hostStatus) || hostResourceState == null) {
30+
return ControlState.Unknown;
31+
} else if (hostStatus.lostConnection()) {
32+
return Offline;
33+
} else if (ResourceState.isMaintenanceState(hostResourceState)) {
34+
return Maintenance;
35+
} else if (ResourceState.Enabled.equals(hostResourceState)) {
36+
return Enabled;
37+
} else {
38+
return Disabled;
39+
}
40+
}
41+
}

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public class ApiConstants {
203203
public static final String HOST_ID = "hostid";
204204
public static final String HOST_IDS = "hostids";
205205
public static final String HOST_NAME = "hostname";
206+
public static final String HOST_CONTROL_STATE = "hostcontrolstate";
206207
public static final String HOSTS_MAP = "hostsmap";
207208
public static final String HYPERVISOR = "hypervisor";
208209
public static final String INLINE = "inline";

api/src/main/java/org/apache/cloudstack/api/response/DomainRouterResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public class DomainRouterResponse extends BaseResponseWithAnnotations implements
8989
@Param(description = "the hostname for the router")
9090
private String hostName;
9191

92+
@SerializedName(ApiConstants.HOST_CONTROL_STATE)
93+
@Param(description = "the control state of the host for the router")
94+
private String hostControlState;
95+
9296
@SerializedName("hypervisor")
9397
@Param(description = "the hypervisor on which the template runs")
9498
private String hypervisor;
@@ -302,6 +306,10 @@ public void setHostName(String hostName) {
302306
this.hostName = hostName;
303307
}
304308

309+
public void setHostControlState(String hostControlState) {
310+
this.hostControlState = hostControlState;
311+
}
312+
305313
public String getHypervisor() {
306314
return hypervisor;
307315
}

api/src/main/java/org/apache/cloudstack/api/response/SystemVmResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public class SystemVmResponse extends BaseResponseWithAnnotations {
9090
@Param(description = "the hostname for the system VM")
9191
private String hostName;
9292

93+
@SerializedName(ApiConstants.HOST_CONTROL_STATE)
94+
@Param(description = "the control state of the host for the system VM")
95+
private String hostControlState;
96+
9397
@SerializedName("hypervisor")
9498
@Param(description = "the hypervisor on which the template runs")
9599
private String hypervisor;
@@ -283,6 +287,14 @@ public void setHostName(String hostName) {
283287
this.hostName = hostName;
284288
}
285289

290+
public String getHostControlState() {
291+
return hostControlState;
292+
}
293+
294+
public void setHostControlState(String hostControlState) {
295+
this.hostControlState = hostControlState;
296+
}
297+
286298
public String getHypervisor() {
287299
return hypervisor;
288300
}

api/src/main/java/org/apache/cloudstack/api/response/UserVmResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
118118
@Param(description = "the name of the host for the virtual machine")
119119
private String hostName;
120120

121+
@SerializedName(ApiConstants.HOST_CONTROL_STATE)
122+
@Param(description = "the control state of the host for the virtual machine")
123+
private String hostControlState;
124+
121125
@SerializedName(ApiConstants.TEMPLATE_ID)
122126
@Param(description = "the ID of the template for the virtual machine. A -1 is returned if the virtual machine was created from an ISO file.")
123127
private String templateId;
@@ -461,6 +465,10 @@ public String getHostName() {
461465
return hostName;
462466
}
463467

468+
public String getHostControlState() {
469+
return hostControlState;
470+
}
471+
464472
public String getTemplateId() {
465473
return templateId;
466474
}
@@ -703,6 +711,10 @@ public void setHostName(String hostName) {
703711
this.hostName = hostName;
704712
}
705713

714+
public void setHostControlState(String hostControlState) {
715+
this.hostControlState = hostControlState;
716+
}
717+
706718
public void setTemplateId(String templateId) {
707719
this.templateId = templateId;
708720
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 com.cloud.host;
18+
19+
import com.cloud.resource.ResourceState;
20+
21+
import junit.framework.TestCase;
22+
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
public class ControlStateTest extends TestCase {
27+
28+
void verifyHostControlState(Status hostStatus, ResourceState hostResourceState, ControlState expectedControlState) {
29+
Assert.assertEquals(expectedControlState, ControlState.getControlState(hostStatus, hostResourceState));
30+
}
31+
32+
@Test
33+
public void testHostControlState1() {
34+
// Unknown state
35+
verifyHostControlState(null, null, ControlState.Unknown);
36+
verifyHostControlState(null, ResourceState.Enabled, ControlState.Unknown);
37+
verifyHostControlState(Status.Up, null, ControlState.Unknown);
38+
verifyHostControlState(Status.Disconnected, null, ControlState.Unknown);
39+
verifyHostControlState(Status.Down, null, ControlState.Unknown);
40+
41+
verifyHostControlState(Status.Unknown, null, ControlState.Unknown);
42+
verifyHostControlState(Status.Unknown, ResourceState.Enabled, ControlState.Unknown);
43+
verifyHostControlState(Status.Unknown, ResourceState.ErrorInPrepareForMaintenance, ControlState.Unknown);
44+
verifyHostControlState(Status.Unknown, ResourceState.PrepareForMaintenance, ControlState.Unknown);
45+
verifyHostControlState(Status.Unknown, ResourceState.ErrorInMaintenance, ControlState.Unknown);
46+
verifyHostControlState(Status.Unknown, ResourceState.Maintenance, ControlState.Unknown);
47+
verifyHostControlState(Status.Unknown, ResourceState.Creating, ControlState.Unknown);
48+
verifyHostControlState(Status.Unknown, ResourceState.Disabled, ControlState.Unknown);
49+
verifyHostControlState(Status.Unknown, ResourceState.Error, ControlState.Unknown);
50+
verifyHostControlState(Status.Unknown, ResourceState.Degraded, ControlState.Unknown);
51+
}
52+
@Test
53+
public void testHostControlState2() {
54+
// Host is Up and Enabled
55+
verifyHostControlState(Status.Creating, ResourceState.Enabled, ControlState.Enabled);
56+
verifyHostControlState(Status.Connecting, ResourceState.Enabled, ControlState.Enabled);
57+
verifyHostControlState(Status.Up, ResourceState.Enabled, ControlState.Enabled);
58+
}
59+
60+
@Test
61+
public void testHostControlState3() {
62+
// Host is Up and not Enabled
63+
verifyHostControlState(Status.Up, ResourceState.Creating, ControlState.Disabled);
64+
verifyHostControlState(Status.Up, ResourceState.Disabled, ControlState.Disabled);
65+
verifyHostControlState(Status.Up, ResourceState.Error, ControlState.Disabled);
66+
verifyHostControlState(Status.Up, ResourceState.Degraded, ControlState.Disabled);
67+
68+
// Host is Creating and not Enabled
69+
verifyHostControlState(Status.Creating, ResourceState.Creating, ControlState.Disabled);
70+
verifyHostControlState(Status.Creating, ResourceState.Disabled, ControlState.Disabled);
71+
verifyHostControlState(Status.Creating, ResourceState.Error, ControlState.Disabled);
72+
verifyHostControlState(Status.Creating, ResourceState.Degraded, ControlState.Disabled);
73+
74+
// Host is Connecting and not Enabled
75+
verifyHostControlState(Status.Connecting, ResourceState.Creating, ControlState.Disabled);
76+
verifyHostControlState(Status.Connecting, ResourceState.Disabled, ControlState.Disabled);
77+
verifyHostControlState(Status.Connecting, ResourceState.Error, ControlState.Disabled);
78+
verifyHostControlState(Status.Connecting, ResourceState.Degraded, ControlState.Disabled);
79+
}
80+
81+
@Test
82+
public void testHostControlState4() {
83+
// Host is Up and Maintenance mode
84+
verifyHostControlState(Status.Up, ResourceState.ErrorInPrepareForMaintenance, ControlState.Maintenance);
85+
verifyHostControlState(Status.Up, ResourceState.PrepareForMaintenance, ControlState.Maintenance);
86+
verifyHostControlState(Status.Up, ResourceState.ErrorInMaintenance, ControlState.Maintenance);
87+
verifyHostControlState(Status.Up, ResourceState.Maintenance, ControlState.Maintenance);
88+
}
89+
90+
@Test
91+
public void testHostControlState5() {
92+
// Host in other states and Enabled
93+
verifyHostControlState(Status.Down, ResourceState.Enabled, ControlState.Offline);
94+
verifyHostControlState(Status.Disconnected, ResourceState.Enabled, ControlState.Offline);
95+
verifyHostControlState(Status.Alert, ResourceState.Enabled, ControlState.Offline);
96+
verifyHostControlState(Status.Removed, ResourceState.Enabled, ControlState.Offline);
97+
verifyHostControlState(Status.Error, ResourceState.Enabled, ControlState.Offline);
98+
verifyHostControlState(Status.Rebalancing, ResourceState.Enabled, ControlState.Offline);
99+
100+
// Host in other states and Disabled
101+
verifyHostControlState(Status.Down, ResourceState.Disabled, ControlState.Offline);
102+
verifyHostControlState(Status.Disconnected, ResourceState.Disabled, ControlState.Offline);
103+
verifyHostControlState(Status.Alert, ResourceState.Disabled, ControlState.Offline);
104+
verifyHostControlState(Status.Removed, ResourceState.Disabled, ControlState.Offline);
105+
verifyHostControlState(Status.Error, ResourceState.Disabled, ControlState.Offline);
106+
verifyHostControlState(Status.Rebalancing, ResourceState.Disabled, ControlState.Offline);
107+
}
108+
109+
}

engine/schema/src/main/resources/META-INF/db/schema-41720to41800.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ CREATE VIEW `cloud`.`domain_router_view` AS
6565
host.name host_name,
6666
host.hypervisor_type,
6767
host.cluster_id cluster_id,
68+
host.status host_status,
69+
host.resource_state host_resource_state,
6870
vm_template.id template_id,
6971
vm_template.uuid template_uuid,
7072
service_offering.id service_offering_id,
@@ -744,6 +746,8 @@ SELECT
744746
`host`.`uuid` AS `host_uuid`,
745747
`host`.`name` AS `host_name`,
746748
`host`.`cluster_id` AS `cluster_id`,
749+
`host`.`status` AS `host_status`,
750+
`host`.`resource_state` AS `host_resource_state`,
747751
`vm_template`.`id` AS `template_id`,
748752
`vm_template`.`uuid` AS `template_uuid`,
749753
`vm_template`.`name` AS `template_name`,

server/src/main/java/com/cloud/api/ApiResponseHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import javax.inject.Inject;
3939

40+
import com.cloud.host.ControlState;
4041
import com.cloud.utils.security.CertificateHelper;
4142
import com.cloud.user.UserData;
4243
import com.cloud.api.query.dao.UserVmJoinDao;
@@ -1549,6 +1550,7 @@ public SystemVmResponse createSystemVmResponse(VirtualMachine vm) {
15491550
if (host != null) {
15501551
vmResponse.setHostId(host.getUuid());
15511552
vmResponse.setHostName(host.getName());
1553+
vmResponse.setHostControlState(ControlState.getControlState(host.getStatus(), host.getResourceState()).toString());
15521554
}
15531555
}
15541556

server/src/main/java/com/cloud/api/query/dao/DomainRouterJoinDaoImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.cloud.api.ApiResponseHelper;
3939
import com.cloud.api.query.vo.DomainRouterJoinVO;
4040
import com.cloud.dc.HostPodVO;
41+
import com.cloud.host.ControlState;
4142
import com.cloud.network.Networks.TrafficType;
4243
import com.cloud.network.router.VirtualRouter;
4344
import com.cloud.network.router.VirtualRouter.Role;
@@ -135,6 +136,7 @@ public DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO router, A
135136
if (router.getHostId() != null) {
136137
routerResponse.setHostId(router.getHostUuid());
137138
routerResponse.setHostName(router.getHostName());
139+
routerResponse.setHostControlState(ControlState.getControlState(router.getHostStatus(), router.getHostResourceState()).toString());
138140
}
139141
routerResponse.setPodId(router.getPodUuid());
140142
HostPodVO pod = ApiDBUtils.findPodById(router.getPodId());

server/src/main/java/com/cloud/api/query/dao/UserVmJoinDaoImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.cloud.api.ApiResponseHelper;
5353
import com.cloud.api.query.vo.UserVmJoinVO;
5454
import com.cloud.gpu.GPU;
55+
import com.cloud.host.ControlState;
5556
import com.cloud.service.ServiceOfferingDetailsVO;
5657
import com.cloud.storage.GuestOS;
5758
import com.cloud.user.Account;
@@ -172,6 +173,9 @@ public UserVmResponse newUserVmResponse(ResponseView view, String objectName, Us
172173
userVmResponse.setHostId(userVm.getHostUuid());
173174
userVmResponse.setHostName(userVm.getHostName());
174175
}
176+
if (userVm.getHostStatus() != null) {
177+
userVmResponse.setHostControlState(ControlState.getControlState(userVm.getHostStatus(), userVm.getHostResourceState()).toString());
178+
}
175179

176180
if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
177181
userVmResponse.setTemplateId(userVm.getTemplateUuid());

0 commit comments

Comments
 (0)