Skip to content

Commit

Permalink
Backup full destination info (#12)
Browse files Browse the repository at this point in the history
* add go mod

* change to yaml.v2

* one yaml file per monitor

* multiple monitor files when getting local

* clean go mod for yaml.v2

* remove debug print

* pull full destination info

* pull full destination info

* Revert "remove debug print"

This reverts commit 28edbd4.

* Revert "clean go mod for yaml.v2"

This reverts commit daefd15.

* Revert "multiple monitor files when getting local"

This reverts commit 8900c1e.

* Revert "one yaml file per monitor"

This reverts commit 5c1508c.

* Revert "add go mod"

This reverts commit dfcc4c9.

* lower case name

* need strings import

* go fmt

Co-authored-by: Kusha Gharahi <kusha.gharahi@toyotaconnected.com>
  • Loading branch information
kushagharahi and Kusha Gharahi committed Sep 8, 2020
1 parent 0945357 commit 65993f1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion commands/sync.go
Expand Up @@ -61,7 +61,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
21 changes: 12 additions & 9 deletions destination/destinations.go
Expand Up @@ -16,6 +16,7 @@
package destination

import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -56,7 +57,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 +68,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[strings.ToLower(strings.ReplaceAll(destination.Name, " ", "_"))] = destination
}
}
return remoteDestinations, nil
return allRemoteDestinationsMap, nil

}
28 changes: 28 additions & 0 deletions destination/models.go
@@ -0,0 +1,28 @@
package destination

// Destination object
type Destination struct {
ID string
Name string `json:"name"`
Type string `json:"type"`
Slack Slack `json:"slack,omitempty" yaml:",omitempty"`
CustomWebhook CustomWebhook `json:"custom_webhook,omitempty" yaml:",omitempty"`
}

// Slack destination object
type Slack struct {
URL string `json:"url,omitempty" yaml:",omitempty"`
}

// CustomWebhook destination object
type CustomWebhook struct {
Path string `json:"path,omitempty" yaml:",omitempty"`
HeaderParams map[string]string `json:"header_params,omitempty" yaml:",omitempty"`
Password string `json:"password,omitempty" yaml:",omitempty"`
Port int `json:"port,omitempty" yaml:",omitempty"`
Scheme string `json:"scheme,omitempty" yaml:",omitempty"`
QueryParams map[string]string `json:"query_params,omitempty" yaml:",omitempty"`
Host string `json:"host,omitempty" yaml:",omitempty"`
URL string `json:"url,omitempty" yaml:",omitempty"`
Username string `json:"username,omitempty" yaml:",omitempty"`
}
20 changes: 14 additions & 6 deletions monitor/remoteOperations.go
Expand Up @@ -21,13 +21,13 @@ import (
"strconv"

mapset "github.com/deckarep/golang-set"
"github.com/mihirsoni/odfe-monitor-cli/destination"
"github.com/mihirsoni/odfe-monitor-cli/es"
"github.com/mihirsoni/odfe-monitor-cli/utils"
"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 65993f1

Please sign in to comment.