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

Load HANA database IP address in agent side #20

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion internal/sapsystem/sapsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -57,6 +58,8 @@ type SAPSystem struct {
Instances map[string]*SAPInstance `mapstructure:"instances,omitempty"`
// Only for Database type
Databases []*DatabaseData `mapstructure:"databases,omitempty"`
// Only for Application type
DBAddress string `mapstructure:"db_address,omitempty"`
}

// The value is interface{} as some of the entries in the SAP profiles files and commands
Expand Down Expand Up @@ -226,13 +229,21 @@ func NewSAPSystem(fs afero.Fs, sysPath string) (*SAPSystem, error) {
system.Instances[instance.Name] = instance
}

if system.Type == Database {
switch system.Type {
case Database:
databaseList, err := getDatabases(fs, system.SID)
if err != nil {
log.Printf("Error getting the database list: %s", err)
} else {
system.Databases = databaseList
}
case Application:
addr, err := getDBAddress(system)
if err != nil {
log.Printf("Error getting the database address: %s", err)
} else {
system.DBAddress = addr
}
}

system, err = setSystemId(fs, system)
Expand Down Expand Up @@ -315,6 +326,29 @@ func getProfileData(fs afero.Fs, profilePath string) (map[string]interface{}, er
return configMap, nil
}

func getDBAddress(system *SAPSystem) (string, error) {
sapdbhost, found := system.Profile["SAPDBHOST"]
if !found {
return "", fmt.Errorf("SAPDBHOST field not found in the SAP profile")
}

addrList, err := net.LookupIP(sapdbhost.(string))
if err != nil {
return "", fmt.Errorf("could not resolve \"%s\" hostname", sapdbhost)
}

// Get 1st IPv4 address
for _, addr := range addrList {
addrStr := addr.String()
ip := net.ParseIP(addrStr)
if ip.To4() != nil {
return addrStr, nil
}
}

return "", fmt.Errorf("could not get any IPv4 address")
}

func setSystemId(fs afero.Fs, system *SAPSystem) (*SAPSystem, error) {
// Set system ID
switch system.Type {
Expand Down
13 changes: 13 additions & 0 deletions internal/sapsystem/sapsystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ ERR:::
assert.ElementsMatch(t, expectedDbs, dbs)
}

func TestGetDBAddress(t *testing.T) {
s := &SAPSystem{Profile: SAPProfile{"SAPDBHOST": "localhost"}}
addr, err := getDBAddress(s)
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", addr)
}

func TestGetDBAddress_ResolveError(t *testing.T) {
s := &SAPSystem{Profile: SAPProfile{"SAPDBHOST": "other"}}
_, err := getDBAddress(s)
assert.EqualError(t, err, "could not resolve \"other\" hostname")
}

func TestNewSAPInstanceDatabase(t *testing.T) {
mockWebService := new(sapControlMocks.WebService)
mockCommand := new(sapSystemMocks.CustomCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "7b65dc281f9fae2c8e68e6cab669993e",
"SID": "HA1",
"Type": 2,
"DBAddress": "10.74.1.12",
"Profile": {
"SAPDBHOST": "10.74.1.12",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "e06e328f8d6b0f46c1e66ffcd44d0dd7",
"SID": "PRD",
"Type": 1,
"DBAddress": "",
"Profile": {
"SAPGLOBALHOST": "vmhana01",
"SAPSYSTEMNAME": "PRD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Id": "7b65dc281f9fae2c8e68e6cab669993e",
"SID": "HA1",
"Type": 2,
"DBAddress": "10.74.1.12",
"Profile": {
"SAPDBHOST": "10.74.1.12",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Id": "e06e328f8d6b0f46c1e66ffcd44d0dd7",
"SID": "PRD",
"Type": 1,
"DBAddress": "",
"Profile": {
"SAPGLOBALHOST": "vmhana01",
"SAPSYSTEMNAME": "PRD",
Expand Down