/
BackendClustersResource.java
161 lines (135 loc) · 6.54 KB
/
BackendClustersResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package org.ovirt.engine.api.restapi.resource;
import static org.ovirt.engine.api.restapi.resource.BackendDataCenterResource.getStoragePool;
import java.util.List;
import javax.ws.rs.core.Response;
import org.ovirt.engine.api.model.ActionableResource;
import org.ovirt.engine.api.model.Clusters;
import org.ovirt.engine.api.model.Network;
import org.ovirt.engine.api.resource.ClusterResource;
import org.ovirt.engine.api.resource.ClustersResource;
import org.ovirt.engine.api.restapi.resource.utils.LinksTreeNode;
import org.ovirt.engine.api.restapi.util.LinkHelper;
import org.ovirt.engine.core.common.action.ActionType;
import org.ovirt.engine.core.common.action.ClusterOperationParameters;
import org.ovirt.engine.core.common.businessentities.Cluster;
import org.ovirt.engine.core.common.businessentities.StoragePool;
import org.ovirt.engine.core.common.interfaces.SearchType;
import org.ovirt.engine.core.common.mode.ApplicationMode;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.QueryParametersBase;
import org.ovirt.engine.core.common.queries.QueryType;
import org.ovirt.engine.core.compat.Guid;
public class BackendClustersResource extends AbstractBackendCollectionResource<org.ovirt.engine.api.model.Cluster, Cluster>
implements ClustersResource {
static final String[] VIRT_ONLY_MODE_COLLECTIONS_TO_HIDE = {"glustervolumes", "glusterhooks" };
private final ManagementNetworkFinder managementNetworkFinder;
public BackendClustersResource() {
super(org.ovirt.engine.api.model.Cluster.class, Cluster.class);
managementNetworkFinder = new ManagementNetworkFinder(this);
}
@Override
public Clusters list() {
ApplicationMode appMode = getCurrent().getApplicationMode();
if (appMode == ApplicationMode.VirtOnly) {
return listVirtOnly();
} else {
return listAll();
}
}
private Clusters listVirtOnly() {
if (isFiltered()) {
return mapVirtOnlyCollection(getBackendCollection(QueryType.GetAllClusters,
new QueryParametersBase(), SearchType.Cluster));
} else {
return mapVirtOnlyCollection(getBackendCollection(SearchType.Cluster));
}
}
private Clusters listAll() {
if (isFiltered()) {
return mapCollection(getBackendCollection(QueryType.GetAllClusters,
new QueryParametersBase(), SearchType.Cluster));
} else {
return mapCollection(getBackendCollection(SearchType.Cluster));
}
}
@Override
public ClusterResource getClusterResource(String id) {
return inject(new BackendClusterResource(id, this));
}
@Override
public Response add(org.ovirt.engine.api.model.Cluster cluster) {
validateParameters(cluster, getMandatoryParameters());
StoragePool dataCenter = getDataCenter(cluster);
BackendExternalProviderHelper.completeExternalProviders(this, cluster.getExternalNetworkProviders());
return performCreate(ActionType.AddCluster,
createAddCommandParams(cluster, dataCenter),
new QueryIdResolver<Guid>(QueryType.GetClusterById, IdQueryParameters.class));
}
protected String[] getMandatoryParameters() {
return new String[] { "name", "dataCenter.name|id" };
}
protected StoragePool getDataCenter(org.ovirt.engine.api.model.Cluster cluster) {
return getStoragePool(cluster.getDataCenter(), this);
}
private ClusterOperationParameters createAddCommandParams(org.ovirt.engine.api.model.Cluster cluster, StoragePool dataCenter) {
Cluster clusterEntity = map(cluster, map(dataCenter));
if (!(cluster.isSetErrorHandling() && cluster.getErrorHandling().isSetOnError())) {
clusterEntity.setMigrateOnError(null);
}
final Guid managementNetworkId = managementNetworkFinder.getManagementNetworkId(cluster, dataCenter.getId());
return new ClusterOperationParameters(clusterEntity, managementNetworkId);
}
protected Clusters mapCollection(List<Cluster> entities) {
Clusters collection = new Clusters();
for (Cluster entity : entities) {
// Specifying LinkHelper.NO_PARENT to explicitly point link to API root:
// <cluster href=".../api/clusters/xxx">
// rather than under datacenter:
// <cluster href=".../api/datacenters/yyy/clusters/xxx">
//
// (The second option would be selected by default due to the fact that
// the cluster has a datacenter-id set in it. That is the current
// LinkHelper behavior)
collection.getClusters().add(addLinks(populate(map(entity), entity), LinkHelper.NO_PARENT));
}
return collection;
}
private Clusters mapVirtOnlyCollection(List<Cluster> entities) {
Clusters collection = new Clusters();
for (Cluster entity : entities) {
collection.getClusters().add(addLinks(populate(map(entity), entity), LinkHelper.NO_PARENT, VIRT_ONLY_MODE_COLLECTIONS_TO_HIDE));
}
return collection;
}
/**
* Map the storage pool (i.e. datacenter entity) to a Cluster instance
* with the same compatibility version
*/
protected Cluster map(StoragePool pool) {
return getMapper(StoragePool.class, Cluster.class).map(pool, null);
}
@Override
protected org.ovirt.engine.api.model.Cluster doPopulate(org.ovirt.engine.api.model.Cluster cluster, Cluster entity) {
final Guid clusterId = entity.getId();
final org.ovirt.engine.core.common.businessentities.network.Network network =
getOptionalEntity(org.ovirt.engine.core.common.businessentities.network.Network.class,
QueryType.GetManagementNetwork,
new IdQueryParameters(clusterId),
clusterId.toString(),
false);
if (network != null) {
final Network managementNetwork = new Network();
managementNetwork.setCluster(cluster);
managementNetwork.setId(network.getId().toString());
cluster.setManagementNetwork(managementNetwork);
}
return cluster;
}
@Override
public void follow(ActionableResource entity, LinksTreeNode linksTree) {
Clusters clusters = (Clusters)entity;
for (org.ovirt.engine.api.model.Cluster cluster : clusters.getClusters()) {
BackendClusterResource.removeGlusterVolumesFromFollowTree(linksTree, cluster);
}
}
}