Skip to content

Commit

Permalink
feat(project): adds command to manage user access in projects
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 4, 2023
1 parent 5fe74bf commit e2bb854
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 0 deletions.
2 changes: 2 additions & 0 deletions riocli/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from riocli.project.inspect import inspect_project
from riocli.project.list import list_project
from riocli.project.select import select_project
from riocli.project.users import users


@click.group(
Expand All @@ -41,3 +42,4 @@ def project() -> None:
project.add_command(select_project)
project.add_command(inspect_project)
project.add_command(features)
project.add_command(users)
38 changes: 38 additions & 0 deletions riocli/project/users/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsGroup

from riocli.constants import Colors
from riocli.project.users.add import add_user_to_project
from riocli.project.users.list import list_project_users
from riocli.project.users.remove import remove_user_from_project


@click.group(
invoke_without_command=False,
cls=HelpColorsGroup,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def users():
"""
User access management on a project
"""
pass


users.add_command(list_project_users)
users.add_command(add_user_to_project)
users.add_command(remove_user_from_project)
50 changes: 50 additions & 0 deletions riocli/project/users/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click
from click_help_colors import HelpColorsCommand
from yaspin.api import Yaspin

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.project.users.util import get_user_guid_from_email
from riocli.utils.spinner import with_spinner


@click.command(
'add',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('user-email', type=str)
@click.pass_context
@with_spinner(text="Adding user to project...")
def add_user_to_project(ctx: click.Context, user_email: str, spinner: Yaspin = None) -> None:
"""
Add user to the project
"""
project_guid = ctx.obj.data.get('project_id')
organization_guid = ctx.obj.data.get('organization_id')

try:
user_guid = get_user_guid_from_email(organization_guid, user_email)
client = new_client(with_project=False)
client.add_user_to_project(project_guid, user_guid)
spinner.text = click.style('User added to the project', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = click.style('Failed to add user: {}'.format(e), fg=Colors.RED)
spinner.red.ok(Symbols.ERROR)
raise SystemExit(1)
55 changes: 55 additions & 0 deletions riocli/project/users/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typing

import click
from click_help_colors import HelpColorsCommand
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import Colors
from riocli.utils import tabulate_data


@click.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.pass_context
def list_project_users(ctx: click.Context) -> None:
"""
List users in the project
"""
try:
client = new_v2_client(with_project=False)
project = client.get_project(ctx.obj.data.get('project_id'))
display_project_users(unmunchify(project.spec.users))
except Exception as e:
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


def display_project_users(users: typing.List[typing.Dict]) -> None:
headers = ['ID', 'Name', 'Email', 'Status']
data = []
for user in users:
data.append([
user.get('userGUID'),
'{} {}'.format(user.get('firstName'), user.get('lastName')),
user.get('emailID'),
])

tabulate_data(data, headers)
50 changes: 50 additions & 0 deletions riocli/project/users/remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click
from click_help_colors import HelpColorsCommand
from yaspin.api import Yaspin

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.project.users.util import get_user_guid_from_email
from riocli.utils.spinner import with_spinner


@click.command(
'remove',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('user-email', type=str)
@click.pass_context
@with_spinner(text="Removing user from project...")
def remove_user_from_project(ctx: click.Context, user_email: str, spinner: Yaspin = None) -> None:
"""
Remove user from a project
"""
project_guid = ctx.obj.data.get('project_id')
organization_guid = ctx.obj.data.get('organization_id')

try:
user_guid = get_user_guid_from_email(organization_guid, user_email)
client = new_client(with_project=False)
client.remove_user_from_project(project_guid, user_guid)
spinner.text = click.style('User removed from the project', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = click.style('Failed to remove user: {}'.format(e), fg=Colors.RED)
spinner.red.ok(Symbols.ERROR)
raise SystemExit(1)
26 changes: 26 additions & 0 deletions riocli/project/users/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typing

from riocli.organization.utils import get_organization_details


def get_user_guid_from_email(organization_guid: str, user_email: str) -> typing.Any:
organization = get_organization_details(organization_guid)
users = organization.get('users')
for user in users:
if user['emailID'] == user_email:
return user['guid']

return None

0 comments on commit e2bb854

Please sign in to comment.