forked from openshift/openshift-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APIResource.java
executable file
·230 lines (193 loc) · 6.62 KB
/
APIResource.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*******************************************************************************
* Copyright (c) 2012 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.openshift.internal.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.openshift.client.ICartridge;
import com.openshift.client.IDomain;
import com.openshift.client.IEmbeddableCartridge;
import com.openshift.client.IOpenShiftConnection;
import com.openshift.client.IUser;
import com.openshift.client.OpenShiftException;
import com.openshift.internal.client.response.CartridgeResourceDTO;
import com.openshift.internal.client.response.DomainResourceDTO;
import com.openshift.internal.client.response.Link;
import com.openshift.internal.client.response.UserResourceDTO;
import com.openshift.internal.client.utils.CollectionUtils;
import com.openshift.internal.client.utils.IOpenShiftJsonConstants;
/**
* @author Andre Dietisheim
* @author Xavier Coulon
*/
public class APIResource extends AbstractOpenShiftResource implements IOpenShiftConnection {
private static final String SYSPROPERTY_PROXY_PORT = "proxyPort";
private static final String SYSPROPERTY_PROXY_HOST = "proxyHost";
private static final String SYSPROPERTY_PROXY_SET = "proxySet";
private final String login;
private final String password;
private List<IDomain> domains;
private UserResource user;
//TODO: implement switch that allows to turn ssl checks on/off
private boolean doSSLChecks = false;
private final List<ICartridge> standaloneCartridgeNames = new ArrayList<ICartridge>();
private final List<IEmbeddableCartridge> embeddedCartridgeNames = new ArrayList<IEmbeddableCartridge>();
protected APIResource(final String login, final String password, final IRestService service,
final Map<String, Link> links) {
super(service, links, null);
this.login = login;
this.password = password;
}
/**
* @return the login
*/
protected final String getLogin() {
return login;
}
protected final String getPassword() {
return password;
}
public void setEnableSSLCertChecks(boolean doSSLChecks) {
this.doSSLChecks = doSSLChecks;
}
public void setProxySet(boolean proxySet) {
if (proxySet) {
System.setProperty(SYSPROPERTY_PROXY_SET, "true");
} else {
System.setProperty(SYSPROPERTY_PROXY_SET, "false");
}
}
public void setProxyHost(String proxyHost) {
System.setProperty(SYSPROPERTY_PROXY_HOST, proxyHost);
}
public void setProxyPort(String proxyPort) {
System.setProperty(SYSPROPERTY_PROXY_PORT, proxyPort);
}
public IUser getUser() throws OpenShiftException {
if (user == null) {
this.user = new UserResource(this, new GetUserRequest().execute(), this.password);
}
return this.user;
}
public List<IDomain> getDomains() throws OpenShiftException {
if (domains == null) {
this.domains = loadDomains();
}
return CollectionUtils.toUnmodifiableCopy(this.domains);
}
private List<IDomain> loadDomains() throws OpenShiftException {
List<IDomain> domains = new ArrayList<IDomain>();
for (DomainResourceDTO domainDTO : new ListDomainsRequest().execute()) {
domains.add(new DomainResource(domainDTO, this));
}
return domains;
}
public IDomain getDomain(String id) throws OpenShiftException {
for (IDomain domain : getDomains()) {
if (domain.getId().equals(id)) {
return domain;
}
}
return null;
}
public IDomain getDefaultDomain() {
final List<IDomain> domains = getDomains();
if (domains.size() > 0) {
return domains.get(0);
}
return null;
}
public IDomain createDomain(String id) throws OpenShiftException {
if (hasDomain(id)) {
throw new OpenShiftException("Domain {0} already exists", id);
}
final DomainResourceDTO domainDTO = new AddDomainRequest().execute(id);
final IDomain domain = new DomainResource(domainDTO, this);
this.domains.add(domain);
return domain;
}
public List<ICartridge> getStandaloneCartridges() throws OpenShiftException {
if (standaloneCartridgeNames.isEmpty()) {
retrieveCartridges();
}
return standaloneCartridgeNames;
}
public List<IEmbeddableCartridge> getEmbeddableCartridges() throws OpenShiftException {
if (embeddedCartridgeNames.isEmpty()) {
retrieveCartridges();
}
return CollectionUtils.toUnmodifiableCopy(embeddedCartridgeNames);
}
private void retrieveCartridges() throws OpenShiftException {
final List<CartridgeResourceDTO> cartridgeDTOs = new GetCartridgesRequest().execute();
for (CartridgeResourceDTO cartridgeDTO : cartridgeDTOs) {
// TODO replace by enum (standalone, embedded)
switch (cartridgeDTO.getType()) {
case STANDALONE:
this.standaloneCartridgeNames.add(new Cartridge(cartridgeDTO.getName()));
break;
case EMBEDDED:
this.embeddedCartridgeNames.add(new EmbeddableCartridge(cartridgeDTO.getName()));
break;
default:
}
}
}
@Override
public void refresh() throws OpenShiftException {
this.domains = null;
}
/**
* Called after a domain has been destroyed
*
* @param domain
* the domain to remove from the API's domains list.
*/
protected void removeDomain(final IDomain domain) {
this.domains.remove(domain);
}
protected boolean hasDomain(String name) throws OpenShiftException {
return getDomain(name) != null;
}
private class AddDomainRequest extends ServiceRequest {
public AddDomainRequest() throws OpenShiftException {
super("ADD_DOMAIN");
}
public DomainResourceDTO execute(String namespace) throws OpenShiftException {
return execute(new ServiceParameter(IOpenShiftJsonConstants.PROPERTY_ID, namespace));
}
}
private class ListDomainsRequest extends ServiceRequest {
public ListDomainsRequest() throws OpenShiftException {
super("LIST_DOMAINS");
}
public List<DomainResourceDTO> execute() throws OpenShiftException {
return super.execute();
}
}
private class GetUserRequest extends ServiceRequest {
public GetUserRequest() throws OpenShiftException {
super("GET_USER");
}
public UserResourceDTO execute() throws OpenShiftException {
return super.execute();
}
}
private class GetCartridgesRequest extends ServiceRequest {
public GetCartridgesRequest() throws OpenShiftException {
super("LIST_CARTRIDGES");
}
public List<CartridgeResourceDTO> execute() throws OpenShiftException {
return super.execute();
}
}
}