@@ -29,34 +29,40 @@ func ImportDashboards(beatName, beatVersion, homePath string,
2929 return err
3030 }
3131
32- if esConfig != nil {
33- status , err := ImportDashboardsViaElasticsearch (esConfig , & dashConfig , msgOutputter )
34- if err != nil {
35- return err
36- }
37- if status {
38- // the dashboards were imported via Elasticsearch
39- return nil
40- }
41- }
42-
43- err = ImportDashboardsViaKibana (kibanaConfig , & dashConfig , msgOutputter )
32+ esLoader , err := NewElasticsearchLoader (esConfig , & dashConfig , msgOutputter )
4433 if err != nil {
45- return err
34+ return fmt . Errorf ( "fail to create the Elasticsearch loader: %v" , err )
4635 }
36+ defer esLoader .Close ()
4737
48- return nil
49- }
38+ esLoader .statusMsg ("Elasticsearch URL %v" , esLoader .client .Connection .URL )
5039
51- func ImportDashboardsViaKibana ( config * common. Config , dashConfig * Config , msgOutputter MessageOutputter ) error {
52- if config = = nil {
53- config = common . NewConfig ( )
40+ majorVersion , _ , err := getMajorAndMinorVersion ( esLoader . version )
41+ if err ! = nil {
42+ return fmt . Errorf ( "wrong Elasticsearch version: %v" , err )
5443 }
55- if ! config .Enabled () {
56- return nil
44+
45+ if majorVersion < 6 {
46+ return ImportDashboardsViaElasticsearch (esLoader )
47+ }
48+
49+ logp .Info ("For Elasticsearch version >= 6.0.0, the Kibana dashboards need to be imported via the Kibana API." )
50+
51+ if kibanaConfig == nil {
52+ kibanaConfig = common .NewConfig ()
5753 }
5854
59- kibanaLoader , err := NewKibanaLoader (config , dashConfig , msgOutputter )
55+ // In Cloud, the Kibana URL is different than the Elasticsearch URL,
56+ // but the credentials are the same.
57+ // So, by default, use same credentials for connecting to Kibana as to Elasticsearch
58+ if ! kibanaConfig .HasField ("username" ) && len (esLoader .client .Username ) > 0 {
59+ kibanaConfig .SetString ("username" , - 1 , esLoader .client .Username )
60+ }
61+ if ! kibanaConfig .HasField ("password" ) && len (esLoader .client .Password ) > 0 {
62+ kibanaConfig .SetString ("password" , - 1 , esLoader .client .Password )
63+ }
64+
65+ kibanaLoader , err := NewKibanaLoader (kibanaConfig , & dashConfig , msgOutputter )
6066 if err != nil {
6167 return fmt .Errorf ("fail to create the Kibana loader: %v" , err )
6268 }
@@ -65,11 +71,16 @@ func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOut
6571
6672 kibanaLoader .statusMsg ("Kibana URL %v" , kibanaLoader .client .Connection .URL )
6773
74+ return ImportDashboardsViaKibana (kibanaLoader )
75+ }
76+
77+ func ImportDashboardsViaKibana (kibanaLoader * KibanaLoader ) error {
78+
6879 if ! isKibanaAPIavailable (kibanaLoader .version ) {
6980 return fmt .Errorf ("Kibana API is not available in Kibana version %s" , kibanaLoader .version )
7081 }
7182
72- importer , err := NewImporter ("default" , dashConfig , * kibanaLoader )
83+ importer , err := NewImporter ("default" , kibanaLoader . config , kibanaLoader )
7384 if err != nil {
7485 return fmt .Errorf ("fail to create a Kibana importer for loading the dashboards: %v" , err )
7586 }
@@ -81,39 +92,22 @@ func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOut
8192 return nil
8293}
8394
84- func ImportDashboardsViaElasticsearch (config * common.Config , dashConfig * Config , msgOutputter MessageOutputter ) (bool , error ) {
85- esLoader , err := NewElasticsearchLoader (config , dashConfig , msgOutputter )
86- if err != nil {
87- return false , fmt .Errorf ("fail to create the Elasticsearch loader: %v" , err )
88- }
89- defer esLoader .Close ()
90-
91- esLoader .statusMsg ("Elasticsearch URL %v" , esLoader .client .Connection .URL )
92-
93- majorVersion , _ , err := getMajorAndMinorVersion (esLoader .version )
94- if err != nil {
95- return false , fmt .Errorf ("wrong Elasticsearch version: %v" , err )
96- }
97-
98- if majorVersion >= 6 {
99- logp .Info ("For Elasticsearch version >= 6.0.0, the Kibana dashboards need to be imported via the Kibana API." )
100- return false , nil
101- }
95+ func ImportDashboardsViaElasticsearch (esLoader * ElasticsearchLoader ) error {
10296
10397 if err := esLoader .CreateKibanaIndex (); err != nil {
104- return false , fmt .Errorf ("fail to create the kibana index: %v" , err )
98+ return fmt .Errorf ("fail to create the kibana index: %v" , err )
10599 }
106100
107- importer , err := NewImporter ("5.x" , dashConfig , * esLoader )
101+ importer , err := NewImporter ("5.x" , esLoader . config , esLoader )
108102 if err != nil {
109- return false , fmt .Errorf ("fail to create an Elasticsearch importer for loading the dashboards: %v" , err )
103+ return fmt .Errorf ("fail to create an Elasticsearch importer for loading the dashboards: %v" , err )
110104 }
111105
112106 if err := importer .Import (); err != nil {
113- return false , fmt .Errorf ("fail to import the dashboards in Elasticsearch: %v" , err )
107+ return fmt .Errorf ("fail to import the dashboards in Elasticsearch: %v" , err )
114108 }
115109
116- return true , nil
110+ return nil
117111}
118112
119113func getMajorAndMinorVersion (version string ) (int , int , error ) {
0 commit comments