Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
config: fix some of the external name configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Muvaffak Onus <[email protected]>
  • Loading branch information
muvaf committed Nov 19, 2021
1 parent c2b019c commit 6c88364
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 89 deletions.
15 changes: 11 additions & 4 deletions config/autoscaling/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ import (
func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_autoscaling_group", func(r *config.Resource) {
r.Kind = "AutoscalingGroup"
r.ExternalName = config.NameAsIdentifier
r.References["vpc_zone_identifier"] = config.Reference{
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/ec2/v1alpha1.Subnet",
}
r.References["target_group_arns"] = config.Reference{
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/elasticloadbalancing/v1alpha1.TargetGroup",
}

r.UseAsync = true

// Managed by Attachment resource.
if s, ok := r.TerraformResource.Schema["load_balancers"]; ok {
s.Optional = false
s.Computed = true
}
if s, ok := r.TerraformResource.Schema["target_group_arns"]; ok {
s.Optional = false
s.Computed = true
}
})
p.AddResourceConfigurator("aws_autoscaling_attachment", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
Expand Down
19 changes: 19 additions & 0 deletions config/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ func ARNExtractor() reference.ExtractValueFn {
return r
}
}

// func GetExternalNameFromARN() config.GetExternalNameFn {
// return func(tfstate map[string]interface{}) (string, error) {
// arn, ok := tfstate["id"].(string)
// if !ok || arn == "" {
// return "", errors.New("cannot get id from tfstate")
// }
// // arn:partition:service:region:account-id:resource-id
// // arn:partition:service:region:account-id:resource-type/resource-id
// // arn:partition:service:region:account-id:resource-type:resource-id
// w := strings.Split(arn, ":")
// name := w[len(w)-1]
// if strings.Contains(name, "/") {
// ids := strings.Split(name, "/")
// return ids[len(ids)-1], nil
// }
// return name, nil
// }
// }
1 change: 0 additions & 1 deletion config/ebs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
// Configure adds configurations for ebs group.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_ebs_volume", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.References = map[string]config.Reference{
"kms_key_id": {
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/kms/v1alpha1.Key",
Expand Down
15 changes: 7 additions & 8 deletions config/ec2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_eip", func(r *config.Resource) {
r.Kind = "ElasticIP"
r.ExternalName = config.IdentifierFromProvider
r.References["instance"] = config.Reference{
Type: "Instance",
r.References = config.References{
"instance": config.Reference{
Type: "Instance",
},
"network_interface": config.Reference{
Type: "NetworkInterface",
},
}

r.UseAsync = true
})

Expand Down Expand Up @@ -126,11 +130,6 @@ func Configure(p *config.Provider) {
})

