def getAllGroupsInDomain(domain, auth_token, display_obj=None):
    disp_groups_info = {}
    groups_info      = {}

    server = "apps-apis.google.com"
    path   = "/a/feeds/group/2.0/{0}?start=".format(domain)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get group feed
    group_feed = data.GroupFeed(status, response)

    # Retrieve group information from xml string
    for group_entry in group_feed.entries:
        group_id = group_entry.getGroupId().split("@")[0]
        groups_info[group_id] = group_entry

        if display_obj:
            description = group_entry.getGroupDescription()
            groupname   = group_entry.getGroupName()
            disp_groups_info[group_id] = { "description" : description
                                         , "groupname"   : groupname
                                         }
            header = ["group_id", "groupname", "description"]
            value  = {"group_id" : group_id}
            value.update(disp_groups_info[group_id])
            display_obj.display(*header, **value)

    return True
def getAllAliasesOfUser(domain, auth_token, user_email, display_obj=None):
    # Get additional domain
    (_, mail_domain) = user_email.split("@")

    aliases_info    = {}
    alias_emails    = []

    server = "apps-apis.google.com"
    path   = "/a/feeds/alias/2.0/{0}?userEmail={1}".format(mail_domain, user_email)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get feed
    nickname_feed = data.AliasFeed_M(status, response)

    # Retrieve information from xml string
    for nickname_entry in nickname_feed.entries:
        alias_email                 = nickname_entry.getAliasEmail()
        aliases_info[alias_email]   = nickname_entry

        if display_obj:
            alias_emails += [alias_email]

    if display_obj:
        header = ["user_email", "alias_email1:alias_email2:..."]
        value  = {"user_email" : user_email}
        value.update({"alias_email1:alias_email2:..." : ":".join(alias_emails)})
        display_obj.display(*header, **value)

    return aliases_info
def getAllOwnersOfGroup(domain, auth_token, group_id, display_obj=None):
    group_owners_info      = {}
    owners                 = []

    #FIXME: if more than 200 entries, start key is needed
    server = "apps-apis.google.com"
    path   = "/a/feeds/group/2.0/{0}/{1}/owner".format(domain, group_id)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get group feed
    group_owner_feed = data.GroupOwnerFeed(status, response)

    # Retrieve group information from xml string
    for group_owner_entry in group_owner_feed.entries:
        owner_name = group_owner_entry.getOwnerEmail().split("@")[0]
        group_owners_info[owner_name] = group_owner_entry

        if display_obj:
            owners += [owner_name]

    if display_obj:
        header = ["group_id", "owner_name1:owner_name2:.."]
        value  = {"group_id" : group_id}
        value.update({"owner_name1:owner_name2:.." : ":".join(owners)})
        display_obj.display(*header, **value)

    return group_owners_info
def get100Aliases(domain, auth_token, next_alias_email="", display_obj=None):
    aliases_info      = {} # {user : nickname_entry object}
    disp_aliases_info = {}

    server = "apps-apis.google.com"
    path   = "/a/feeds/alias/2.0/{0}?start={1}".format(domain, next_alias_email)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get user feed
    alias_feed = data.AliasFeed_M(status, response)

    # Retrieve user information from xml string
    for nickname_entry in alias_feed.entries:
        alias_email   = nickname_entry.getAliasEmail()
        aliases_info[alias_email] = nickname_entry

        if display_obj:
            user_email = nickname_entry.getUserEmail()
            disp_aliases_info[alias_email] = { "user_email" : user_email }
            header = ["alias_email", "user_email"]
            value  = {"alias_email" : alias_email}
            value.update(disp_aliases_info[alias_email])
            display_obj.display(*header, **value)

    # Check 100 over?
    next_alias_email = alias_feed.getNextLink()
    if "?" in next_alias_email:
        next_alias_email = next_alias_email.split("?")[1].split("=")[1]

    return (aliases_info, next_alias_email)
def getAllNicknamesOfUser(domain, auth_token, user_name, display_obj=None):
    nicknames_info    = {}
    nicknames         = []

    server = "apps-apis.google.com"
    path   = "/a/feeds/{0}/nickname/2.0?username={1}".format(domain, user_name)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get feed
    nickname_feed = data.NicknameFeed(status, response)

    # Retrieve information from xml string
    for nickname_entry in nickname_feed.entries:
        nickname                 = nickname_entry.getNickname()
        nicknames_info[nickname] = nickname_entry

        if display_obj:
            nicknames += [nickname]

    if display_obj:
        header = ["user_name", "nickname1:nickname2:..."]
        value  = {"user_name" : user_name}
        value.update({"nickname1:nickname2:..." : ":".join(nicknames)})
        display_obj.display(*header, **value)

    return nicknames_info
def get100Nicknames(domain, auth_token, next_nickname="", display_obj=None):
    nicknames_info      = {} # {nickname : nickname_entry object}
    disp_nicknames_info = {}

    server = "apps-apis.google.com"
    path   = "/a/feeds/{0}/nickname/2.0?startnickname={1}".format(domain, next_nickname) #FIXME
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get feed
    nickname_feed = data.NicknameFeed(status, response)

    # Retrieve information from xml string
    for nickname_entry in nickname_feed.entries:
        nickname                 = nickname_entry.getNickname()
        nicknames_info[nickname] = nickname_entry

        if display_obj:
            user_name    = nickname_entry.getUserName()
            disp_nicknames_info[nickname] = { "user_name" : user_name }
            header = ["nickname", "user_name"]
            value  = {"nickname" : nickname}
            value.update(disp_nicknames_info[nickname])
            display_obj.display(*header, **value)

    # Check 100 over?
    next_nickname = nickname_feed.getNextLink()
    if "?" in next_nickname:
        next_nickname = next_nickname.split("?")[1].split("=")[1]

    return (nicknames_info, next_nickname)
def getAllGroupsOfMember(domain, auth_token, membername, display_obj=None):
    member_groups_info      = {}
    groups                  = []

    server = "apps-apis.google.com"
    path   = ( "/a/feeds/group/2.0/{0}?member={1}&directOnly=false"
               .format(domain, membername)
             )
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get group feed
    group_feed = data.GroupFeed(status, response)

    # Retrieve group information from xml string
    for group_entry in group_feed.entries:
        member_groups_info[membername] = group_entry

        if display_obj:
            group_id = group_entry.getGroupId().split("@")[0]
            groups += [group_id]

    if display_obj:
        header = ["membername", "group1:group2:.."]
        value  = {"membername" : membername}
        value.update({ "group1:group2:.." : ":".join(groups)})
        display_obj.display(*header, **value)

    return member_groups_info
def getCustomerId(auth_token):
    server = "apps-apis.google.com"
    path   = "/a/feeds/customer/2.0/customerId"
    header = { "Content-type" : "application/atom+xml"
              , "Authorization": "GoogleLogin auth={0}".format(auth_token)
              }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    customer_id_entry = data.CustomerIdEntry(status, xml_string=response)
    return customer_id_entry.getCustomerId()
def get1Nickname(domain, auth_token, nickname):
    server = "apps-apis.google.com"
    path   = "/a/feeds/{0}/nickname/2.0/{1}".format(domain, nickname)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get entry
    nickname_entry = data.NicknameEntry(status, xml_string=response)

    return {nickname : nickname_entry}
def get100Ous(auth_token, customer_id, next_link=None, display_obj=None):
    disp_ous_info = {}
    ou_info       = {}

    server = "apps-apis.google.com"
    path   = "/a/feeds/orgunit/2.0/{0}?get=all".format(customer_id)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    if next_link is None:
        (response, status) = http.httpsGETRequest(server, path, header)
    else:
        path     = "/" + "/".join(next_link.split("/")[3:])
        (response, status) = http.httpsGETRequest(server, path, header)

    # Get organizational unit feed
    ou_feed = data.OuFeed(status, response)

    # Retrieve organizational unit information from xml string
    for ou_entry in ou_feed.entries:
        ou_path          = ou_entry.getOuPath()
        ou_info[ou_path] = ou_entry

        if display_obj:
            description = ou_entry.getOuDescription()
            disp_ous_info[ou_path] = {"description" : description}
            header = ["ou_path", "description"]
            value  = {"ou_path" : ou_path}
            value.update(disp_ous_info[ou_path])
            display_obj.display(*header, **value)

    # Check 100 over?
    next_link = ou_feed.getNextLink()

    return (ou_info, next_link)
def get100UsersWithOu(auth_token, customer_id, next_link=None, display_obj=None):
    ou_user_info         = {}
    organizational_units = []

    server = "apps-apis.google.com"
    path   = "/a/feeds/orguser/2.0/{0}?get=all".format(customer_id)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    if next_link is None:
        (response, status) = http.httpsGETRequest(server, path, header)
    else:
        path     = "/" + "/".join(next_link.split("/")[3:])
        (response, status) = http.httpsGETRequest(server, path, header)

    # Get organizational unit feed
    ou_user_feed = data.OuUserFeed(status, response)

    # Retrieve organizational unit information from xml string
    for ou_user_entry in ou_user_feed.entries:
        user_email = ou_user_entry.getUserEmail()
        ou_user_info[user_email] = ou_user_entry

        if display_obj:
            username = user_email.split("@")[0]
            ou_path = ou_user_entry.getOuPath()
            header = ["username", "ou"]
            value  = {"username" : username, "ou": ou_path}
            display_obj.display(*header, **value)

    # Check 100 over?
    next_link = ou_user_feed.getNextLink()

    return (ou_user_info, next_link)
