vault.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "context"
  15. vault "github.com/hashicorp/vault/api"
  16. )
  17. type MockNewRequestFn func(method, requestPath string) *vault.Request
  18. type MockRawRequestWithContextFn func(ctx context.Context, r *vault.Request) (*vault.Response, error)
  19. type MockSetTokenFn func(v string)
  20. type MockSetNamespaceFn func(namespace string)
  21. func NewMockNewRequestFn(req *vault.Request) MockNewRequestFn {
  22. return func(method, requestPath string) *vault.Request {
  23. return req
  24. }
  25. }
  26. // An RequestFn operates on the supplied Request. You might use an RequestFn to
  27. // test or update the contents of an Request.
  28. type RequestFn func(req *vault.Request) error
  29. func NewMockRawRequestWithContextFn(res *vault.Response, err error, ofn ...RequestFn) MockRawRequestWithContextFn {
  30. return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
  31. for _, fn := range ofn {
  32. if err := fn(r); err != nil {
  33. return res, err
  34. }
  35. }
  36. return res, err
  37. }
  38. }
  39. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  40. return func(v string) {
  41. for _, fn := range ofn {
  42. fn(v)
  43. }
  44. }
  45. }
  46. func NewSetNamespaceFn() MockSetNamespaceFn {
  47. return func(namespace string) {}
  48. }
  49. type VaultClient struct {
  50. MockNewRequest MockNewRequestFn
  51. MockRawRequestWithContext MockRawRequestWithContextFn
  52. MockSetToken MockSetTokenFn
  53. MockSetNamespace MockSetNamespaceFn
  54. }
  55. func (c *VaultClient) NewRequest(method, requestPath string) *vault.Request {
  56. return c.MockNewRequest(method, requestPath)
  57. }
  58. func (c *VaultClient) RawRequestWithContext(ctx context.Context, r *vault.Request) (*vault.Response, error) {
  59. return c.MockRawRequestWithContext(ctx, r)
  60. }
  61. func (c *VaultClient) SetToken(v string) {
  62. c.MockSetToken(v)
  63. }
  64. func (c *VaultClient) SetNamespace(namespace string) {
  65. c.MockSetNamespace(namespace)
  66. }