diff --git a/internal/sapsystem/sapsystem.go b/internal/sapsystem/sapsystem.go index cd846d225..00aea8093 100644 --- a/internal/sapsystem/sapsystem.go +++ b/internal/sapsystem/sapsystem.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io/ioutil" + "net" "os" "os/exec" "path" @@ -63,6 +64,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 @@ -202,13 +205,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) @@ -291,6 +302,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 var err error diff --git a/internal/sapsystem/sapsystem_test.go b/internal/sapsystem/sapsystem_test.go index a37cb0de8..9b44f0e11 100644 --- a/internal/sapsystem/sapsystem_test.go +++ b/internal/sapsystem/sapsystem_test.go @@ -273,6 +273,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/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/098fc159-3ed6-58e7-91be-38fda8a833ea_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/098fc159-3ed6-58e7-91be-38fda8a833ea_sap_system_discovery.json index dc522ce51..75b8597ba 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/098fc159-3ed6-58e7-91be-38fda8a833ea_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/098fc159-3ed6-58e7-91be-38fda8a833ea_sap_system_discovery.json @@ -6,8 +6,9 @@ "Id": "d01fdc69aeba7bd5133b210eb2884853", "SID": "NWQ", "Type": 2, + "DBAddress": "10.90.1.13", "Profile": { - "SAPDBHOST": "10.90.1.13", + "SAPDBHOST": "ha-cluster.suse.de", "dbms/name": "HDQ", "dbms/type": "hdb", "gw/acl_mode": "1", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/0fc07435-7ee2-54ca-b0de-fb27ffdc5deb_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/0fc07435-7ee2-54ca-b0de-fb27ffdc5deb_sap_system_discovery.json index a19b4d63e..80332f3c4 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/0fc07435-7ee2-54ca-b0de-fb27ffdc5deb_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/0fc07435-7ee2-54ca-b0de-fb27ffdc5deb_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "97a1e70aeff3c0685d65c4c3d32d533b", "SID": "NWP", "Type": 2, + "DBAddress": "10.80.1.13", "Profile": { "SAPDBHOST": "10.80.1.13", "dbms/name": "HDP", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/116d49bd-85e1-5e59-b820-83f66db8800c_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/116d49bd-85e1-5e59-b820-83f66db8800c_sap_system_discovery.json index 4f3132a4c..ed17786d9 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/116d49bd-85e1-5e59-b820-83f66db8800c_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/116d49bd-85e1-5e59-b820-83f66db8800c_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "97a1e70aeff3c0685d65c4c3d32d533b", "SID": "NWP", "Type": 2, + "DBAddress": "10.80.1.13", "Profile": { "SAPDBHOST": "10.80.1.13", "dbms/name": "HDP", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery.json index fe7d439b5..6938a0cf8 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/25677e37-fd33-5005-896c-9275b1284534_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/25677e37-fd33-5005-896c-9275b1284534_sap_system_discovery.json index 81b5eaf6d..f4c857dbd 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/25677e37-fd33-5005-896c-9275b1284534_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/25677e37-fd33-5005-896c-9275b1284534_sap_system_discovery.json @@ -6,8 +6,9 @@ "Id": "d01fdc69aeba7bd5133b210eb2884853", "SID": "NWQ", "Type": 2, + "DBAddress": "10.90.1.13", "Profile": { - "SAPDBHOST": "10.90.1.13", + "SAPDBHOST": "ha-cluster.suse.de", "dbms/name": "HDQ", "dbms/type": "hdb", "gw/acl_mode": "1", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/3711ea88-9ccc-5b07-8f9d-042be449d72b_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/3711ea88-9ccc-5b07-8f9d-042be449d72b_sap_system_discovery.json index 3daa4effc..f7b395678 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/3711ea88-9ccc-5b07-8f9d-042be449d72b_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/3711ea88-9ccc-5b07-8f9d-042be449d72b_sap_system_discovery.json @@ -6,8 +6,9 @@ "Id": "d01fdc69aeba7bd5133b210eb2884853", "SID": "NWQ", "Type": 2, + "DBAddress": "10.90.1.13", "Profile": { - "SAPDBHOST": "10.90.1.13", + "SAPDBHOST": "ha-cluster.suse.de", "dbms/name": "HDQ", "dbms/type": "hdb", "gw/acl_mode": "1", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/4b30a6af-4b52-5bda-bccb-f2248a12c992_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/4b30a6af-4b52-5bda-bccb-f2248a12c992_sap_system_discovery.json index 2bcff1fdf..777621204 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/4b30a6af-4b52-5bda-bccb-f2248a12c992_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/4b30a6af-4b52-5bda-bccb-f2248a12c992_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "97a1e70aeff3c0685d65c4c3d32d533b", "SID": "NWP", "Type": 2, + "DBAddress": "10.80.1.13", "Profile": { "SAPDBHOST": "10.80.1.13", "dbms/name": "HDP", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery.json index a7c71d39e..525ab8355 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/81e9b629-c1e7-538f-bff1-47d3a6580522_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/81e9b629-c1e7-538f-bff1-47d3a6580522_sap_system_discovery.json index 82a67a3f9..b7c39e942 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/81e9b629-c1e7-538f-bff1-47d3a6580522_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/81e9b629-c1e7-538f-bff1-47d3a6580522_sap_system_discovery.json @@ -6,8 +6,9 @@ "Id": "d01fdc69aeba7bd5133b210eb2884853", "SID": "NWQ", "Type": 2, + "DBAddress": "10.90.1.13", "Profile": { - "SAPDBHOST": "10.90.1.13", + "SAPDBHOST": "ha-cluster.suse.de", "dbms/name": "HDQ", "dbms/type": "hdb", "gw/acl_mode": "1", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery.json index d539533be..733508b54 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/a3297d85-5e8b-5ac5-b8a3-55eebc2b8d12_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/a3297d85-5e8b-5ac5-b8a3-55eebc2b8d12_sap_system_discovery.json index 86bc94930..aa94490d4 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/a3297d85-5e8b-5ac5-b8a3-55eebc2b8d12_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/a3297d85-5e8b-5ac5-b8a3-55eebc2b8d12_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "97a1e70aeff3c0685d65c4c3d32d533b", "SID": "NWP", "Type": 2, + "DBAddress": "10.80.1.13", "Profile": { "SAPDBHOST": "10.80.1.13", "dbms/name": "HDP", diff --git a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/fb2c6b8a-9915-5969-a6b7-8b5a42de1971_sap_system_discovery.json b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/fb2c6b8a-9915-5969-a6b7-8b5a42de1971_sap_system_discovery.json index 9914045b1..b0674f46b 100644 --- a/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/fb2c6b8a-9915-5969-a6b7-8b5a42de1971_sap_system_discovery.json +++ b/test/e2e/cypress/fixtures/healthy-27-node-SAP-cluster/fb2c6b8a-9915-5969-a6b7-8b5a42de1971_sap_system_discovery.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GRAY.json b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GRAY.json index 8dda710fa..3981c5bfc 100644 --- a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GRAY.json +++ b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GRAY.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GREEN.json b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GREEN.json index b0a6a72e6..7a2ec91d4 100644 --- a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GREEN.json +++ b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_GREEN.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json index d9e2cdb8e..d63211c3c 100644 --- a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json +++ b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_YELLOW.json b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_YELLOW.json index a0d0a3d63..1b676cb69 100644 --- a/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_YELLOW.json +++ b/test/e2e/cypress/fixtures/sap-system-details/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_YELLOW.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-system-details/newagent_sap_system_discovery_new.json b/test/e2e/cypress/fixtures/sap-system-details/newagent_sap_system_discovery_new.json index 2e26e0c50..70154b1dc 100644 --- a/test/e2e/cypress/fixtures/sap-system-details/newagent_sap_system_discovery_new.json +++ b/test/e2e/cypress/fixtures/sap-system-details/newagent_sap_system_discovery_new.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-systems-overview/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json b/test/e2e/cypress/fixtures/sap-systems-overview/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json index 5a0450669..84ac90134 100644 --- a/test/e2e/cypress/fixtures/sap-systems-overview/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json +++ b/test/e2e/cypress/fixtures/sap-systems-overview/1b0e9297-97dd-55d6-9874-8efde4d84c90_sap_system_discovery_RED.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-systems-overview/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery_GRAY.json b/test/e2e/cypress/fixtures/sap-systems-overview/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery_GRAY.json index abae51060..45b15063f 100644 --- a/test/e2e/cypress/fixtures/sap-systems-overview/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery_GRAY.json +++ b/test/e2e/cypress/fixtures/sap-systems-overview/7269ee51-5007-5849-aaa7-7c4a98b0c9ce_sap_system_discovery_GRAY.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-systems-overview/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery_YELLOW.json b/test/e2e/cypress/fixtures/sap-systems-overview/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery_YELLOW.json index 38f2977bc..b89256512 100644 --- a/test/e2e/cypress/fixtures/sap-systems-overview/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery_YELLOW.json +++ b/test/e2e/cypress/fixtures/sap-systems-overview/9a3ec76a-dd4f-5013-9cf0-5eb4cf89898f_sap_system_discovery_YELLOW.json @@ -6,6 +6,7 @@ "Id": "a1e80e3e152a903662f7882fb3f8a851", "SID": "NWD", "Type": 2, + "DBAddress": "10.100.1.13", "Profile": { "SAPDBHOST": "10.100.1.13", "dbms/name": "HDD", diff --git a/test/e2e/cypress/fixtures/sap-systems-overview/available_sap_systems.js b/test/e2e/cypress/fixtures/sap-systems-overview/available_sap_systems.js index 96321f306..cf0498975 100644 --- a/test/e2e/cypress/fixtures/sap-systems-overview/available_sap_systems.js +++ b/test/e2e/cypress/fixtures/sap-systems-overview/available_sap_systems.js @@ -189,7 +189,7 @@ export const availableSAPSystems = [ sid: 'HDQ', id: '9953878f07bb54cac20d5d5d7ff08af2', tenant: 'HDQ', - dbAddress: '10.90.1.13', + dbAddress: 'ha-cluster.suse.de', }, instances: [ { 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 34c161858..8bbe38231 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 262694fcc..0f63d6835 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 547a26855..9185d1436 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 75cefbdb9..23081bed9 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", diff --git a/test/fixtures/discovery/sap_system/sap_system_discovery_diagnostics.json b/test/fixtures/discovery/sap_system/sap_system_discovery_diagnostics.json index 443fa9aac..ca6bd29fe 100644 --- a/test/fixtures/discovery/sap_system/sap_system_discovery_diagnostics.json +++ b/test/fixtures/discovery/sap_system/sap_system_discovery_diagnostics.json @@ -5,6 +5,7 @@ "Type": 3, "Profile": {}, "Databases": null, + "DBAddress": "", "Instances": { "SMDA98": { "Host": "vmhana01", diff --git a/web/datapipeline/sap_systems_projector.go b/web/datapipeline/sap_systems_projector.go index 0ec378eb0..394c6381a 100644 --- a/web/datapipeline/sap_systems_projector.go +++ b/web/datapipeline/sap_systems_projector.go @@ -36,7 +36,7 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl } for _, s := range discoveredSAPSystems { - var sapSystemType, dbHost, dbName string + var sapSystemType, dbHost, dbName, dbAddress string var tenants []string switch s.Type { @@ -52,6 +52,7 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl if hdb, ok := s.Profile["dbs/hdb/dbname"]; ok { dbName = hdb.(string) } + dbAddress = s.DBAddress case 3: log.Infof("SAP diagnostics agent with %s identifier found. Skipping projection", s.SID) continue @@ -60,13 +61,14 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl var instances []entities.SAPSystemInstance for _, i := range s.Instances { instance := entities.SAPSystemInstance{ - AgentID: dataCollectedEvent.AgentID, - ID: s.Id, - SID: s.SID, - Type: sapSystemType, - Tenants: tenants, - DBHost: dbHost, - DBName: dbName, + AgentID: dataCollectedEvent.AgentID, + ID: s.Id, + SID: s.SID, + Type: sapSystemType, + Tenants: tenants, + DBHost: dbHost, + DBName: dbName, + DBAddress: dbAddress, } var features string @@ -100,7 +102,7 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl "id", "sid", "type", "features", "instance_number", "system_replication", "system_replication_status", "sap_hostname", "start_priority", "http_port", "https_port", "status", - "tenants", "db_host", "db_name") + "tenants", "db_host", "db_name", "db_address") if err != nil { return err } diff --git a/web/entities/sap_system.go b/web/entities/sap_system.go index 36e598311..e0f354f39 100644 --- a/web/entities/sap_system.go +++ b/web/entities/sap_system.go @@ -25,6 +25,7 @@ type SAPSystemInstance struct { SystemReplicationStatus string DBHost string DBName string + DBAddress string Tenants pq.StringArray `gorm:"type:text[]"` Host *Host `gorm:"foreignKey:AgentID"` UpdatedAt time.Time @@ -52,12 +53,13 @@ func (s SAPSystemInstances) ToModel() []*models.SAPSystem { } sapSystem = &models.SAPSystem{ - ID: i.ID, - Type: i.Type, - SID: i.SID, - DBName: i.DBName, - DBHost: i.DBHost, - Tags: tags, + ID: i.ID, + Type: i.Type, + SID: i.SID, + DBName: i.DBName, + DBHost: i.DBHost, + DBAddress: i.DBAddress, + Tags: tags, } set[i.ID] = sapSystem } diff --git a/web/models/sap_system.go b/web/models/sap_system.go index f9cf0ebaf..6a8eb56a1 100644 --- a/web/models/sap_system.go +++ b/web/models/sap_system.go @@ -22,6 +22,7 @@ type SAPSystem struct { AttachedDatabase *SAPSystem DBName string DBHost string + DBAddress string Health string Tags []string // TODO: this is frontend specific, should be removed diff --git a/web/services/sap_systems.go b/web/services/sap_systems.go index 2af56d40f..1ff4a38d4 100644 --- a/web/services/sap_systems.go +++ b/web/services/sap_systems.go @@ -2,9 +2,11 @@ package services import ( "errors" + "fmt" "net" "github.com/lib/pq" + log "github.com/sirupsen/logrus" "github.com/trento-project/trento/web/entities" "github.com/trento-project/trento/web/models" "gorm.io/gorm" @@ -205,20 +207,20 @@ func (s *sapSystemsService) getAllByType(sapSystemType string, tagResourceType s return sapSystemList, nil } -func (s *sapSystemsService) getAttachedDatabase(dbName string, dbHost string) (*models.SAPSystem, error) { +func (s *sapSystemsService) getAttachedDatabase(dbName string, dbAddress string) (*models.SAPSystem, error) { var primaryInstance entities.SAPSystemInstance db := s.db. Model(&entities.SAPSystemInstance{}). Joins("JOIN hosts ON sap_system_instances.agent_id = hosts.agent_id") - ip := net.ParseIP(dbHost) + ip := net.ParseIP(dbAddress) if ip.To4() == nil { - db = db.Where("hosts.name = ?", dbHost) - } else { - db = db.Where("hosts.ip_addresses && ?", pq.Array([]string{dbHost})) + return nil, fmt.Errorf("received database address is not valid: %s", dbAddress) } + db = db.Where("hosts.ip_addresses && ?", pq.Array([]string{dbAddress})) + err := db.Where("tenants && ?", pq.Array([]string{dbName})). Select("id"). First(&primaryInstance). @@ -255,7 +257,7 @@ func (s *sapSystemsService) enrichSAPSystemList(sapSystemList models.SAPSystemLi for _, sapSystem := range sapSystemList { err := s.attachDatabase(sapSystem) if err != nil { - return err + log.Warnf("could not attach database: %s", err) } s.computeHealth(sapSystem) // Store already found SIDs to find duplicates @@ -273,7 +275,7 @@ func (s *sapSystemsService) enrichSAPSystemList(sapSystemList models.SAPSystemLi func (s *sapSystemsService) attachDatabase(sapSystem *models.SAPSystem) error { if sapSystem.Type == models.SAPSystemTypeApplication { - attachedDatabase, err := s.getAttachedDatabase(sapSystem.DBName, sapSystem.DBHost) + attachedDatabase, err := s.getAttachedDatabase(sapSystem.DBName, sapSystem.DBAddress) if err != nil { return err } diff --git a/web/services/sap_systems_test.go b/web/services/sap_systems_test.go index ec1979165..7741e47bd 100644 --- a/web/services/sap_systems_test.go +++ b/web/services/sap_systems_test.go @@ -24,6 +24,7 @@ func sapSystemsFixtures() entities.SAPSystemInstances { Status: string(sapcontrol.STATECOLOR_RED), DBHost: "dbhost_1", DBName: "tenant", + DBAddress: "192.168.1.10", Host: &entities.Host{ AgentID: "1", Name: "apphost", @@ -54,6 +55,7 @@ func sapSystemsFixtures() entities.SAPSystemInstances { Name: "dbhost_1", ClusterID: "cluster_id_2", ClusterName: "dbcluster", + IPAddresses: pq.StringArray{"192.168.1.10"}, }, Tags: []*models.Tag{ { @@ -126,12 +128,13 @@ func (suite *SAPSystemsServiceTestSuite) TestSAPSystemsService_GetAllApplication suite.EqualValues(models.SAPSystemList{ { - ID: "sap_system_1", - SID: "HA1", - Type: models.SAPSystemTypeApplication, - DBHost: "dbhost_1", - DBName: "tenant", - Health: models.SAPSystemHealthCritical, + ID: "sap_system_1", + SID: "HA1", + Type: models.SAPSystemTypeApplication, + DBHost: "dbhost_1", + DBName: "tenant", + DBAddress: "192.168.1.10", + Health: models.SAPSystemHealthCritical, Instances: []*models.SAPSystemInstance{ { Features: "features",