Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for elasticsearch interface
- Loading branch information
1 parent
81553b8
commit 79fcde3
Showing
276 changed files
with
50,409 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package notify | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/infracloudio/botkube/pkg/config" | ||
"github.com/infracloudio/botkube/pkg/events" | ||
log "github.com/infracloudio/botkube/pkg/logging" | ||
"github.com/olivere/elastic" | ||
) | ||
|
||
// ElasticSearch contains auth cred and index setting | ||
type ElasticSearch struct { | ||
Username string | ||
Password string | ||
Server string | ||
Index string | ||
Shards int | ||
Replicas int | ||
Type string | ||
} | ||
|
||
// NewElasticSearch returns new Slack object | ||
func NewElasticSearch(c *config.Config) Notifier { | ||
return &ElasticSearch{ | ||
Username: c.Communications.ElasticSearch.Username, | ||
Password: c.Communications.ElasticSearch.Password, | ||
Server: c.Communications.ElasticSearch.Server, | ||
Index: c.Communications.ElasticSearch.Index.Name, | ||
Type: c.Communications.ElasticSearch.Index.Type, | ||
Shards: c.Communications.ElasticSearch.Index.Shards, | ||
Replicas: c.Communications.ElasticSearch.Index.Replicas, | ||
} | ||
} | ||
|
||
type mapping struct { | ||
Settings settings `json:"settings"` | ||
} | ||
|
||
type settings struct { | ||
Index index `json:"index"` | ||
} | ||
type index struct { | ||
Shards int `json:"number_of_shards"` | ||
Replicas int `json:"number_of_replicas"` | ||
} | ||
|
||
// SendEvent sends event notification to slack | ||
func (e *ElasticSearch) SendEvent(event events.Event) error { | ||
log.Logger.Debug(fmt.Sprintf(">> Sending to ElasticSearch: %+v", event)) | ||
ctx := context.Background() | ||
client, err := elastic.NewClient(elastic.SetURL(e.Server), elastic.SetBasicAuth(e.Username, e.Password), elastic.SetSniff(false), elastic.SetHealthcheck(false), elastic.SetGzip(true)) | ||
if err != nil { | ||
log.Logger.Error(fmt.Sprintf("Failed to create els client. Error:%s", err.Error())) | ||
return err | ||
} | ||
|
||
// Create index if not exists | ||
exists, err := client.IndexExists(e.Index).Do(ctx) | ||
if err != nil { | ||
log.Logger.Error(fmt.Sprintf("Failed to get index. Error:%s", err.Error())) | ||
return err | ||
} | ||
if !exists { | ||
// Create a new index. | ||
mapping := mapping{ | ||
Settings: settings{ | ||
index{ | ||
Shards: e.Shards, | ||
Replicas: e.Replicas, | ||
}, | ||
}, | ||
} | ||
_, err := client.CreateIndex(e.Index).BodyJson(mapping).Do(ctx) | ||
if err != nil { | ||
log.Logger.Error(fmt.Sprintf("Failed to create index. Error:%s", err.Error())) | ||
return err | ||
} | ||
} | ||
|
||
// Send event to els | ||
_, err = client.Index().Index(e.Index).Type(e.Type).BodyJson(event).Do(ctx) | ||
if err != nil { | ||
log.Logger.Error(fmt.Sprintf("Failed to post data to els. Error:%s", err.Error())) | ||
return err | ||
} | ||
_, err = client.Flush().Index(e.Index).Do(ctx) | ||
if err != nil { | ||
log.Logger.Error(fmt.Sprintf("Failed to flush data to els. Error:%s", err.Error())) | ||
return err | ||
} | ||
log.Logger.Debugf("Event successfully sent to ElasticSearch index %s", e.Index) | ||
return nil | ||
} | ||
|
||
// SendMessage sends message to slack channel | ||
func (e *ElasticSearch) SendMessage(msg string) error { | ||
return nil | ||
} |
Oops, something went wrong.