Passed
Pull Request — master (#11)
by eval
01:29
created

kms.*KMS.Encrypt   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 2
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
package kms
2
3
import (
4
	"context"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/kms"
7
8
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
9
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers"
10
)
11
12
// Encrypt executes `Encrypt` operation.
13
func (svc *KMS) Encrypt(ctx context.Context, r EncryptRequest) (*EncryptResult, error) {
14
	out, err := svc.RawEncrypt(ctx, r.ToInput())
15
	if err != nil {
16
		err = svc.errWrap(errors.ErrorData{
17
			Err:          err,
18
			AWSOperation: "Encrypt",
19
		})
20
		svc.Errorf(err.Error())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
21
		return nil, err
22
	}
23
	return NewEncryptResult(out), nil
24
}
25
26
// EncryptRequest has parameters for `Encrypt` operation.
27
type EncryptRequest struct {
28
	KeyID     string
29
	Plaintext []byte
30
31
	// optional
32
	EncryptionAlgorithm EncryptionAlgorithmSpec
33
	EncryptionContext   map[string]string
34
	GrantTokens         []string
35
}
36
37
func (r EncryptRequest) ToInput() *SDK.EncryptInput {
0 ignored issues
show
introduced by
exported method EncryptRequest.ToInput should have comment or be unexported
Loading history...
38
	in := &SDK.EncryptInput{}
39
40
	if r.KeyID != "" {
41
		in.KeyId = pointers.String(r.KeyID)
42
	}
43
	in.Plaintext = r.Plaintext
44
45
	in.EncryptionAlgorithm = SDK.EncryptionAlgorithmSpec(r.EncryptionAlgorithm)
46
	in.EncryptionContext = r.EncryptionContext
47
	in.GrantTokens = r.GrantTokens
48
	return in
49
}
50
51
type EncryptResult struct {
0 ignored issues
show
introduced by
exported type EncryptResult should have comment or be unexported
Loading history...
52
	CiphertextBlob      []byte
53
	EncryptionAlgorithm EncryptionAlgorithmSpec
54
	KeyID               string
55
}
56
57
func NewEncryptResult(output *SDK.EncryptResponse) *EncryptResult {
0 ignored issues
show
introduced by
exported function NewEncryptResult should have comment or be unexported
Loading history...
58
	r := &EncryptResult{}
59
	if output == nil {
60
		return r
61
	}
62
63
	r.CiphertextBlob = output.CiphertextBlob
64
	r.EncryptionAlgorithm = EncryptionAlgorithmSpec(output.EncryptionAlgorithm)
65
	if output.KeyId != nil {
66
		r.KeyID = *output.KeyId
67
	}
68
	return r
69
}
70