Skip to content

Commit 6036326

Browse files
Akash-Patilmohammed786
authored andcommitted
Added hooks functionality
Updated minor changes
1 parent db3bf71 commit 6036326

9 files changed

Lines changed: 332 additions & 5 deletions

File tree

api/deployment.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,15 @@ func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
166166
}
167167
return siteInfo
168168
}
169+
170+
func CurrentPlan() error {
171+
sitesResp, err := GetSites()
172+
if err != nil {
173+
return err
174+
}
175+
if sitesResp.Productplan.Name == "free" {
176+
return errors.New("Please switch to an app which enables this feature or upgrade your plan from Free Plan.")
177+
178+
}
179+
return nil
180+
}

api/integration.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
"github.com/loginradius/lr-cli/request"
9+
)
10+
11+
type HooksResponse struct {
12+
Data []struct {
13+
ID string `json:"Id"`
14+
Appid int `json:"AppId"`
15+
Createddate time.Time `json:"CreatedDate"`
16+
Lastmodifieddate time.Time `json:"LastModifiedDate"`
17+
Targeturl string `json:"TargetUrl"`
18+
Event string `json:"Event"`
19+
Name string `json:"Name"`
20+
} `json:"Data"`
21+
}
22+
23+
func Hooks(method string, body string) (*HooksResponse, error) {
24+
hooks := conf.AdminConsoleAPIDomain + "/integrations/webhook?"
25+
resp, err := request.Rest(method, hooks, nil, body)
26+
if err != nil {
27+
return nil, err
28+
}
29+
var resultResp HooksResponse
30+
err = json.Unmarshal(resp, &resultResp)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return &resultResp, nil
35+
}
36+
37+
func CheckHookID(hookid string) (bool, error) {
38+
Hooks, err := Hooks(http.MethodGet, "")
39+
if err != nil {
40+
return false, err
41+
}
42+
for i := 0; i < len(Hooks.Data); i++ {
43+
if hookid == Hooks.Data[i].ID {
44+
return true, nil
45+
}
46+
}
47+
return false, nil
48+
}

cmd/add/add.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func NewaddCmd() *cobra.Command {
1818
Long: `This commmand acts as a base command for add subcommands`,
1919
}
2020

21+
hooksCmd := hooks.NewHooksCmd()
22+
cmd.AddCommand(hooksCmd)
23+
2124
siteCmd := site.NewSiteCmd()
2225
cmd.AddCommand(siteCmd)
2326

cmd/add/hooks/hooks.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package hooks
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/loginradius/lr-cli/api"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var Name string
14+
var Event string
15+
var eventOption string
16+
var TargetUrl string
17+
18+
func NewHooksCmd() *cobra.Command {
19+
cmd := &cobra.Command{
20+
Use: "hooks",
21+
Short: "Adds hooks",
22+
Long: heredoc.Doc(`
23+
This command adds webhooks which are configured to an App.
24+
`),
25+
Example: heredoc.Doc(`
26+
$ lr adds hooks
27+
28+
Webhook has been added.
29+
30+
`),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return addHooks()
33+
},
34+
}
35+
return cmd
36+
}
37+
38+
func addHooks() error {
39+
err := api.CurrentPlan()
40+
if err != nil {
41+
return err
42+
}
43+
checkInput := input()
44+
if !checkInput {
45+
fmt.Println("Please enter the input paramaters properly.")
46+
return nil
47+
}
48+
49+
err = add()
50+
if err != nil {
51+
return err
52+
}
53+
fmt.Println("Webhook has been added.")
54+
55+
return nil
56+
}
57+
58+
func input() bool {
59+
fmt.Printf("Enter Name: ")
60+
fmt.Scanf("%s", &Name)
61+
if Name == "" {
62+
fmt.Println("Name is a required entry")
63+
return false
64+
}
65+
event := map[string]string{
66+
"1": "Login",
67+
"2": "Register",
68+
"3": "ResetPassword",
69+
"4": "UpdateProfile",
70+
}
71+
72+
//Currently supports only Developer plan event options.
73+
fmt.Println("To select an Event, choose a correponding number from the following options: ")
74+
fmt.Println("1 - Login")
75+
fmt.Println("2 - Register")
76+
fmt.Println("3 - ResetPassword")
77+
fmt.Println("4 - UpdateProfile")
78+
fmt.Printf("Option: ")
79+
fmt.Scanf("%s", &eventOption)
80+
if eventOption == "" {
81+
fmt.Println("Event is a required entry")
82+
return false
83+
}
84+
Event = event[eventOption]
85+
86+
fmt.Printf("Enter TargetUrl: ")
87+
fmt.Scanf("%s", &TargetUrl)
88+
if TargetUrl == "" {
89+
fmt.Println("TargetUrl is a required entry")
90+
return false
91+
}
92+
return true
93+
94+
}
95+
96+
func add() error {
97+
body, _ := json.Marshal(map[string]string{
98+
"Event": Event,
99+
"Name": Name,
100+
"TargetUrl": TargetUrl,
101+
})
102+
_, err := api.Hooks(http.MethodPost, string(body))
103+
if err != nil {
104+
return err
105+
}
106+
return nil
107+
}

