Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Allow multiple SAP system tags per host #160

Merged
merged 4 commits into from Jul 27, 2021

Conversation

rtorrero
Copy link
Contributor

@rtorrero rtorrero commented Jul 21, 2021

This PR adds support for having multiple SIDs on the same host (useful in cost-optimized scenario). The host metadata field is now trento-sap-systems and the filters have been updated to allow matching e.g. PRD in a host where PRD,QAS is stored in the metadata.

Templates have been updated too to now show multiple SIDs where relevant.

Updated: Screenshots
hostdetails
hosts

@arbulu89
Copy link
Contributor

@rtorrero I would appreciate some screenshots of the changed templates of the frontend

@rtorrero
Copy link
Contributor Author

@rtorrero I would appreciate some screenshots of the changed templates of the frontend

yes! I wanted to post some but my test environment is broken right now. Deploying a new one and should be able to add them soon

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.

Hi @rtorrero ,
Many comments hehe
In general the implementation looks good, but I think we can remove and re-order many of the new changes.
Finally, as added in independent comment, it would be nice to have a screenshot/gif of the final result

@@ -33,33 +33,49 @@ func (d SAPSystemsDiscovery) Discover() error {
return err
}

sapSystems := make([]*sapsystem.SAPSystem, len(systems))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need a new variable sapSystems to store this information?
This looks like a replica of the systems/d.SAPSystems variable just changing the map to list format

(systems is of sapsystem.SAPSystemsList, which is already a list)

envName, landName, sysName, err := loadSAPSystemTags(client, system.SID)
if err != nil {
return err
func storeSAPSystemTags(client consul.Client, systems []*sapsystem.SAPSystem) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

As commented above, I think we could use sapsystem.SAPSystemsList as systems type

var envName, landName string
sysNames := ""
var err error
for i, system := range systems {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks a bit over complicated.
I suggest the next:
As envName and landName will be the same independtly which system.SID we use, we could just call it once with the 1st SID.
After that, i would prefer the usage of strings.Join to create the sids string. I don't know if we could use some kind of lamdba method to get the SID, otherwise we will need to loop though all the systems, create a SID string array, and apply the Join function

agent/discovery/sapsystem.go Show resolved Hide resolved
internal/hosts/hosts.go Outdated Show resolved Hide resolved
return nil, err
}

systemsList := make(sapsystem.SAPSystemsList, len(systems))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have this loop? Cannot we use directly the systems map? At the end they have the same content

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 assume that by "using directly the systems map you mean using the GetSAPSystems method?
The thing is that if I use GetSAPSystems in the template, I can't easily add a comma after each SID but the last one (since in a string map, each key is not an int but a string, so I can't easily figure out the len-1 case :/... It might be that I'm not familiar enough with templates. A bit more details if you can think of a way of using the string map in the template would help

Copy link
Contributor

Choose a reason for hiding this comment

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

As we just need to split the string by the ,, why don't we just add the Split method to our templating system, so we can use it?
Here an example: https://stackoverflow.com/questions/19771787/golang-how-to-split-string-in-template
We would need to add the func here: https://github.com/trento-project/trento/blob/main/web/layout.go#L142

I see that you need this already 2 times, so it would make sense

internal/hosts/hosts.go Outdated Show resolved Hide resolved
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.

@rtorrero Some new comments

internal/sapsystem/sapsystem.go Outdated Show resolved Hide resolved
return nil, err
}

systemsList := make(sapsystem.SAPSystemsList, len(systems))
Copy link
Contributor

Choose a reason for hiding this comment

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

As we just need to split the string by the ,, why don't we just add the Split method to our templating system, so we can use it?
Here an example: https://stackoverflow.com/questions/19771787/golang-how-to-split-string-in-template
We would need to add the func here: https://github.com/trento-project/trento/blob/main/web/layout.go#L142

I see that you need this already 2 times, so it would make sense

@rtorrero
Copy link
Contributor Author

rtorrero commented Jul 26, 2021

I think I've addressed all pending conversations; but be sure to take another look please @arbulu89

I'm aware of the repetition of code that happens between the two GetSIDsString methods in SAPSystemsList and SAPSystemsMap. I explored the possibility of using reflection to avoid this but in the end, the result is probably worse in the sense that it makes the code way less readable and doesn't save too many lines anyways. Check it out here in any case if you have doubts about it.

I've also added the missing screenshots now that my test env is working fine again

@rtorrero rtorrero requested a review from arbulu89 July 26, 2021 19:02
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.

Hi @rtorrero ,
More comments

func storeSAPSystemTags(client consul.Client, system *sapsystem.SAPSystem) error {
envName, landName, sysName, err := loadSAPSystemTags(client, system.SID)
func storeSAPSystemTags(client consul.Client, systems sapsystem.SAPSystemsList) error {
envName, landName, _, err := loadSAPSystemTags(client, systems[0].SID)
Copy link
Contributor

Choose a reason for hiding this comment

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

The loadSAPSystemTags is only used here, so we could even remove this 3rd return value (which is not used)

@@ -107,6 +107,12 @@ func (l *Landscape) AddSystem(system *SAPSystem) {
l.SAPSystems[system.Name] = system
}

func (l *Landscape) AddSystems(systems []*SAPSystem) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This new method is not used anywhere. I think you created this to replace the loop used above maybe, at line 62 of agent/discovery/sapsystem.go.
You could whether remove this function or use it above replacing the loop (you might need to change the incoming variable type)

web/layout.go Outdated
@@ -149,6 +150,9 @@ func (r *LayoutRender) addFileFromFS(templatesFS fs.FS, file string) {
return a + b
},
"markdown": markdownToHTML,
"split": func(s string, sep string) []string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you try just with "split": strings.Split here?

<dd class="inline"><a
href="/sapsystems/{{ $SAPSys }}?environment={{ $Env }}&landscape={{ $Land }}">{{ $SAPSys }}</a>
<dd class="inline">
{{- range $i, $v := split (.Host).GetSIDsString ","}}{{- if $i }},{{- end }} <a
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this .Host.GetSIDsSting function?
It will return the same value we have in the metadata, right?
I mean, why simply not: split (index .Host.TrentoMeta "trento-sap-systems) ","
And we get rid off GetSIDsString function from the Host struct

@@ -77,6 +78,26 @@ type CustomCommand func(name string, arg ...string) *exec.Cmd

var customExecCommand CustomCommand = exec.Command

func (sm SAPSystemsMap) GetSIDsString() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

This duplication looks horrible hehe
I'm sure you will be able to remove this 1st function once you have remove the .Host.GetSIDstring

@rtorrero
Copy link
Contributor Author

@arbulu89 should be fine now! thanks for the comments

@rtorrero rtorrero requested a review from arbulu89 July 27, 2021 11:40
arbulu89
arbulu89 previously approved these changes Jul 27, 2021
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.

Thanks @rtorrero ,
Now it looks great.
I have put some minor comments if you want to have a look

internal/sapsystem/sapsystem.go Outdated Show resolved Hide resolved
web/hosts_test.go Outdated Show resolved Hide resolved
web/hosts_test.go Outdated Show resolved Hide resolved
@rtorrero
Copy link
Contributor Author

Sorry again... fixed

@rtorrero rtorrero requested a review from arbulu89 July 27, 2021 12:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants