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

[scd] factor out parameter validation for oir upsert method #1089

Merged

Conversation

Shastick
Copy link
Contributor

@Shastick Shastick commented Aug 30, 2024

A first small step towards #1088.

This takes care of moving the parameter validation to a submethod. It is not yet improving anything with respect to the core business logic, but allows readers to more directly focus on the code of what upsertOperationalIntentReference does.

Note about the usage of the stacktrace library and our dependency on error codes: as we use the error codes to decide what HTTP response to send back, it is important that we properly propagate them when we move logic one or several layers of method calls.

@Shastick Shastick force-pushed the oir-upsert-refactor-0-param-validation branch from bc92dcc to 8524493 Compare August 30, 2024 10:05
@Shastick Shastick marked this pull request as ready for review August 30, 2024 10:05
Copy link
Contributor

@mickmis mickmis left a comment

Choose a reason for hiding this comment

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

Nice, thanks! That's indeed a good first step in cutting up this function. With this construct it will also be easy when it makes sense to start factoring away some logic into functions of validOIRUpsertParams
I just have some minor comments.

subscriptionID dssmodels.ID
}

func (a *Server) validateAndReturnUpsertParams(
Copy link
Contributor

Choose a reason for hiding this comment

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

Pass AllowHTTPBaseUrls as a param to avoid attaching this to Server?

Comment on lines 483 to 491
upsertParams, err := a.validateAndReturnUpsertParams(authorizedManager, entityid, ovn, params)
if err != nil {
// Important note:
// - stacktrace.Propagate sets NoCode as the code for the error
// - subsequently, stacktrace.GetCode(err) only looks into the first error in the stack and returns NoCode if the error has no code
// - therefore, we need to explicitly use stacktrace.PropagateWithCode if we want the calling logic
// to be able to retrieve the code of the error
// TODO we can consider fixing this in the stacktrace library
return nil, nil, stacktrace.PropagateWithCode(err, stacktrace.GetCode(err), "Failed to validate Operational Intent Reference upsert parameters")
Copy link
Contributor

Choose a reason for hiding this comment

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

Here I suggest making validateAndReturnUpsertParams purely a validation function by:

  • move out of validateAndReturnUpsertParams the two cases of errors dsserr.PermissionDenied
  • never returning any code within validateAndReturnUpsertParams
  • if err != nil set the code dsserr.BadRequest

That makes it clear this function validates the input to create the op intent and any error implies bad input. That should also enable passing the manager instead of the whole authorizedManager.

@Shastick Shastick force-pushed the oir-upsert-refactor-0-param-validation branch 4 times, most recently from eb4a8b5 to 9030146 Compare August 30, 2024 11:22
@Shastick Shastick force-pushed the oir-upsert-refactor-0-param-validation branch from 9030146 to f041be8 Compare August 30, 2024 11:23
@Shastick Shastick requested a review from mickmis August 30, 2024 11:24
Copy link
Contributor

@mickmis mickmis left a comment

Choose a reason for hiding this comment

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

LGTM - thanks

@mickmis mickmis merged commit 3ee9054 into interuss:master Aug 30, 2024
6 checks passed
@mickmis mickmis deleted the oir-upsert-refactor-0-param-validation branch August 30, 2024 11:31
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.

2 participants