pycontribs / python-crowd

A python client to the Atlassian Crowd REST API. ❗️seeking-new-maintainer
https://github.com/pycontribs/python-crowd
BSD 2-Clause "Simplified" License
45 stars 41 forks source link

add_user_to_group #35

Closed JGONGSQ closed 7 years ago

JGONGSQ commented 8 years ago

I'm not sure anyone has done this yet, but this would be feature could added for the crowd, if an organisation has multi-group and multi-users, they might want to switch between them for automation purpose. Hope someone is watching this good repo.

Cheers

JGONGSQ commented 8 years ago

I spend about an hour and write a new method in this package. Hope some one could update it. Here is the code, put it under the class 'Crowd Server'. Appreciated.

def add_user_to_group(self, username, groupname, raise_on_error=False, **kwargs):

    # Populate data with default and mandatory values.
    # A KeyError means a mandatory value was not provided,
    # so raise a ValueError indicating bad args.
    try:
        data = {
            "name": groupname
        }
    except KeyError:
        return ValueError

    print data

    response = self._post(self.rest_url + "/user/group/direct",
                          params={"username": username},
                          data=json.dumps(data)
                          )

    if response.status_code == 201:
        return True

    if raise_on_error:
        return RuntimeError(response.json(['message']))

    return False