fake.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "fmt"
  15. "github.com/aws/aws-sdk-go/aws"
  16. "github.com/aws/aws-sdk-go/aws/request"
  17. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  18. "github.com/google/go-cmp/cmp"
  19. )
  20. // Client implements the aws secretsmanager interface.
  21. type Client struct {
  22. ExecutionCounter int
  23. valFn map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)
  24. CreateSecretWithContextFn CreateSecretWithContextFn
  25. GetSecretValueWithContextFn GetSecretValueWithContextFn
  26. PutSecretValueWithContextFn PutSecretValueWithContextFn
  27. DescribeSecretWithContextFn DescribeSecretWithContextFn
  28. DeleteSecretWithContextFn DeleteSecretWithContextFn
  29. }
  30. type CreateSecretWithContextFn func(aws.Context, *awssm.CreateSecretInput, ...request.Option) (*awssm.CreateSecretOutput, error)
  31. type GetSecretValueWithContextFn func(aws.Context, *awssm.GetSecretValueInput, ...request.Option) (*awssm.GetSecretValueOutput, error)
  32. type PutSecretValueWithContextFn func(aws.Context, *awssm.PutSecretValueInput, ...request.Option) (*awssm.PutSecretValueOutput, error)
  33. type DescribeSecretWithContextFn func(aws.Context, *awssm.DescribeSecretInput, ...request.Option) (*awssm.DescribeSecretOutput, error)
  34. type DeleteSecretWithContextFn func(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (*awssm.DeleteSecretOutput, error)
  35. func (sm Client) CreateSecretWithContext(ctx aws.Context, input *awssm.CreateSecretInput, options ...request.Option) (*awssm.CreateSecretOutput, error) {
  36. return sm.CreateSecretWithContextFn(ctx, input, options...)
  37. }
  38. func NewCreateSecretWithContextFn(output *awssm.CreateSecretOutput, err error) CreateSecretWithContextFn {
  39. return func(ctx aws.Context, input *awssm.CreateSecretInput, options ...request.Option) (*awssm.CreateSecretOutput, error) {
  40. return output, err
  41. }
  42. }
  43. func (sm Client) DeleteSecretWithContext(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (*awssm.DeleteSecretOutput, error) {
  44. return sm.DeleteSecretWithContextFn(ctx, input, opts...)
  45. }
  46. func NewDeleteSecretWithContextFn(output *awssm.DeleteSecretOutput, err error) DeleteSecretWithContextFn {
  47. return func(ctx aws.Context, input *awssm.DeleteSecretInput, opts ...request.Option) (output *awssm.DeleteSecretOutput, err error) {
  48. return output, err
  49. }
  50. }
  51. func (sm Client) GetSecretValueWithContext(ctx aws.Context, input *awssm.GetSecretValueInput, options ...request.Option) (*awssm.GetSecretValueOutput, error) {
  52. return sm.GetSecretValueWithContextFn(ctx, input, options...)
  53. }
  54. func NewGetSecretValueWithContextFn(output *awssm.GetSecretValueOutput, err error) GetSecretValueWithContextFn {
  55. return func(aws.Context, *awssm.GetSecretValueInput, ...request.Option) (*awssm.GetSecretValueOutput, error) {
  56. return output, err
  57. }
  58. }
  59. func (sm Client) PutSecretValueWithContext(ctx aws.Context, input *awssm.PutSecretValueInput, options ...request.Option) (*awssm.PutSecretValueOutput, error) {
  60. return sm.PutSecretValueWithContextFn(ctx, input, options...)
  61. }
  62. func NewPutSecretValueWithContextFn(output *awssm.PutSecretValueOutput, err error) PutSecretValueWithContextFn {
  63. return func(aws.Context, *awssm.PutSecretValueInput, ...request.Option) (*awssm.PutSecretValueOutput, error) {
  64. return output, err
  65. }
  66. }
  67. func (sm Client) DescribeSecretWithContext(ctx aws.Context, input *awssm.DescribeSecretInput, options ...request.Option) (*awssm.DescribeSecretOutput, error) {
  68. return sm.DescribeSecretWithContextFn(ctx, input, options...)
  69. }
  70. func NewDescribeSecretWithContextFn(output *awssm.DescribeSecretOutput, err error) DescribeSecretWithContextFn {
  71. return func(aws.Context, *awssm.DescribeSecretInput, ...request.Option) (*awssm.DescribeSecretOutput, error) {
  72. return output, err
  73. }
  74. }
  75. // NewClient init a new fake client.
  76. func NewClient() *Client {
  77. return &Client{
  78. valFn: make(map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)),
  79. }
  80. }
  81. func (sm *Client) GetSecretValue(in *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  82. sm.ExecutionCounter++
  83. if entry, found := sm.valFn[sm.cacheKeyForInput(in)]; found {
  84. return entry(in)
  85. }
  86. return nil, fmt.Errorf("test case not found")
  87. }
  88. func (sm *Client) ListSecrets(*awssm.ListSecretsInput) (*awssm.ListSecretsOutput, error) {
  89. return nil, nil
  90. }
  91. func (sm *Client) cacheKeyForInput(in *awssm.GetSecretValueInput) string {
  92. var secretID, versionID string
  93. if in.SecretId != nil {
  94. secretID = *in.SecretId
  95. }
  96. if in.VersionId != nil {
  97. versionID = *in.VersionId
  98. }
  99. return fmt.Sprintf("%s#%s", secretID, versionID)
  100. }
  101. func (sm *Client) WithValue(in *awssm.GetSecretValueInput, val *awssm.GetSecretValueOutput, err error) {
  102. sm.valFn[sm.cacheKeyForInput(in)] = func(paramIn *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  103. if !cmp.Equal(paramIn, in) {
  104. return nil, fmt.Errorf("unexpected test argument")
  105. }
  106. return val, err
  107. }
  108. }