Skip to content

Commit 7477c8c

Browse files
author
anton.voskresensky
committed
проверка ip на актуальность
1 parent 33d7688 commit 7477c8c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

commands/indexpatterns.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ func runIndexPatterns(cmd *cobra.Command, args []string) error {
6868
logger.Info(fmt.Sprintf("Skipping index-pattern %s:%s - no matching indices found in cluster", ip_id, ip_title))
6969
continue
7070
}
71+
exists, err := kb.CheckIndexPatternExists(ip_id)
72+
if err != nil {
73+
logger.Warn(fmt.Sprintf("Failed to check if index-pattern exists %s:%s: %v, will try to refresh anyway", ip_id, ip_title, err))
74+
} else if !exists {
75+
logger.Info(fmt.Sprintf("Index-pattern %s:%s not found in Kibana, skipping refresh", ip_id, ip_title))
76+
continue
77+
}
7178
if cfg.GetDryRun() {
7279
logger.Info(fmt.Sprintf("DRY RUN: Would refresh index-pattern %s:%s (matches %d indices)", ip_id, ip_title, len(indices)))
7380
refreshedPatterns = append(refreshedPatterns, ip_title)

pkg/kibana/service.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ func (c *Client) GetActualMappingForIndexPattern(title string) ([]byte, error) {
136136
return fields_string, nil
137137
}
138138

139+
func (c *Client) CheckIndexPatternExists(id string) (bool, error) {
140+
u := fmt.Sprintf("%s/api/saved_objects/index-pattern/%s", c.baseURL, id)
141+
req, err := http.NewRequest("GET", u, nil)
142+
if err != nil {
143+
return false, err
144+
}
145+
resp, err := c.do(req)
146+
if err != nil {
147+
return false, err
148+
}
149+
defer resp.Body.Close()
150+
if resp.StatusCode == 404 {
151+
return false, nil
152+
}
153+
if resp.StatusCode >= 300 {
154+
return false, fmt.Errorf("kibana check index pattern failed: %s", resp.Status)
155+
}
156+
return true, nil
157+
}
158+
139159
func (c *Client) RefreshIndexPattern(id string, title string) error {
140160
if id == "" {
141161
return fmt.Errorf("index pattern id cannot be empty")

0 commit comments

Comments
 (0)