Skip to content

Commit b7c78fc

Browse files
authored
Merge pull request #14 from bugout-dev/brood-resources
find user and main endpoints for Brood resources
2 parents 46f57f5 + 99677fb commit b7c78fc

File tree

8 files changed

+358
-3
lines changed

8 files changed

+358
-3
lines changed

cmd/bugout/brood/command.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010

1111
func PopulateBroodCommands(cmd *cobra.Command) {
1212
groupsCmd := CreateGroupsCommand()
13+
resourcesCmd := GenerateResourcesCommand()
1314
pingCmd := CreatePingCommand()
1415
userCmd := CreateUserCommand()
1516
versionCmd := CreateVersionCommand()
1617

17-
cmd.AddCommand(groupsCmd, pingCmd, userCmd, versionCmd)
18+
cmd.AddCommand(groupsCmd, resourcesCmd, pingCmd, userCmd, versionCmd)
1819
}
1920

2021
func CreatePingCommand() *cobra.Command {

cmd/bugout/brood/resources.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package broodcmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/bugout-dev/bugout-go/cmd/bugout/cmdutils"
10+
bugout "github.com/bugout-dev/bugout-go/pkg"
11+
)
12+
13+
func GenerateResourcesCommand() *cobra.Command {
14+
resourcesCmd := &cobra.Command{
15+
Use: "resources",
16+
Short: "Bugout resources operations",
17+
}
18+
19+
resourcesCreateCmd := GenerateResourceCreateCommand()
20+
resourcesDeleteCmd := GenerateResourceDeleteCommand()
21+
resourcesGetCmd := GenerateResourcesGetCommand()
22+
23+
resourcesCmd.AddCommand(resourcesCreateCmd, resourcesDeleteCmd, resourcesGetCmd)
24+
25+
return resourcesCmd
26+
}
27+
28+
func GenerateResourceCreateCommand() *cobra.Command {
29+
var token, applicationId string
30+
resourceCreateCmd := &cobra.Command{
31+
Use: "create [resource]",
32+
Short: "Create new resource",
33+
PreRunE: cmdutils.TokenArgPopulator,
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
if len(args) != 1 {
36+
return fmt.Errorf("One resource argument as json string must be specified")
37+
}
38+
var resourceRaw interface{}
39+
err := json.Unmarshal([]byte(args[0]), &resourceRaw)
40+
if err != nil {
41+
return err
42+
}
43+
44+
client, clientErr := bugout.ClientFromEnv()
45+
if clientErr != nil {
46+
return clientErr
47+
}
48+
49+
resource, err := client.Brood.CreateResource(token, applicationId, resourceRaw)
50+
if err != nil {
51+
return err
52+
}
53+
54+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resource)
55+
return encodeErr
56+
},
57+
}
58+
59+
resourceCreateCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
60+
resourceCreateCmd.Flags().StringVarP(&applicationId, "application_id", "a", "", "Application ID resource belongs to")
61+
62+
return resourceCreateCmd
63+
}
64+
65+
func GenerateResourceDeleteCommand() *cobra.Command {
66+
var token, resourceId string
67+
resourceDeleteCmd := &cobra.Command{
68+
Use: "delete",
69+
Short: "Delete resource",
70+
PreRunE: cmdutils.TokenArgPopulator,
71+
RunE: func(cmd *cobra.Command, args []string) error {
72+
client, clientErr := bugout.ClientFromEnv()
73+
if clientErr != nil {
74+
return clientErr
75+
}
76+
77+
resource, err := client.Brood.DeleteResource(token, resourceId)
78+
if err != nil {
79+
return nil
80+
}
81+
82+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resource)
83+
return encodeErr
84+
},
85+
}
86+
87+
resourceDeleteCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
88+
resourceDeleteCmd.Flags().StringVarP(&resourceId, "resource_id", "r", "", "Resource ID")
89+
90+
return resourceDeleteCmd
91+
}
92+
93+
func GenerateResourcesGetCommand() *cobra.Command {
94+
var token, applicationId string
95+
var queryParams map[string]string
96+
resourcesGetCmd := &cobra.Command{
97+
Use: "get",
98+
Short: "Get resources of application",
99+
PreRunE: cmdutils.TokenArgPopulator,
100+
RunE: func(cmd *cobra.Command, args []string) error {
101+
client, clientErr := bugout.ClientFromEnv()
102+
if clientErr != nil {
103+
return clientErr
104+
}
105+
106+
resources, err := client.Brood.GetResources(token, applicationId, queryParams)
107+
if err != nil {
108+
return nil
109+
}
110+
111+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resources)
112+
return encodeErr
113+
},
114+
}
115+
116+
resourcesGetCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
117+
resourcesGetCmd.Flags().StringVarP(&applicationId, "application_id", "a", "", "Application ID resource belongs to")
118+
resourcesGetCmd.Flags().StringToStringVarP(&queryParams, "params", "p", nil, "Optional query parameters to filter resources")
119+
120+
return resourcesGetCmd
121+
}

