Skip to content

Commit

Permalink
Convert snow app events to v2-native
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fcampbell committed Oct 9, 2024
1 parent f6448d2 commit bf80695
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
69 changes: 29 additions & 40 deletions src/snowflake/cli/_plugins/nativeapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@
from snowflake.cli._plugins.nativeapp.entities.application_package import (
ApplicationPackageEntityModel,
)
from snowflake.cli._plugins.nativeapp.manager import NativeAppManager
from snowflake.cli._plugins.nativeapp.teardown_processor import (
NativeAppTeardownProcessor,
)
from snowflake.cli._plugins.nativeapp.v2_conversions.compat import (
find_entity,
nativeapp_definition_v2_to_v1,
single_app_and_package,
)
from snowflake.cli._plugins.nativeapp.version.commands import app as versions_app
Expand All @@ -62,7 +60,6 @@
ObjectResult,
StreamResult,
)
from snowflake.cli.api.project.project_verification import assert_project_type
from snowflake.cli.api.project.schemas.project_definition import ProjectDefinitionV1
from typing_extensions import Annotated

Expand Down Expand Up @@ -406,7 +403,7 @@ class RecordType(Enum):

@app.command("events", requires_connection=True)
@with_project_definition()
@nativeapp_definition_v2_to_v1(app_required=True)
@single_app_and_package(app_required=True)
def app_events(
since: str = typer.Option(
default="",
Expand Down Expand Up @@ -486,48 +483,40 @@ def app_events(
if first >= 0:
raise IncompatibleParametersError(["--follow", "--first"])

assert_project_type("native_app")
cli_context = get_cli_context()
ws = WorkspaceManager(
project_definition=cli_context.project_definition,
project_root=cli_context.project_root,
)
app_id = options["app_entity_id"]

record_type_names = [r.name for r in record_types]
manager = NativeAppManager(
project_definition=get_cli_context().project_definition.native_app,
project_root=get_cli_context().project_root,

if follow and last == -1 and not since:
# If we don't have a value for --last or --since, assume a value
# for --last so we at least print something before starting the stream
last = DEFAULT_EVENT_FOLLOW_LAST
stream: Iterable[CommandResult] = (
EventResult(event)
for event in ws.perform_action(
app_id,
EntityActions.EVENTS,
since=since,
until=until,
record_types=record_type_names,
scopes=scopes,
consumer_org=consumer_org,
consumer_account=consumer_account,
consumer_app_hash=consumer_app_hash,
first=first,
last=last,
follow=follow,
interval_seconds=follow_interval,
)
)
if follow:
if last == -1 and not since:
# If we don't have a value for --last or --since, assume a value
# for --last so we at least print something before starting the stream
last = DEFAULT_EVENT_FOLLOW_LAST
stream: Iterable[CommandResult] = (
EventResult(event)
for event in manager.stream_events(
since=since,
last=last,
interval_seconds=follow_interval,
record_types=record_type_names,
scopes=scopes,
consumer_org=consumer_org,
consumer_account=consumer_account,
consumer_app_hash=consumer_app_hash,
)
)
# Append a newline at the end to make the CLI output clean when we hit Ctrl-C
stream = itertools.chain(stream, [MessageResult("")])
else:
stream = (
EventResult(event)
for event in manager.get_events(
since=since,
until=until,
record_types=record_type_names,
scopes=scopes,
first=first,
last=last,
consumer_org=consumer_org,
consumer_account=consumer_account,
consumer_app_hash=consumer_app_hash,
)
)

# Cast the stream to a Generator since that's what StreamResult wants
return StreamResult(cast(Generator[CommandResult, None, None], stream))
Expand Down
53 changes: 52 additions & 1 deletion src/snowflake/cli/_plugins/nativeapp/entities/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,58 @@ def action_drop(
cascade=cascade,
)

def action_events(
self,
action_ctx: ActionContext,
since: str | datetime | None = None,
until: str | datetime | None = None,
record_types: list[str] | None = None,
scopes: list[str] | None = None,
consumer_org: str = "",
consumer_account: str = "",
consumer_app_hash: str = "",
first: int = -1,
last: int = -1,
follow: bool = False,
interval_seconds: int = 10,
*args,
**kwargs,
):
model = self._entity_model
package_entity: ApplicationPackageEntity = action_ctx.get_entity(
model.from_.target
)
package_model: ApplicationPackageEntityModel = (
package_entity._entity_model # noqa: SLF001
)
if follow:
return self.stream_events(
app_name=model.fqn.identifier,
package_name=package_model.fqn.identifier,
interval_seconds=interval_seconds,
since=since,
record_types=record_types,
scopes=scopes,
consumer_org=consumer_org,
consumer_account=consumer_account,
consumer_app_hash=consumer_app_hash,
last=last,
)
else:
return self.get_events(
app_name=model.fqn.identifier,
package_name=package_model.fqn.identifier,
since=since,
until=until,
record_types=record_types,
scopes=scopes,
consumer_org=consumer_org,
consumer_account=consumer_account,
consumer_app_hash=consumer_app_hash,
first=first,
last=last,
)

@classmethod
def drop(
cls,
Expand Down Expand Up @@ -763,7 +815,6 @@ def get_events(
first: int = -1,
last: int = -1,
):

record_types = record_types or []
scopes = scopes or []

Expand Down
1 change: 1 addition & 0 deletions src/snowflake/cli/api/entities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class EntityActions(str, Enum):
DEPLOY = "action_deploy"
DROP = "action_drop"
VALIDATE = "action_validate"
EVENTS = "action_events"

VERSION_LIST = "action_version_list"
VERSION_CREATE = "action_version_create"
Expand Down

0 comments on commit bf80695

Please sign in to comment.