p.AddResourceConfigurator("aws_launch_template", func(r *config.Resource) {
// Note(turkenh): Kind as "LaunchTemplate" fails with:
// panic: cannot generate crd: cannot build types for LaunchTemplate:
// cannot build the types: cannot generate parameters type name of
// LaunchTemplate: could not generate a unique name for
// LaunchTemplateParameters
r.Kind = "LaunchTemplate"
r.ExternalName = config.IdentifierFromProvider
r.References["security_group_names"] = config.Reference{
Expand Down
1 change: 1 addition & 0 deletions config/ecr/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// Configure adds configurations for ecrs group.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_ecr_repository", func(r *config.Resource) {
r.ExternalName = config.NameAsIdentifier
r.References = map[string]config.Reference{
"encryption_configuration.kms_key": {
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/kms/v1alpha1.Key",
Expand Down
35 changes: 21 additions & 14 deletions config/ecs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ limitations under the License.
package ecs

import (
"context"
"path/filepath"
"strings"

"github.com/crossplane-contrib/terrajet/pkg/config"
"github.com/pkg/errors"

"github.com/crossplane-contrib/provider-jet-aws/config/common"
)

// Configure adds configurations for ecs group.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_ecs_cluster", func(r *config.Resource) {
// todo: use new ID operation overrides because ID is ARN.
r.ExternalName = config.IdentifierFromProvider
r.ExternalName = config.NameAsIdentifier
r.References = config.References{
"capacity_providers": config.Reference{
Type: "CapacityProvider",
Expand All @@ -43,6 +47,21 @@ func Configure(p *config.Provider) {

p.AddResourceConfigurator("aws_ecs_service", func(r *config.Resource) {
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetExternalNameFn = func(tfstate map[string]interface{}) (string, error) {
// cluster-name/service-name
w := strings.Split(tfstate["id"].(string), "/")
if len(w) != 2 {
return "", errors.New("external name should have the following format: cluster-name/service-name")
}
return w[len(w)-1], nil
}
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]interface{}, _ map[string]interface{}) (string, error) {
cl, ok := parameters["cluster"].(string)
if !ok {
return "", errors.New("cannot generate id without cluster paramater")
}
return filepath.Join(cl, externalName), nil
}
r.References = config.References{
"cluster": config.Reference{
Type: "Cluster",
Expand Down Expand Up @@ -76,18 +95,6 @@ func Configure(p *config.Provider) {
}
})

p.AddResourceConfigurator("aws_ecs_tag", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.References = config.References{
// Note(turkenh): This reference could correspond multiple types as
// per documentation, any ecs resource: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_tag#resource_arn
// However, we could only reference to one type.
"resource_arn": config.Reference{
Type: "Cluster",
Extractor: common.PathARNExtractor,
},
}
})
p.AddResourceConfigurator("aws_ecs_task_definition", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.References = config.References{
Expand Down
57 changes: 55 additions & 2 deletions config/eks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ limitations under the License.
package eks

import (
"context"
"fmt"
"strings"

"github.com/crossplane-contrib/terrajet/pkg/config"
"github.com/pkg/errors"

"github.com/crossplane-contrib/provider-jet-aws/config/common"
)

// Configure adds configurations for eks group.
func Configure(p *config.Provider) {
func Configure(p *config.Provider) { // nolint:gocyclo
p.AddResourceConfigurator("aws_eks_cluster", func(r *config.Resource) {
r.ExternalName = config.NameAsIdentifier
r.References = config.References{
"role_arn": {
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/iam/v1alpha1.Role",
Expand Down Expand Up @@ -52,6 +58,8 @@ func Configure(p *config.Provider) {
"node_group_name",
"node_group_name_prefix",
},
GetExternalNameFn: config.IDAsExternalName,
GetIDFn: config.ExternalNameAsID,
}
r.References = config.References{
"cluster_name": {
Expand Down Expand Up @@ -91,6 +99,25 @@ func Configure(p *config.Provider) {
OmittedFields: []string{
"fargate_profile_name",
},
GetIDFn: func(_ context.Context, externalName string, parameters map[string]interface{}, _ map[string]interface{}) (string, error) {
cl, ok := parameters["cluster_name"].(string)
if !ok || cl == "" {
return "", errors.New("cannot get cluster_name from parameters")
}
return fmt.Sprintf("%s:%s", cl, externalName), nil
},
GetExternalNameFn: func(tfstate map[string]interface{}) (string, error) {
id, ok := tfstate["id"].(string)
if !ok || id == "" {
return "", errors.New("cannot get id from tfstate")
}
// my_cluster:my_fargate_profile
w := strings.Split(id, ":")
if len(w) != 2 {
return "", errors.New("format of id should be my_cluster:my_fargate_profile")
}
return w[len(w)-1], nil
},
}

r.References = config.References{
Expand All @@ -109,7 +136,33 @@ func Configure(p *config.Provider) {
}
})
p.AddResourceConfigurator("aws_eks_addon", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.ExternalName = config.ExternalName{
SetIdentifierArgumentFn: func(base map[string]interface{}, externalName string) {
base["addon_name"] = externalName
},
OmittedFields: []string{
"addon_name",
},
GetIDFn: func(_ context.Context, externalName string, parameters map[string]interface{}, _ map[string]interface{}) (string, error) {
cl, ok := parameters["cluster_name"].(string)
if !ok || cl == "" {
return "", errors.New("cannot get cluster_name from parameters")
}
return fmt.Sprintf("%s:%s", cl, externalName), nil
},
GetExternalNameFn: func(tfstate map[string]interface{}) (string, error) {
id, ok := tfstate["id"].(string)
if !ok || id == "" {
return "", errors.New("cannot get id from tfstate")
}
// my_cluster:my_fargate_profile
w := strings.Split(id, ":")
if len(w) != 2 {
return "", errors.New("format of id should be my_cluster:my_fargate_profile")
}
return w[len(w)-1], nil
},
}
r.References = config.References{
"cluster_name": {
Type: "Cluster",
Expand Down
6 changes: 5 additions & 1 deletion config/elasticache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Configure adds configurations for elasticache group.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_elasticache_parameter_group", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.ExternalName = config.NameAsIdentifier
})

p.AddResourceConfigurator("aws_elasticache_cluster", func(r *config.Resource) {
Expand All @@ -34,6 +34,8 @@ func Configure(p *config.Provider) {
OmittedFields: []string{
"cluster_id",
},
GetExternalNameFn: config.IDAsExternalName,
GetIDFn: config.ExternalNameAsID,
}
r.References = config.References{
"parameter_group_name": config.Reference{
Expand All @@ -51,6 +53,8 @@ func Configure(p *config.Provider) {
OmittedFields: []string{
"replication_group_id",
},
GetExternalNameFn: config.IDAsExternalName,
GetIDFn: config.ExternalNameAsID,
}
})

Expand Down
7 changes: 7 additions & 0 deletions config/elasticloadbalancing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("aws_lb", func(r *config.Resource) {
r.Kind = "LoadBalancer"
r.ExternalName = config.IdentifierFromProvider
r.ExternalName.OmittedFields = append(r.ExternalName.OmittedFields, "name_prefix")
r.References = config.References{
"security_groups": {
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/ec2/v1alpha1.SecurityGroup",
Expand Down Expand Up @@ -64,11 +65,17 @@ func Configure(p *config.Provider) {

p.AddResourceConfigurator("aws_lb_target_group", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.ExternalName.OmittedFields = append(r.ExternalName.OmittedFields, "name_prefix")
r.References = config.References{
"vpc_id": {
Type: "github.com/crossplane-contrib/provider-jet-aws/apis/ec2/v1alpha1.VPC",
},
}
if s, ok := r.TerraformResource.Schema["name"]; ok {
s.Optional = false
s.ForceNew = true
s.Computed = false
}
})
p.AddResourceConfigurator("aws_lb_target_group_attachment", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
Expand Down
Loading

0 comments on commit 6c88364

Please sign in to comment.