-
Notifications
You must be signed in to change notification settings - Fork 22
/
destinations.go
86 lines (78 loc) · 2.84 KB
/
destinations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package destination
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/mihirsoni/odfe-monitor-cli/es"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
//FileName where destinations are stored and read from
const FileName = "destinations.yaml"
const indexSearchURL = "/.opendistro-alerting-config/_search"
//GetLocal Parse local destinations
func GetLocal(rootDir string) (map[string]string, error) {
destinations := make(map[string]string)
destinationsPath := filepath.Join(rootDir, FileName)
if _, err := os.Stat(destinationsPath); os.IsNotExist(err) {
return nil, errors.Wrap(err, FileName+"doesn't exist")
}
yamlFile, err := ioutil.ReadFile(destinationsPath)
if err != nil {
return nil, errors.Wrap(err, "Unable to read"+FileName)
}
yaml.Unmarshal(yamlFile, &destinations)
if err != nil {
return nil, errors.Wrap(err, "Unable to parse "+FileName)
}
return destinations, nil
}
func getCommonHeaders() map[string]string {
return map[string]string{
"Content-Type": "application/json",
}
}
// GetRemote This will get all the monitor and write them into destinations.yaml file on the root directory
func GetRemote(esClient es.Client) (map[string]Destination, error) {
// Adding 10k which will not be the case.
getAllDestinationQuery := []byte(`{"size": 10000, "query":{ "bool": {"must": { "exists": { "field" : "destination" }}}}}`)
resp, err := esClient.MakeRequest(http.MethodPost,
indexSearchURL,
getAllDestinationQuery,
getCommonHeaders(),
)
if err != nil {
return nil, errors.Wrap(err, "Unable to fetch destinations from elasticsearch")
}
allRemoteDestinationsMap := make(map[string]Destination)
if resp.Status == 200 {
for _, hit := range resp.Data["hits"].(map[string]interface{})["hits"].([]interface{}) {
var destination Destination
parsedDestination, err := json.Marshal(hit.(map[string]interface{})["_source"].(map[string]interface{})["destination"])
if err != nil {
return nil, errors.Wrap(err, "Invalid remote JSON document")
}
json.Unmarshal(parsedDestination, &destination)
destination.ID = hit.(map[string]interface{})["_id"].(string)
allRemoteDestinationsMap[strings.ToLower(strings.ReplaceAll(destination.Name, " ", "_"))] = destination
}
}
return allRemoteDestinationsMap, nil
}