Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive facts gathered event #6

Merged
merged 8 commits into from
Aug 17, 2022
Merged

Conversation

fabriziosestito
Copy link
Member

@fabriziosestito fabriziosestito commented Aug 8, 2022

This PR adds the setup to receive events and it adds the first handled event FactsGatheredEvent

It adds the basic setup for cloudevents and jsonschema validation that are going to be moved in https://github.com/trento-project/contracts ASAP.

The execution API now implements a behaviour, this simplify the DI and the mocking in the event handling logic so we do not rely on the GenServer/message passing while testing.

The event handling logic is responsibility of the Wanda.Policy module, in this PR the GenRMQ.Consumer implementation is not tested but it could be easily tested with an integration test while we iterate.

@fabriziosestito fabriziosestito force-pushed the receive_facts_gathered_event branch 2 times, most recently from 521004b to a424169 Compare August 8, 2022 09:28
Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nice @fabriziosestito !
Nothing really to complain about hehe


@facts_gathered_event "trento.checks.v1.FactsGathered"

@spec handle_event(CloudEvent.t()) :: :ok | {:error, any}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this signature correct?
Shouldn't return {:ok, [Fact.t()]} or similar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope it returns :ok because it sends a message to the genserver

Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +1 to +23
defmodule Wanda.JsonSchema do
@moduledoc """
TODO: move to separate repo.
"""

@schema_path Path.join(File.cwd!(), "priv/json_schema/")

for schema_file <- File.ls!(@schema_path) do
@external_resource schema_file

schema =
@schema_path
|> Path.join(schema_file)
|> File.read!()
|> Jason.decode!()
|> ExJsonSchema.Schema.resolve()

def validate(unquote(Path.basename(schema_file, ".schema.json")), data),
do: ExJsonSchema.Validator.validate(unquote(Macro.escape(schema)), data)
end

def validate(_, _), do: {:error, :schema_not_found}
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna print this and make some posters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or in the contract repo :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Copy link
Member

@nelsonkopliku nelsonkopliku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah man, really good job! 🚀


alias Wanda.Execution.Fact

@callback receive_facts(execution_id :: String.t(), agent_id :: String.t(), [Fact.t()]) ::
Copy link
Member

@nelsonkopliku nelsonkopliku Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are sometimes using the agent concept and sometimes the target concept.
Are we fine with the discrepancy, or should we aim to converge on naming?

Things are:

  • we keep agent for the sake of simplicity and explicitness
  • we user target as a more abstract thing at wanda level

As a reminder, if that helps with the decision, we decided to go for group instead of cluster in wanda, in order to be open to the possibility that targets on which we do facts gathering for a check execution can be non necessarily part of the same cluster, but possibly also from different clusters or even different sap systems.
Do I remember correctly?

Copy link
Member Author

@fabriziosestito fabriziosestito Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nelsonkopliku those are for sure related to me, but the agent_id is the id of an agent while the target is the struct representing a target execution (which also has agent_id inside, and the check list) so I'm not sure they need to be squashed to the same name.
I think Wanda can be agnostic about clusters but for sure agent is part of its concern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I just thought that converging toward Target+target_id or Agent+agent_id would have been more consistent.

However, I'm completely fine with how it is.


@schema_path Path.join(File.cwd!(), "priv/json_schema/")

for schema_file <- File.ls!(@schema_path) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so cool! ❤️

|> Jason.decode!()
|> ExJsonSchema.Schema.resolve()

def validate(unquote(Path.basename(schema_file, ".schema.json")), data),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should resolve to

def validate("trento.checks.v1.FactsGathered", data),

in this example for this event.
And all the supported schemas.
Very nice!

@@ -0,0 +1,28 @@
defmodule Wanda.Policy do
Copy link
Member

@nelsonkopliku nelsonkopliku Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a reasoning point.

Could this Policy be something like a Processor|Injestor|SomethingThatProcessesIntegrationEvents? 🤔

I am not sure, will need to see how that evolves while adding more events.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep on the radar, i didn't came out with a proper name yet. maybe event handler?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, could work

@fabriziosestito fabriziosestito merged commit b24db9e into main Aug 17, 2022
@fabriziosestito fabriziosestito deleted the receive_facts_gathered_event branch August 17, 2022 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants