forked from giantswarm/aws-operator
/
resource.go
97 lines (78 loc) · 1.97 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package kmskey
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/giantswarm/apiextensions/pkg/apis/provider/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/aws-operator/service/awsconfig/v5/key"
)
const (
// Name is the identifier of the resource.
Name = "kmskeyv5"
)
// Config represents the configuration used to create a new cloudformation resource.
type Config struct {
// Dependencies.
Clients Clients
Logger micrologger.Logger
}
// DefaultConfig provides a default configuration to create a new cloudformation
// resource by best effort.
func DefaultConfig() Config {
return Config{
// Dependencies.
Clients: Clients{},
Logger: nil,
}
}
// Resource implements the cloudformation resource.
type Resource struct {
// Dependencies.
awsClients Clients
logger micrologger.Logger
}
// New creates a new configured cloudformation resource.
func New(config Config) (*Resource, error) {
// Dependencies.
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "config.Logger must not be empty")
}
newService := &Resource{
// Dependencies.
awsClients: config.Clients,
logger: config.Logger.With(
"resource", Name,
),
}
return newService, nil
}
func (r *Resource) Name() string {
return Name
}
func toKMSKeyState(v interface{}) (KMSKeyState, error) {
if v == nil {
return KMSKeyState{}, nil
}
kmsKey, ok := v.(KMSKeyState)
if !ok {
return KMSKeyState{}, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", kmsKey, v)
}
return kmsKey, nil
}
func toAlias(keyID string) string {
return fmt.Sprintf("alias/%s", keyID)
}
func getKMSTags(customObject v1alpha1.AWSConfig) []*kms.Tag {
clusterTags := key.ClusterTags(customObject)
kmsTags := []*kms.Tag{}
for k, v := range clusterTags {
tag := &kms.Tag{
TagKey: aws.String(k),
TagValue: aws.String(v),
}
kmsTags = append(kmsTags, tag)
}
return kmsTags
}