Skip to content

Commit

Permalink
Add --org option to all relevant CLI commands.
Browse files Browse the repository at this point in the history
--org is the organization slug, not the name.
Allows the management of users, datasources and groups
with respect to organisations.
All commands default to 'default' slug, or None where
relevant, which means the commands will still work
as they did before without any changes.
  • Loading branch information
adamlwgriffiths committed Jun 17, 2016
1 parent 96dd811 commit a66e182
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
39 changes: 28 additions & 11 deletions redash/cli/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
manager = Manager(help="Data sources management commands.")


@manager.command
def list():
@manager.option('--org', dest='organization', default=None, help="The organization the user belongs to")
def list(organization=None):
"""List currently configured data sources."""
for i, ds in enumerate(models.DataSource.select()):
if organization:
org = models.Organization.get_by_slug(organization)
data_sources = models.DataSource.select().where(models.DataSource.org==org.id)
else:
data_sources = models.DataSource.select()
for i, ds in enumerate(data_sources):
if i > 0:
print "-" * 20

Expand All @@ -24,8 +29,11 @@ def validate_data_source_type(type):
exit()


@manager.command
def new(name=None, type=None, options=None):
@manager.option('name', default=None, help="name of data source to create")
@manager.option('--type', dest='type', default=None, help="new type for the data source")
@manager.option('--options', dest='options', default=None, help="updated options for the data source")
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
def new(name=None, type=None, options=None, organization='default'):
"""Create new data source."""
if name is None:
name = click.prompt("Name")
Expand Down Expand Up @@ -84,15 +92,20 @@ def new(name=None, type=None, options=None):
data_source = models.DataSource.create_with_group(name=name,
type=type,
options=options,
org=models.Organization.get_by_slug('default'))
org=models.Organization.get_by_slug(organization))
print "Id: {}".format(data_source.id)


@manager.command
def delete(name):
@manager.option('name', default=None, help="name of data source to delete")
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
def delete(name, organization):
"""Delete data source by name."""
try:
data_source = models.DataSource.get(models.DataSource.name==name)
org = models.Organization.get_by_slug(organization)
data_source = models.DataSource.get(
models.DataSource.name==name,
models.DataSource.org==org,
)
print "Deleting data source: {} (id={})".format(name, data_source.id)
data_source.delete_instance(recursive=True)
except models.DataSource.DoesNotExist:
Expand All @@ -110,13 +123,17 @@ def update_attr(obj, attr, new_value):
@manager.option('--name', dest='new_name', default=None, help="new name for the data source")
@manager.option('--options', dest='options', default=None, help="updated options for the data source")
@manager.option('--type', dest='type', default=None, help="new type for the data source")
def edit(name, new_name=None, options=None, type=None):
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
def edit(name, new_name=None, options=None, type=None, organization='default'):
"""Edit data source settings (name, options, type)."""
try:
if type is not None:
validate_data_source_type(type)

data_source = models.DataSource.get(models.DataSource.name==name)
data_source = models.DataSource.get(
models.DataSource.name==name,
models.DataSource.org==org,
)

if options is not None:
schema = get_configuration_schema_for_query_runner_type(data_source.type)
Expand Down
13 changes: 7 additions & 6 deletions redash/cli/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
manager = Manager(help="Groups management commands. This commands assume single organization operation.")

@manager.option('name', help="Group's name")
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
@manager.option('--permissions', dest='permissions', default=None, help="Comma seperated list of permissions ('create_dashboard', 'create_query', 'edit_dashboard', 'edit_query', 'view_query', 'view_source', 'execute_query', 'list_users', 'schedule_query', 'list_dashboards', 'list_alerts', 'list_data_sources') (leave blank for default).")
def create(name, permissions=None):
def create(name, organization, permissions=None):
print "Creating group (%s)..." % (name)

org = models.Organization.get_by_slug('default')
org = models.Organization.get_by_slug(organization)

permissions = extract_permissions_string(permissions)

Expand All @@ -17,7 +18,7 @@ def create(name, permissions=None):
try:
models.Group.create(name=name, org=org, permissions=permissions)
except Exception, e:
print "Failed create group: %s" % e.message
print "Failed create group: %s" % e.message

@manager.option('id', help="Group's id")
@manager.option('--permissions', dest='permissions', default=None, help="Comma seperated list of permissions ('create_dashboard', 'create_query', 'edit_dashboard', 'edit_query', 'view_query', 'view_source', 'execute_query', 'list_users', 'schedule_query', 'list_dashboards', 'list_alerts', 'list_data_sources') (leave blank for default).")
Expand All @@ -30,15 +31,15 @@ def change_permissions(id, permissions=None):
print "User [%s] not found." % id
return

permissions = extract_permissions_string(permissions)
permissions = extract_permissions_string(permissions)
print "current permissions [%s] will be modify to [%s]" % (",".join(group.permissions), ",".join(permissions))

group.permissions = permissions

try:
group.save()
except Exception, e:
print "Failed change permission: %s" % e.message
print "Failed change permission: %s" % e.message