cmd/add/site/site.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ func NewSiteCmd() *cobra.Command {
4141
return cmd
4242
}
4343
func addSite() error {
44-
checkInput := input()
45-
if !checkInput {
46-
fmt.Println("Please enter the input paramaters properly.")
47-
return nil
48-
}
4944
checkPlan, err := plans()
5045
if err != nil {
5146
return err
@@ -54,6 +49,12 @@ func addSite() error {
5449
fmt.Println("Please upgrade your plan to add more sites. ")
5550
return nil
5651
}
52+
checkInput := input()
53+
if !checkInput {
54+
fmt.Println("Please enter the input paramaters properly.")
55+
return nil
56+
}
57+
5758
err = add()
5859
if err != nil {
5960
return err

cmd/delete/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package delete
33
import (
44
"github.com/loginradius/lr-cli/cmd/delete/account"
55
"github.com/loginradius/lr-cli/cmd/delete/domain"
6+
"github.com/loginradius/lr-cli/cmd/delete/hooks"
67
"github.com/loginradius/lr-cli/cmd/delete/schema"
78

89
"github.com/loginradius/lr-cli/cmd/delete/site"
@@ -19,6 +20,9 @@ func NewdeleteCmd() *cobra.Command {
1920
Long: `This commmand acts as a base command for delete subcommands`,
2021
}
2122

23+
hooksCmd := hooks.NewHooksCmd()
24+
cmd.AddCommand((hooksCmd))
25+
2226
siteCmd := site.NewSiteCmd()
2327
cmd.AddCommand((siteCmd))
2428

cmd/delete/hooks/hooks.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package hooks
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/loginradius/lr-cli/api"
11+
"github.com/loginradius/lr-cli/cmdutil"
12+
"github.com/loginradius/lr-cli/config"
13+
"github.com/loginradius/lr-cli/request"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
type Delete struct {
18+
Isdeleted bool `json:"isdeleted"`
19+
}
20+
21+
var hookid string
22+
var option string
23+
24+
func NewHooksCmd() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "hooks",
27+
Short: "deletes hooks",
28+
Long: heredoc.Doc(`
29+
This command deletes webhooks configured with an App.
30+
`),
31+
Example: heredoc.Doc(`
32+
$ lr delete hooks --hookid <hookid>
33+
(Y)
34+
35+
Webhook has been deleted.
36+
37+
`),
38+
RunE: func(cmd *cobra.Command, args []string) error {
39+
if hookid == "" {
40+
return &cmdutil.FlagError{Err: errors.New("`--hookid` is required argument")}
41+
}
42+
return deleteHooks()
43+
},
44+
}
45+
fl := cmd.Flags()
46+
fl.StringVarP(&hookid, "hookid", "i", "", "Hook Unique ID")
47+
return cmd
48+
}
49+
50+
func deleteHooks() error {
51+
err := api.CurrentPlan()
52+
if err != nil {
53+
return err
54+
}
55+
checkHookID, err := api.CheckHookID(hookid)
56+
if err != nil {
57+
return err
58+
}
59+
if !checkHookID {
60+
fmt.Println("Hook ID does not exist.")
61+
return nil
62+
}
63+
fmt.Printf("Are you sure you want to proceed ? Press Y to continue: ")
64+
fmt.Scanf("%s", &option)
65+
if option != "Y" {
66+
return nil
67+
}
68+
isDeleted, err := delete()
69+
if err != nil {
70+
return err
71+
}
72+
if isDeleted {
73+
fmt.Println("Webhook has been deleted.")
74+
} else {
75+
fmt.Println("Delete action failed.")
76+
}
77+
return nil
78+
}
79+
80+
func delete() (bool, error) {
81+
conf := config.GetInstance()
82+
delete := conf.AdminConsoleAPIDomain + "/integrations/webhook/" + hookid
83+
resp, err := request.Rest(http.MethodDelete, delete, nil, "")
84+
if err != nil {
85+
return false, err
86+
}
87+
var status Delete
88+
err = json.Unmarshal(resp, &status)
89+
if err != nil {
90+
return false, err
91+
}
92+
if status.Isdeleted == true {
93+
return true, nil
94+
}
95+
return false, nil
96+
97+
}

cmd/get/get.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/loginradius/lr-cli/cmd/get/account"
55
"github.com/loginradius/lr-cli/cmd/get/accountPassword"
66
"github.com/loginradius/lr-cli/cmd/get/config"
7+
"github.com/loginradius/lr-cli/cmd/get/hooks"
78
"github.com/loginradius/lr-cli/cmd/get/profiles"
89
"github.com/loginradius/lr-cli/cmd/get/schema"
910
"github.com/loginradius/lr-cli/cmd/get/site"
@@ -25,6 +26,9 @@ func NewGetCmd() *cobra.Command {
2526
Long: `This commmand acts as a base command for get subcommands`,
2627
}
2728

29+
hooksCmd := hooks.NewHooksCmd()
30+
cmd.AddCommand(hooksCmd)
31+
2832
siteCmd := site.NewSiteCmd()
2933
cmd.AddCommand(siteCmd)
3034

cmd/get/hooks/hooks.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package hooks
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/MakeNowJust/heredoc"
8+
"github.com/loginradius/lr-cli/api"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewHooksCmd() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "hooks",
15+
Short: "Gets hooks",
16+
Long: heredoc.Doc(`
17+
This command fetches the list of webhooks configured with an App.
18+
`),
19+
Example: heredoc.Doc(`
20+
$ lr get hooks
21+
22+
`),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
return getHooks()
25+
},
26+
}
27+
return cmd
28+
}
29+
30+
func getHooks() error {
31+
err := api.CurrentPlan()
32+
if err != nil {
33+
return err
34+
}
35+
Hooks, err := api.Hooks(http.MethodGet, "")
36+
if err != nil {
37+
return err
38+
}
39+
numberOfHooks := len(Hooks.Data)
40+
for i := 0; i < numberOfHooks; i++ {
41+
fmt.Println(i + 1)
42+
fmt.Println(" ID: ", Hooks.Data[i].ID)
43+
fmt.Println(" Name: ", Hooks.Data[i].Name)
44+
fmt.Println(" Event: ", Hooks.Data[i].Event)
45+
fmt.Println(" TargetUrl: ", Hooks.Data[i].Targeturl)
46+
if i != numberOfHooks-1 {
47+
fmt.Println("-----------------------------------------")
48+
}
49+
}
50+
return nil
51+
}

0 commit comments

Comments
 (0)