Skip to content

Commit

Permalink
Add methods with context propagation to api (hashicorp#14070)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyakaznacheev committed Feb 23, 2022
1 parent 8ca7059 commit a91694e
Show file tree
Hide file tree
Showing 25 changed files with 102 additions and 96 deletions.
28 changes: 14 additions & 14 deletions api/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *TokenAuth) CreateWithContext(ctx context.Context, opts *TokenCreateRequ
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -49,7 +49,7 @@ func (c *TokenAuth) CreateOrphanWithContext(ctx context.Context, opts *TokenCrea
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -71,7 +71,7 @@ func (c *TokenAuth) CreateWithRoleWithContext(ctx context.Context, opts *TokenCr
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -95,7 +95,7 @@ func (c *TokenAuth) LookupWithContext(ctx context.Context, token string) (*Secre
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -119,7 +119,7 @@ func (c *TokenAuth) LookupAccessorWithContext(ctx context.Context, accessor stri
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -138,7 +138,7 @@ func (c *TokenAuth) LookupSelfWithContext(ctx context.Context) (*Secret, error)

r := c.c.NewRequest("GET", "/v1/auth/token/lookup-self")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -163,7 +163,7 @@ func (c *TokenAuth) RenewAccessorWithContext(ctx context.Context, accessor strin
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -188,7 +188,7 @@ func (c *TokenAuth) RenewWithContext(ctx context.Context, token string, incremen
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -212,7 +212,7 @@ func (c *TokenAuth) RenewSelfWithContext(ctx context.Context, increment int) (*S
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (c *TokenAuth) RenewTokenAsSelfWithContext(ctx context.Context, token strin
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -267,7 +267,7 @@ func (c *TokenAuth) RevokeAccessorWithContext(ctx context.Context, accessor stri
return err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return err
}
Expand All @@ -294,7 +294,7 @@ func (c *TokenAuth) RevokeOrphanWithContext(ctx context.Context, token string) e
return err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return err
}
Expand All @@ -317,7 +317,7 @@ func (c *TokenAuth) RevokeSelfWithContext(ctx context.Context, token string) err

r := c.c.NewRequest("PUT", "/v1/auth/token/revoke-self")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return err
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func (c *TokenAuth) RevokeTreeWithContext(ctx context.Context, token string) err
return err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,12 @@ func (c *Client) RawRequest(r *Request) (*Response, error) {
// a Vault server not configured with this client. This is an advanced operation
// that generally won't need to be called externally.
func (c *Client) RawRequestWithContext(ctx context.Context, r *Request) (*Response, error) {
ctx, cancelFunc := c.withConfiguredTimeout(ctx)
defer cancelFunc()
return c.rawRequestWithContext(ctx, r)
}

func (c *Client) rawRequestWithContext(ctx context.Context, r *Request) (*Response, error) {
c.modifyLock.RLock()
token := c.token

Expand Down
2 changes: 1 addition & 1 deletion api/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Client) HelpWithContext(ctx context.Context, path string) (*Help, error
r := c.NewRequest("GET", fmt.Sprintf("/v1/%s", path))
r.Params.Add("help", "1")

resp, err := c.RawRequestWithContext(ctx, r)
resp, err := c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions api/logical.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *Logical) ReadWithDataWithContext(ctx context.Context, path string, data
r.Params = values
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if resp != nil {
defer resp.Body.Close()
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *Logical) ListWithContext(ctx context.Context, path string) (*Secret, er
r.Method = "GET"
r.Params.Set("list", "true")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if resp != nil {
defer resp.Body.Close()
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *Logical) write(ctx context.Context, path string, request *Request) (*Se
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

resp, err := c.c.RawRequestWithContext(ctx, request)
resp, err := c.c.rawRequestWithContext(ctx, request)
if resp != nil {
defer resp.Body.Close()
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (c *Logical) DeleteWithDataWithContext(ctx context.Context, path string, da
r.Params = values
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if resp != nil {
defer resp.Body.Close()
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *Logical) UnwrapWithContext(ctx context.Context, wrappingToken string) (
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if resp != nil {
defer resp.Body.Close()
}
Expand Down
4 changes: 2 additions & 2 deletions api/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *SSH) CredentialWithContext(ctx context.Context, role string, data map[s
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -64,7 +64,7 @@ func (c *SSH) SignKeyWithContext(ctx context.Context, role string, data map[stri
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/ssh_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (c *SSHHelper) VerifyWithContext(ctx context.Context, otp string) (*SSHVeri
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions api/sys_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Sys) AuditHashWithContext(ctx context.Context, path string, input strin
return "", err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func (c *Sys) ListAuditWithContext(ctx context.Context) (map[string]*Audit, erro

r := c.c.NewRequest("GET", "/v1/sys/audit")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (c *Sys) EnableAuditWithOptionsWithContext(ctx context.Context, path string
return err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return err
}
Expand All @@ -126,7 +126,7 @@ func (c *Sys) DisableAuditWithContext(ctx context.Context, path string) error {

r := c.c.NewRequest("DELETE", fmt.Sprintf("/v1/sys/audit/%s", path))

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)

if err == nil {
defer resp.Body.Close()
Expand Down
6 changes: 3 additions & 3 deletions api/sys_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Sys) ListAuthWithContext(ctx context.Context) (map[string]*AuthMount, e

r := c.c.NewRequest("GET", "/v1/sys/auth")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func (c *Sys) EnableAuthWithOptionsWithContext(ctx context.Context, path string,
return err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return err
}
Expand All @@ -81,7 +81,7 @@ func (c *Sys) DisableAuthWithContext(ctx context.Context, path string) error {

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

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err == nil {
defer resp.Body.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion api/sys_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Sys) CapabilitiesWithContext(ctx context.Context, token, path string) (
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions api/sys_config_cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (c *Sys) CORSStatusWithContext(ctx context.Context) (*CORSResponse, error)

r := c.c.NewRequest("GET", "/v1/sys/config/cors")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (c *Sys) ConfigureCORSWithContext(ctx context.Context, req *CORSRequest) er
return err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err == nil {
defer resp.Body.Close()
}
Expand All @@ -70,7 +70,7 @@ func (c *Sys) DisableCORSWithContext(ctx context.Context) error {

r := c.c.NewRequest("DELETE", "/v1/sys/config/cors")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err == nil {
defer resp.Body.Close()
}
Expand Down
8 changes: 4 additions & 4 deletions api/sys_generate_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Sys) generateRootStatusCommonWithContext(ctx context.Context, path stri

r := c.c.NewRequest("GET", path)

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (c *Sys) generateRootInitCommonWithContext(ctx context.Context, path, otp,
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *Sys) generateRootCancelCommonWithContext(ctx context.Context, path stri

r := c.c.NewRequest("DELETE", path)

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err == nil {
defer resp.Body.Close()
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (c *Sys) generateRootUpdateCommonWithContext(ctx context.Context, path, sha
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sys_hastatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (c *Sys) HAStatusWithContext(ctx context.Context) (*HAStatusResponse, error

r := c.c.NewRequest("GET", "/v1/sys/ha-status")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sys_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (c *Sys) HealthWithContext(ctx context.Context) (*HealthResponse, error) {
r.Params.Add("drsecondarycode", "299")
r.Params.Add("performancestandbycode", "299")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/sys_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (c *Sys) InitStatusWithContext(ctx context.Context) (bool, error) {

r := c.c.NewRequest("GET", "/v1/sys/init")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return false, err
}
Expand All @@ -36,7 +36,7 @@ func (c *Sys) InitWithContext(ctx context.Context, opts *InitRequest) (*InitResp
return nil, err
}

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sys_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (c *Sys) LeaderWithContext(ctx context.Context) (*LeaderResponse, error) {

r := c.c.NewRequest("GET", "/v1/sys/leader")

resp, err := c.c.RawRequestWithContext(ctx, r)
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a91694e

Please sign in to comment.