Skip to content

refactor Name() and ExtendedName(c) methods #865

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

Merged
merged 18 commits into from
Sep 29, 2024
Merged

refactor Name() and ExtendedName(c) methods #865

merged 18 commits into from
Sep 29, 2024

Conversation

olasaadi99
Copy link
Contributor

No description provided.

@olasaadi99 olasaadi99 requested a review from adisos September 10, 2024 09:06
@olasaadi99 olasaadi99 linked an issue Sep 10, 2024 that may be closed by this pull request
Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

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

added a comment with some questions.

@@ -21,6 +21,7 @@ const (
type VPCResourceIntf interface {
UID() string
Name() string
NameForAnalyzerOut() string
Copy link
Collaborator

Choose a reason for hiding this comment

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

still confusing/unclear functionality for Name / NameForAnalyzerOut / ExtendedName / ExtendedPrefix.

  • Does the interface need to have all these methods?
  • what is the use case for each method?
  • which methods are overriden for certain types? is this required? (cannot be implemented explicitly instead of changing expected behavior of certain method?)
  • is there a method that always returns the original resource name from the config object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • is there a method that always returns the original resource name from the config object?

Name()
consider renaming to resourceName() + doc

Does the interface need to have all these methods?

we need Name() and NameForAnalyzerOut , ExtendedName and ExtendedPrefix will check if we can merge it with NameForAnalyzerOut

@olasaadi99 olasaadi99 requested a review from adisos September 23, 2024 10:00
Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

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

looks good!
few more comments

Comment on lines 70 to 73
prefix := ""
if c != nil && c.IsMultipleVPCsConfig {
prefix = pip.VPC().Name() + vpcmodel.Deliminator
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this code seems to be duplicated across this file.. can be moved to some other internal function that gets config and VPC name as inputs?

return n.ExtendedPrefix(c) + n.Name()
func (n *VPCResource) NameForAnalyzerOut(c *VPCConfig) string {
prefix := ""
if c != nil && c.IsMultipleVPCsConfig {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe the config type should have a method that encapsulates this concept of required prefix?

@adisos
Copy link
Collaborator

adisos commented Sep 23, 2024

one more comment: can you revisit where NameForAnalyzerOut is called with nil and where not?
when is it required? when it can be assumed it is not needed?
are there places where the config can actually be retrieved but currently not used in the call? and if so, why?

@olasaadi99
Copy link
Contributor Author

one more comment: can you revisit where NameForAnalyzerOut is called with nil and where not? when is it required? when it can be assumed it is not needed? are there places where the config can actually be retrieved but currently not used in the call? and if so, why?

I assumed that it is not needed where we originally called the previous func without the config... and passed the config where we originaly called ExtendedName (with the config).

@adisos
Copy link
Collaborator

adisos commented Sep 24, 2024

one more comment: can you revisit where NameForAnalyzerOut is called with nil and where not? when is it required? when it can be assumed it is not needed? are there places where the config can actually be retrieved but currently not used in the call? and if so, why?

I assumed that it is not needed where we originally called the previous func without the config... and passed the config where we originaly called ExtendedName (with the config).

makes sense, but if you can revisit as suggested above this may help to better understand these aspects, update documentation as to where this is expected to be used, etc.. (maybe also find if it makes sense to add the config in other places, to be less error-prone).

when should call NameForAnalyzerOut with config arg (not nil)?
debug messages are also part of "output".. where a more accurate string name is provided.
I think it is best to add where possible, for both consistency and accuracy.

Comment on lines 42 to 43
if c != nil {
prefix = c.MultipleVPCsConfigPrefix(r.VPC().Name())
Copy link
Collaborator

Choose a reason for hiding this comment

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

can add another internal function that gets as input arg the config object and returns that prefix, and here always just have prefix + nameWithBracketsInfo(r.vpe, r.Address())
(to avoid this duplication across this file as well) .

@@ -44,6 +44,14 @@ type VPCConfig struct {
IsMultipleVPCsConfig bool
}

// MultipleVPCsConfigPrefix returns the passed prefix when config is multi-vpc
func (c *VPCConfig) MultipleVPCsConfigPrefix(prefix string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

change arg name to vpcName ?


// MultipleVPCsConfigPrefix returns the passed vpcName when config is multi-vpc
func MultipleVPCsConfigPrefix(c *vpcmodel.VPCConfig, vpcName string) string {
if c != nil && c.IsMultipleVPCsConfig {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if c != nil && c.IsMultipleVPCsConfig {
if c != nil {

Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

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

looks good.
There are more cases with NameForAnalyzerOut(nil) is used. Do you have insights about those cases?


// MultipleVPCsConfigPrefix returns the vpcName of the passed resource when config is multi-vpc
func MultipleVPCsConfigPrefix(c *vpcmodel.VPCConfig, resource *vpcmodel.VPCResource) string {
if c != nil && resource.VPC() != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

when is resource.VPC() expected to be nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got some tests failed before I added this condition, it seems like there are some cases we do not init config file in tests

Copy link
Collaborator

Choose a reason for hiding this comment

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

only unit tests or end-to-end tests?
this is not about initializing config file, but having the resource a pointer to the VPC it is contained in.

@olasaadi99
Copy link
Contributor Author

looks good. There are more cases with NameForAnalyzerOut(nil) is used. Do you have insights about those cases?

in these cases the config param is not reachable, or that we need to make a lot of changes

@adisos
Copy link
Collaborator

adisos commented Sep 25, 2024

looks good. There are more cases with NameForAnalyzerOut(nil) is used. Do you have insights about those cases?

in these cases the config param is not reachable, or that we need to make a lot of changes

@ShiriMoran can you review the places where NameForAnalyzerOut(nil) is now used in this PR, and whether it is safe to keep these calls, or whether the full name (with vpc prefix for multi-vpc cases) should be used instead? (such as explainability output)

@adisos adisos requested a review from ShiriMoran September 25, 2024 13:45
return n.ExtendedPrefix(c) + n.Name()
func (n *VPCResource) NameForAnalyzerOut(c *VPCConfig) string {
prefix := ""
if c != nil && n.VPC() != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

if c != nil, can n.VPC() be nil?

@ShiriMoran
Copy link
Contributor

ShiriMoran commented Sep 29, 2024

@ShiriMoran can you review the places where NameForAnalyzerOut(nil) is now used in this PR, and whether it is safe to keep these calls, or whether the full name (with vpc prefix for multi-vpc cases) should be used instead? (such as explainability output)

  1. Syntactic and backwards compatibility wise, it is safe. Namely, all the places in which NameForAnalyzerOut(nil) are places in which Name() was used before
  2. There are places in which Name() was used before and NameForAnalyzerOut(c) is called now, which is good
  3. This leaves us in some mode of confusion, in which there seems to be no rule, or one that makes sense for a user, as to when we avoid the relevant VPC prefix
  4. The above rule is "places where before we used Name() in functions that do not have the VPC config". I think we should avoid this as much as possible, namely avoid calling NameForAnalyzerOut with nil.
  5. Thus, we should go over all the cases in which NameForAnalyzerOut(nil) is called and try to replace it with NameForAnalyzerOut(c); this will require adding vpc config parm to the relevant functions. This can be done in a followup PR @adisos @olasaadi99 (or in this one, what ever you prefer)

@adisos
Copy link
Collaborator

adisos commented Sep 29, 2024

@ShiriMoran can you review the places where NameForAnalyzerOut(nil) is now used in this PR, and whether it is safe to keep these calls, or whether the full name (with vpc prefix for multi-vpc cases) should be used instead? (such as explainability output)

1. Syntactic and backwards compatibility wise, it is safe. Namely, all the places in which `NameForAnalyzerOut(nil)` are places in which `Name()` was used before

2. There are places in which  `Name()` was used before and `NameForAnalyzerOut(c)` is called now, which is good

3. This leaves us in some mode of confusion, in which there seems to be no rule, or one that makes sense for a user, as to when we avoid the relevant `VPC` prefix

4. The above rule is "places where before we used `Name()` in functions that do not have the `VPC config`". I think we should avoid this as much as possible, namely avoid calling `NameForAnalyzerOut` with `nil`.

5. Thus, we should go over all the cases in which  `NameForAnalyzerOut(nil)` is called and try to replace it with  `NameForAnalyzerOut(c)`; this will require adding `vpc config` parm to the relevant functions. This can be done in a followup PR @adisos @olasaadi99 (or in this one, what ever you prefer)

Thanks @ShiriMoran , sounds good. I think so as well.
@olasaadi99 please check if you prefer a separate PR for this.

@olasaadi99
Copy link
Contributor Author

olasaadi99 commented Sep 29, 2024

thanks
prefer new pr
#892

@olasaadi99 olasaadi99 merged commit 7497375 into main Sep 29, 2024
4 checks passed
@olasaadi99 olasaadi99 deleted the 590_refactor branch September 29, 2024 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor methods for VPC resource names interfaces
3 participants