-
Notifications
You must be signed in to change notification settings - Fork 117
/
registry.go
185 lines (165 loc) · 6.49 KB
/
registry.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
181
182
183
184
185
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: CC0-1.0
package config
import (
"context"
// Note(ezgidemirel): we are importing this to embed provider schema document
_ "embed"
"github.com/crossplane/upjet/pkg/config"
"github.com/crossplane/upjet/pkg/registry/reference"
conversiontfjson "github.com/crossplane/upjet/pkg/types/conversion/tfjson"
tfjson "github.com/hashicorp/terraform-json"
fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/xpprovider"
"github.com/pkg/errors"
"github.com/upbound/provider-aws/hack"
)
var (
//go:embed schema.json
providerSchema string
//go:embed provider-metadata.yaml
providerMetadata []byte
//go:embed field-rename.yaml
fieldRename []byte
)
var skipList = []string{
"aws_waf_rule_group$", // Too big CRD schema
"aws_wafregional_rule_group$", // Too big CRD schema
"aws_mwaa_environment$", // See https://github.com/crossplane-contrib/terrajet/issues/100
"aws_ecs_tag$", // tags are already managed by ecs resources.
"aws_alb$", // identical with aws_lb
"aws_alb_target_group_attachment$", // identical with aws_lb_target_group_attachment
"aws_iam_policy_attachment$", // identical with aws_iam_*_policy_attachment resources.
"aws_iam_group_policy$", // identical with aws_iam_*_policy_attachment resources.
"aws_iam_user_policy$", // identical with aws_iam_*_policy_attachment resources.
"aws_alb$", // identical with aws_lb.
"aws_alb_listener$", // identical with aws_lb_listener.
"aws_alb_target_group$", // identical with aws_lb_target_group.
"aws_alb_target_group_attachment$", // identical with aws_lb_target_group_attachment.
"aws_iot_authorizer$", // failure with unknown reason.
"aws_location_map$", // failure with unknown reason.
"aws_appflow_connector_profile$", // failure with unknown reason.
"aws_rds_reserved_instance", // Expense of testing
}
// workaround for the TF AWS v4.67.0-based no-fork release: We would like to
// keep the types in the generated CRDs intact
// (prevent number->int type replacements).
func getProviderSchema(s string) (*schema.Provider, error) {
ps := tfjson.ProviderSchemas{}
if err := ps.UnmarshalJSON([]byte(s)); err != nil {
panic(err)
}
if len(ps.Schemas) != 1 {
return nil, errors.Errorf("there should exactly be 1 provider schema but there are %d", len(ps.Schemas))
}
var rs map[string]*tfjson.Schema
for _, v := range ps.Schemas {
rs = v.ResourceSchemas
break
}
return &schema.Provider{
ResourcesMap: conversiontfjson.GetV2ResourceMap(rs),
}, nil
}
// GetProvider returns the provider configuration.
// The `generationProvider` argument specifies whether the provider
// configuration is being read for the code generation pipelines.
// In that case, we will only use the JSON schema for generating
// the CRDs.
func GetProvider(ctx context.Context, generationProvider bool) (*config.Provider, error) {
var p *schema.Provider
var fwProvider fwprovider.Provider
var err error
if generationProvider {
p, err = getProviderSchema(providerSchema)
fwProvider, _, _ = xpprovider.GetProvider(ctx)
} else {
fwProvider, p, err = xpprovider.GetProvider(ctx)
}
if err != nil {
return nil, errors.Wrapf(err, "cannot get the Terraform provider schema with generation mode set to %t", generationProvider)
}
modulePath := "github.com/upbound/provider-aws"
pc := config.NewProvider([]byte(providerSchema), "aws",
modulePath, providerMetadata,
config.WithShortName("aws"),
config.WithRootGroup("aws.upbound.io"),
config.WithIncludeList(CLIReconciledResourceList()),
config.WithTerraformPluginSDKIncludeList(TerraformPluginSDKResourceList()),
config.WithTerraformPluginFrameworkIncludeList(TerraformPluginFrameworkResourceList()),
config.WithReferenceInjectors([]config.ReferenceInjector{reference.NewInjector(modulePath)}),
config.WithSkipList(skipList),
config.WithFeaturesPackage("internal/features"),
config.WithMainTemplate(hack.MainTemplate),
config.WithTerraformProvider(p),
config.WithTerraformPluginFrameworkProvider(fwProvider),
config.WithDefaultResourceOptions(
GroupKindOverrides(),
KindOverrides(),
RegionAddition(),
TagsAllRemoval(),
IdentifierAssignedByAWS(),
KnownReferencers(),
AddExternalTagsField(),
ResourceConfigurator(),
NamePrefixRemoval(),
DocumentationForTags(),
injectFieldRenamingConversionFunctions(),
),
)
pc.BasePackages.ControllerMap["internal/controller/eks/clusterauth"] = "eks"
for _, configure := range ProviderConfiguration {
configure(pc)
}
pc.ConfigureResources()
return pc, nil
}
// CLIReconciledResourceList returns the list of resources that have external
// name configured in ExternalNameConfigs table and to be reconciled under
// the TF CLI based architecture.
func CLIReconciledResourceList() []string {
l := make([]string, len(CLIReconciledExternalNameConfigs))
i := 0
for name := range CLIReconciledExternalNameConfigs {
// Expected format is regex, and we'd like to have exact matches.
l[i] = name + "$"
i++
}
return l
}
// TerraformPluginSDKResourceList returns the list of resources that have external
// name configured in ExternalNameConfigs table and to be reconciled under
// the no-fork architecture.
func TerraformPluginSDKResourceList() []string {
l := make([]string, len(TerraformPluginSDKExternalNameConfigs))
i := 0
for name := range TerraformPluginSDKExternalNameConfigs {
// Expected format is regex, and we'd like to have exact matches.
l[i] = name + "$"
i++
}
return l
}
func TerraformPluginFrameworkResourceList() []string {
l := make([]string, len(TerraformPluginFrameworkExternalNameConfigs))
i := 0
for name := range TerraformPluginFrameworkExternalNameConfigs {
// Expected format is regex, and we'd like to have exact matches.
l[i] = name + "$"
i++
}
return l
}
// Configure configures the specified Provider.
type Configure func(provider *config.Provider)
// Configurator is a registry for provider Configs.
type Configurator []Configure
// AddConfig adds a Config to the Configurator registry.
func (c *Configurator) AddConfig(conf Configure) {
*c = append(*c, conf)
}
// ProviderConfiguration is a global registry to be used by
// the resource providers to register their Config functions.
var ProviderConfiguration = Configurator{}