2626import java .util .concurrent .ExecutionException ;
2727
2828import org .apache .commons .lang .StringUtils ;
29- import org .elasticsearch .action .admin .cluster .health .ClusterHealthRequest ;
30- import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
31- import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
32- import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
33- import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
34- import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
3529import org .elasticsearch .action .delete .DeleteRequest ;
3630import org .elasticsearch .action .get .GetRequest ;
3731import org .elasticsearch .action .get .GetResponse ;
3832import org .elasticsearch .action .search .SearchRequest ;
3933import org .elasticsearch .action .search .SearchResponse ;
40- import org .elasticsearch .action .update .UpdateRequest ;
4134import org .elasticsearch .client .Client ;
4235import org .elasticsearch .common .inject .Inject ;
4336import org .elasticsearch .common .logging .ESLogger ;
4437import org .elasticsearch .common .logging .Loggers ;
38+ import org .elasticsearch .common .xcontent .XContentFactory ;
4539import org .elasticsearch .index .IndexNotFoundException ;
4640import org .elasticsearch .search .SearchHit ;
4741import org .elasticsearch .transport .RemoteTransportException ;
@@ -69,40 +63,75 @@ public class KibanaSeed implements ConfigurationSettings {
6963 private final IndexMappingLoader mappingLoader ;
7064 private final PluginClient pluginClient ;
7165 private final String defaultKibanaIndex ;
66+ private final PluginSettings settings ;
7267
7368 @ Inject
7469 public KibanaSeed (final PluginSettings settings , final IndexMappingLoader loader , final PluginClient pluginClient ) {
7570 this .mappingLoader = loader ;
7671 this .pluginClient = pluginClient ;
7772 this .defaultKibanaIndex = settings .getDefaultKibanaIndex ();
73+ this .settings = settings ;
7874 }
79-
75+
8076 public void setDashboards (final OpenshiftRequestContext context , Client client , String kibanaVersion , final String projectPrefix ) {
8177
8278 LOGGER .debug ("Begin setDashboards: projectPrefix '{}' for user '{}' projects '{}' kibanaIndex '{}'" ,
8379 projectPrefix , context .getUser (), context .getProjects (), context .getKibanaIndex ());
84-
80+
8581 // We want to seed the Kibana user index initially
8682 // since the logic from Kibana has changed to create before this plugin
8783 // starts...
8884 boolean changed = initialSeedKibanaIndex (context , client );
85+
86+ if (context .isOperationsUser ()) {
87+ changed = seedOperationsIndexPatterns (context , client , kibanaVersion , projectPrefix );
88+ } else {
89+ changed = seedUsersIndexPatterns (context , client , kibanaVersion , projectPrefix );
90+ }
91+
92+ if ( changed ) {
93+ pluginClient .refreshIndices (context .getKibanaIndex ());
94+ }
95+ }
8996
97+ private boolean seedOperationsIndexPatterns (final OpenshiftRequestContext context , final Client client , String kibanaVersion , final String projectPrefix ) {
98+ boolean changed = false ;
99+ boolean defaultSet = false ;
100+ for (String pattern : settings .getKibanaOpsIndexPatterns ()) {
101+ if (!pluginClient .documentExists (context .getKibanaIndex (), INDICIES_TYPE , pattern )) {
102+ LOGGER .trace ("Creating index-pattern '{}'" , pattern );
103+ String source = StringUtils .replace (mappingLoader .getOperationsMappingsTemplate (), "$TITLE$" , pattern );
104+ pluginClient .createDocument (context .getKibanaIndex (), INDICIES_TYPE , pattern , source );
105+ if (!defaultSet ) {
106+ try {
107+ String update = XContentFactory .jsonBuilder ()
108+ .startObject ()
109+ .field (KibanaSeed .DEFAULT_INDEX_FIELD , pattern )
110+ .endObject ().string ();
111+ pluginClient .update (context .getKibanaIndex (), DEFAULT_INDEX_TYPE , kibanaVersion , update );
112+ defaultSet = true ;
113+ } catch (IOException e ) {
114+ LOGGER .error ("Unable to set default index-pattern" , e );
115+ }
116+ }
117+ changed = true ;
118+ }
119+ }
120+ return changed ;
121+ }
122+
123+ private boolean seedUsersIndexPatterns (final OpenshiftRequestContext context , final Client client , final String kibanaVersion , final String projectPrefix ) {
124+ boolean changed = false ;
90125 // GET .../.kibana/index-pattern/_search?pretty=true&fields=
91126 // compare results to projects; handle any deltas (create, delete?)
92-
93127 Set <String > indexPatterns = getProjectNamesFromIndexes (context , client , projectPrefix );
94128 LOGGER .debug ("Found '{}' Index patterns for user" , indexPatterns .size ());
95129
96130 Set <String > projects = new HashSet <>(context .getProjects ());
97- if (context .isOperationsUser ()) {
98- projects .add (OPERATIONS_PROJECT );
99- }
100131 List <String > filteredProjects = new ArrayList <String >(filterProjectsWithIndices (projectPrefix , projects ));
101132 LOGGER .debug ("projects for '{}' that have existing indexes: '{}'" , context .getUser (), filteredProjects );
102133
103- if (context .isOperationsUser ()) {
104- filteredProjects .add (ADMIN_ALIAS_NAME );
105- }else if (filteredProjects .isEmpty ()) {
134+ if (filteredProjects .isEmpty ()) {
106135 filteredProjects .add (BLANK_PROJECT );
107136 }
108137
@@ -153,12 +182,9 @@ public void setDashboards(final OpenshiftRequestContext context, Client client,
153182 }
154183 }
155184 }
156-
157- if ( changed ) {
158- refreshKibanaUser (context .getKibanaIndex (), client );
159- }
185+ return changed ;
160186 }
161-
187+
162188 /*
163189 * Given a list of projects, filter out those which do not have any
164190 * index associated with it
@@ -174,16 +200,6 @@ private List<String> filterProjectsWithIndices(final String projectPrefix, Set<S
174200 return result ;
175201 }
176202
177- private void refreshKibanaUser (String kibanaIndex , Client esClient ) {
178-
179- RefreshRequest request = new RefreshRequest ().indices (kibanaIndex );
180- request .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
181- RefreshResponse response = esClient .admin ().indices ().refresh (request ).actionGet ();
182-
183- LOGGER .debug ("Refreshed '{}' successfully on {} of {} shards" , kibanaIndex , response .getSuccessfulShards (),
184- response .getTotalShards ());
185- }
186-
187203 private boolean initialSeedKibanaIndex (final OpenshiftRequestContext context , Client esClient ) {
188204
189205 try {
@@ -194,29 +210,7 @@ private boolean initialSeedKibanaIndex(final OpenshiftRequestContext context, Cl
194210 // copy the defaults if the userindex is not the kibanaindex
195211 if (!kibanaIndexExists && !defaultKibanaIndex .equals (userIndex )) {
196212 LOGGER .debug ("Copying '{}' to '{}'" , defaultKibanaIndex , userIndex );
197-
198- GetIndexRequest getRequest = new GetIndexRequest ().indices (defaultKibanaIndex );
199- getRequest .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
200- GetIndexResponse getResponse = esClient .admin ().indices ().getIndex (getRequest ).get ();
201-
202- CreateIndexRequest createRequest = new CreateIndexRequest ().index (userIndex );
203- createRequest .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
204-
205- createRequest .settings (getResponse .settings ().get (defaultKibanaIndex ));
206-
207- Map <String , Object > configMapping = getResponse .mappings ().get (defaultKibanaIndex ).get ("config" )
208- .getSourceAsMap ();
209-
210- createRequest .mapping ("config" , configMapping );
211-
212- esClient .admin ().indices ().create (createRequest ).actionGet ();
213-
214- // Wait for health status of YELLOW
215- ClusterHealthRequest healthRequest = new ClusterHealthRequest ().indices (new String [] { userIndex })
216- .waitForYellowStatus ();
217-
218- esClient .admin ().cluster ().health (healthRequest ).actionGet ().getStatus ();
219-
213+ pluginClient .copyIndex (defaultKibanaIndex , userIndex , DEFAULT_INDEX_TYPE );
220214 return true ;
221215 }
222216 } catch (ExecutionException | InterruptedException | IOException e ) {
@@ -233,7 +227,7 @@ private void setDefaultIndex(String kibanaIndex, String project, Client esClient
233227 // .kibana.username
234228 String source = new DocumentBuilder ().defaultIndex (getIndexPattern (project , projectPrefix )).build ();
235229
236- executeUpdate (kibanaIndex , DEFAULT_INDEX_TYPE , kibanaVersion , source , esClient );
230+ pluginClient . update (kibanaIndex , DEFAULT_INDEX_TYPE , kibanaVersion , source );
237231 }
238232
239233 private String getDefaultIndex (OpenshiftRequestContext context , Client esClient , String kibanaVersion , String projectPrefix ) {
@@ -339,9 +333,7 @@ private void createIndexPattern(String kibanaIndex, String project, Client esCli
339333
340334 final String indexPattern = getIndexPattern (project , projectPrefix );
341335 String source ;
342- if (project .equalsIgnoreCase (OPERATIONS_PROJECT ) || project .startsWith (ADMIN_ALIAS_NAME )) {
343- source = mappingLoader .getOperationsMappingsTemplate ();
344- } else if (project .equalsIgnoreCase (BLANK_PROJECT )) {
336+ if (project .equalsIgnoreCase (BLANK_PROJECT )) {
345337 source = mappingLoader .getEmptyProjectMappingsTemplate ();
346338 } else {
347339 source = mappingLoader .getApplicationMappingsTemplate ();
@@ -361,17 +353,6 @@ private void deleteIndex(String kibanaIndex, String project, Client esClient, St
361353 executeDelete (kibanaIndex , INDICIES_TYPE , getIndexPattern (project , projectPrefix ), esClient );
362354 }
363355
364- private void executeUpdate (String index , String type , String id , String source , Client esClient ) {
365-
366- LOGGER .debug ("UPDATE: '{}/{}/{}' source: '{}'" , index , type , id , source );
367-
368- UpdateRequest request = esClient .prepareUpdate (index , type , id ).setDoc (source ).setDocAsUpsert (true ).request ();
369- request .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
370-
371-
372- LOGGER .debug ("Created with update? '{}'" , esClient .update (request ).actionGet ().isCreated ());
373- }
374-
375356 private void executeDelete (String index , String type , String id , Client esClient ) {
376357
377358 LOGGER .debug ("DELETE: '{}/{}/{}'" , index , type , id );
0 commit comments