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

Asked by yaolu

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

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
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

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

Question information

Language:
English Edit question
Status:
Solved
For:
OpenStack Identity (keystone) Edit question
Assignee:
No assignee Edit question
Solved by:
Liang Chen
Solved:
Last query:
Last reply:
Revision history for this message
Best Liang Chen (cbjchen) said :
#1

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.

Revision history for this message
yaolu (lykx1986) said :
#2

Thanks Liang Chen, that solved my question.