def process(resources): response = [] changed = True for resource in resources: p_state = resource["state"] p_name = resource["name"] try: team_role = TeamRole.get(name=p_name) except TeamRole.DoesNotExist: team_role = None if p_state == "absent": if team_role is not None: changed = True team_role.delete_instance() response.append("Team Role '%s' deleted" % p_name) changed = True else: response.append("Team Role '%s' does not exist" % p_name) else: if team_role is None: changed = True team_role = TeamRole.create(name=p_name) response.append("Team Role '%s' created" % p_name) else: response.append("Team Role '%s' exists" % p_name) return {"failed": False, "changed": changed, "meta": response}, 200
def add_user_as_admin(user_obj, org_obj): try: admin_role = TeamRole.get(name="admin") admin_team = (Team.select().where(Team.role == admin_role, Team.organization == org_obj).get()) team.add_user_to_team(user_obj, admin_team) except team.UserAlreadyInTeam: pass
def create_team(name, org_obj, team_role_name, description=""): (teamname_valid, teamname_issue) = validate_team_name(name) if not teamname_valid: raise InvalidTeamException("Invalid team name %s: %s" % (name, teamname_issue)) if not org_obj.organization: raise InvalidTeamException( "Specified organization %s was not an organization" % org_obj.username) team_role = TeamRole.get(TeamRole.name == team_role_name) return Team.create(name=name, organization=org_obj, role=team_role, description=description)
def set_team_org_permission(team, team_role_name, set_by_username): if team.role.name == "admin" and team_role_name != "admin": # We need to make sure we're not removing the users only admin role user_admin_teams = __get_user_admin_teams(team.organization.username, set_by_username) admin_team_set = {admin_team.name for admin_team in user_admin_teams} if team.name in admin_team_set and len(admin_team_set) <= 1: msg = ("Cannot remove admin from team '%s' because calling user " + "would no longer have admin on org '%s'") % ( team.name, team.organization.username) raise DataModelException(msg) new_role = TeamRole.get(TeamRole.name == team_role_name) team.role = new_role team.save() return team