From 2e1b422e29ca8d465b061853940b45f4c1664098 Mon Sep 17 00:00:00 2001 From: "timothee.germain" Date: Tue, 27 Oct 2015 16:49:12 +0000 Subject: [PATCH] feat: add webhook operations in the CLI SAILABOVE-1209 Add 3 new commands : sail application webhook list sail application webhook add sail application webhook delete 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 --- application/application.go | 6 +++ application/webhook.go | 106 +++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 application/webhook.go diff --git a/application/application.go b/application/application.go index ab0a1aa..89cb2ee 100644 --- a/application/application.go +++ b/application/application.go @@ -10,6 +10,12 @@ func init() { cmdApplicationDomain.AddCommand(cmdApplicationDomainDetach) Cmd.AddCommand(cmdApplicationDomain) + + cmdApplicationWebhook.AddCommand(cmdApplicationWebhookList) + cmdApplicationWebhook.AddCommand(cmdApplicationWebhookAdd) + cmdApplicationWebhook.AddCommand(cmdApplicationWebhookDelete) + + Cmd.AddCommand(cmdApplicationWebhook) } // Cmd application diff --git a/application/webhook.go b/application/webhook.go new file mode 100644 index 0000000..d46585b --- /dev/null +++ b/application/webhook.go @@ -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 `, +} + +var cmdApplicationWebhookList = &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: "List the webhooks of an app: sail application webhook list ", + Long: `List the webhooks of an app: sail application webhook list + 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 ", + Long: `Add a webhook to an application ; sail application webhook add + 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 ", + Long: `Delete a webhook to an application ; sail application webhook delete + 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())) +}