Skip to content

Commit

Permalink
Add list users to Java client. Modified getCloudInfo to return update…
Browse files Browse the repository at this point in the history
… cloud info after changing the proxy user.
  • Loading branch information
soudmaijer committed Jan 4, 2012
1 parent eef6c79 commit 9b93f27
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 33 deletions.
Expand Up @@ -16,30 +16,6 @@

package org.cloudfoundry.client.lib;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.cloudfoundry.client.lib.CloudApplication.AppState;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper;
Expand Down Expand Up @@ -67,6 +43,30 @@
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class CloudFoundryClient {

private class ErrorHandler extends DefaultResponseErrorHandler {
Expand Down Expand Up @@ -168,14 +168,18 @@ public URL getCloudControllerUrl() {
return cloudControllerUrl;
}

public CloudInfo getCloudInfo() {
if (info == null) {
@SuppressWarnings("unchecked")
Map<String,Object> infoMap = restTemplate.getForObject(getUrl("info"), Map.class);
info = new CloudInfo(infoMap);
}
return info;
}
/**
* Returns the Cloud info.
* Removed the check for info==null, because after setting the proxyUser the CloudInfo changes.
*
* @return the CloudInfo for the (proxy)User.
*/
public CloudInfo getCloudInfo() {
@SuppressWarnings("unchecked")
Map<String, Object> infoMap = restTemplate.getForObject(getUrl("info"), Map.class);
info = new CloudInfo(infoMap);
return info;
}

public void register(String email, String password) {
Map<String, String> payload = new HashMap<String, String>();
Expand All @@ -192,6 +196,29 @@ public void updatePassword(String newPassword) {
restTemplate.put(getUrl("users/{id}"), userInfo, email);
}

/**
* Returns all user accounts.
*
* @return the users
*/
public List<CloudUser> getUsers() {
List<Map<String, Object>> servicesAsMap = restTemplate.getForObject(getUrl("users"), List.class);
List<CloudUser> users = new ArrayList<CloudUser>();
for (Map<String, Object> serviceAsMap : servicesAsMap) {
users.add(new CloudUser(serviceAsMap));
}
return users;
}

/**
* Unregister the specified account.
*
* @param theEmail the user which should be unregistered.
*/
public void unregister(String theEmail) {
restTemplate.delete(getUrl("users/{email}"), theEmail);
}

public void unregister() {
restTemplate.delete(getUrl("users/{email}"), email);
token = null;
Expand Down Expand Up @@ -238,7 +265,6 @@ public ApplicationStats getApplicationStats(String appName) {
/**
* Get choices for application memory quota
*
* @param framework
* @return memory choices in MB
*/
public int[] getApplicationMemoryChoices() {
Expand Down
@@ -0,0 +1,55 @@
/*
* Copyright 2009-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.cloudfoundry.client.lib;

/**
* Cloud Foundry Account information.
*/
public class CloudUser {
private java.util.Map<String, Object> attributes;
private String email;
private boolean admin;

/**
* Account constructor. Password will not be set in this object.
*
* @param attributes The attributes to set.
*/
public CloudUser(java.util.Map<String, Object> attributes) {
this.attributes = attributes;
this.email = CloudUtil.parse(String.class, attributes.get("email"));
this.admin = CloudUtil.parse(Boolean.class, attributes.get("admin"));
}

/**
* The email address for this account.
*
* @return should never be empty
*/
public String getEmail() {
return email;
}

/**
* If this user is an admin.
*
* @return true if an admin
*/
public boolean isAdmin() {
return admin;
}
}

0 comments on commit 9b93f27

Please sign in to comment.