Skip to content

Commit

Permalink
pull full destination info
Browse files Browse the repository at this point in the history
  • Loading branch information
Kusha Gharahi committed Sep 4, 2020
1 parent 28edbd4 commit b7fc03e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
2 changes: 1 addition & 1 deletion commands/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func runSync(cmd *cobra.Command, args []string) {
}
}

func writeDestinations(destinations map[string]string) {
func writeDestinations(destinations map[string]destination.Destination) {
destinationsPath := filepath.Join(rootDir, destination.FileName)
file, err := os.OpenFile(destinationsPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
check(err)
Expand Down
22 changes: 12 additions & 10 deletions destination/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package destination

import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/mihirsoni/odfe-monitor-cli/es"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,7 +56,7 @@ func getCommonHeaders() map[string]string {
}

// 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]string, error) {
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,
Expand All @@ -67,17 +67,19 @@ func GetRemote(esClient es.Client) (map[string]string, error) {
if err != nil {
return nil, errors.Wrap(err, "Unable to fetch destinations from elasticsearch")
}
var remoteDestinations = make(map[string]string)

allRemoteDestinationsMap := make(map[string]Destination)
if resp.Status == 200 {
for _, hit := range resp.Data["hits"].(map[string]interface{})["hits"].([]interface{}) {
// Improve using gJson , if more complex operation required
id := hit.(map[string]interface{})["_id"].(string)
name := hit.(map[string]interface{})["_source"].(map[string]interface{})["destination"].(map[string]interface{})["name"].(string)
name = strings.ToLower(strings.ReplaceAll(name, " ", "_"))
remoteDestinations[name] = id
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[destination.Name] = destination
}
}
return remoteDestinations, nil
return allRemoteDestinationsMap, nil

}
20 changes: 14 additions & 6 deletions monitor/remoteOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (

mapset "github.com/deckarep/golang-set"
"github.com/mihirsoni/odfe-monitor-cli/es"
"github.com/mihirsoni/odfe-monitor-cli/utils"
"github.com/mihirsoni/odfe-monitor-cli/destination"
"github.com/pkg/errors"
)

// GetAllRemote will pull all the monitors from ES cluster
func GetAllRemote(esClient es.Client, destinationsMap map[string]string) (map[string]Monitor, mapset.Set, error) {
func GetAllRemote(esClient es.Client, destinationsMap map[string]destination.Destination) (map[string]Monitor, mapset.Set, error) {
// Since this is very simple call to match all maximum monitors which is 1000 for now
byt := []byte(`{"size": 1000, "query":{ "match_all": {}}}`)
resp, err := esClient.MakeRequest(http.MethodPost,
Expand All @@ -40,7 +40,7 @@ func GetAllRemote(esClient es.Client, destinationsMap map[string]string) (map[st
allRemoteMonitorsMap := make(map[string]Monitor)
remoteMonitorsSet := mapset.NewSet()
// Reversed map for destinations
flippedDestinations := utils.ReverseMap(destinationsMap)
flippedDestinations := mapIDAsKey(destinationsMap)
if resp.Status == 404 {
// No monitors exists so no index exists, returning empty and will create new monitors
return allRemoteMonitorsMap, remoteMonitorsSet, nil
Expand All @@ -63,7 +63,7 @@ func GetAllRemote(esClient es.Client, destinationsMap map[string]string) (map[st
monitor.Triggers[index].YCondition = monitor.Triggers[index].Condition.Script.Source
for k := range monitor.Triggers[index].Actions {
destinationID := monitor.Triggers[index].Actions[k].DestinationID
destintionName := flippedDestinations[destinationID]
destintionName := flippedDestinations[destinationID].Name
if destintionName == "" {
return nil, nil, errors.New("Invalid destination" + destinationID + " in monitor " +
monitor.Name + ".If out of sync update using odfe-monitor-cli sync --destination or update")
Expand All @@ -82,7 +82,7 @@ func GetAllRemote(esClient es.Client, destinationsMap map[string]string) (map[st
// Prepare will modify the monitor to populate correct IDs
func (monitor *Monitor) Prepare(
remoteMonitor Monitor,
destinationsMap map[string]string,
destinationsMap map[string]destination.Destination,
isUpdate bool,
odVersion int) error {

Expand Down Expand Up @@ -123,7 +123,7 @@ func (monitor *Monitor) Prepare(
for k := range monitor.Triggers[index].Actions {
currentAction := monitor.Triggers[index].Actions[k]
currentAction.ID = ""
remoteDestinationID := destinationsMap[currentAction.DestinationID]
remoteDestinationID := destinationsMap[currentAction.DestinationID].ID
if remoteDestinationID == "" {
return errors.New("Specified destination " + currentAction.DestinationID +
" in monitor " + monitor.Name +
Expand Down Expand Up @@ -251,3 +251,11 @@ func (monitor *Monitor) Delete(esClient es.Client) error {
}
return nil
}

func mapIDAsKey(m map[string]destination.Destination) map[string]destination.Destination {
n := make(map[string]destination.Destination)
for _, v := range m {
n[v.ID] = v
}
return n
}
24 changes: 0 additions & 24 deletions utils/utils.go

This file was deleted.

0 comments on commit b7fc03e

Please sign in to comment.