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

fix environment migration #2534

Merged
merged 9 commits into from
May 12, 2023
Merged

fix environment migration #2534

merged 9 commits into from
May 12, 2023

Conversation

mathnogueira
Copy link
Member

This PR finishes migrating the environment from the old api endpoints to the new resource format.

Checklist

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of having an Environment2, can we kill this model? By doing that we are able to see if we migrated all the env stuff too. Does it make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

Forgot to delete it haha

Copy link
Collaborator

@schoren schoren left a comment

Choose a reason for hiding this comment

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

Left a few suggestions, but this looks great! love how much code you killed

@@ -56,11 +59,19 @@ func (f EnvironmentsFormatter) ToStruct(file *file.File) (interface{}, error) {
var environmentResource openapi.EnvironmentResource
nullableEnvironment := openapi.NewNullableEnvironmentResource(&environmentResource)

if strings.HasSuffix(file.Path(), ".yaml") || strings.HasSuffix(file.Path(), ".yml") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this validation? Maybe we could try to parse the file, assuming that file definitions must be yaml, and throw an error if it's not parseable. Does that make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

is it guaranteed that the file will always be yaml?

Copy link
Member Author

Choose a reason for hiding this comment

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

A solution (I don't like it much) is to try to parse it as YAML, and then as JSON as a fallback:

func (f EnvironmentsFormatter) ToStruct(file *file.File) (interface{}, error) {
	var environmentResource openapi.EnvironmentResource
	nullableEnvironment := openapi.NewNullableEnvironmentResource(&environmentResource)

	if err := yaml.Unmarshal([]byte(file.Contents()), &environmentResource); err == nil {
		return environmentResource, nil
	}

	err := nullableEnvironment.UnmarshalJSON([]byte(file.Contents()))
	if err != nil {
		return nil, err
	}
	return environmentResource, nil
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

the original implementation fallbacks to json, and that is also not guaranteed. I think in the end its a product decision: do we want to support files in formats other than YAML?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could assume that it will be YAML and return an error message in case of a parsing error, to avoid having too much parsing logic or guessing on the CLI. Does it make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm considering the file ContentType. It's hardcoded as YAML. But I still left the json fallback there just in case we change that in the future

testDB model.Repository
runner runner
newTraceDBFn func(ds datastoreresource.DataStore) (tracedb.TraceDB, error)
environmentRepository environment.Repository
Copy link
Collaborator

Choose a reason for hiding this comment

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

you are only using the Get function, so maybe instead of depending on the repo directly, you could depend on a local interface:

type envGetter interface {
  Get(context.Context, id.ID) (environment.Environment, error) 
}

type controller struct {
  //...
  environmentRepository envGetter
}

that way if we need to pass a mock or any other substitution, we only need to implement that func and not the entire repo

@@ -22,6 +23,16 @@ type (
)

func (e Environment) Validate() error {
if e.Name == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️ validation being used

@@ -120,6 +121,7 @@ func setupController(t *testing.T) controllerFixture {
nil,
nil,
mappings.New(traces.NewConversionConfig(), comparator.DefaultRegistry(), mdb),
environment.NewRepository(nil),
Copy link
Collaborator

@schoren schoren May 12, 2023

Choose a reason for hiding this comment

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

referecing my previous comment, this could also be just nil instead of an invalid repo, which reads nicer I think (personal opinion)

@@ -32,15 +32,6 @@ type RunRepository interface {
GetLatestRunByTestVersion(context.Context, id.ID, int) (Run, error)
}

type EnvironmentRepository interface {
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️

@mathnogueira mathnogueira merged commit 8b373ac into main May 12, 2023
@mathnogueira mathnogueira deleted the feat/migrate-env-cli branch May 12, 2023 19:53
schoren pushed a commit that referenced this pull request Jun 5, 2023
* migrate environment usage to use new resource structure

* fix env apply and add test to server env model

* fix stuff

* fix tests

* delete old environment struct

* PR suggestions

* remove json fallback

* use pointer to repository instead of passing by value

* fix test
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

3 participants