From 16f63afa2cd7f397ec0075243e3e11cb9e9d473a Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 16 Aug 2024 21:10:25 +0000 Subject: [PATCH 1/2] Get single resource --- cmd/bugout/brood/resources.go | 39 ++++++++++++++++++++++++++++++----- pkg/brood/client.go | 1 + pkg/brood/resources.go | 25 ++++++++++++++++++++++ pkg/brood/user.go | 4 +++- pkg/version.go | 2 +- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/cmd/bugout/brood/resources.go b/cmd/bugout/brood/resources.go index 0a96e49..55e9dc1 100644 --- a/cmd/bugout/brood/resources.go +++ b/cmd/bugout/brood/resources.go @@ -20,11 +20,12 @@ func GenerateResourcesCommand() *cobra.Command { resourcesCreateCmd := GenerateResourceCreateCommand() resourcesUpdateCmd := GenerateResourceUpdateCommand() resourcesDeleteCmd := GenerateResourceDeleteCommand() - resourcesGetCmd := GenerateResourcesGetCommand() + resourceGetCmd := GenerateResourceGetCommand() + resourcesListCmd := GenerateResourcesListCommand() resourceHoldersCmd := GenerateResourceHoldersCommand() - resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourcesGetCmd, resourceHoldersCmd) + resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourceGetCmd, resourcesListCmd, resourceHoldersCmd) return resourcesCmd } @@ -151,12 +152,40 @@ func GenerateResourceDeleteCommand() *cobra.Command { return resourceDeleteCmd } -func GenerateResourcesGetCommand() *cobra.Command { +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 GenerateResourcesListCommand() *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() diff --git a/pkg/brood/client.go b/pkg/brood/client.go index a003791..c3eae3f 100644 --- a/pkg/brood/client.go +++ b/pkg/brood/client.go @@ -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) diff --git a/pkg/brood/resources.go b/pkg/brood/resources.go index 1404fec..ab042ae 100644 --- a/pkg/brood/resources.go +++ b/pkg/brood/resources.go @@ -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) diff --git a/pkg/brood/user.go b/pkg/brood/user.go index d8fb82b..28e37b5 100644 --- a/pkg/brood/user.go +++ b/pkg/brood/user.go @@ -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 diff --git a/pkg/version.go b/pkg/version.go index 00894c0..9d30248 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -1,3 +1,3 @@ package bugout -const Version string = "0.4.3" +const Version string = "0.4.4" From 8332d94a48f5769d2abacb9d98ed25efd97bba15 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 16 Aug 2024 21:12:04 +0000 Subject: [PATCH 2/2] Save previous func names --- cmd/bugout/brood/resources.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/bugout/brood/resources.go b/cmd/bugout/brood/resources.go index 55e9dc1..4d9ffef 100644 --- a/cmd/bugout/brood/resources.go +++ b/cmd/bugout/brood/resources.go @@ -21,11 +21,11 @@ func GenerateResourcesCommand() *cobra.Command { resourcesUpdateCmd := GenerateResourceUpdateCommand() resourcesDeleteCmd := GenerateResourceDeleteCommand() resourceGetCmd := GenerateResourceGetCommand() - resourcesListCmd := GenerateResourcesListCommand() + resourcesGetCmd := GenerateResourcesGetCommand() resourceHoldersCmd := GenerateResourceHoldersCommand() - resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourceGetCmd, resourcesListCmd, resourceHoldersCmd) + resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourceGetCmd, resourcesGetCmd, resourceHoldersCmd) return resourcesCmd } @@ -180,7 +180,7 @@ func GenerateResourceGetCommand() *cobra.Command { return resourcesGetCmd } -func GenerateResourcesListCommand() *cobra.Command { +func GenerateResourcesGetCommand() *cobra.Command { var token, applicationId string var queryParams map[string]string resourcesGetCmd := &cobra.Command{