Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add wrapper functions for GET auth and mount #25499

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api/sys_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ import (
"github.com/mitchellh/mapstructure"
)

func (c *Sys) GetAuth(path string) (*AuthMount, error) {
return c.GetAuthWithContext(context.Background(), path)
}
Comment on lines +15 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this one if we have the one below? From my experience, usually we add the *WithContext and keep the one method without *WithContext for backwards compatibility, but since these are both new, I think we could get away with just having GetAuthWithContext.

With that said, the Vault conventions could be different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same question but kept it for consistency.


func (c *Sys) GetAuthWithContext(ctx context.Context, path string) (*AuthMount, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest(http.MethodGet, fmt.Sprintf("/v1/sys/auth/%s", path))

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
defer resp.Body.Close()

secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}

mount := AuthMount{}
err = mapstructure.Decode(secret.Data, &mount)
if err != nil {
return nil, err
}

return &mount, nil
}

func (c *Sys) ListAuth() (map[string]*AuthMount, error) {
return c.ListAuthWithContext(context.Background())
}
Expand Down
33 changes: 33 additions & 0 deletions api/sys_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@ import (
"github.com/mitchellh/mapstructure"
)

func (c *Sys) GetMount(path string) (*MountOutput, error) {
return c.GetMountWithContext(context.Background(), path)
}

func (c *Sys) GetMountWithContext(ctx context.Context, path string) (*MountOutput, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest(http.MethodGet, fmt.Sprintf("/v1/sys/mounts/%s", path))

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
defer resp.Body.Close()

secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}

mount := MountOutput{}
err = mapstructure.Decode(secret.Data, &mount)
if err != nil {
return nil, err
}

return &mount, nil
}

func (c *Sys) ListMounts() (map[string]*MountOutput, error) {
return c.ListMountsWithContext(context.Background())
}
Expand Down
3 changes: 3 additions & 0 deletions changelog/25499.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: Add wrapper functions for GET /sys/mounts/:path and GET /sys/auth/:path
```
Loading