diff --git a/internal/sapsystem/sapsystem.go b/internal/sapsystem/sapsystem.go index 0aed7bc7..1c27f8e6 100644 --- a/internal/sapsystem/sapsystem.go +++ b/internal/sapsystem/sapsystem.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "fmt" "io/ioutil" + "net" "os" "os/exec" "path" @@ -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 @@ -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) @@ -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 { diff --git a/internal/sapsystem/sapsystem_test.go b/internal/sapsystem/sapsystem_test.go index 42c0455b..04cd06ca 100644 --- a/internal/sapsystem/sapsystem_test.go +++ b/internal/sapsystem/sapsystem_test.go @@ -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) diff --git a/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_application.json b/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_application.json index 34c16185..8bbe3823 100644 --- a/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_application.json +++ b/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_application.json @@ -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", diff --git a/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_database.json b/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_database.json index 262694fc..0f63d683 100644 --- a/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_database.json +++ b/test/fixtures/discovery/sap_system/expected_published_sap_system_discovery_database.json @@ -6,6 +6,7 @@ "Id": "e06e328f8d6b0f46c1e66ffcd44d0dd7", "SID": "PRD", "Type": 1, + "DBAddress": "", "Profile": { "SAPGLOBALHOST": "vmhana01", "SAPSYSTEMNAME": "PRD", diff --git a/test/fixtures/discovery/sap_system/sap_system_discovery_application.json b/test/fixtures/discovery/sap_system/sap_system_discovery_application.json index 547a2685..9185d143 100644 --- a/test/fixtures/discovery/sap_system/sap_system_discovery_application.json +++ b/test/fixtures/discovery/sap_system/sap_system_discovery_application.json @@ -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", diff --git a/test/fixtures/discovery/sap_system/sap_system_discovery_database.json b/test/fixtures/discovery/sap_system/sap_system_discovery_database.json index 75cefbdb..23081bed 100644 --- a/test/fixtures/discovery/sap_system/sap_system_discovery_database.json +++ b/test/fixtures/discovery/sap_system/sap_system_discovery_database.json @@ -3,6 +3,7 @@ "Id": "e06e328f8d6b0f46c1e66ffcd44d0dd7", "SID": "PRD", "Type": 1, + "DBAddress": "", "Profile": { "SAPGLOBALHOST": "vmhana01", "SAPSYSTEMNAME": "PRD",