Ask Your Question
0

How to use keystone API extenstion to send a JSON format request “add global role to user ”?

asked 2013-03-09 11:01:37 -0600

lykx1986 gravatar image

I also ask this question in: http://stackoverflow.com/questions/15309704/how-to-use-keystone-api-extenstion-to-send-a-json-format-request-add-global-rol (http://stackoverflow.com/questions/15...)

I want to use java to send a JSON format request to add an Admin role for openstack users, I see there is an keystone API extension that provide the "add global role to user" API:

Theis is the link 2.1.1.5. Add Global roles to a user http://docs.rackspace.com/openstack-extensions/auth/OS-KSADM-admin (http://docs.rackspace.com/openstack-e...) devguide/content/PUT_addUserRole_v2.0_users__userId__roles_OS-KSADM__roleId__Admin_API_Service_Developer_Operations-d1e1357.html

but I do not know how to send this in a correct way by JSON format, the following is my code: I can get "key_admin_url" in another method,it is like: http://130.237.215.18:35357/v2.0 .

user_id and role_id are two strings.

 //create connection
public static void addRole(){
try{
URL url = new URL(key_admin_url + "/users/" + "user_id" + "/roles/OS-KSADM/" + "role_id");
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("PUT");
    connection.setUseCaches(false);
    connection.setInstanceFollowRedirects(true);
    connection.setRequestProperty("Content-Type",
            "application/json");
    connection.setRequestProperty("X-Auth-Token",
            "012345SECRET99TOKEN012345");
    connection.connect();
    //put request 
  DataOutputStream out = new DataOutputStream(
            connection.getOutputStream());
              out.flush();
             out.close();
  //read response
 BufferedReader reader = new BufferedReader(new InputStreamReader(
            connection.getInputStream()));
    String lines;
    StringBuffer sb = new StringBuffer("");
    while ((lines = reader.readLine()) != null) {
        lines = new String(lines.getBytes(), "utf-8");
        sb.append(lines);
    }
    System.out.println(sb);
    reader.close();
    // disconnect
    connection.disconnect();
   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

} But I did not succeed and get :java.io.IOException: Server returned HTTP response code: 501 for URL: http://130.237.215.18:35357/v2.0/users/c53612ae37ec4721a7a72863a1c48726/roles/OS-KSADM/3a6089fd5802419ea97d14a4909b9853 (http://130.237.215.18:35357/v2.0/user...)

Is there anyone know how to send a correct JSON request to add role to users? Thank you very much.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2013-03-20 09:25:04 -0600

That should be something like this /users/{user_id}/roles/OS-KSADM/{role_id} in community code base. However adding role to user without tenant id isn't implemented yet.

Please try use /tenants/{tenant_id}/users/{user_id}/roles/OS-KSADM/{role_id} instead. And admin context is required.

PS: Dumping the http request here would be more helpful than Java code.

edit flag offensive delete link more
0

answered 2013-03-20 15:04:20 -0600

lykx1986 gravatar image

Thanks Liang Chen, that solved my question.

edit flag offensive delete link more

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2013-03-09 11:01:37 -0600

Seen: 80 times

Last updated: Mar 20 '13