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,43 +63,78 @@ 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 if (!pluginClient .indexExists (defaultKibanaIndex )) {
8278 LOGGER .debug ("Default Kibana index '{}' does not exist. Skipping Kibana seeding" , defaultKibanaIndex );
8379 return ;
8480 }
8581 LOGGER .debug ("Begin setDashboards: projectPrefix '{}' for user '{}' projects '{}' kibanaIndex '{}'" ,
8682 projectPrefix , context .getUser (), context .getProjects (), context .getKibanaIndex ());
87-
83+
8884 // We want to seed the Kibana user index initially
8985 // since the logic from Kibana has changed to create before this plugin
9086 // starts...
9187 boolean changed = initialSeedKibanaIndex (context , client );
88+
89+ if (context .isOperationsUser ()) {
90+ changed = seedOperationsIndexPatterns (context , client , kibanaVersion , projectPrefix );
91+ } else {
92+ changed = seedUsersIndexPatterns (context , client , kibanaVersion , projectPrefix );
93+ }
94+
95+ if ( changed ) {
96+ pluginClient .refreshIndices (context .getKibanaIndex ());
97+ }
98+ }
9299
100+ private boolean seedOperationsIndexPatterns (final OpenshiftRequestContext context , final Client client , String kibanaVersion , final String projectPrefix ) {
101+ boolean changed = false ;
102+ boolean defaultSet = false ;
103+ for (String pattern : settings .getKibanaOpsIndexPatterns ()) {
104+ if (!pluginClient .documentExists (context .getKibanaIndex (), INDICIES_TYPE , pattern )) {
105+ LOGGER .trace ("Creating index-pattern '{}'" , pattern );
106+ String source = StringUtils .replace (mappingLoader .getOperationsMappingsTemplate (), "$TITLE$" , pattern );
107+ pluginClient .createDocument (context .getKibanaIndex (), INDICIES_TYPE , pattern , source );
108+ if (!defaultSet ) {
109+ try {
110+ String update = XContentFactory .jsonBuilder ()
111+ .startObject ()
112+ .field (KibanaSeed .DEFAULT_INDEX_FIELD , pattern )
113+ .endObject ().string ();
114+ pluginClient .update (context .getKibanaIndex (), DEFAULT_INDEX_TYPE , kibanaVersion , update );
115+ defaultSet = true ;
116+ } catch (IOException e ) {
117+ LOGGER .error ("Unable to set default index-pattern" , e );
118+ }
119+ }
120+ changed = true ;
121+ }
122+ }
123+ return changed ;
124+ }
125+
126+ private boolean seedUsersIndexPatterns (final OpenshiftRequestContext context , final Client client , final String kibanaVersion , final String projectPrefix ) {
127+ boolean changed = false ;
93128 // GET .../.kibana/index-pattern/_search?pretty=true&fields=
94129 // compare results to projects; handle any deltas (create, delete?)
95-
96130 Set <String > indexPatterns = getProjectNamesFromIndexes (context , client , projectPrefix );
97131 LOGGER .debug ("Found '{}' Index patterns for user" , indexPatterns .size ());
98132
99133 Set <String > projects = new HashSet <>(context .getProjects ());
100- if (context .isOperationsUser ()) {
101- projects .add (OPERATIONS_PROJECT );
102- }
103134 List <String > filteredProjects = new ArrayList <String >(filterProjectsWithIndices (projectPrefix , projects ));
104135 LOGGER .debug ("projects for '{}' that have existing indexes: '{}'" , context .getUser (), filteredProjects );
105136
106- if (context .isOperationsUser ()) {
107- filteredProjects .add (ADMIN_ALIAS_NAME );
108- }else if (filteredProjects .isEmpty ()) {
137+ if (filteredProjects .isEmpty ()) {
109138 filteredProjects .add (BLANK_PROJECT );
110139 }
111140
@@ -156,12 +185,9 @@ public void setDashboards(final OpenshiftRequestContext context, Client client,
156185 }
157186 }
158187 }
159-
160- if ( changed ) {
161- refreshKibanaUser (context .getKibanaIndex (), client );
162- }
188+ return changed ;
163189 }
164-
190+
165191 /*
166192 * Given a list of projects, filter out those which do not have any
167193 * index associated with it
@@ -177,16 +203,6 @@ private List<String> filterProjectsWithIndices(final String projectPrefix, Set<S
177203 return result ;
178204 }
179205
180- private void refreshKibanaUser (String kibanaIndex , Client esClient ) {
181-
182- RefreshRequest request = new RefreshRequest ().indices (kibanaIndex );
183- request .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
184- RefreshResponse response = esClient .admin ().indices ().refresh (request ).actionGet ();
185-
186- LOGGER .debug ("Refreshed '{}' successfully on {} of {} shards" , kibanaIndex , response .getSuccessfulShards (),
187- response .getTotalShards ());
188- }
189-
190206 private boolean initialSeedKibanaIndex (final OpenshiftRequestContext context , Client esClient ) {
191207
192208 try {
@@ -197,29 +213,7 @@ private boolean initialSeedKibanaIndex(final OpenshiftRequestContext context, Cl
197213 // copy the defaults if the userindex is not the kibanaindex
198214 if (!kibanaIndexExists && !defaultKibanaIndex .equals (userIndex )) {
199215 LOGGER .debug ("Copying '{}' to '{}'" , defaultKibanaIndex , userIndex );
200-
201- GetIndexRequest getRequest = new GetIndexRequest ().indices (defaultKibanaIndex );
202- getRequest .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
203- GetIndexResponse getResponse = esClient .admin ().indices ().getIndex (getRequest ).get ();
204-
205- CreateIndexRequest createRequest = new CreateIndexRequest ().index (userIndex );
206- createRequest .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
207-
208- createRequest .settings (getResponse .settings ().get (defaultKibanaIndex ));
209-
210- Map <String , Object > configMapping = getResponse .mappings ().get (defaultKibanaIndex ).get ("config" )
211- .getSourceAsMap ();
212-
213- createRequest .mapping ("config" , configMapping );
214-
215- esClient .admin ().indices ().create (createRequest ).actionGet ();
216-
217- // Wait for health status of YELLOW
218- ClusterHealthRequest healthRequest = new ClusterHealthRequest ().indices (new String [] { userIndex })
219- .waitForYellowStatus ();
220-
221- esClient .admin ().cluster ().health (healthRequest ).actionGet ().getStatus ();
222-
216+ pluginClient .copyIndex (defaultKibanaIndex , userIndex , DEFAULT_INDEX_TYPE );
223217 return true ;
224218 }
225219 } catch (ExecutionException | InterruptedException | IOException e ) {
@@ -236,7 +230,7 @@ private void setDefaultIndex(String kibanaIndex, String project, Client esClient
236230 // .kibana.username
237231 String source = new DocumentBuilder ().defaultIndex (getIndexPattern (project , projectPrefix )).build ();
238232
239- executeUpdate (kibanaIndex , DEFAULT_INDEX_TYPE , kibanaVersion , source , esClient );
233+ pluginClient . update (kibanaIndex , DEFAULT_INDEX_TYPE , kibanaVersion , source );
240234 }
241235
242236 private String getDefaultIndex (OpenshiftRequestContext context , Client esClient , String kibanaVersion , String projectPrefix ) {
@@ -342,9 +336,7 @@ private void createIndexPattern(String kibanaIndex, String project, Client esCli
342336
343337 final String indexPattern = getIndexPattern (project , projectPrefix );
344338 String source ;
345- if (project .equalsIgnoreCase (OPERATIONS_PROJECT ) || project .startsWith (ADMIN_ALIAS_NAME )) {
346- source = mappingLoader .getOperationsMappingsTemplate ();
347- } else if (project .equalsIgnoreCase (BLANK_PROJECT )) {
339+ if (project .equalsIgnoreCase (BLANK_PROJECT )) {
348340 source = mappingLoader .getEmptyProjectMappingsTemplate ();
349341 } else {
350342 source = mappingLoader .getApplicationMappingsTemplate ();
@@ -364,17 +356,6 @@ private void deleteIndex(String kibanaIndex, String project, Client esClient, St
364356 executeDelete (kibanaIndex , INDICIES_TYPE , getIndexPattern (project , projectPrefix ), esClient );
365357 }
366358
367- private void executeUpdate (String index , String type , String id , String source , Client esClient ) {
368-
369- LOGGER .debug ("UPDATE: '{}/{}/{}' source: '{}'" , index , type , id , source );
370-
371- UpdateRequest request = esClient .prepareUpdate (index , type , id ).setDoc (source ).setDocAsUpsert (true ).request ();
372- request .putHeader (ConfigConstants .SG_CONF_REQUEST_HEADER , "true" );
373-
374-
375- LOGGER .debug ("Created with update? '{}'" , esClient .update (request ).actionGet ().isCreated ());
376- }
377-
378359 private void executeDelete (String index , String type , String id , Client esClient ) {
379360
380361 LOGGER .debug ("DELETE: '{}/{}/{}'" , index , type , id );
0 commit comments