Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Adding new Ambari user with assigned group with API

avatar
Expert Contributor

I would like to add a new user to Ambari Views and add it to a group.

Here is my file:

{
    "Users/user_name": "user1",
    "Users/password":  "user1",
    "Users/groups": ["group1"],
    "Users/active": true,
    "Users/admin": false
}

So I run the file:

curl -iv -u admin:admin -d @user1.amb -H "X-Requested-By: ambari" -X POST http://ambari-server:8080/api/v1/users

The user is created, but not added to the group. Here is the GET output for this user:

{
  "href" : "http://ambari-server:8080/api/v1/users/user1",
  "Users" : {
    "active" : true,
    "admin" : false,
    "groups" : [ ],
    "ldap_user" : false,
    "user_name" : "user1"
  },
  "widget_layouts" : [ ],
  "privileges" : [ ]
}

Groups is empty. Does anyone have any idea what Im doing wrong in this user creation process?

1 ACCEPTED SOLUTION

avatar
Expert Contributor

I made it work now. its not -X PUT but -X POST

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X POST -d  '[{"MemberInfo/user_name":"user1", "MemberInfo/group_name":"group1"}]' http://ambari-server:8080/api/v1/groups/group1/members

This adds new user to a group without deleting the existing group members.

View solution in original post

7 REPLIES 7

avatar
Expert Contributor

Hi @marko,

You may add the user to a group using the following steps :

1) add a user using API :

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X POST -d '{"Users/user_name":"{USER}","Users/password":"{PASSWORD}","Users/active":"{ISACTIVE}","Users/admin":"{ISADMIN}"}' http://ambari-server:8080/api/v1/users

2) add a group using API :

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X POST -d '{"Groups/group_name":"{GROUP_NAME}"}' http://ambari-server:8080/api/v1/groups

3) add the user to the group :

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X PUT -d '[{"MemberInfo/user_name":"{USER_NAME}","MemberInfo/group_name":"{GROUP_NAME}"}]' http://ambari-server:8080/api/v1/groups/{GROUP_NAME}/members

Change {USER_NAME} to the user name you want and {GROUP_NAME} to the group you want

Hope this helps!

avatar
Expert Contributor

@sbhat

I ran what you recommended under adding user to a group and Im getting 405 Method not allowed.

I tried with -X POST rather than PUT and Im getting this error:

{
  "status" : 400,
  "message" : "The properties [MemberInfo/user_name, MemberInfo/group_name] specified in the request or predicate are not supported for the resource type Group."
* Connection #0 to host ambari-server left intact
}

so I updated the string by adding members/ in front of MemberInfo, like this:

{
  "members/MemberInfo/user_name":"user1",
  "members/MemberInfo/group_name":"group1"
}

and the error is the same.

Im using Ambari 2.2.2.0 if this has to do with anything.

avatar
Expert Contributor

Sorry about that. I have updated the answer with the right URI to add user to groups : http://ambari-server:8080/api/v1/groups/{GROUP_NAME}/members

avatar
Expert Contributor

@sbchat

I had to unaccept the answer. What happens now is that the user is added, but the old users from the group are removed from it.

Can you check if this behaviour occurs on your side as well?

avatar
Expert Contributor

So the answer to my question would be:

File add_to_group.amb:

{
  "MemberInfo/user_name":"user1",
  "MemberInfo/group_name":"group1"
}
curl -iv -u admin:admin -H "X-Requested-By: ambari" -X PUT -d @add_to_group.amb  http://ambari-server:8080/api/v1/groups/group1/members

This works! Thank you, @sbhat

avatar
Expert Contributor

I made it work now. its not -X PUT but -X POST

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X POST -d  '[{"MemberInfo/user_name":"user1", "MemberInfo/group_name":"group1"}]' http://ambari-server:8080/api/v1/groups/group1/members

This adds new user to a group without deleting the existing group members.

avatar
Expert Contributor

My assumption was this was a new group. Yes, POST would work for existing groups.