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

feat(cli): refactor list formatter for better resource manager support #2829

Merged
merged 19 commits into from Jun 28, 2023

Conversation

schoren
Copy link
Collaborator

@schoren schoren commented Jun 26, 2023

This PR is a technical refactor that allows leveraging the server's resourcemanager formatting capabilities.

Currently, this PR only updates the LIST cli verb. Meaning that tracetest list [any resource] will use this new code. However, the rest of the verbs are unmodified. They'll be updated in following PRs.

The benefits of this refactor are

  1. don't re implement features: the server already supports handling all the encodings and is very well tested.
  2. avoids discrepancies generated by different encoding methods (server vs cli)

there is an extra benefit, that will only be visible once the refactor is completed, and that is, the amount of code needed to implement a resource (or maintain it, for that matter), is very close to zero. The only thing needed is this:

resources.Register(
	resourcemanager.NewClient(
		httpClient,
		"config", "configs",
		resourcemanager.TableConfig{
			Cells: []resourcemanager.TableCellConfig{
				{Header: "ID", Path: "spec.id"},
				{Header: "NAME", Path: "spec.name"},
				{Header: "ANALYTICS ENABLED", Path: "spec.analyticsEnabled"},
				// etc
			},
		},
	),
)

The rest is general to any resources, due to the standarization that resourcemanager provides.

Loom going over outpu
https://www.loom.com/share/73043af3a23e42e79255bed75e6df46c

Checklist

  • tested locally
  • added new dependencies
  • updated the docs
  • added a test

@xoscar
Copy link
Collaborator

xoscar commented Jun 27, 2023

@schoren do you have diagrams, plans or any information on how we want the CLI to work moving forward?

I really feel that if we go the path of having a "blind" version of the CLI which doesn't know about structures, types, etc might cause problems in the long run if we have to implement more than just a pass-through.

@schoren
Copy link
Collaborator Author

schoren commented Jun 27, 2023

@xoscar what kind of problems do you see?

The whole point of having the resource manager was to have clients as thin as possible, and centralize all the encoding/decoding and other logics in the server/resource manager.

all resources behave exactly the same, as per the resource manager implementation.

Keep in mind that this way of handling things only applies for the CRUD operations, and does not exclude having more advanced implementations for other actions.

For example, the run verb does not follow under the resource manager umbrella, so any code the CLI needs for that can be added without interference from the resource manager code.

About diagrams, plans or any other information, we don't have that right now either. It doesn't feel like the current code had any architectural design behind it. Maybe if we could compare the current implementation design with the new proposed one we could get pros/cons of each.

@xoscar
Copy link
Collaborator

xoscar commented Jun 27, 2023

@schoren I can't think of a problem today, so if you feel this is the best approach and will serve any potential customization in the future that's fine.

