Skip to content

Commit

Permalink
Change CLI Crud operations to use YAML (#1991)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLaPiana authored Dec 8, 2022
1 parent 5a180f6 commit df994f6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The types of changes are:
### Changed

* Update sample project landing page copy to be version-agnostic [#1958](https://github.com/ethyca/fides/pull/1958)
* `get` and `ls` CLI commands now return valid `fides` object YAML [#1991](https://github.com/ethyca/fides/pull/1991)

### Developer Experience

Expand All @@ -45,7 +46,6 @@ The types of changes are:
* Add Fides connector to support parent-child Fides deployments [#1861](https://github.com/ethyca/fides/pull/1861)
* Classification UI now polls for updates to classifications [#1908](https://github.com/ethyca/fides/pull/1908)


### Changed

* The organization info form step is now skipped if the server already has organization info. [#1840](https://github.com/ethyca/fides/pull/1840)
Expand Down
6 changes: 3 additions & 3 deletions src/fides/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .commands.annotate import annotate
from .commands.core import evaluate, parse, pull, push
from .commands.crud import delete, get, ls
from .commands.crud import delete, get_resource, list_resources
from .commands.db import database
from .commands.export import export
from .commands.generate import generate
Expand All @@ -33,8 +33,8 @@
database,
delete,
export,
get,
ls,
get_resource,
list_resources,
status,
pull,
push,
Expand Down
47 changes: 26 additions & 21 deletions src/fides/cli/commands/crud.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Contains all of the CRUD-type CLI commands for fides."""

import click
import yaml

from fides.cli.options import fides_key_argument, resource_type_argument
from fides.cli.utils import handle_cli_response, with_analytics
from fides.cli.utils import handle_cli_response, print_divider, with_analytics
from fides.ctl.core import api as _api
from fides.ctl.core.api_helpers import get_server_resource, list_server_resources
from fides.ctl.core.utils import echo_green


@click.command()
Expand All @@ -27,39 +29,42 @@ def delete(ctx: click.Context, resource_type: str, fides_key: str) -> None:
)


@click.command()
@click.command(name="get")
@click.pass_context
@resource_type_argument
@fides_key_argument
@with_analytics
def get(ctx: click.Context, resource_type: str, fides_key: str) -> None:
def get_resource(ctx: click.Context, resource_type: str, fides_key: str) -> None:
"""
View a resource from the server as a JSON object.
View a resource from the server as a YAML object.
"""
config = ctx.obj["CONFIG"]
handle_cli_response(
_api.get(
url=config.cli.server_url,
resource_type=resource_type,
resource_id=fides_key,
headers=config.user.request_headers,
)
resource = get_server_resource(
url=config.cli.server_url,
resource_type=resource_type,
resource_key=fides_key,
headers=config.user.request_headers,
raw=True,
)
print_divider()
echo_green(yaml.dump({resource_type: [resource]}))


@click.command()
@click.command(name="ls")
@click.pass_context
@resource_type_argument
@with_analytics
def ls(ctx: click.Context, resource_type: str) -> None: # pylint: disable=invalid-name
def list_resources(ctx: click.Context, resource_type: str) -> None:
"""
Get a list of all resources of this type from the server and display them as JSON.
Get a list of all resources of this type from the server and display them as YAML.
"""
config = ctx.obj["CONFIG"]
handle_cli_response(
_api.ls(
url=config.cli.server_url,
resource_type=resource_type,
headers=config.user.request_headers,
)
resources = list_server_resources(
url=config.cli.server_url,
resource_type=resource_type,
headers=config.user.request_headers,
exclude_keys=[],
raw=True,
)
print_divider()
echo_green(yaml.dump({resource_type: resources}))
6 changes: 3 additions & 3 deletions src/fides/ctl/core/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def get_server_resource(
resource_key: str,
headers: Dict[str, str],
raw: bool = False,
) -> Optional[Union[FidesModel, Dict]]:
) -> Union[FidesModel, Dict]:
"""
Attempt to get a given resource from the server.
Returns None if the object does not exist on the server.
Returns {} if the object does not exist on the server.
As we don't always have a way to attribute fides_keys to the
right resource, this function helps check what resource
Expand All @@ -74,7 +74,7 @@ def get_server_resource(
from_server=True,
)

return server_resource
return server_resource or {}


def list_server_resources(
Expand Down
10 changes: 5 additions & 5 deletions tests/ctl/core/test_api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_get_server_resource_found_resource(
"""
resource_type = created_resources[0]
resource_key = created_resources[1][0]
result: FidesModel = _api_helpers.get_server_resource(
result = _api_helpers.get_server_resource(
url=test_config.cli.server_url,
resource_type=resource_type,
resource_key=resource_key,
Expand All @@ -103,13 +103,13 @@ def test_get_server_resource_missing_resource(
Tests that a missing resource returns None
"""
resource_key = str(uuid.uuid4())
result: Optional[FidesModel] = _api_helpers.get_server_resource(
result = _api_helpers.get_server_resource(
url=test_config.cli.server_url,
resource_type=resource_type,
resource_key=resource_key,
headers=test_config.user.request_headers,
)
assert result is None
assert not result


@pytest.mark.integration
Expand Down Expand Up @@ -155,13 +155,13 @@ def test_get_server_resources_missing_resources(
class TestListServerResources:
def test_list_server_resources_passing(self, test_config: FidesConfig) -> None:
resource_type = "data_category"
result: List[FidesModel] = _api_helpers.list_server_resources(
result = _api_helpers.list_server_resources(
url=test_config.cli.server_url,
resource_type=resource_type,
headers=test_config.user.request_headers,
exclude_keys=[],
)
assert len(result) > 1
assert result

def test_list_server_resources_none(self, test_config: FidesConfig) -> None:
resource_type = "system"
Expand Down

0 comments on commit df994f6

Please sign in to comment.