|
| 1 | +// Copyright Quesma, licensed under the Elastic License 2.0. |
| 2 | +// SPDX-License-Identifier: Elastic-2.0 |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | +) |
| 12 | + |
| 13 | +func createDataViews() { |
| 14 | + |
| 15 | + indexes, err := readIndexMappings() |
| 16 | + if err != nil { |
| 17 | + log.Fatalf("Error reading index mappings: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + for _, index := range indexes { |
| 21 | + |
| 22 | + var timestampField string |
| 23 | + for field, mapping := range index.Properties { |
| 24 | + |
| 25 | + if mapping.Type == "date" { |
| 26 | + timestampField = field |
| 27 | + break |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + err = CreateDataView(index.Pattern, timestampField) |
| 32 | + if err != nil { |
| 33 | + log.Printf("Failed to create Data View: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +type DataViewRequest struct { |
| 41 | + Attributes struct { |
| 42 | + Title string `json:"title"` // Index pattern name |
| 43 | + TimeFieldName string `json:"timeFieldName,omitempty"` // Optional time field |
| 44 | + } `json:"attributes"` |
| 45 | +} |
| 46 | + |
| 47 | +// CreateDataView creates a new Data View (Index Pattern) in Kibana |
| 48 | +func CreateDataView(dataViewName string, timeField string) error { |
| 49 | + // Construct the API URL |
| 50 | + url := "http://localhost:5601/api/saved_objects/index-pattern" |
| 51 | + |
| 52 | + // Create the request payload |
| 53 | + payload := DataViewRequest{} |
| 54 | + payload.Attributes.Title = dataViewName |
| 55 | + if timeField != "" { |
| 56 | + payload.Attributes.TimeFieldName = timeField |
| 57 | + } |
| 58 | + |
| 59 | + // Convert to JSON |
| 60 | + jsonData, err := json.Marshal(payload) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to marshal JSON: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Create HTTP request |
| 66 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to create request: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Set headers |
| 72 | + req.Header.Set("Content-Type", "application/json") |
| 73 | + req.Header.Set("kbn-xsrf", "true") // Required to bypass CSRF protection |
| 74 | + |
| 75 | + // Execute the request |
| 76 | + client := &http.Client{} |
| 77 | + resp, err := client.Do(req) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("failed to send request: %v", err) |
| 80 | + } |
| 81 | + defer resp.Body.Close() |
| 82 | + |
| 83 | + // Check response status |
| 84 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { |
| 85 | + return fmt.Errorf("failed to create Data View, status: %d", resp.StatusCode) |
| 86 | + } |
| 87 | + |
| 88 | + log.Printf("✅ Successfully created Data View: %s", dataViewName) |
| 89 | + return nil |
| 90 | +} |
0 commit comments