This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add webhook operations in the CLI SAILABOVE-1209
Add 3 new commands : sail application webhook list <applicationName> sail application webhook add <applicationName> <webhookUrl> sail application webhook delete <applicationName> <webhookUrl> Those commands help to manage webhook linked to a namespace. Sail Above will send updates on container state of every containers of every services of this namespace to the webhook if it's present. Those update are passed as json. Example : { "application": "devel" "service": "foobar", "timestamp": 1446653035.631104, "event": "state", "state": "STOPPED", "prev_state": "STOPPING", "type": "Container", "data": null, "id": "b9e7bef7-d571-4ee4-b80e-835a2377040e", "counters": { "start": 17 } } Signed-off-by: Jean-Tiare Le Bigot <[email protected]>
- Loading branch information
timothee.germain
authored and
Jean-Tiare Le Bigot
committed
Nov 13, 2015
1 parent
47fffa0
commit 2e1b422
Showing
2 changed files
with
112 additions
and
0 deletions.
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,106 @@ | ||
package application | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/runabove/sail/internal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cmdApplicationWebhook = &cobra.Command{ | ||
Use: "webhook", | ||
Short: "Application webhook commands: sail application webhook --help", | ||
Long: `Application webhook commands: sail application webhook <command>`, | ||
} | ||
|
||
var cmdApplicationWebhookList = &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "List the webhooks of an app: sail application webhook list <applicationName>", | ||
Long: `List the webhooks of an app: sail application webhook list <applicationName> | ||
example: sail application webhook list my-app" | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 || args[0] == "" { | ||
fmt.Fprintln(os.Stderr, "Invalid usage. Please see sail application webhook list --help") | ||
} else { | ||
// Sanity | ||
err := internal.CheckName(args[0]) | ||
internal.Check(err) | ||
|
||
internal.FormatOutputDef(internal.GetWantJSON(fmt.Sprintf("/applications/%s/hook", args[0]))) | ||
} | ||
}, | ||
} | ||
|
||
var cmdApplicationWebhookAdd = &cobra.Command{ | ||
Use: "add", | ||
Short: "Add a webhook to an application ; sail application webhook add <applicationName> <WebhookURL>", | ||
Long: `Add a webhook to an application ; sail application webhook add <applicationName> <WebhookURL> | ||
example: sail application webhook add my-app http://www.endpoint.com/hook | ||
Endpoint url must except POST with json body. | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 2 { | ||
fmt.Fprintln(os.Stderr, "Invalid usage. Please see sail application webhook add --help") | ||
} else { | ||
// Sanity | ||
err := internal.CheckName(args[0]) | ||
internal.Check(err) | ||
webhookAdd(args[0], args[1]) | ||
} | ||
}, | ||
} | ||
|
||
var cmdApplicationWebhookDelete = &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete a webhook to an application ; sail application webhook delete <applicationName> <WebhookURL>", | ||
Long: `Delete a webhook to an application ; sail application webhook delete <applicationName> <WebhookURL> | ||
example: sail application webhook delete my-app http://www.endpoint.com/hook | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 2 { | ||
fmt.Fprintln(os.Stderr, "Invalid usage. Please see sail application webhook delete --help") | ||
} else { | ||
// Sanity | ||
err := internal.CheckName(args[0]) | ||
internal.Check(err) | ||
webhookDelete(args[0], args[1]) | ||
} | ||
}, | ||
} | ||
|
||
type webhookStruct struct { | ||
URL string `json:"url"` | ||
} | ||
|
||
func webhookAdd(namespace, webhookURL string) { | ||
|
||
path := fmt.Sprintf("/applications/%s/hook", namespace) | ||
|
||
rawBody := webhookStruct{URL: webhookURL} | ||
body, err := json.MarshalIndent(rawBody, " ", " ") | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Fatal: %s\n", err) | ||
return | ||
} | ||
internal.FormatOutputDef(internal.PostBodyWantJSON(path, body)) | ||
} | ||
|
||
func webhookDelete(namespace, webhookURL string) { | ||
urlEscape := url.QueryEscape(webhookURL) | ||
|
||
path := fmt.Sprintf("/applications/%s/hook", namespace) | ||
// pass urlEscape as query string argument | ||
BaseURL, err := url.Parse(path) | ||
internal.Check(err) | ||
|
||
params := url.Values{} | ||
params.Add("url", urlEscape) | ||
|
||
BaseURL.RawQuery = params.Encode() | ||
internal.FormatOutputDef(internal.DeleteWantJSON(BaseURL.String())) | ||
} |