Skip to content

Commit

Permalink
add get-by-ids to Go wrapper (#620)
Browse files Browse the repository at this point in the history
## Type of change

- [ ] Bug fix
- [ ] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [x] Other

## Objective

Allow secret fetching by a list of IDs, rather than requesting multiple
secrets individually. Exposes the functionality from #150 in Go.

## Code changes

- **languages/go/secrets.go:** Add `GetByIDS` func.
- **languages/go/example/example.go:** Get secrets by ID. Added JSON
output for QA/ease-of-use.
  • Loading branch information
tangowithfoxtrot authored Feb 29, 2024
1 parent c3d809b commit e16cea0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions languages/go/example/example.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"encoding/json"
"fmt"
"log"
"os"

sdk "github.com/bitwarden/sdk/languages/go"
Expand Down Expand Up @@ -87,5 +89,28 @@ func main() {
panic(err)
}

secretIdentifiers, err := bitwardenClient.Secrets.List(organizationID.String())
if err != nil {
panic(err)
}

// Get secrets with a list of IDs
secretIDs := make([]string, len(secretIdentifiers.Data))
for i, identifier := range secretIdentifiers.Data {
secretIDs[i] = identifier.ID
}

secrets, err := bitwardenClient.Secrets.GetByIDS(secretIDs)
if err != nil {
log.Fatalf("Error getting secrets: %v", err)
}

jsonSecrets, err := json.MarshalIndent(secrets, "", " ")
if err != nil {
log.Fatalf("Error marshalling secrets to JSON: %v", err)
}

fmt.Println(string(jsonSecrets))

defer bitwardenClient.Close()
}
17 changes: 17 additions & 0 deletions languages/go/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type SecretsInterface interface {
Create(key, value, note string, organizationID string, projectIDs []string) (*SecretResponse, error)
List(organizationID string) (*SecretIdentifiersResponse, error)
Get(secretID string) (*SecretResponse, error)
GetByIDS(secretIDs []string) (*SecretsResponse, error)
Update(secretID string, key, value, note string, organizationID string, projectIDs []string) (*SecretResponse, error)
Delete(secretIDs []string) (*SecretsDeleteResponse, error)
}
Expand Down Expand Up @@ -76,6 +77,22 @@ func (s *Secrets) Get(id string) (*SecretResponse, error) {
return &response, nil
}

func (s *Secrets) GetByIDS(ids []string) (*SecretsResponse, error) {
command := Command{
Secrets: &SecretsCommand{
GetByIDS: &SecretsGetRequest{
IDS: ids,
},
},
}

var response SecretsResponse
if err := s.executeCommand(command, &response); err != nil {
return nil, err
}
return &response, nil
}

func (s *Secrets) Update(id string, key, value, note string, organizationID string, projectIDs []string) (*SecretResponse, error) {
command := Command{
Secrets: &SecretsCommand{
Expand Down

0 comments on commit e16cea0

Please sign in to comment.