Skip to content

Commit

Permalink
Use a list of maps as there can be multiple hostnames per sid
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Oct 16, 2023
1 parent 4b09eea commit ef73390
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
21 changes: 12 additions & 9 deletions internal/factsengine/gatherers/saplocalhostresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func NewSapLocalhostResolver(fs afero.Fs, hr utils.HostnameResolver) *SapLocalho
func (r *SapLocalhostResolverGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) {
var facts []entities.Fact

rd, err := r.getInstanceHostnameDetails()
details, err := r.getInstanceHostnameDetails()
if err != nil {
log.Error(err)
return nil, SapLocalhostResolverHostnameResolutionError.Wrap(err.Error())
}

for _, factReq := range factsRequests {
var fact entities.Fact
factValue, err := mapReachabilityDetailsToFactValue(rd)
factValue, err := mapReachabilityDetailsToFactValue(details)
if err != nil {
log.Error(err)
fact = entities.NewFactGatheredWithError(factReq, SapLocalhostResolverGathererDecodingError.Wrap(err.Error()))
Expand All @@ -89,14 +89,13 @@ func (r *SapLocalhostResolverGatherer) Gather(factsRequests []entities.FactReque
return facts, nil
}

func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string]ResolvabilityDetails, error) {
func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string][]ResolvabilityDetails, error) {
systems, err := sapsystem.FindSystems(r.fs)
reachabilityDetails := make(map[string]ResolvabilityDetails)

if err != nil {
return nil, err
}

resolvabilityDetails := make(map[string][]ResolvabilityDetails)
for _, system := range systems {
sid := filepath.Base(system)
profileFiles, err := sapsystem.FindProfiles(r.fs, sid)
Expand All @@ -105,7 +104,7 @@ func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string]
}

for _, profileFile := range profileFiles {
if filepath.Base(profileFile) == "DEFAULT.PFL" {
if profileFile == "DEFAULT.PFL" {
continue
}

Expand All @@ -123,14 +122,18 @@ func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string]
Addresses: addresses,
InstanceName: match[2],
}
reachabilityDetails[match[1]] = details
if _, ok := resolvabilityDetails[match[1]]; !ok {
resolvabilityDetails[match[1]] = []ResolvabilityDetails{details}
} else {
resolvabilityDetails[match[1]] = append(resolvabilityDetails[match[1]], details)
}
}
}

return reachabilityDetails, nil
return resolvabilityDetails, nil
}

func mapReachabilityDetailsToFactValue(entries map[string]ResolvabilityDetails) (entities.FactValue, error) {
func mapReachabilityDetailsToFactValue(entries map[string][]ResolvabilityDetails) (entities.FactValue, error) {
marshalled, err := json.Marshal(&entries)
if err != nil {
return nil, err
Expand Down
18 changes: 11 additions & 7 deletions internal/factsengine/gatherers/saplocalhostresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverSuccess() {
CheckID: "check1",
Value: &entities.FactValueMap{
Value: map[string]entities.FactValue{
"QAS": &entities.FactValueMap{
Value: map[string]entities.FactValue{
"hostname": &entities.FactValueString{Value: "sapqasas"},
"addresses": &entities.FactValueList{
Value: []entities.FactValue{
&entities.FactValueString{Value: "10.1.1.5"},
"QAS": &entities.FactValueList{
Value: []entities.FactValue{
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"hostname": &entities.FactValueString{Value: "sapqasas"},
"addresses": &entities.FactValueList{
Value: []entities.FactValue{
&entities.FactValueString{Value: "10.1.1.5"},
},
},
"instance_name": &entities.FactValueString{Value: "ASCS00"},
},
},
"instance_name": &entities.FactValueString{Value: "ASCS00"},
},
},
},
Expand Down

0 comments on commit ef73390

Please sign in to comment.