def get1OwnerOfGroup(domain, auth_token, user_name, group_id):
    server = "apps-apis.google.com"
    path   = "/a/feeds/group/2.0/{0}/{1}/owner/{2}".format(domain, group_id, user_name)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get group entry
    group_owner_entry = data.GroupOwnerEntry(status, xml_string=response)
    owner_name   = group_owner_entry.getOwnerEmail().split("@")[0]

    return {owner_name : group_owner_entry}
def get1Group(domain, auth_token, group_id):
    server = "apps-apis.google.com"
    path   = "/a/feeds/group/2.0/{0}/{1}".format(domain, group_id)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get group entry
    group_entry = data.GroupEntry(status, xml_string=response)
    group_id   = group_entry.getGroupId().split("@")[0]

    return {group_id : group_entry}
def getOuOfUser(auth_token, customer_id, user_email):
    user_email_quoted = urllib.quote(user_email)

    server = "apps-apis.google.com"
    path   = "/a/feeds/orguser/2.0/{0}/{1}".format(customer_id, user_email_quoted)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get organizational unit entry
    ou_user_entry = data.OuUserEntry(status, xml_string=response)

    return {user_email: ou_user_entry}
def get1Alias(domain, auth_token, alias_email):
    # Get additional domain
    (_, mail_domain) = alias_email.split("@")

    server = "apps-apis.google.com"
    path   = "/a/feeds/alias/2.0/{0}/{1}".format(mail_domain, alias_email)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get entry
    nickname_entry = data.AliasEntry_M(status, xml_string=response)

    return {alias_email: nickname_entry}
def get100Users(domain, auth_token, next_user_email="", display_obj=None):
    users_info      = {} # {user : user_entry object}
    disp_users_info = {}

    server = "apps-apis.google.com"
    path   = "/a/feeds/user/2.0/{0}?start={1}".format(domain, next_user_email)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get user feed
    user_feed = data.UserFeed_M(status, response)

    # Retrieve user information from xml string
    for user_entry in user_feed.entries:
        user_email   = user_entry.getUserEmail()
        users_info[user_email] = user_entry

        if display_obj:
            sn         = user_entry.getSn()
            given_name = user_entry.getGivenName()
            is_admin   = user_entry.isAdmin()
            is_change  = user_entry.isChangePasswordAtNextLogin()
            is_active  = user_entry.isActive()
            disp_users_info[user_email] = { "sn" : sn
                                          , "given_name"           : given_name
                                          , "is_admin?"            : is_admin
                                          , "is_change_pw_needed?" : is_change
                                          , "is_active?"           : is_active
                                          }
            header = ["user_email", "sn", "given_name", "is_admin?"
                                        , "is_change_pw_needed?", "is_active?"]
            value  = {"user_email" : user_email}
            value.update(disp_users_info[user_email])
            display_obj.display(*header, **value)

    # Check 100 over?
    next_user_email = user_feed.getNextLink()
    if "?" in next_user_email:
        next_user_email = next_user_email.split("?")[1].split("=")[1]

    return (users_info, next_user_email)
def get1Ou(auth_token, customer_id, ou_path):
    ou_path_rel = urllib.quote(ou_path[1:])
    if ou_path_rel == "":
        ou_path_rel = "/"

    server = "apps-apis.google.com"
    path   = "/a/feeds/orgunit/2.0/{0}/{1}".format(customer_id, ou_path_rel)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get organizational unit entry
    ou_entry = data.OuEntry(status, xml_string=response)

    return {ou_path: ou_entry}
def get100Users(domain, auth_token, next_user="", display_obj=None):
    users_info      = {} # {user : user_entry object}
    disp_users_info = {}

    server = "apps-apis.google.com"
    path   = "/a/feeds/{0}/user/2.0?startUsername={1}".format(domain, next_user)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    # HTTP Request
    (response, status) = http.httpsGETRequest(server, path, header)

    # Get user feed
    user_feed = data.UserFeed(status, response)

    # Retrieve user information from xml string
    for user_entry in user_feed.entries:
        user_name   = user_entry.getUserName()
        users_info[user_name] = user_entry

        if display_obj:
            sn        = user_entry.getSn()
            given_name = user_entry.getGivenName()
            active    = user_entry.getActive()
            quota     = user_entry.getQuota()
            disp_users_info[user_name] = { "sn" : sn
                                        , "given_name" : given_name
                                        , "quota"      : quota
                                        , "active"     : active
                                        }
            header = ["user_name", "sn", "given_name", "quota", "active"]
            value  = {"user_name" : user_name}
            value.update(disp_users_info[user_name])
            display_obj.display(*header, **value)

    # Check 100 over?
    next_user = user_feed.getNextLink()
    if "?" in next_user:
        next_user = next_user.split("?")[1].split("=")[1]

    return (users_info, next_user)