This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
add google chat handler #120
Open
nilesh93
wants to merge
10
commits into
vmware-archive:master
Choose a base branch
from
nilesh93:googlechat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
18f02fd
google chat integration
nilesh93 aa046e9
platformer
nilesh93 e6e7e8d
configmap
nilesh93 a5e7053
minor changes
nilesh93 2af930d
minor changes
nilesh93 10de33f
config map add
nilesh93 19145fc
config map add
nilesh93 6fe1ab2
minor bug fixes
nilesh93 80a8c21
add unit tests
nilesh93 ac0d614
google chat integration
nilesh93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,53 @@ | ||
/* | ||
Copyright 2018 Bitnami | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/bitnami-labs/kubewatch/config" | ||
"github.com/Sirupsen/logrus" | ||
) | ||
|
||
// googleChatConfigCmd represents the googleChat subcommand | ||
var googleChatConfigCmd = &cobra.Command{ | ||
Use: "googlechat FLAG", | ||
Short: "specific googlechat configuration", | ||
Long: `specific googlechat configuration`, | ||
Run: func(cmd *cobra.Command, args []string){ | ||
conf, err := config.New() | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
url, err := cmd.Flags().GetString("url") | ||
if err == nil { | ||
if len(url) > 0 { | ||
conf.Handler.GoogleChat.Url = url | ||
} | ||
} else { | ||
logrus.Fatal(err) | ||
} | ||
|
||
if err = conf.Write(); err != nil { | ||
logrus.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
googleChatConfigCmd.Flags().StringP("url", "u", "", "Specify googleChat url") | ||
} |
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,173 @@ | ||
/* | ||
Copyright 2016 Skippbox, Ltd. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 googlechat | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/bitnami-labs/kubewatch/config" | ||
kbEvent "github.com/bitnami-labs/kubewatch/pkg/event" | ||
) | ||
|
||
var GoogleChatErrorMsg = ` | ||
%s | ||
|
||
You need to set GoogleChat url | ||
using "--url/-u" or using environment variables: | ||
|
||
export KW_GOOGLECHAT_URL=googleChat url | ||
|
||
Command line flags will override environment variables | ||
|
||
` | ||
|
||
// GoogleChat handler implements handler.Handler interface, | ||
// Notify event to GoogleChat channel | ||
type GoogleChat struct { | ||
Url string | ||
} | ||
|
||
type GoogleChatMessage struct { | ||
Text string `json:"text"` | ||
Cards []GoogleCard `json:"cards"` | ||
} | ||
|
||
type GoogleCard struct{ | ||
Header GoogleCardHeader `json:"header"` | ||
Sections []GoogleCardSection `json:"sections"` | ||
} | ||
|
||
type GoogleCardHeader struct{ | ||
Title string `json:"title"` | ||
} | ||
|
||
type GoogleCardSection struct{ | ||
Header string `json:"header"` | ||
Widgets []GoogleCardWidgetParagraph `json:"widgets"` | ||
} | ||
|
||
type GoogleCardWidgetParagraph struct{ | ||
TextParagraph GoogleCardWidgetText `json:"textParagraph"` | ||
} | ||
|
||
type GoogleCardWidgetText struct{ | ||
Text string `json:"text"` | ||
} | ||
|
||
// Init prepares GoogleChat configuration | ||
func (m *GoogleChat) Init(c *config.Config) error { | ||
url := c.Handler.GoogleChat.Url | ||
|
||
if url == "" { | ||
url = os.Getenv("KW_GOOGLECHAT_URL") | ||
} | ||
|
||
m.Url = url | ||
|
||
|
||
log.Printf("Google chat initialized") | ||
return checkMissingGChatVars(m) | ||
} | ||
|
||
func (m *GoogleChat) ObjectCreated(obj interface{}) { | ||
notifyGoogleChat(m, obj, "created") | ||
} | ||
|
||
func (m *GoogleChat) ObjectDeleted(obj interface{}) { | ||
notifyGoogleChat(m, obj, "deleted") | ||
} | ||
|
||
func (m *GoogleChat) ObjectUpdated(oldObj, newObj interface{}) { | ||
notifyGoogleChat(m, newObj, "updated") | ||
} | ||
|
||
|
||
func notifyGoogleChat(m *GoogleChat, obj interface{}, action string) { | ||
e := kbEvent.New(obj, action) | ||
|
||
chatMessage := prepareChatMessage(e) | ||
|
||
err := postMessage(m.Url, chatMessage) | ||
if err != nil { | ||
log.Printf("%s\n", err) | ||
return | ||
} | ||
res2B, _ := json.Marshal(chatMessage) | ||
fmt.Println(string(res2B)) | ||
|
||
log.Printf("Message successfully sent to %s at %s ", m.Url, time.Now()) | ||
} | ||
|
||
func checkMissingGChatVars(s *GoogleChat) error { | ||
if s.Url == "" { | ||
return fmt.Errorf(GoogleChatErrorMsg, "Missing GoogleChat url") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func prepareChatMessage(e kbEvent.Event) *GoogleChatMessage { | ||
return &GoogleChatMessage{ | ||
Text: "", | ||
Cards: []GoogleCard{ | ||
{ | ||
Header: GoogleCardHeader{ | ||
Title: "Kubewatch Notifications", | ||
}, | ||
Sections: []GoogleCardSection{ | ||
{ | ||
Header: "Message", | ||
Widgets: []GoogleCardWidgetParagraph{ | ||
{ | ||
TextParagraph: GoogleCardWidgetText{ | ||
Text: e.Message(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func postMessage(url string, chatMessage *GoogleChatMessage) error { | ||
message, err := json.Marshal(chatMessage) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(message)) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
_, err = client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,46 @@ | ||
/* | ||
Copyright 2016 Skippbox, Ltd. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 googlechat | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/bitnami-labs/kubewatch/config" | ||
) | ||
|
||
func TestGoogleChatInit(t *testing.T) { | ||
s := &GoogleChat{} | ||
expectedError := fmt.Errorf(GoogleChatErrorMsg, "Missing GoogleChat url") | ||
|
||
var Tests = []struct { | ||
googleChat config.GoogleChat | ||
err error | ||
}{ | ||
{config.GoogleChat{Url: "foo"}, nil}, | ||
{config.GoogleChat{}, expectedError}, | ||
} | ||
|
||
for _, tt := range Tests { | ||
c := &config.Config{} | ||
c.Handler.GoogleChat = tt.googleChat | ||
if err := s.Init(c); !reflect.DeepEqual(err, tt.err) { | ||
t.Fatalf("Init(): %v", err) | ||
} | ||
} | ||
} |
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,21 @@ | ||
{ | ||
"cards": [ | ||
{ | ||
"header": { | ||
"title": "Kubewatch Notifications" | ||
}, | ||
"sections": [ | ||
{ | ||
"header": "Message", | ||
"widgets": [ | ||
{ | ||
"textParagraph": { | ||
"text": "some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message some message " | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file should be duplicated for google chat, not overwriting the webhook example.