cmd/bugout/brood/user.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package broodcmd
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67

78
"github.com/spf13/cobra"
@@ -20,10 +21,11 @@ func CreateUserCommand() *cobra.Command {
2021
userLoginCmd := CreateUserLoginCommand()
2122
userTokensCmd := CreateUserTokensCommand()
2223
userGetCmd := CreateUserGetCommand()
24+
userFindCmd := CreateUserFindCommand()
2325
userVerifyCmd := CreateUserVerifyCommand()
2426
userChangePasswordCmd := CreateUserChangePasswordCommand()
2527

26-
userCmd.AddCommand(userCreateCmd, userLoginCmd, userTokensCmd, userGetCmd, userVerifyCmd, userChangePasswordCmd)
28+
userCmd.AddCommand(userCreateCmd, userLoginCmd, userTokensCmd, userGetCmd, userFindCmd, userVerifyCmd, userChangePasswordCmd)
2729

2830
return userCmd
2931
}
@@ -155,6 +157,58 @@ func CreateUserGetCommand() *cobra.Command {
155157
return userGetCmd
156158
}
157159

160+
func CreateUserFindCommand() *cobra.Command {
161+
var token, user_id, username, email, application_id string
162+
userFindCmd := &cobra.Command{
163+
Use: "find",
164+
Short: "Find user if exists",
165+
Args: func(cmd *cobra.Command, args []string) error {
166+
if user_id == "" && username == "" && email == "" && application_id == "" {
167+
return errors.New("Exactly one of --user_id or --username or --email or application_id must be specified")
168+
}
169+
170+
return nil
171+
},
172+
PreRunE: cmdutils.TokenArgPopulator,
173+
RunE: func(cmd *cobra.Command, args []string) error {
174+
client, err := bugout.ClientFromEnv()
175+
if err != nil {
176+
return err
177+
}
178+
179+
queryParams := make(map[string]string)
180+
if user_id != "" {
181+
queryParams["user_id"] = user_id
182+
}
183+
if username != "" {
184+
queryParams["username"] = username
185+
}
186+
if email != "" {
187+
queryParams["email"] = email
188+
}
189+
if application_id != "" {
190+
queryParams["application_id"] = application_id
191+
}
192+
193+
user, err := client.Brood.FindUser(token, queryParams)
194+
if err != nil {
195+
return err
196+
}
197+
198+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&user)
199+
return encodeErr
200+
},
201+
}
202+
203+
userFindCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
204+
userFindCmd.Flags().StringVarP(&user_id, "user_id", "i", "", "Bugout user ID")
205+
userFindCmd.Flags().StringVarP(&username, "username", "u", "", "User name")
206+
userFindCmd.Flags().StringVarP(&email, "email", "e", "", "User email address")
207+
userFindCmd.Flags().StringVarP(&application_id, "application_id", "a", "", "Application user belongs to")
208+
209+
return userFindCmd
210+
}
211+
158212
func CreateUserVerifyCommand() *cobra.Command {
159213
var token, code string
160214
userVerifyCmd := &cobra.Command{

pkg/brood/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type BroodCaller interface {
2222
GenerateToken(string, string) (string, error)
2323
AnnotateToken(token, tokenType, note string) (string, error)
2424
ListTokens(token string) (UserTokensList, error)
25+
FindUser(token string, queryParameters map[string]string) (User, error)
2526
GetUser(token string) (User, error)
2627
VerifyUser(token, code string) (User, error)
2728
ChangePassword(token, currentPassword, newPassword string) (User, error)
@@ -31,12 +32,16 @@ type BroodCaller interface {
3132
RenameGroup(token, groupID, name string) (Group, error)
3233
AddUserToGroup(token, groupID, username, role string) (UserGroup, error)
3334
RemoveUserFromGroup(token, groupID, username string) (UserGroup, error)
35+
CreateResource(token, applicationId string, resourceData interface{}) (Resource, error)
36+
GetResources(token, applicationId string, queryParameters map[string]string) (Resources, error)
37+
DeleteResource(token, resourceId string) (Resource, error)
3438
}
3539

3640
type BroodRoutes struct {
3741
Ping string
3842
Version string
3943
User string
44+
FindUser string
4045
Groups string
4146
Token string
4247
RevokeToken string
@@ -45,6 +50,7 @@ type BroodRoutes struct {
4550
ChangePassword string
4651
RequestReset string
4752
ConfirmReset string
53+
Resources string
4854
}
4955

5056
func RoutesFromURL(broodURL string) BroodRoutes {
@@ -54,13 +60,15 @@ func RoutesFromURL(broodURL string) BroodRoutes {
5460
Ping: fmt.Sprintf("%s/ping", cleanURL),
5561
Version: fmt.Sprintf("%s/version", cleanURL),
5662
User: fmt.Sprintf("%s/user", cleanURL),
63+
FindUser: fmt.Sprintf("%s/user/find", cleanURL),
5764
Groups: fmt.Sprintf("%s/groups", cleanURL),
5865
Token: fmt.Sprintf("%s/token", cleanURL),
5966
ListTokens: fmt.Sprintf("%s/tokens", cleanURL),
6067
ConfirmRegistration: fmt.Sprintf("%s/confirm", cleanURL),
6168
ChangePassword: fmt.Sprintf("%s/profile/password", cleanURL),
6269
RequestReset: fmt.Sprintf("%s/reset", cleanURL),
6370
ConfirmReset: fmt.Sprintf("%s/reset_password", cleanURL),
71+
Resources: fmt.Sprintf("%s/resources", cleanURL),
6472
}
6573
}
6674

pkg/brood/data.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,19 @@ type UserGroup struct {
4747
type UserGroupsList struct {
4848
Groups []UserGroup `json:"groups"`
4949
}
50+
51+
// Resources
52+
type Resource struct {
53+
Id string `json:"id"`
54+
ApplicationId string `json:"application_id"`
55+
ResourceData interface{} `json:"resource_data"`
56+
}
57+
58+
type Resources struct {
59+
Resources []Resource `json:"resources"`
60+
}
61+
62+
type resourceCreateRequest struct {
63+
ApplicationId string `json:"application_id"`
64+
ResourceData interface{} `json:"resource_data"`
65+
}

pkg/brood/resources.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package brood
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/bugout-dev/bugout-go/pkg/utils"
10+
)
11+
12+
func (client BroodClient) CreateResource(token, applicationId string, resourceData interface{}) (Resource, error) {
13+
resourcesRoute := client.Routes.Resources
14+
requestBody := resourceCreateRequest{
15+
ApplicationId: applicationId,
16+
ResourceData: resourceData,
17+
}
18+
requestBuffer := new(bytes.Buffer)
19+
encodeErr := json.NewEncoder(requestBuffer).Encode(requestBody)
20+
if encodeErr != nil {
21+
return Resource{}, encodeErr
22+
}
23+
request, requestErr := http.NewRequest("POST", resourcesRoute, requestBuffer)
24+
if requestErr != nil {
25+
return Resource{}, requestErr
26+
}
27+
request.Header.Add("Content-Type", "application/json")
28+
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
29+
request.Header.Add("Accept", "application/json")
30+
31+
response, responseErr := client.HTTPClient.Do(request)
32+
if responseErr != nil {
33+
return Resource{}, responseErr
34+
}
35+
defer response.Body.Close()
36+
37+
statusErr := utils.HTTPStatusCheck(response)
38+
if statusErr != nil {
39+
return Resource{}, statusErr
40+
}
41+
42+
var resource Resource
43+
decodeErr := json.NewDecoder(response.Body).Decode(&resource)
44+
return resource, decodeErr
45+
}
46+
47+
func (client BroodClient) GetResources(token, applicationId string, queryParameters map[string]string) (Resources, error) {
48+
resourcesRoute := client.Routes.Resources
49+
request, requestErr := http.NewRequest("GET", resourcesRoute, nil)
50+
if requestErr != nil {
51+
return Resources{}, requestErr
52+
}
53+
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
54+
request.Header.Add("Accept", "application/json")
55+
56+
query := request.URL.Query()
57+
query.Add("application_id", applicationId)
58+
for k, v := range queryParameters {
59+
query.Add(k, v)
60+
}
61+
request.URL.RawQuery = query.Encode()
62+
63+
response, responseErr := client.HTTPClient.Do(request)
64+
if responseErr != nil {
65+
return Resources{}, responseErr
66+
}
67+
defer response.Body.Close()
68+
69+
statusErr := utils.HTTPStatusCheck(response)
70+
if statusErr != nil {
71+
return Resources{}, statusErr
72+
}
73+
74+
var resources Resources
75+
decodeErr := json.NewDecoder(response.Body).Decode(&resources)
76+
return resources, decodeErr
77+
}
78+
79+
func (client BroodClient) DeleteResource(token, resourceId string) (Resource, error) {
80+
resourcesRoute := fmt.Sprintf("%s/%s", client.Routes.Resources, resourceId)
81+
request, requestErr := http.NewRequest("DELETE", resourcesRoute, nil)
82+
if requestErr != nil {
83+
return Resource{}, requestErr
84+
}
85+
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
86+
request.Header.Add("Accept", "application/json")
87+
88+
response, responseErr := client.HTTPClient.Do(request)
89+
if responseErr != nil {
90+
return Resource{}, responseErr
91+
}
92+
defer response.Body.Close()
93+
94+
statusErr := utils.HTTPStatusCheck(response)
95+
if statusErr != nil {
96+
return Resource{}, statusErr
97+
}
98+
99+
var resource Resource
100+
decodeErr := json.NewDecoder(response.Body).Decode(&resource)
101+
102+
return resource, decodeErr
103+
}

0 commit comments

Comments
 (0)