require.True(analyzerYAML[0].Spec.Enabled)
require.Equal(analyzerYAML[0].Spec.MinimumScore, 95)
require.Len(analyzerYAML[0].Spec.Plugins, 3)
analyzerList := helpers.UnmarshalJSON[types.ResourceList[types.AnalyzerResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

Copy link
Collaborator

Choose a reason for hiding this comment

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

@schoren I think that's expected, the CLI is expected to return an array of resources:

---
resource1
---
resource2
---
resourc3

As per the requirements, you can see some of that from the CLI improvements epic
Screenshot 2023-06-28 at 8 10 30

Copy link
Collaborator Author

@schoren schoren Jun 28, 2023

Choose a reason for hiding this comment

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

@xoscar The originally defined specs (environments for example #2182 (comment)) defined the json resposne to be a resource list. This allows direct passthrough from the server.

Keep in mind that count hold the total amount of available resources, not just the returned ones, so it's very useful for pagination. If you are using the JSON ouptut, it's reasonable to assume that this is a useful info to provide

Copy link
Collaborator

Choose a reason for hiding this comment

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

So then, how should it work? ResourceList for json and plane array list for yaml?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@kdhamric had some comments about this first time we implement it as a ResourceList

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The use case for each formmat are different.

The YAML list is almost surely used to export a list into a file to later import on another instace, hence the multi-document format.

JSON is mainly machine readable, so its most likely be used in scripting. In this case, you might need extra info.

for this same reason JSON uses the augmented verison and YAML doesn’t.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IIRC @kdhamric comment was around the yaml format and how that should be multidocument instead of a ResourceList. This is still the case here.

This change is only for the JSON format.

Copy link
Collaborator Author

@schoren schoren Jun 28, 2023

Choose a reason for hiding this comment

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

@xoscar @kdhamric I've added a loom about the output formats so we're all on the same page on what this PR proposes

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perfect, that makes sense, thanks for the video!

require.Equal("current", configYAML[0].Spec.ID)
require.Equal("Config", configYAML[0].Spec.Name)
require.False(configYAML[0].Spec.AnalyticsEnabled)
configList := helpers.UnmarshalJSON[types.ResourceList[types.ConfigResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

@@ -84,12 +84,13 @@ func TestListDatastore(t *testing.T) {
result := tracetestcli.Exec(t, "list datastore --sortBy name --output json", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)

dataStoresJSON := helpers.UnmarshalJSON[[]types.DataStoreResource](t, result.StdOut)
dataStoresList := helpers.UnmarshalJSON[types.ResourceList[types.DataStoreResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

@@ -108,18 +108,19 @@ func TestListDemos(t *testing.T) {
result := tracetestcli.Exec(t, "list demo --sortBy name --sortDirection asc --output json", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)

demos := helpers.UnmarshalJSON[[]types.DemoResource](t, result.StdOut)
require.Len(demos, 2)
demos := helpers.UnmarshalJSON[types.ResourceList[types.DemoResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

@@ -127,12 +127,13 @@ func TestListEnvironments(t *testing.T) {
result := tracetestcli.Exec(t, "list environment --sortBy name --sortDirection asc --output json", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)

environmentVarsList := helpers.UnmarshalJSON[[]types.EnvironmentResource](t, result.StdOut)
require.Len(environmentVarsList, 3)
environmentVarsList := helpers.UnmarshalJSON[types.ResourceList[types.EnvironmentResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

require.Equal("periodic", pollingProfileYAML[0].Spec.Strategy)
require.Equal("50s", pollingProfileYAML[0].Spec.Periodic.RetryDelay)
require.Equal("10m", pollingProfileYAML[0].Spec.Periodic.Timeout)
pollingProfilesList := helpers.UnmarshalJSON[types.ResourceList[types.PollingProfileResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

@@ -134,10 +134,11 @@ func TestListTransactions(t *testing.T) {
result := tracetestcli.Exec(t, "list transaction --sortBy name --sortDirection asc --output json", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)

transactions := helpers.UnmarshalJSON[[]types.AugmentedTransactionResource](t, result.StdOut)
require.Len(transactions, 3)
transactions := helpers.UnmarshalJSON[types.ResourceList[types.AugmentedTransactionResource]](t, result.StdOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this scenario was incorrect (as well as the old implementation). it was returning an array of resources instead of a ResourceList

@@ -35,16 +34,19 @@ An error ocurred when executing the command
}
}

func WithParamsHandler(params ...parameters.Params) MiddlewareWrapper {
func WithParamsHandler(validators ...Validator) MiddlewareWrapper {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this changes allows the resourcemanager client to provide its own parameters without needing to depend on the parameters package

}

return errors
func (pe paramError) Error() string {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in go, error is an interface, so implemeting this method makes the original paramError be treated as any other error. just this changes makes it compliant with the new validator approach from here

Copy link
Contributor

@danielbdias danielbdias left a comment

Choose a reason for hiding this comment

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

🤘

if lastRunTime != "" {
date, err := time.Parse(time.RFC3339, lastRunTime)
if err != nil {
panic(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we panic here or only emit a warning log and set the time as empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch! I changed it to return an error!

@schoren schoren merged commit e6a6d5e into main Jun 28, 2023
25 checks passed
@schoren schoren deleted the cli-format-from-server branch June 28, 2023 18:44
mathnogueira added a commit that referenced this pull request Jul 3, 2023
* chore(docs): Adding App Insights Configuration Page (#2820)

* chore(docs): Adding App Insights Configuration Page

* fixing typo

* fix(frontend): Fixing Resizable Panels UI bugs (#2827)

* fix(tests): add analyzer resource to cli e2e table (#2826)

* chore(docs): Azure App Insights Recipes (#2821)

* chore(docs): Adding App Insights Configuration Page

* chore(docs): Adding App Insights Recipes

* chore(docs): Adding App Insights Recipes

* chore(docs): fixing typo

* adding recipe links

* adding recipe links

* fixing typo

* chore(frotend): Test Definition Name Input (#2830)

* chore(frotend): Updating Panel Splitter w/ Tooltip (#2828)

* chore(frotend): Updating Panel Splitter w/ Tooltip

* Updating theme color

* removing unnecessary flag

* undo change

* fix(frontend): fix missing font-face (#2833)

* feat(cli): refactor list formatter for better resource manager support (#2829)

* feat(frontend): add independent trace vs test data (#2815)

* feat(cli): refactor Get formatter for better resource manager support (#2831)

* feat(cli): dynamic list of available resources (#2832)

* feat(cli): refactor Delete to new resource manager client (#2836)

* feat(cli): update export command with new resourcemanager client (#2838)

* Standardize old resources to to use list method with SQL Injection protection and stardardize PollingProfile file (#2839)

* wip: update list on demo and environment to use the same standards as tests and transactions

* Fixing provisioning test

* Update provisioning test

* feature(frontend): Contact Us Modal (#2835)

* feature(frontend): Contact Us Modal

* feature(frontend): Contact Us Modal

* feat(cli): update apply command with new resourcemanager client (#2841)

* feat(frontend): trace vs test data for timeline and attribute list (#2837)

* feat: new tests openapi spec (#2658)

update get tests endpoint to use resource manager

* fix: move transactions to it's own module (#2664)

* fix: move transactions to the transactions folder

* remove alias to transactions module

* feat: test models + list tests endpoint (#2681)

* add test, run, and trigger objects

* feat: move test and run structs into the test package

* list tests

* add old format attributes again

* fix mapping

* compatibility mode

* fix server prefix integration test

* force server unit tests to run

* fix tracetest test

* trigger CI

* Apply suggestions from code review

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* fix build

* version test specs

* update test names

* use sqlutils

* dont specify branch target in pull_request

* fix list test

---------

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* 2659 cli improvements migrate tests to resource manager architecture get tests frontend (#2702)

chore(frontend): updating FE to support GET /tests endpoint changes

* feat: test list augmented response (#2732)

add augmented list

* fix(server): fix trigger json encoding versioning and test (#2795)

* feat: test models + list tests endpoint (#2681)

* add test, run, and trigger objects

* feat: move test and run structs into the test package

* list tests

* add old format attributes again

* fix mapping

* compatibility mode

* fix server prefix integration test

* force server unit tests to run

* fix tracetest test

* trigger CI

* Apply suggestions from code review

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* fix build

* version test specs

* update test names

* use sqlutils

* dont specify branch target in pull_request

* fix list test

---------

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* GET endpoint

* fix rebase

* add create method and fix tests

* add rest of methods

* Changing test structure and fixing method by method

* Update tests

* Fixing test errors for Delete

* Adding gRPC tests

* Adding tests for traceid trigger

* migrate rest of endpoints and fix compilation errors

* fix all unit and integration tests

* Improving tests

* fix building errors

---------

Co-authored-by: Oscar Reyes <oscar-rreyes1@hotmail.com>
Co-authored-by: Jorge Padilla <jorge.esteban.padilla@gmail.com>
Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>
Co-authored-by: Daniel Baptista Dias <danielbdias@users.noreply.github.com>
Co-authored-by: Daniel Dias <danielbpdias@gmail.com>
xoscar added a commit that referenced this pull request Jul 6, 2023
* feat: new tests openapi spec (#2658)

update get tests endpoint to use resource manager

* fix: move transactions to it's own module (#2664)

* fix: move transactions to the transactions folder

* remove alias to transactions module

* feat: test models + list tests endpoint (#2681)

* add test, run, and trigger objects

* feat: move test and run structs into the test package

* list tests

* add old format attributes again

* fix mapping

* compatibility mode

* fix server prefix integration test

* force server unit tests to run

* fix tracetest test

* trigger CI

* Apply suggestions from code review

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* fix build

* version test specs

* update test names

* use sqlutils

* dont specify branch target in pull_request

* fix list test

---------

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* 2659 cli improvements migrate tests to resource manager architecture get tests frontend (#2702)

chore(frontend): updating FE to support GET /tests endpoint changes

* feat: test list augmented response (#2732)

add augmented list

* fix(server): fix trigger json encoding versioning and test (#2795)

* WIP feat: get endpoint (#2789)

* chore(docs): Adding App Insights Configuration Page (#2820)

* chore(docs): Adding App Insights Configuration Page

* fixing typo

* fix(frontend): Fixing Resizable Panels UI bugs (#2827)

* fix(tests): add analyzer resource to cli e2e table (#2826)

* chore(docs): Azure App Insights Recipes (#2821)

* chore(docs): Adding App Insights Configuration Page

* chore(docs): Adding App Insights Recipes

* chore(docs): Adding App Insights Recipes

* chore(docs): fixing typo

* adding recipe links

* adding recipe links

* fixing typo

* chore(frotend): Test Definition Name Input (#2830)

* chore(frotend): Updating Panel Splitter w/ Tooltip (#2828)

* chore(frotend): Updating Panel Splitter w/ Tooltip

* Updating theme color

* removing unnecessary flag

* undo change

* fix(frontend): fix missing font-face (#2833)

* feat(cli): refactor list formatter for better resource manager support (#2829)

* feat(frontend): add independent trace vs test data (#2815)

* feat(cli): refactor Get formatter for better resource manager support (#2831)

* feat(cli): dynamic list of available resources (#2832)

* feat(cli): refactor Delete to new resource manager client (#2836)

* feat(cli): update export command with new resourcemanager client (#2838)

* Standardize old resources to to use list method with SQL Injection protection and stardardize PollingProfile file (#2839)

* wip: update list on demo and environment to use the same standards as tests and transactions

* Fixing provisioning test

* Update provisioning test

* feature(frontend): Contact Us Modal (#2835)

* feature(frontend): Contact Us Modal

* feature(frontend): Contact Us Modal

* feat(cli): update apply command with new resourcemanager client (#2841)

* feat(frontend): trace vs test data for timeline and attribute list (#2837)

* feat: new tests openapi spec (#2658)

update get tests endpoint to use resource manager

* fix: move transactions to it's own module (#2664)

* fix: move transactions to the transactions folder

* remove alias to transactions module

* feat: test models + list tests endpoint (#2681)

* add test, run, and trigger objects

* feat: move test and run structs into the test package

* list tests

* add old format attributes again

* fix mapping

* compatibility mode

* fix server prefix integration test

* force server unit tests to run

* fix tracetest test

* trigger CI

* Apply suggestions from code review

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* fix build

* version test specs

* update test names

* use sqlutils

* dont specify branch target in pull_request

* fix list test

---------

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* 2659 cli improvements migrate tests to resource manager architecture get tests frontend (#2702)

chore(frontend): updating FE to support GET /tests endpoint changes

* feat: test list augmented response (#2732)

add augmented list

* fix(server): fix trigger json encoding versioning and test (#2795)

* feat: test models + list tests endpoint (#2681)

* add test, run, and trigger objects

* feat: move test and run structs into the test package

* list tests

* add old format attributes again

* fix mapping

* compatibility mode

* fix server prefix integration test

* force server unit tests to run

* fix tracetest test

* trigger CI

* Apply suggestions from code review

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* fix build

* version test specs

* update test names

* use sqlutils

* dont specify branch target in pull_request

* fix list test

---------

Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>

* GET endpoint

* fix rebase

* add create method and fix tests

* add rest of methods

* Changing test structure and fixing method by method

* Update tests

* Fixing test errors for Delete

* Adding gRPC tests

* Adding tests for traceid trigger

* migrate rest of endpoints and fix compilation errors

* fix all unit and integration tests

* Improving tests

* fix building errors

---------

Co-authored-by: Oscar Reyes <oscar-rreyes1@hotmail.com>
Co-authored-by: Jorge Padilla <jorge.esteban.padilla@gmail.com>
Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>
Co-authored-by: Daniel Baptista Dias <danielbdias@users.noreply.github.com>
Co-authored-by: Daniel Dias <danielbpdias@gmail.com>

* fix a couple of issues

* fix wrong variable name

* fix part of the tracetests

* Fixing small bugs on server

* Fixing test run problem

* fix: tracetests (#2852)

fix grpc tests and transaction tracetests

* fixing FE tests and types

* Updating test assertion validation

* Tests cli resourcemanager client (#2849)

* fixing FE tests and updating models

* fixing FE tests and updating models

* Updating trigger mapping to populate httpRequest field on OpenAPI based endpoints

* Removing deprecated fields

* fix trigger type in fe

* add cli e2e test for test resource (#2854)

* Fixed bug in dogfooding test

* Making backend-arch-graph step not block CI on failure

* fix trigger result type in fe

* Deprecate commands (#2857)

* Move test deprecated notice to subcommands

* Update CLI e2e docs

* fix(docs): fix deprecated test command (#2874)

---------

Co-authored-by: Matheus Nogueira <matheus.nogueira2008@gmail.com>
Co-authored-by: Sebastian Choren <schoren@users.noreply.github.com>
Co-authored-by: Jorge Padilla <jorge.esteban.padilla@gmail.com>
Co-authored-by: Daniel Baptista Dias <danielbdias@users.noreply.github.com>
Co-authored-by: Daniel Dias <danielbpdias@gmail.com>
Co-authored-by: Sebastian Choren <sebastian.choren@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants