-
Notifications
You must be signed in to change notification settings - Fork 123
/
overrides.go
180 lines (168 loc) · 5.84 KB
/
overrides.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 2021 Upbound Inc.
*/
package config
import (
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
"github.com/upbound/upjet/pkg/config"
"github.com/upbound/upjet/pkg/types/comments"
"github.com/upbound/upjet/pkg/types/name"
"github.com/upbound/provider-aws/config/common"
)
// RegionAddition adds region to the spec of all resources except iam group which
// does not have a region notion.
func RegionAddition() config.ResourceOption {
return func(r *config.Resource) {
if r.ShortGroup == "iam" || r.ShortGroup == "opsworks" {
return
}
c := "Region is the region you'd like your resource to be created in.\n"
comment, err := comments.New(c, comments.WithTFTag("-"))
if err != nil {
panic(errors.Wrap(err, "cannot build comment for region"))
}
r.TerraformResource.Schema["region"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: comment.String(),
}
if r.MetaResource == nil {
return
}
for _, ex := range r.MetaResource.Examples {
defaultRegion := "us-west-1"
if err := ex.SetPathValue("region", defaultRegion); err != nil {
panic(err)
}
for k := range ex.Dependencies {
if strings.HasPrefix(k, "aws_iam") {
continue
}
if err := ex.Dependencies.SetPathValue(k, "region", defaultRegion); err != nil {
panic(err)
}
}
}
}
}
// TagsAllRemoval removes the tags_all field that is used only in tfstate to
// accumulate provider-wide default tags in TF, which is not something we support.
// So, we don't need it as a parameter while "tags" is already in place.
func TagsAllRemoval() config.ResourceOption {
return func(r *config.Resource) {
if t, ok := r.TerraformResource.Schema["tags_all"]; ok {
t.Computed = true
t.Optional = false
}
}
}
// IdentifierAssignedByAWS will work for all AWS types because even if the ID
// is assigned by user, we'll see it in the TF State ID.
// The resource-specific configurations should override this whenever possible.
func IdentifierAssignedByAWS() config.ResourceOption {
return func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
}
}
// NamePrefixRemoval makes sure we remove name_prefix from all since it is mostly
// for Terraform functionality.
func NamePrefixRemoval() config.ResourceOption {
return func(r *config.Resource) {
for _, f := range r.ExternalName.OmittedFields {
if f == "name_prefix" {
return
}
}
r.ExternalName.OmittedFields = append(r.ExternalName.OmittedFields, "name_prefix")
}
}
// KnownReferencers adds referencers for fields that are known and common among
// more than a few resources.
func KnownReferencers() config.ResourceOption { //nolint:gocyclo
return func(r *config.Resource) {
for k, s := range r.TerraformResource.Schema {
// We shouldn't add referencers for status fields and sensitive fields
// since they already have secret referencer.
if (s.Computed && !s.Optional) || s.Sensitive {
continue
}
switch {
case strings.HasSuffix(k, "role_arn"):
r.References[k] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/iam/v1beta1.Role",
Extractor: common.PathARNExtractor,
}
case strings.HasSuffix(k, "security_group_ids"):
r.References[k] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/ec2/v1beta1.SecurityGroup",
RefFieldName: name.NewFromSnake(strings.TrimSuffix(k, "s")).Camel + "Refs",
SelectorFieldName: name.NewFromSnake(strings.TrimSuffix(k, "s")).Camel + "Selector",
}
case r.ShortGroup == "glue" && k == "database_name":
r.References["database_name"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/glue/v1beta1.CatalogDatabase",
}
}
switch k {
case "vpc_id":
r.References["vpc_id"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/ec2/v1beta1.VPC",
}
case "subnet_ids":
r.References["subnet_ids"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/ec2/v1beta1.Subnet",
RefFieldName: "SubnetIDRefs",
SelectorFieldName: "SubnetIDSelector",
}
case "subnet_id":
r.References["subnet_id"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/ec2/v1beta1.Subnet",
}
case "iam_roles":
r.References["iam_roles"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/iam/v1beta1.Role",
RefFieldName: "IAMRoleRefs",
SelectorFieldName: "IAMRoleSelector",
}
case "security_group_id":
r.References["security_group_id"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/ec2/v1beta1.SecurityGroup",
}
case "kms_key_id":
r.References["kms_key_id"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/kms/v1beta1.Key",
}
case "kms_key_arn":
r.References["kms_key_arn"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/kms/v1beta1.Key",
}
case "kms_key":
r.References["kms_key"] = config.Reference{
Type: "github.com/upbound/provider-aws/apis/kms/v1beta1.Key",
}
}
}
}
}
// AddExternalTagsField adds ExternalTagsFieldName configuration for resources that have tags field.
func AddExternalTagsField() config.ResourceOption {
return func(r *config.Resource) {
if s, ok := r.TerraformResource.Schema["tags"]; ok && s.Type == schema.TypeMap {
r.InitializerFns = append(r.InitializerFns, config.TagInitializer)
}
}
}
// DocumentationForTags overrides the API documentation of the tags fields since
// it contains Terraform-specific feature call out.
func DocumentationForTags() config.ResourceOption {
return func(r *config.Resource) {
if r.MetaResource == nil {
return
}
if _, ok := r.MetaResource.ArgumentDocs["tags"]; ok {
r.MetaResource.ArgumentDocs["tags"] = "- (Optional) Key-value map of resource tags."
}
}
}