Skip to content

Commit

Permalink
Merge pull request #20 from bugout-dev/get-resource-by-id
Browse files Browse the repository at this point in the history
Get single resource
  • Loading branch information
kompotkot authored Aug 16, 2024
2 parents f855e27 + 8332d94 commit 5bcd581
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
35 changes: 32 additions & 3 deletions cmd/bugout/brood/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ func GenerateResourcesCommand() *cobra.Command {
resourcesCreateCmd := GenerateResourceCreateCommand()
resourcesUpdateCmd := GenerateResourceUpdateCommand()
resourcesDeleteCmd := GenerateResourceDeleteCommand()
resourceGetCmd := GenerateResourceGetCommand()
resourcesGetCmd := GenerateResourcesGetCommand()

resourceHoldersCmd := GenerateResourceHoldersCommand()

resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourcesGetCmd, resourceHoldersCmd)
resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourceGetCmd, resourcesGetCmd, resourceHoldersCmd)

return resourcesCmd
}
Expand Down Expand Up @@ -151,12 +152,40 @@ func GenerateResourceDeleteCommand() *cobra.Command {
return resourceDeleteCmd
}

func GenerateResourceGetCommand() *cobra.Command {
var token, resourceId string
resourcesGetCmd := &cobra.Command{
Use: "get",
Short: "Get resource of application",
PreRunE: cmdutils.TokenArgPopulator,
RunE: func(cmd *cobra.Command, args []string) error {
client, clientErr := bugout.ClientFromEnv()
if clientErr != nil {
return clientErr
}

resources, err := client.Brood.GetResource(token, resourceId)
if err != nil {
return nil
}

encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resources)
return encodeErr
},
}

resourcesGetCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
resourcesGetCmd.Flags().StringVarP(&resourceId, "resource_id", "r", "", "Resource ID")

return resourcesGetCmd
}

func GenerateResourcesGetCommand() *cobra.Command {
var token, applicationId string
var queryParams map[string]string
resourcesGetCmd := &cobra.Command{
Use: "get",
Short: "Get resources of application",
Use: "list",
Short: "List resources of application",
PreRunE: cmdutils.TokenArgPopulator,
RunE: func(cmd *cobra.Command, args []string) error {
client, clientErr := bugout.ClientFromEnv()
Expand Down
1 change: 1 addition & 0 deletions pkg/brood/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type BroodCaller interface {
RemoveUserFromGroup(token, groupID, username string) (UserGroup, error)
CreateResource(token, applicationId string, resourceData interface{}) (Resource, error)
UpdateResource(token, resourceId string, update interface{}, dropKeys []string) (Resource, error)
GetResource(token, resourceId string) (Resource, error)
GetResources(token, applicationId string, queryParameters map[string]string) (Resources, error)
DeleteResource(token, resourceId string) (Resource, error)
GetResourceHolders(token, resourceId string) (ResourceHolders, error)
Expand Down
25 changes: 25 additions & 0 deletions pkg/brood/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ func (client BroodClient) UpdateResource(token, resourceId string, update interf
return resource, decodeErr
}

func (client BroodClient) GetResource(token, resourceId string) (Resource, error) {
resourcesRoute := fmt.Sprintf("%s/%s", client.Routes.Resources, resourceId)
request, requestErr := http.NewRequest("GET", resourcesRoute, nil)
if requestErr != nil {
return Resource{}, requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
request.Header.Add("Accept", "application/json")

response, responseErr := client.HTTPClient.Do(request)
if responseErr != nil {
return Resource{}, responseErr
}
defer response.Body.Close()

statusErr := utils.HTTPStatusCheck(response)
if statusErr != nil {
return Resource{}, statusErr
}

var resource Resource
decodeErr := json.NewDecoder(response.Body).Decode(&resource)
return resource, decodeErr
}

func (client BroodClient) GetResources(token, applicationId string, queryParameters map[string]string) (Resources, error) {
resourcesRoute := client.Routes.Resources
request, requestErr := http.NewRequest("GET", resourcesRoute, nil)
Expand Down
4 changes: 3 additions & 1 deletion pkg/brood/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ func (client BroodClient) ListTokens(token string) (UserTokensList, error) {
return result, decodeErr
}

/* Find Brood user if exists
/*
Find Brood user if exists
query parameters:
- **user_id** (UUID): Brood user ID
- **username** (string): User name
Expand Down
2 changes: 1 addition & 1 deletion pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package bugout

const Version string = "0.4.3"
const Version string = "0.4.4"

0 comments on commit 5bcd581

Please sign in to comment.