From b104b840c4056ddbb08256428f1d72784b0b061b Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Wed, 1 Jun 2022 12:28:41 +0300 Subject: [PATCH] Move "org" command to the account module --- platformio/account/helpers.py | 4 + platformio/account/org/__init__.py | 13 ++ platformio/account/org/cli.py | 38 +++++ platformio/account/org/commands/__init__.py | 13 ++ platformio/account/org/commands/add.py | 34 ++++ platformio/account/org/commands/create.py | 38 +++++ platformio/account/org/commands/destroy.py | 34 ++++ platformio/account/org/commands/list.py | 48 ++++++ platformio/account/org/commands/remove.py | 34 ++++ platformio/account/org/commands/update.py | 52 ++++++ platformio/commands/org.py | 165 -------------------- tests/commands/test_account_org_team.py | 2 +- 12 files changed, 309 insertions(+), 166 deletions(-) create mode 100644 platformio/account/org/__init__.py create mode 100644 platformio/account/org/cli.py create mode 100644 platformio/account/org/commands/__init__.py create mode 100644 platformio/account/org/commands/add.py create mode 100644 platformio/account/org/commands/create.py create mode 100644 platformio/account/org/commands/destroy.py create mode 100644 platformio/account/org/commands/list.py create mode 100644 platformio/account/org/commands/remove.py create mode 100644 platformio/account/org/commands/update.py delete mode 100644 platformio/commands/org.py diff --git a/platformio/account/helpers.py b/platformio/account/helpers.py index adf9413e..2f469936 100644 --- a/platformio/account/helpers.py +++ b/platformio/account/helpers.py @@ -46,3 +46,7 @@ def validate_password(value): " including a number and a lowercase letter" ) return value + + +def validate_orgname(value): + return validate_username(value, "Organization name") diff --git a/platformio/account/org/__init__.py b/platformio/account/org/__init__.py new file mode 100644 index 00000000..b0514903 --- /dev/null +++ b/platformio/account/org/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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. diff --git a/platformio/account/org/cli.py b/platformio/account/org/cli.py new file mode 100644 index 00000000..5f1f0fc9 --- /dev/null +++ b/platformio/account/org/cli.py @@ -0,0 +1,38 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 platformio.account.org.commands.add import org_add_cmd +from platformio.account.org.commands.create import org_create_cmd +from platformio.account.org.commands.destroy import org_destroy_cmd +from platformio.account.org.commands.list import org_list_cmd +from platformio.account.org.commands.remove import org_remove_cmd +from platformio.account.org.commands.update import org_update_cmd + + +@click.group( + "account", + commands=[ + org_add_cmd, + org_create_cmd, + org_destroy_cmd, + org_list_cmd, + org_remove_cmd, + org_update_cmd, + ], + short_help="Manage organizations", +) +def cli(): + pass diff --git a/platformio/account/org/commands/__init__.py b/platformio/account/org/commands/__init__.py new file mode 100644 index 00000000..b0514903 --- /dev/null +++ b/platformio/account/org/commands/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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. diff --git a/platformio/account/org/commands/add.py b/platformio/account/org/commands/add.py new file mode 100644 index 00000000..55e658e7 --- /dev/null +++ b/platformio/account/org/commands/add.py @@ -0,0 +1,34 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 platformio.account.client import AccountClient + + +@click.command("add", short_help="Add a new owner to organization") +@click.argument( + "orgname", +) +@click.argument( + "username", +) +def org_add_cmd(orgname, username): + client = AccountClient() + client.add_org_owner(orgname, username) + return click.secho( + "The new owner `%s` has been successfully added to the `%s` organization." + % (username, orgname), + fg="green", + ) diff --git a/platformio/account/org/commands/create.py b/platformio/account/org/commands/create.py new file mode 100644 index 00000000..f4b7737a --- /dev/null +++ b/platformio/account/org/commands/create.py @@ -0,0 +1,38 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 platformio.account.client import AccountClient +from platformio.account.helpers import validate_email, validate_orgname + + +@click.command("create", short_help="Create a new organization") +@click.argument( + "orgname", + callback=lambda _, __, value: validate_orgname(value), +) +@click.option( + "--email", callback=lambda _, __, value: validate_email(value) if value else value +) +@click.option( + "--displayname", +) +def org_create_cmd(orgname, email, displayname): + client = AccountClient() + client.create_org(orgname, email, displayname) + return click.secho( + "The organization `%s` has been successfully created." % orgname, + fg="green", + ) diff --git a/platformio/account/org/commands/destroy.py b/platformio/account/org/commands/destroy.py new file mode 100644 index 00000000..2a202f9a --- /dev/null +++ b/platformio/account/org/commands/destroy.py @@ -0,0 +1,34 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 platformio.account.client import AccountClient + + +@click.command("destroy", short_help="Destroy organization") +@click.argument("orgname") +def org_destroy_cmd(orgname): + client = AccountClient() + click.confirm( + "Are you sure you want to delete the `%s` organization account?\n" + "Warning! All linked data will be permanently removed and can not be restored." + % orgname, + abort=True, + ) + client.destroy_org(orgname) + return click.secho( + "Organization `%s` has been destroyed." % orgname, + fg="green", + ) diff --git a/platformio/account/org/commands/list.py b/platformio/account/org/commands/list.py new file mode 100644 index 00000000..471814ea --- /dev/null +++ b/platformio/account/org/commands/list.py @@ -0,0 +1,48 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 json + +import click +from tabulate import tabulate + +from platformio.account.client import AccountClient + + +@click.command("list", short_help="List organizations and their members") +@click.option("--json-output", is_flag=True) +def org_list_cmd(json_output): + client = AccountClient() + orgs = client.list_orgs() + if json_output: + return click.echo(json.dumps(orgs)) + if not orgs: + return click.echo("You do not have any organization") + for org in orgs: + click.echo() + click.secho(org.get("orgname"), fg="cyan") + click.echo("-" * len(org.get("orgname"))) + data = [] + if org.get("displayname"): + data.append(("Display Name:", org.get("displayname"))) + if org.get("email"): + data.append(("Email:", org.get("email"))) + data.append( + ( + "Owners:", + ", ".join((owner.get("username") for owner in org.get("owners"))), + ) + ) + click.echo(tabulate(data, tablefmt="plain")) + return click.echo() diff --git a/platformio/account/org/commands/remove.py b/platformio/account/org/commands/remove.py new file mode 100644 index 00000000..9dee462a --- /dev/null +++ b/platformio/account/org/commands/remove.py @@ -0,0 +1,34 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 platformio.account.client import AccountClient + + +@click.command("remove", short_help="Remove an owner from organization") +@click.argument( + "orgname", +) +@click.argument( + "username", +) +def org_remove_cmd(orgname, username): + client = AccountClient() + client.remove_org_owner(orgname, username) + return click.secho( + "The `%s` owner has been successfully removed from the `%s` organization." + % (username, orgname), + fg="green", + ) diff --git a/platformio/account/org/commands/update.py b/platformio/account/org/commands/update.py new file mode 100644 index 00000000..7a7d2b4c --- /dev/null +++ b/platformio/account/org/commands/update.py @@ -0,0 +1,52 @@ +# Copyright (c) 2014-present PlatformIO +# +# 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 platformio.account.client import AccountClient +from platformio.account.helpers import validate_email, validate_orgname + + +@click.command("update", short_help="Update organization") +@click.argument("cur_orgname") +@click.option( + "--orgname", + callback=lambda _, __, value: validate_orgname(value), + help="A new orgname", +) +@click.option("--email") +@click.option("--displayname") +def org_update_cmd(cur_orgname, **kwargs): + client = AccountClient() + org = client.get_org(cur_orgname) + del org["owners"] + new_org = org.copy() + if not any(kwargs.values()): + for field in org: + new_org[field] = click.prompt( + field.replace("_", " ").capitalize(), default=org[field] + ) + if field == "email": + validate_email(new_org[field]) + if field == "orgname": + validate_orgname(new_org[field]) + else: + new_org.update( + {key.replace("new_", ""): value for key, value in kwargs.items() if value} + ) + client.update_org(cur_orgname, new_org) + return click.secho( + "The organization `%s` has been successfully updated." % cur_orgname, + fg="green", + ) diff --git a/platformio/commands/org.py b/platformio/commands/org.py deleted file mode 100644 index bd569e76..00000000 --- a/platformio/commands/org.py +++ /dev/null @@ -1,165 +0,0 @@ -# Copyright (c) 2014-present PlatformIO -# -# 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. - -# pylint: disable=unused-argument - -import json - -import click -from tabulate import tabulate - -from platformio.account.client import AccountClient -from platformio.account.helpers import validate_email, validate_username - - -@click.group("org", short_help="Manage organizations") -def cli(): - pass - - -def validate_orgname(value): - return validate_username(value, "Organization name") - - -@cli.command("create", short_help="Create a new organization") -@click.argument( - "orgname", - callback=lambda _, __, value: validate_orgname(value), -) -@click.option( - "--email", callback=lambda _, __, value: validate_email(value) if value else value -) -@click.option( - "--displayname", -) -def org_create(orgname, email, displayname): - client = AccountClient() - client.create_org(orgname, email, displayname) - return click.secho( - "The organization `%s` has been successfully created." % orgname, - fg="green", - ) - - -@cli.command("list", short_help="List organizations and their members") -@click.option("--json-output", is_flag=True) -def org_list(json_output): - client = AccountClient() - orgs = client.list_orgs() - if json_output: - return click.echo(json.dumps(orgs)) - if not orgs: - return click.echo("You do not have any organization") - for org in orgs: - click.echo() - click.secho(org.get("orgname"), fg="cyan") - click.echo("-" * len(org.get("orgname"))) - data = [] - if org.get("displayname"): - data.append(("Display Name:", org.get("displayname"))) - if org.get("email"): - data.append(("Email:", org.get("email"))) - data.append( - ( - "Owners:", - ", ".join((owner.get("username") for owner in org.get("owners"))), - ) - ) - click.echo(tabulate(data, tablefmt="plain")) - return click.echo() - - -@cli.command("update", short_help="Update organization") -@click.argument("cur_orgname") -@click.option( - "--orgname", - callback=lambda _, __, value: validate_orgname(value), - help="A new orgname", -) -@click.option("--email") -@click.option("--displayname") -def org_update(cur_orgname, **kwargs): - client = AccountClient() - org = client.get_org(cur_orgname) - del org["owners"] - new_org = org.copy() - if not any(kwargs.values()): - for field in org: - new_org[field] = click.prompt( - field.replace("_", " ").capitalize(), default=org[field] - ) - if field == "email": - validate_email(new_org[field]) - if field == "orgname": - validate_orgname(new_org[field]) - else: - new_org.update( - {key.replace("new_", ""): value for key, value in kwargs.items() if value} - ) - client.update_org(cur_orgname, new_org) - return click.secho( - "The organization `%s` has been successfully updated." % cur_orgname, - fg="green", - ) - - -@cli.command("destroy", short_help="Destroy organization") -@click.argument("orgname") -def account_destroy(orgname): - client = AccountClient() - click.confirm( - "Are you sure you want to delete the `%s` organization account?\n" - "Warning! All linked data will be permanently removed and can not be restored." - % orgname, - abort=True, - ) - client.destroy_org(orgname) - return click.secho( - "Organization `%s` has been destroyed." % orgname, - fg="green", - ) - - -@cli.command("add", short_help="Add a new owner to organization") -@click.argument( - "orgname", -) -@click.argument( - "username", -) -def org_add_owner(orgname, username): - client = AccountClient() - client.add_org_owner(orgname, username) - return click.secho( - "The new owner `%s` has been successfully added to the `%s` organization." - % (username, orgname), - fg="green", - ) - - -@cli.command("remove", short_help="Remove an owner from organization") -@click.argument( - "orgname", -) -@click.argument( - "username", -) -def org_remove_owner(orgname, username): - client = AccountClient() - client.remove_org_owner(orgname, username) - return click.secho( - "The `%s` owner has been successfully removed from the `%s` organization." - % (username, orgname), - fg="green", - ) diff --git a/tests/commands/test_account_org_team.py b/tests/commands/test_account_org_team.py index 29d85971..eb0c3edf 100644 --- a/tests/commands/test_account_org_team.py +++ b/tests/commands/test_account_org_team.py @@ -22,7 +22,7 @@ import pytest import requests from platformio.account.cli import cli as cmd_account -from platformio.commands.org import cli as cmd_org +from platformio.account.org.cli import cli as cmd_org from platformio.commands.team import cli as cmd_team pytestmark = pytest.mark.skipif(