diff --git a/cmd/bugout/brood/resources.go b/cmd/bugout/brood/resources.go index 0a96e49..4d9ffef 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() + resourceGetCmd := GenerateResourceGetCommand() resourcesGetCmd := GenerateResourcesGetCommand() resourceHoldersCmd := GenerateResourceHoldersCommand() - resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourcesGetCmd, resourceHoldersCmd) + resourcesCmd.AddCommand(resourcesCreateCmd, resourcesUpdateCmd, resourcesDeleteCmd, resourceGetCmd, resourcesGetCmd, resourceHoldersCmd) return resourcesCmd } @@ -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() 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"