def extract_permissions_string(permissions):
Expand All @@ -47,4 +48,4 @@ def extract_permissions_string(permissions):
else:
permissions = permissions.split(',')
permissions = [p.strip() for p in permissions]
return permissions
return permissions
59 changes: 41 additions & 18 deletions redash/cli/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
manager = Manager(help="Users management commands. This commands assume single organization operation.")


def build_groups(groups, is_admin):
org = models.Organization.get_by_slug('default')
def build_groups(org, groups, is_admin):
if isinstance(groups, basestring):
groups= groups.split(',')
groups.remove('') # in case it was empty string
Expand All @@ -23,9 +22,10 @@ def build_groups(groups, is_admin):
return groups

@manager.option('email', help="email address of the user to grant admin to")
def grant_admin(email):
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
def grant_admin(email, organization):
try:
org = models.Organization.get_by_slug('default')
org = models.Organization.get_by_slug(organization)
admin_group = org.admin_group
user = models.User.get_by_email_and_org(email, org)

Expand All @@ -42,17 +42,18 @@ def grant_admin(email):

@manager.option('email', help="User's email")
@manager.option('name', help="User's full name")
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
@manager.option('--admin', dest='is_admin', action="store_true", default=False, help="set user as admin")
@manager.option('--google', dest='google_auth', action="store_true", default=False, help="user uses Google Auth to login")
@manager.option('--password', dest='password', default=None, help="Password for users who don't use Google Auth (leave blank for prompt).")
@manager.option('--groups', dest='groups', default=None, help="Comma seperated list of groups (leave blank for default).")
def create(email, name, groups, is_admin=False, google_auth=False, password=None):
print "Creating user (%s, %s)..." % (email, name)
def create(email, name, organization, groups, is_admin=False, google_auth=False, password=None):
print "Creating user (%s, %s) in organization %s..." % (email, name, organization)
print "Admin: %r" % is_admin
print "Login with Google Auth: %r\n" % google_auth

org = models.Organization.get_by_slug('default')
groups = build_groups(groups, is_admin)
org = models.Organization.get_by_slug(organization)
groups = build_groups(org, groups, is_admin)

user = models.User(org=org, email=email, name=name, groups=groups)
if not google_auth:
Expand All @@ -66,16 +67,32 @@ def create(email, name, groups, is_admin=False, google_auth=False, password=None


@manager.option('email', help="email address of user to delete")
def delete(email):
deleted_count = models.User.delete().where(models.User.email == email).execute()
@manager.option('--org', dest='organization', default=None, help="The organization the user belongs to")
def delete(email, organization):
if organization:
org = models.Organization.get_by_slug(organization)
deleted_count = models.User.delete().where(
models.User.email == email,
models.User.org == org.id,
).execute()
else:
deleted_count = models.User.delete().where(models.User.email == email).execute()
print "Deleted %d users." % deleted_count


@manager.option('password', help="new password for the user")
@manager.option('email', help="email address of the user to change password for")
def password(email, password):
@manager.option('--org', dest='organization', default=None, help="The organization the user belongs to")
def password(email, password, organization):
try:
user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))
if organization:
org = models.Organization.get_by_slug(organization)
user = models.User.select().where(
models.User.email == email,
models.User.org == org.id,
).first()
else:
user = models.User.select().where(models.User.email == email).first()

user.hash_password(password)
user.save()
Expand All @@ -88,11 +105,12 @@ def password(email, password):
@manager.option('email', help="The invitee's email")
@manager.option('name', help="The invitee's full name")
@manager.option('inviter_email', help="The email of the inviter")
@manager.option('--org', dest='organization', default='default', help="The organization the user belongs to")
@manager.option('--admin', dest='is_admin', action="store_true", default=False, help="set user as admin")
@manager.option('--groups', dest='groups', default=None, help="Comma seperated list of groups (leave blank for default).")
def invite(email, name, inviter_email, groups, is_admin=False):
org = models.Organization.get_by_slug('default')
groups = build_groups(groups, is_admin)
def invite(email, name, inviter_email, organization, groups, is_admin=False):
org = models.Organization.get_by_slug(organization)
groups = build_groups(org, groups, is_admin)
try:
user_from = models.User.get_by_email_and_org(inviter_email, org)
user = models.User(org=org, name=name, email=email, groups=groups)
Expand All @@ -110,11 +128,16 @@ def invite(email, name, inviter_email, groups, is_admin=False):
print "The inviter [%s] was not found." % inviterEmail


@manager.command
@manager.option('--org', dest='organization', default=None, help="The organization the user belongs to")
def list():
"""List all users"""
for i, user in enumerate(models.User.select()):
if organization:
org = models.Organization.get_by_slug(organization)
users = models.Users.select().where(models.Users.org==org.id)
else:
users = models.DataSource.select()
for i, user in enumerate(users):
if i > 0:
print "-" * 20

print "Id: {}\nName: {}\nEmail: {}".format(user.id, user.name.encode('utf-8'), user.email)
print "Id: {}\nName: {}\nEmail: {}\nOrganization: {}".format(user.id, user.name.encode('utf-8'), user.email, user.org.name)

0 comments on commit a66e182

Please sign in to comment.