diff options
author | Shireesh Anjal <shireesh@gluster.com> | 2011-11-25 20:13:35 +0530 |
---|---|---|
committer | Shireesh Anjal <shireesh@gluster.com> | 2011-11-25 20:13:35 +0530 |
commit | 1142b0e41de39010de7845cf70d71dbb001fc1dc (patch) | |
tree | 3513487f65c1a7df47996bd2852393aceaac1b8a /src/org.gluster.storage.management.client/src/org/gluster/storage/management | |
parent | 92c52d8edf285945d31e446503fc742fde9dcc49 (diff) |
Renamed projects / packages com.gluster.* to org.gluster.*
Diffstat (limited to 'src/org.gluster.storage.management.client/src/org/gluster/storage/management')
10 files changed, 1329 insertions, 0 deletions
diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/AbstractClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/AbstractClient.java new file mode 100644 index 00000000..bef3f87b --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/AbstractClient.java @@ -0,0 +1,429 @@ +package org.gluster.storage.management.client;
+
+import static org.gluster.storage.management.client.constants.ClientConstants.ALGORITHM_SUNX509;
+import static org.gluster.storage.management.client.constants.ClientConstants.KEYSTORE_TYPE_JKS;
+import static org.gluster.storage.management.client.constants.ClientConstants.PROTOCOL_TLS;
+import static org.gluster.storage.management.client.constants.ClientConstants.TRUSTED_KEYSTORE;
+import static org.gluster.storage.management.client.constants.ClientConstants.TRUSTED_KEYSTORE_ACCESS;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.ConnectException;
+import java.net.URI;
+import java.security.KeyStore;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManagerFactory;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+import org.gluster.storage.management.client.utils.ClientUtil;
+import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.WebResource.Builder;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.representation.Form;
+import com.sun.jersey.client.urlconnection.HTTPSProperties;
+import com.sun.jersey.core.util.MultivaluedMapImpl;
+import com.sun.jersey.multipart.FormDataMultiPart;
+
+public abstract class AbstractClient {
+ private static final String HTTP_HEADER_AUTH = "Authorization";
+ protected static final MultivaluedMap<String, String> NO_PARAMS = new MultivaluedMapImpl();
+ protected static String clusterName;
+ protected static String securityToken;
+ protected WebResource resource;
+ private String authHeader;
+ private Client client;
+
+ /**
+ * This constructor will work only after the data model manager has been initialized.
+ */
+ public AbstractClient() {
+ this(securityToken, clusterName);
+ }
+
+ /**
+ * This constructor will work only after the data model manager has been initialized.
+ */
+ public AbstractClient(String clusterName) {
+ this(securityToken, clusterName);
+ }
+
+ public AbstractClient(String securityToken, String clusterName) {
+ AbstractClient.clusterName = clusterName;
+ setSecurityToken(securityToken);
+
+ createClient();
+
+ // this must be after setting clusterName as sub-classes may refer to cluster name in the getResourcePath method
+ resource = client.resource(ClientUtil.getServerBaseURI()).path(getResourcePath());
+ }
+
+ private void createClient() {
+ SSLContext context = initializeSSLContext();
+ DefaultClientConfig config = createClientConfig(context);
+ client = Client.create(config);
+ }
+
+ private DefaultClientConfig createClientConfig(SSLContext context) {
+ DefaultClientConfig config = new DefaultClientConfig();
+ config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
+ new HTTPSProperties(createHostnameVerifier(), context));
+ return config;
+ }
+
+ private HostnameVerifier createHostnameVerifier() {
+ HostnameVerifier hostnameVerifier = new HostnameVerifier() {
+ @Override
+ public boolean verify(String arg0, SSLSession arg1) {
+ return true;
+ }
+ };
+ return hostnameVerifier;
+ }
+
+ private SSLContext initializeSSLContext() {
+ SSLContext context = null;
+ try {
+ context = SSLContext.getInstance(PROTOCOL_TLS);
+
+ KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE_JKS);
+ keyStore.load(loadResource(TRUSTED_KEYSTORE), TRUSTED_KEYSTORE_ACCESS.toCharArray());
+
+ KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(ALGORITHM_SUNX509);
+ keyManagerFactory.init(keyStore, TRUSTED_KEYSTORE_ACCESS.toCharArray());
+
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(ALGORITHM_SUNX509);
+ trustManagerFactory.init(keyStore);
+
+ context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
+ } catch (Exception e) {
+ throw new GlusterRuntimeException(
+ "Couldn't initialize SSL Context with Gluster Management Gateway! Error: " + e, e);
+ }
+ return context;
+ }
+
+ private InputStream loadResource(String resourcePath) {
+ return this.getClass().getClassLoader().getResourceAsStream(resourcePath);
+ }
+
+ /**
+ * Fetches the given resource by dispatching a GET request
+ *
+ * @param res
+ * Resource to be fetched
+ * @param queryParams
+ * Query parameters to be sent for the GET request
+ * @param responseClass
+ * Expected class of the response
+ * @return Object of responseClass received as a result of the GET request
+ */
+ private <T> T fetchResource(WebResource res, MultivaluedMap<String, String> queryParams, Class<T> responseClass) {
+ try {
+ return res.queryParams(queryParams).header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.APPLICATION_XML)
+ .get(responseClass);
+ } catch (Exception e1) {
+ throw createGlusterException(e1);
+ }
+ }
+
+ private GlusterRuntimeException createGlusterException(Exception e) {
+ if (e instanceof GlusterRuntimeException) {
+ return (GlusterRuntimeException) e;
+ }
+
+ if (e instanceof UniformInterfaceException) {
+ UniformInterfaceException uie = (UniformInterfaceException) e;
+ if ((uie.getResponse().getStatus() == Response.Status.UNAUTHORIZED.getStatusCode())) {
+ // authentication failed. clear security token.
+ setSecurityToken(null);
+ return new GlusterRuntimeException("Invalid credentials!");
+ } else {
+ return new GlusterRuntimeException("[" + uie.getResponse().getStatus() + "]["
+ + uie.getResponse().getEntity(String.class) + "]");
+ }
+ } else {
+ Throwable cause = e.getCause();
+ if (cause != null && cause instanceof ConnectException) {
+ return new GlusterRuntimeException("Couldn't connect to Gluster Management Gateway!");
+ }
+
+ return new GlusterRuntimeException("Exception in REST communication! [" + e.getMessage() + "]", e);
+ }
+ }
+
+ protected void downloadResource(WebResource res, String filePath) {
+ ClientResponse response = null;
+ try {
+ response = res.header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.APPLICATION_OCTET_STREAM)
+ .get(ClientResponse.class);
+ checkResponseStatus(response);
+ } catch (Exception e1) {
+ throw createGlusterException(e1);
+ }
+
+ try {
+ if (!response.hasEntity()) {
+ throw new GlusterRuntimeException("No entity in response!");
+ }
+
+ InputStream inputStream = response.getEntityInputStream();
+ FileOutputStream outputStream = new FileOutputStream(filePath);
+
+ int c;
+ while ((c = inputStream.read()) != -1) {
+ outputStream.write(c);
+ }
+ inputStream.close();
+ outputStream.close();
+ } catch (IOException e) {
+ throw new GlusterRuntimeException("Error while downloading resource [" + res.getURI().getPath() + "]", e);
+ }
+ }
+
+ public void uploadResource(WebResource res, FormDataMultiPart form) {
+ try {
+ res.header(HTTP_HEADER_AUTH, authHeader).type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, form);
+ } catch (Exception e) {
+ throw createGlusterException(e);
+ }
+ }
+
+ /**
+ * Fetches the default resource (the one returned by {@link AbstractClient#getResourcePath()}) by dispatching a GET
+ * request on the resource
+ *
+ * @param queryParams
+ * Query parameters to be sent for the GET request
+ * @param responseClass
+ * Expected class of the response
+ * @return Object of responseClass received as a result of the GET request
+ */
+ protected <T> T fetchResource(MultivaluedMap<String, String> queryParams, Class<T> responseClass) {
+ return fetchResource(resource, queryParams, responseClass);
+ }
+
+ /**
+ * Fetches the default resource (the one returned by {@link AbstractClient#getResourcePath()}) by dispatching a GET
+ * request on the resource
+ *
+ * @param responseClass
+ * Expected class of the response
+ * @return Object of responseClass received as a result of the GET request
+ */
+ protected <T> T fetchResource(Class<T> responseClass) {
+ return fetchResource(resource, NO_PARAMS, responseClass);
+ }
+
+ /**
+ * Fetches the resource whose name is arrived at by appending the "subResourceName" parameter to the default
+ * resource (the one returned by {@link AbstractClient#getResourcePath()})
+ *
+ * @param subResourceName
+ * Name of the sub-resource
+ * @param responseClass
+ * Expected class of the response
+ * @return Object of responseClass received as a result of the GET request on the sub-resource
+ */
+ protected <T> T fetchSubResource(String subResourceName, Class<T> responseClass) {
+ return fetchResource(resource.path(subResourceName), NO_PARAMS, responseClass);
+ }
+
+ protected void downloadSubResource(String subResourceName, String filePath) {
+ downloadResource(resource.path(subResourceName), filePath);
+ }
+
+ /**
+ * Fetches the resource whose name is arrived at by appending the "subResourceName" parameter to the default
+ * resource (the one returned by {@link AbstractClient#getResourcePath()})
+ *
+ * @param subResourceName
+ * Name of the sub-resource
+ * @param queryParams
+ * Query parameters to be sent for the GET request
+ * @param responseClass
+ * Expected class of the response
+ * @return Object of responseClass received as a result of the GET request on the sub-resource
+ */
+ protected <T> T fetchSubResource(String subResourceName, MultivaluedMap<String, String> queryParams,
+ Class<T> responseClass) {
+ return fetchResource(resource.path(subResourceName), queryParams, responseClass);
+ }
+
+ private ClientResponse postRequest(WebResource resource, Form form) {
+ try {
+ ClientResponse response = prepareFormRequestBuilder(resource).post(ClientResponse.class, form);
+ checkResponseStatus(response);
+ return response;
+ } catch (UniformInterfaceException e) {
+ throw new GlusterRuntimeException(e.getResponse().getEntity(String.class));
+ }
+ }
+
+ /**
+ * Submits given object to the resource and returns the object received as response
+ *
+ * @param responseClass
+ * Class of the object expected as response
+ * @param requestObject
+ * the Object to be submitted
+ * @return Object of given class received as response
+ */
+ protected <T> T postObject(Class<T> responseClass, Object requestObject) {
+ return resource.type(MediaType.APPLICATION_XML).header(HTTP_HEADER_AUTH, authHeader)
+ .accept(MediaType.APPLICATION_XML).post(responseClass, requestObject);
+ }
+
+ /**
+ * Submits given Form using POST method to the resource and returns the object received as response
+ *
+ * @param form
+ * Form to be submitted
+ */
+ protected URI postRequest(Form form) {
+ return postRequest(resource, form).getLocation();
+ }
+
+ /**
+ * Submits given Form using POST method to the given sub-resource and returns the object received as response
+ *
+ * @param subResourceName
+ * Name of the sub-resource to which the request is to be posted
+ * @param form
+ * Form to be submitted
+ */
+ protected void postRequest(String subResourceName, Form form) {
+ postRequest(resource.path(subResourceName), form);
+ }
+
+ private ClientResponse putRequest(WebResource resource, Form form) {
+ try {
+ ClientResponse response = prepareFormRequestBuilder(resource).put(ClientResponse.class, form);
+ checkResponseStatus(response);
+ return response;
+ } catch (Exception e) {
+ throw createGlusterException(e);
+ }
+ }
+
+ private void checkResponseStatus(ClientResponse response) {
+ if ((response.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode())) {
+ // authentication failed. clear security token.
+ setSecurityToken(null);
+ throw new GlusterRuntimeException("Invalid credentials!");
+ }
+ if (response.getStatus() >= 300) {
+ throw new GlusterRuntimeException(response.getEntity(String.class));
+ }
+ }
+
+ public Builder prepareFormRequestBuilder(WebResource resource) {
+ return resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).header(HTTP_HEADER_AUTH, authHeader)
+ .accept(MediaType.APPLICATION_XML);
+ }
+
+ /**
+ * Submits given Form using PUT method to the given sub-resource and returns the object received as response
+ *
+ * @param subResourceName
+ * Name of the sub-resource to which the request is to be posted
+ * @param form
+ * Form to be submitted
+ */
+ protected void putRequest(String subResourceName, Form form) {
+ putRequest(resource.path(subResourceName), form);
+ }
+
+ protected URI putRequestURI(String subResourceName, Form form) {
+ ClientResponse response = putRequest(resource.path(subResourceName), form);
+ return response.getLocation();
+ }
+
+ /**
+ * Submits given Form using PUT method to the given sub-resource and returns the object received as response
+ *
+ * @param form
+ * Form to be submitted
+ */
+ protected void putRequest(Form form) {
+ putRequest(resource, form);
+ }
+
+ /**
+ * Submits given Form using PUT method to the given sub-resource and returns the object received as response
+ *
+ * @param subResourceName
+ * Name of the sub-resource to which the request is to be posted
+ */
+ protected void putRequest(String subResourceName) {
+ try {
+ prepareFormRequestBuilder(resource.path(subResourceName)).put();
+ } catch (UniformInterfaceException e) {
+ throw new GlusterRuntimeException(e.getResponse().getEntity(String.class));
+ }
+ }
+
+ private void deleteResource(WebResource resource, MultivaluedMap<String, String> queryParams) {
+ try {
+ resource.queryParams(queryParams).header(HTTP_HEADER_AUTH, authHeader).delete();
+ } catch (UniformInterfaceException e) {
+ throw new GlusterRuntimeException(e.getResponse().getEntity(String.class));
+ }
+ }
+
+ protected void deleteResource(MultivaluedMap<String, String> queryParams) {
+ deleteResource(resource, queryParams);
+ }
+
+ protected void deleteSubResource(String subResourceName, MultivaluedMap<String, String> queryParams) {
+ deleteResource(resource.path(subResourceName), queryParams);
+ }
+
+ protected void deleteSubResource(String subResourceName) {
+ try {
+ resource.path(subResourceName).header(HTTP_HEADER_AUTH, authHeader).delete();
+ } catch (UniformInterfaceException e) {
+ throw new GlusterRuntimeException(e.getResponse().getEntity(String.class));
+ }
+ }
+
+ public abstract String getResourcePath();
+
+ /**
+ * @return the securityToken
+ */
+ protected String getSecurityToken() {
+ return securityToken;
+ }
+
+ /**
+ * @param securityToken
+ * the securityToken to set
+ */
+ protected void setSecurityToken(String securityToken) {
+ AbstractClient.securityToken = securityToken;
+ authHeader = "Basic " + securityToken;
+ }
+
+ /**
+ * @param uri
+ * The URI to be fetched using GET API
+ * @param responseClass
+ * Expected type of response object
+ * @return Object of the given class
+ */
+ protected <T> T fetchResource(URI uri, Class<T> responseClass) {
+ return fetchResource(client.resource(uri), NO_PARAMS, responseClass);
+ }
+}
diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/ClustersClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/ClustersClient.java new file mode 100644 index 00000000..cbb08f2c --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/ClustersClient.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + *******************************************************************************/ +package org.gluster.storage.management.client; + +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_CLUSTER_NAME; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_SERVER_NAME; +import static org.gluster.storage.management.core.constants.RESTConstants.RESOURCE_PATH_CLUSTERS; + +import java.util.List; + +import org.gluster.storage.management.core.response.ClusterNameListResponse; + +import com.sun.jersey.api.representation.Form; + +/** + * + */ +public class ClustersClient extends AbstractClient { + public ClustersClient() { + super(); + } + + public ClustersClient(String securityToken) { + super(); + setSecurityToken(securityToken); + } + + /* (non-Javadoc) + * @see org.gluster.storage.management.client.AbstractClient#getResourcePath() + */ + @Override + public String getResourcePath() { + return RESOURCE_PATH_CLUSTERS; + } + + public List<String> getClusterNames() { + return ((ClusterNameListResponse)fetchResource(ClusterNameListResponse.class)).getClusterNames(); + } + + public void createCluster(String clusterName) { + Form form = new Form(); + form.add(FORM_PARAM_CLUSTER_NAME, clusterName); + + postRequest(form); + } + + public void registerCluster(String clusterName, String knownServer) { + Form form = new Form(); + form.add(FORM_PARAM_CLUSTER_NAME, clusterName); + form.add(FORM_PARAM_SERVER_NAME, knownServer); + putRequest(form); + } + + public void deleteCluster(String clusterName) { + deleteSubResource(clusterName); + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/DiscoveredServersClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/DiscoveredServersClient.java new file mode 100644 index 00000000..b18e0d17 --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/DiscoveredServersClient.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + *******************************************************************************/ +package org.gluster.storage.management.client; + +import static org.gluster.storage.management.core.constants.RESTConstants.QUERY_PARAM_DETAILS; +import static org.gluster.storage.management.core.constants.RESTConstants.RESOURCE_PATH_DISCOVERED_SERVERS; + +import java.util.List; + +import javax.ws.rs.core.MultivaluedMap; + +import org.gluster.storage.management.core.model.Server; +import org.gluster.storage.management.core.response.ServerListResponse; +import org.gluster.storage.management.core.response.ServerNameListResponse; +import org.gluster.storage.management.core.utils.GlusterCoreUtil; + +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public class DiscoveredServersClient extends AbstractClient { + + public DiscoveredServersClient(String clusterName) { + super(clusterName); + } + + public DiscoveredServersClient(String securityToken, String clusterName) { + super(securityToken, clusterName); + } + + @Override + public String getResourcePath() { + return RESOURCE_PATH_DISCOVERED_SERVERS; + } + + private <T> T getDiscoveredServers(Boolean details, Class<T> responseClass) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.putSingle(QUERY_PARAM_DETAILS, details.toString()); + return fetchResource(queryParams, responseClass); + } + + public List<String> getDiscoveredServerNames() { + return ((ServerNameListResponse) getDiscoveredServers(Boolean.FALSE, ServerNameListResponse.class)) + .getServerNames(); + } + + public List<Server> getDiscoveredServerDetails() { + List<Server> servers = ((ServerListResponse) getDiscoveredServers(Boolean.TRUE, ServerListResponse.class)) + .getServers(); + + for (Server server : servers) { + GlusterCoreUtil.updateServerNameOnDevices(server); + } + return servers; + } + + public Server getServer(String serverName) { + return (Server) fetchSubResource(serverName, Server.class); + } + + public static void main(String[] args) { + UsersClient usersClient = new UsersClient(); + try { + usersClient.authenticate("gluster", "gluster"); + DiscoveredServersClient serverResource = new DiscoveredServersClient(usersClient.getSecurityToken(), "new"); + List<String> discoveredServerNames = serverResource.getDiscoveredServerNames(); + System.out.println(discoveredServerNames); + List<Server> discoveredServers = serverResource.getDiscoveredServerDetails(); + System.out.println(discoveredServers); + } catch(Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/GlusterServersClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/GlusterServersClient.java new file mode 100644 index 00000000..46200c9a --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/GlusterServersClient.java @@ -0,0 +1,140 @@ +/******************************************************************************* +* Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + *******************************************************************************/ +package org.gluster.storage.management.client; + +import static org.gluster.storage.management.core.constants.RESTConstants.QUERY_PARAM_DETAILS; +import static org.gluster.storage.management.core.constants.RESTConstants.RESOURCE_PATH_CLUSTERS; +import static org.gluster.storage.management.core.constants.RESTConstants.RESOURCE_SERVERS; + +import java.net.URI; +import java.util.List; + +import javax.ws.rs.core.MultivaluedMap; + +import org.gluster.storage.management.core.constants.RESTConstants; +import org.gluster.storage.management.core.model.GlusterServer; +import org.gluster.storage.management.core.model.ServerStats; +import org.gluster.storage.management.core.response.FsTypeListResponse; +import org.gluster.storage.management.core.response.GlusterServerListResponse; +import org.gluster.storage.management.core.response.StringListResponse; +import org.gluster.storage.management.core.utils.GlusterCoreUtil; + +import com.sun.jersey.api.representation.Form; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public class GlusterServersClient extends AbstractClient { + + public GlusterServersClient() { + super(); + } + + public GlusterServersClient(String clusterName) { + super(clusterName); + } + + public GlusterServersClient(String securityToken, String clusterName) { + super(securityToken, clusterName); + } + + @Override + public String getResourcePath() { + return RESOURCE_PATH_CLUSTERS + "/" + clusterName + "/" + RESOURCE_SERVERS; + } + + public List<GlusterServer> getServers() { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.putSingle(QUERY_PARAM_DETAILS, "true"); + List<GlusterServer> servers = ((GlusterServerListResponse) fetchResource(queryParams, GlusterServerListResponse.class)).getServers(); + for(GlusterServer server : servers) { + GlusterCoreUtil.updateServerNameOnDevices(server); + } + return servers; + } + + public GlusterServer getGlusterServer(String serverName) { + GlusterServer server = (GlusterServer) fetchSubResource(serverName, GlusterServer.class); + GlusterCoreUtil.updateServerNameOnDevices(server); + return server; + } + + public URI addServer(String serverName) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_SERVER_NAME, serverName); + return postRequest(form); + } + + public List<String> getFSTypes(String serverName) { + FsTypeListResponse fsTypeListResponse = ((FsTypeListResponse) fetchSubResource(serverName + "/" + RESTConstants.RESOURCE_FSTYPES, + FsTypeListResponse.class)); + return fsTypeListResponse.getFsTypes(); + } + + public URI initializeDisk(String serverName, String diskName, String fsType, String mountPoint) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_FSTYPE, fsType); + form.add(RESTConstants.FORM_PARAM_MOUNTPOINT, fsType); + return putRequestURI(serverName + "/" + RESTConstants.RESOURCE_DISKS + "/" + diskName, form); + } + + public void removeServer(String serverName) { + deleteSubResource(serverName); + } + + public ServerStats getCpuStats(String serverName, String period) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_TYPE, RESTConstants.STATISTICS_TYPE_CPU); + queryParams.add(RESTConstants.QUERY_PARAM_PERIOD, period); + return fetchSubResource(serverName + "/" + RESTConstants.RESOURCE_STATISTICS, queryParams, ServerStats.class); + } + + public ServerStats getMemoryStats(String serverName, String period) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_TYPE, RESTConstants.STATISTICS_TYPE_MEMORY); + queryParams.add(RESTConstants.QUERY_PARAM_PERIOD, period); + return fetchSubResource(serverName + "/" + RESTConstants.RESOURCE_STATISTICS, queryParams, ServerStats.class); + } + + public ServerStats getNetworkStats(String serverName, String networkInterface, String period) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_TYPE, RESTConstants.STATISTICS_TYPE_NETWORK); + queryParams.add(RESTConstants.QUERY_PARAM_PERIOD, period); + queryParams.add(RESTConstants.QUERY_PARAM_INTERFACE, networkInterface); + return fetchSubResource(serverName + "/" + RESTConstants.RESOURCE_STATISTICS, queryParams, ServerStats.class); + } + + public ServerStats getAggregatedCpuStats(String period) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_TYPE, RESTConstants.STATISTICS_TYPE_CPU); + queryParams.add(RESTConstants.QUERY_PARAM_PERIOD, period); + return fetchSubResource(RESTConstants.RESOURCE_STATISTICS, queryParams, ServerStats.class); + } + + public ServerStats getAggregatedNetworkStats(String period) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_TYPE, RESTConstants.STATISTICS_TYPE_NETWORK); + queryParams.add(RESTConstants.QUERY_PARAM_PERIOD, period); + return fetchSubResource(RESTConstants.RESOURCE_STATISTICS, queryParams, ServerStats.class); + } + + public GlusterServer getGlusterServer(URI uri) { + GlusterServer server = fetchResource(uri, GlusterServer.class); + GlusterCoreUtil.updateServerNameOnDevices(server); + return server; + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/KeysClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/KeysClient.java new file mode 100644 index 00000000..de4abd69 --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/KeysClient.java @@ -0,0 +1,56 @@ +/** + * KeysClient.java + * + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ +package org.gluster.storage.management.client; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +import javax.ws.rs.core.MediaType; + +import org.gluster.storage.management.core.constants.RESTConstants; + +import com.sun.jersey.multipart.FormDataMultiPart; + +public class KeysClient extends AbstractClient { + + public KeysClient() { + super(); + } + + @Override + public String getResourcePath() { + return RESTConstants.RESOURCE_KEYS; + } + + public void exportSshKeys(String filePath) { + downloadResource(resource, filePath); + } + + public void importSshKeys(String keysFile) { + FormDataMultiPart form = new FormDataMultiPart(); + try { + form.field("file", new FileInputStream(keysFile), MediaType.TEXT_PLAIN_TYPE); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + uploadResource(resource, form); + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/TasksClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/TasksClient.java new file mode 100644 index 00000000..80e10691 --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/TasksClient.java @@ -0,0 +1,104 @@ +/** + * tasksClient.java + * + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ +package org.gluster.storage.management.client; + +import java.net.URI; +import java.util.List; + +import javax.ws.rs.core.MultivaluedMap; + +import org.gluster.storage.management.core.constants.RESTConstants; +import org.gluster.storage.management.core.model.TaskInfo; +import org.gluster.storage.management.core.response.TaskInfoListResponse; + +import com.sun.jersey.api.representation.Form; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public class TasksClient extends AbstractClient { + + public TasksClient() { + super(); + } + + public TasksClient(String clusterName) { + super(clusterName); + } + + public TasksClient(String securityToken,String clusterName) { + super(securityToken, clusterName); + } + + @Override + public String getResourcePath() { + return RESTConstants.RESOURCE_PATH_CLUSTERS + "/" + clusterName + "/" + RESTConstants.RESOURCE_TASKS + "/"; + } + + public List<TaskInfo> getAllTasks() { // TaskListResponse get only the list of taskInfo not list of Tasks + return ((TaskInfoListResponse) fetchResource(TaskInfoListResponse.class)).getTaskList(); + } + + // see startMigration @ VolumesClient, etc + public void pauseTask(String taskId) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_PAUSE); + + putRequest( taskId, form); + } + + public void resumeTask(String taskId) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_RESUME); + + putRequest(taskId, form); + } + + public void stopTask(String taskId) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_STOP); + + putRequest(taskId, form); + } + + public void commitTask(String taskId) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_COMMIT); + + putRequest(taskId, form); + } + + public void getTaskStatus(String taskId) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_STATUS); + + putRequest(taskId, form); + } + + public void deleteTask(String taskId) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_DELETE); + + deleteSubResource(taskId, queryParams); + } + + public TaskInfo getTaskInfo(URI uri) { + return ((TaskInfo) fetchResource(uri, TaskInfo.class)); + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/UsersClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/UsersClient.java new file mode 100644 index 00000000..b35444f7 --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/UsersClient.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + *******************************************************************************/ +package org.gluster.storage.management.client; + +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_NEW_PASSWORD; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_OLD_PASSWORD; + +import org.gluster.storage.management.core.constants.RESTConstants; +import org.gluster.storage.management.core.exceptions.GlusterRuntimeException; +import org.gluster.storage.management.core.model.Status; + +import com.sun.jersey.api.representation.Form; +import com.sun.jersey.core.util.Base64; + + +public class UsersClient extends AbstractClient { + private String generateSecurityToken(String user, String password) { + return new String(Base64.encode(user + ":" + password)); + } + + public UsersClient() { + super(); + } + + public void authenticate(String user, String password) { + setSecurityToken(generateSecurityToken(user, password)); + fetchSubResource(user, Status.class); + } + + public void changePassword(String user, String oldPassword, String newPassword) { + String oldSecurityToken = getSecurityToken(); + String newSecurityToken = generateSecurityToken(user, oldPassword); + if(!oldSecurityToken.equals(newSecurityToken)) { + throw new GlusterRuntimeException("Invalid old password!"); + } + + Form form = new Form(); + form.add(FORM_PARAM_OLD_PASSWORD, oldPassword); + form.add(FORM_PARAM_NEW_PASSWORD, newPassword); + putRequest(user, form); + + // password changed. set the new security token + setSecurityToken(generateSecurityToken(user, newPassword)); + //authenticate(user, newPassword); + } + + public static void main(String[] args) { + UsersClient authClient = new UsersClient(); + + // authenticate user + authClient.authenticate("gluster", "gluster"); + + // change password to gluster1 + authClient.changePassword("gluster", "gluster", "gluster1"); + + // change it back to gluster + authClient.changePassword("gluster", "gluster1", "gluster"); + + System.out.println("success"); + } + + /* + * (non-Javadoc) + * + * @see org.gluster.storage.management.client.AbstractClient#getResourceName() + */ + @Override + public String getResourcePath() { + return RESTConstants.RESOURCE_USERS; + } + + /* + * (non-Javadoc) + * + * @see org.gluster.storage.management.client.AbstractClient#getSecurityToken() + */ + @Override + public String getSecurityToken() { + return super.getSecurityToken(); + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/VolumesClient.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/VolumesClient.java new file mode 100644 index 00000000..221ae84a --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/VolumesClient.java @@ -0,0 +1,282 @@ +/** + * VolumesClient.java + * + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ +package org.gluster.storage.management.client; + +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_ACCESS_PROTOCOLS; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_BRICKS; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_CIFS_USERS; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_REPLICA_COUNT; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_STRIPE_COUNT; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_TRANSPORT_TYPE; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_VOLUME_NAME; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_VOLUME_OPTIONS; +import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_VOLUME_TYPE; + +import java.net.URI; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import javax.ws.rs.core.MultivaluedMap; + +import org.gluster.storage.management.core.constants.CoreConstants; +import org.gluster.storage.management.core.constants.GlusterConstants; +import org.gluster.storage.management.core.constants.RESTConstants; +import org.gluster.storage.management.core.model.Brick; +import org.gluster.storage.management.core.model.Volume; +import org.gluster.storage.management.core.model.VolumeLogMessage; +import org.gluster.storage.management.core.model.VolumeOptionInfo; +import org.gluster.storage.management.core.response.LogMessageListResponse; +import org.gluster.storage.management.core.response.VolumeListResponse; +import org.gluster.storage.management.core.response.VolumeOptionInfoListResponse; +import org.gluster.storage.management.core.utils.DateUtil; +import org.gluster.storage.management.core.utils.GlusterCoreUtil; +import org.gluster.storage.management.core.utils.StringUtil; + +import com.sun.jersey.api.representation.Form; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public class VolumesClient extends AbstractClient { + public VolumesClient() { + super(); + } + + public VolumesClient(String clusterName) { + super(clusterName); + } + + public VolumesClient(String securityToken, String clusterName) { + super(securityToken, clusterName); + } + + @Override + public String getResourcePath() { + return RESTConstants.RESOURCE_PATH_CLUSTERS + "/" + clusterName + "/" + RESTConstants.RESOURCE_VOLUMES; + } + + public void createVolume(Volume volume) { + Form form = new Form(); + form.add(FORM_PARAM_VOLUME_NAME, volume.getName()); + form.add(FORM_PARAM_VOLUME_TYPE, volume.getVolumeType().toString()); + form.add(FORM_PARAM_TRANSPORT_TYPE, volume.getTransportType().toString()); + form.add(FORM_PARAM_REPLICA_COUNT, volume.getReplicaCount()); + form.add(FORM_PARAM_STRIPE_COUNT, volume.getStripeCount()); + form.add(FORM_PARAM_BRICKS, StringUtil.collectionToString(volume.getBricks(), ",")); + form.add(FORM_PARAM_ACCESS_PROTOCOLS, StringUtil.collectionToString(volume.getNASProtocols(), ",")); + form.add(FORM_PARAM_VOLUME_OPTIONS, StringUtil.collectionToString(volume.getOptions().getOptions(), ",")); + if (volume.isCifsEnable()) { + form.add(FORM_PARAM_CIFS_USERS, StringUtil.collectionToString(volume.getCifsUsers(), ",")); + } + postRequest(form); + } + + private void performOperation(String volumeName, String operation, Boolean force) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, operation); + form.add(RESTConstants.FORM_PARAM_FORCE, force); + + putRequest(volumeName, form); + } + + public void startVolume(String volumeName, Boolean forceStart) { + performOperation(volumeName, RESTConstants.TASK_START, forceStart); + } + + public void stopVolume(String volumeName, Boolean forceStop) { + performOperation(volumeName, RESTConstants.TASK_STOP, forceStop); + } + + public void setCifsConfig(String volumeName, Boolean isCifsEnabled, String cifsUsers) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.FORM_PARAM_CIFS_CONFIG); + form.add(RESTConstants.FORM_PARAM_CIFS_ENABLE, isCifsEnabled); + form.add(RESTConstants.FORM_PARAM_CIFS_USERS, cifsUsers); + putRequest(volumeName, form); + } + + public boolean volumeExists(String volumeName) { + try { + // TODO: instead of fetching full volume name, fetch list of volumes and check if + // it contains our volume name + getVolume(volumeName); + return true; + } catch(Exception e) { + return false; + } + } + + public void setVolumeOption(String volume, String key, String value) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPTION_KEY, key); + form.add(RESTConstants.FORM_PARAM_OPTION_VALUE, value); + postRequest(volume + "/" + RESTConstants.RESOURCE_OPTIONS, form); + } + + public void resetVolumeOptions(String volume) { + putRequest(volume + "/" + RESTConstants.RESOURCE_OPTIONS); + } + + public List<Volume> getAllVolumes() { + return ((VolumeListResponse) fetchResource(VolumeListResponse.class)).getVolumes(); + } + + public Volume getVolume(String volumeName) { + return (Volume)fetchSubResource(volumeName, Volume.class); + } + + public void deleteVolume(String volumeName, boolean deleteOption) { + MultivaluedMap<String, String> queryParams = prepareDeleteVolumeQueryParams(deleteOption); + deleteSubResource(volumeName, queryParams); + } + + public List<VolumeOptionInfo> getVolumeOptionsInfo() { + return ((VolumeOptionInfoListResponse) fetchSubResource(RESTConstants.RESOURCE_DEFAULT_OPTIONS, + VolumeOptionInfoListResponse.class)).getOptions(); + } + + public void addBricks(String volumeName, Set<String> brickList) { + String bricks = StringUtil.collectionToString(brickList, ","); + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_BRICKS, bricks); + postRequest(volumeName + "/" + RESTConstants.RESOURCE_BRICKS, form); + } + + /** + * Fetches volume logs for the given volume based on given filter criteria + * + * @param volumeName + * Name of volume whose logs are to be fetched + * @param brickName + * Name of the brick whose logs are to be fetched. Pass ALL to fetch log messages from all bricks of the + * volume. + * @param severity + * Log severity {@link GlusterConstants#VOLUME_LOG_LEVELS_ARR}. Pass ALL to fetch log messages of all + * severity levels. + * @param fromTimestamp + * From timestamp. Pass null if this filter is not required. + * @param toTimestamp + * To timestamp. Pass null if this filter is not required. + * @param messageCount + * Number of most recent log messages to be fetched (from each disk) + * @return Log Message List response received from the Gluster Management Server. + */ + public List<VolumeLogMessage> getLogs(String volumeName, String brickName, String severity, Date fromTimestamp, + Date toTimestamp, int messageCount) { + MultivaluedMap<String, String> queryParams = prepareGetLogQueryParams(brickName, severity, fromTimestamp, + toTimestamp, messageCount); + + return ((LogMessageListResponse) fetchSubResource(volumeName + "/" + RESTConstants.RESOURCE_LOGS, + queryParams, LogMessageListResponse.class)).getLogMessages(); + } + + public void downloadLogs(String volumeName, String filePath) { + downloadSubResource(volumeName + "/" + RESTConstants.RESOURCE_LOGS + "/" + RESTConstants.RESOURCE_DOWNLOAD, filePath); + } + + public void removeBricks(String volumeName, Set<Brick> BrickList, boolean deleteOption) { + String bricks = StringUtil.collectionToString(GlusterCoreUtil.getQualifiedBrickList(BrickList), ","); + MultivaluedMap<String, String> queryParams = prepareRemoveBrickQueryParams(volumeName, bricks, deleteOption); + deleteSubResource(volumeName + "/" + RESTConstants.RESOURCE_BRICKS, queryParams); + } + + private MultivaluedMap<String, String> prepareRemoveBrickQueryParams(String volumeName, String bricks, + boolean deleteOption) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_VOLUME_NAME, volumeName); + queryParams.add(RESTConstants.QUERY_PARAM_BRICKS, bricks); + queryParams.add(RESTConstants.QUERY_PARAM_DELETE_OPTION, "" + deleteOption); + return queryParams; + } + + private MultivaluedMap<String, String> prepareDeleteVolumeQueryParams(boolean deleteOption) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_DELETE_OPTION, "" + deleteOption); + return queryParams; + } + + private MultivaluedMap<String, String> prepareGetLogQueryParams(String brickName, String severity, + Date fromTimestamp, Date toTimestamp, int messageCount) { + MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); + queryParams.add(RESTConstants.QUERY_PARAM_LINE_COUNT, "" + messageCount); + if (!brickName.equals(CoreConstants.ALL)) { + queryParams.add(RESTConstants.QUERY_PARAM_BRICK_NAME, brickName); + } + + if (!severity.equals(CoreConstants.ALL)) { + queryParams.add(RESTConstants.QUERY_PARAM_LOG_SEVERITY, severity); + } + + if (fromTimestamp != null) { + queryParams.add(RESTConstants.QUERY_PARAM_FROM_TIMESTAMP, + DateUtil.dateToString(fromTimestamp, CoreConstants.DATE_WITH_TIME_FORMAT)); + } + + if (toTimestamp != null) { + queryParams.add(RESTConstants.QUERY_PARAM_TO_TIMESTAMP, + DateUtil.dateToString(toTimestamp, CoreConstants.DATE_WITH_TIME_FORMAT)); + } + return queryParams; + } + + public URI startMigration(String volumeName, String brickFrom, String brickTo, Boolean autoCommit) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_SOURCE, brickFrom); + form.add(RESTConstants.FORM_PARAM_TARGET, brickTo); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_START); + form.add(RESTConstants.FORM_PARAM_AUTO_COMMIT, autoCommit); + return putRequestURI(volumeName + "/" + RESTConstants.RESOURCE_BRICKS, form); + } + + public URI rebalanceStart(String volumeName, Boolean fixLayout, Boolean migrateData, Boolean forcedDataMigrate) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_REBALANCE_START); + form.add(RESTConstants.FORM_PARAM_FIX_LAYOUT, fixLayout); + form.add(RESTConstants.FORM_PARAM_MIGRATE_DATA, migrateData); + form.add(RESTConstants.FORM_PARAM_FORCED_DATA_MIGRATE, forcedDataMigrate); + return putRequestURI(volumeName, form); + } + + public void rebalanceStop(String volumeName) { + Form form = new Form(); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_REBALANCE_STOP); + putRequest(volumeName, form); + } + + public void volumeLogRotate(String volumeName, List<String> brickList) { + Form form = new Form(); + String bricks = StringUtil.collectionToString(brickList, ","); + form.add(RESTConstants.FORM_PARAM_OPERATION, RESTConstants.TASK_LOG_ROTATE); + form.add(FORM_PARAM_BRICKS, bricks); + putRequest(volumeName, form); + } + + public static void main(String[] args) { + UsersClient usersClient = new UsersClient(); + try { + usersClient.authenticate("gluster", "gluster"); + VolumesClient client = new VolumesClient(usersClient.getSecurityToken()); + System.out.println(client.getAllVolumes()); +// client.downloadLogs("vol1", "/tmp/temp1.tar.gz"); + } catch(Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/constants/ClientConstants.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/constants/ClientConstants.java new file mode 100644 index 00000000..4e6e5602 --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/constants/ClientConstants.java @@ -0,0 +1,39 @@ +/** + * ClientConstants.java + * + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ +package org.gluster.storage.management.client.constants; + +/** + * + */ +public class ClientConstants { + public static final String SYS_PROP_SERVER_URL = "gluster.server.url"; + public static final String DEFAULT_SERVER_URL = "https://localhost:8443/glustermg/linux.gtk.x86_64"; + public static final String CONTEXT_ROOT = "glustermg"; + public static final String REST_API_VERSION = "1.0.0"; + + // SSL related + public static final String TRUSTED_KEYSTORE = "gmc-trusted.keystore"; + public static final String TRUSTED_KEYSTORE_ACCESS = "gluster"; + public static final String PROTOCOL_TLS = "TLS"; + public static final String ALGORITHM_SUNX509 = "SunX509"; + public static final String KEYSTORE_TYPE_JKS = "JKS"; +} + diff --git a/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/utils/ClientUtil.java b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/utils/ClientUtil.java new file mode 100644 index 00000000..0c0bbc8d --- /dev/null +++ b/src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/utils/ClientUtil.java @@ -0,0 +1,21 @@ +package org.gluster.storage.management.client.utils; + +import java.net.URI; + +import javax.ws.rs.core.UriBuilder; + +import org.gluster.storage.management.client.constants.ClientConstants; + + +public class ClientUtil { + + public static URI getServerBaseURI() { + return UriBuilder.fromUri(getBaseURL()).path(ClientConstants.REST_API_VERSION).build(); + } + + private static String getBaseURL() { + // remove the platform path (e.g. /linux.gtk.x86_64) from the URL + return System.getProperty(ClientConstants.SYS_PROP_SERVER_URL, ClientConstants.DEFAULT_SERVER_URL) + .replaceAll(ClientConstants.CONTEXT_ROOT + "\\/.*", ClientConstants.CONTEXT_ROOT + "\\/"); + } +} |