def moveUserToOu(auth_token, customer_id, user_email, new_ou_path):
    from GoogleAppsAccountManager import template

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

    ou_user_info    = getOuOfUser(auth_token, customer_id, user_email)
    old_ou_path_rel = ou_user_info[user_email].getOuPath(unquoted=False)
    new_ou_path_rel = urllib.quote(new_ou_path[1:])
    if new_ou_path_rel == "":
        new_ou_path_rel = "/"

    body   = template.OU_USER_ENTRY.format(
                  customer_id       = customer_id
                , user_email_quoted = user_email_quoted
                , old_ou_path_rel   = old_ou_path_rel
                , new_ou_path_rel   = new_ou_path_rel
                )

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

    # Get organizational unit entry
    ou_user_entry = data.OuUserEntry(status, xml_string=response)
    try:
        return ou_user_entry.getUserEmail()
    except:
        raise errors.ResponseError_xml(ou_user_entry.getRawXML())
def updateGroup( domain
               , auth_token
               , group_id
               , groupname=None
               , description=None
               ):
    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)
             }

    groups_info = get1Group(domain, auth_token, group_id)
    body = ""
    if groupname is not None:
        body = groups_info[group_id].setGroupName(groupname)

    if description is not None:
        body = groups_info[group_id].setGroupDescription(description)

    if body == "":
        raise errors.NoParametersError()

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

    group_entry = data.GroupEntry(status, xml_string=response)
    return True
def updateUser( domain
              , auth_token
              , user_name
              , password=None
              , sn=None
              , given_name=None
              , hash_function_name="SHA-1"
              , suspended=None
              , limit=None # Not available
              ):
    server = "apps-apis.google.com"
    path   = "/a/feeds/{0}/user/2.0/{1}".format(domain, user_name)
    header = { "Content-type" : "application/atom+xml"
             , "Authorization": "GoogleLogin auth={0}".format(auth_token)
             }

    users_info = get1User(domain, auth_token, user_name)
    body   = ""
    if password is not None:
        body = users_info[user_name].setUserPassword(password, hash_function_name)

    if sn is not None:
        body = users_info[user_name].setUserSn(sn)

    if given_name is not None:
        body = users_info[user_name].setUserGivenName(given_name)

    if suspended is not None:
        if (suspended.lower() == "true") or (suspended.lower() == "false"):
            body = users_info[user_name].setUserSuspended(suspended)
        else:
            raise errors.ArgumentsError(
                    "suspended must be \"true\" or \"false\"\n"
                    )

    # Quota is not available.
    # So nothing will be done whether limit has value or not.
    # With an eye toward a future, I leave this implemention
    if (limit is not None) or (limit is None):
        pass
    else:
        try:
            int(limit)
        except ValueError:
            raise errors.ArgumentsError("limit must be Integer\n")
        else:
            body = users_info[user_name].setUserLimit(limit)

    if body == "":
        raise errors.NoParametersError()

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

    user_entry = data.UserEntry(status, xml_string=response)
    return True
def renameAccount(domain, auth_token, old_user_email, new_user_email):
    # Get additional domain
    (user_name, old_domain) = old_user_email.split("@")

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

    users_info = get1User(domain, auth_token, old_user_email)

    body = users_info[old_user_email].setNewUserEmail(new_user_email)

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

    user_entry = data.UserEntry_M(status, xml_string=response)
    return True
def updateOu( auth_token
            , customer_id
            , ou_path
            , description=None
            , blockInheritance=None # true or false
            ):
    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)
             }

    ou_info = get1Ou(auth_token, customer_id, ou_path)
    body = ""
    if description is not None:
        body = ou_info[ou_path].setOuDescription(description)

    if blockInheritance is not None:
        if (blockInheritance.lower() == "true" or
                blockInheritance.lower() == "false"):
            body = ou_info[ou_path].setOuBlockInheritance(blockInheritance)
        else:
            raise errors.ArgumentsError(
                    "blockInheritance must be \"true\" or \"false\"\n")

    if body == "":
        raise errors.NoParametersError()

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

    ou_entry = data.OuEntry(status, xml_string=response)
    return True
def updateUser( domain
              , auth_token
              , user_email
              , password=None
              , sn=None
              , given_name=None
              , hash_function_name="SHA-1"
              , is_suspended=None
              , is_admin=None
              , is_change_password=None
              ):

    # Get additional domain
    (user_name, mail_domain) = user_email.split("@")

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

    users_info = get1User(domain, auth_token, user_email)

    body   = ""
    if password is not None:
        body = users_info[user_email].setPassword(password, hash_function_name)

    if sn is not None:
        body = users_info[user_email].setSn(sn)

    if given_name is not None:
        body = users_info[user_email].setGivenName(given_name)

    if is_suspended is not None:
        if (is_suspended.lower() == "true") or (is_suspended.lower() == "false"):
            body = users_info[user_email].setSuspended(is_suspended.lower())
        else:
            raise errors.ArgumentsError(
                    "is_suspended must be \"true\" or \"false\"\n"
                    )

    if is_admin is not None:
        if (is_admin.lower() == "true") or (is_admin.lower() == "false"):
            body = users_info[user_email].setAdmin(is_admin.lower())
        else:
            raise errors.ArgumentsError(
                    "is_admin must be \"true\" or \"false\"\n"
                    )

    if is_change_password is not None:
        if (is_change_password.lower() == "true") or (is_change_password.lower() == "false"):
            body = users_info[user_email].setChangePassword(is_change_password.lower())
        else:
            raise errors.ArgumentsError(
                    "is_change_password must be \"true\" or \"false\"\n"
                    )

    if body == "":
        raise errors.NoParametersError()

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

    user_entry = data.UserEntry_M(status, xml_string=response)
    return True