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

external name: use function in the config directly instead of generating the call #124

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/config/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2021 The Crossplane Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

// Common ExternalName configurations.
var (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@turkenh I just added those since they seem to be very common. For the plan of generating all automatically, I think we can use these in the providers, i.e. a for loop setting a new configuration for every resource and maybe it can check existence of name attribute in schema and decides which one to use.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, also we could expose this as a provider-level configuration (e.g. default external name config) that could still be overridden by specific resources. For example, for azure, we can just set provider.defaultExternalNameConfig = IdentifierFromProvider and done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but if we're going to have that for loop anyway, it could be simpler to set it every time so that we'd worry about only a single field in a single config object instead of checking the default if it doesn't exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can find a way not to have that loop :)

// NameAsIdentifier uses "name" field in the arguments as the identifier of
// the resource.
NameAsIdentifier = ExternalName{
SetIdentifierArgumentFn: func(base map[string]interface{}, name string) {
base["name"] = name
},
OmittedFields: []string{
"name",
"name_prefix",
turkenh marked this conversation as resolved.
Show resolved Hide resolved
},
}

// IdentifierFromProvider is used in resources whose identifier is assigned by
// the remote client, such as AWS VPC where it gets an identifier like
// vpc-2213das instead of letting user choose a name.
IdentifierFromProvider = ExternalName{
SetIdentifierArgumentFn: NopSetIdentifierArgument,
DisableNameInitializer: true,
}
)
14 changes: 7 additions & 7 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package config

import "github.com/imdario/mergo"

// SetNameAttributeFn sets the name of the resource in Terraform attributes map.
type SetNameAttributeFn func(base map[string]interface{}, name string)
// SetIdentifierArgumentFn sets the name of the resource in Terraform attributes map.
type SetIdentifierArgumentFn func(base map[string]interface{}, name string)

// NopSetNameAttribute does nothing. It's useful for cases where the external
// NopSetIdentifierArgument does nothing. It's useful for cases where the external
// name is calculated by provider and doesn't have any effect on spec fields.
func NopSetNameAttribute(_ map[string]interface{}, _ string) {}
func NopSetIdentifierArgument(_ map[string]interface{}, _ string) {}

// ResourceOption allows setting optional fields of a Resource object.
type ResourceOption func(*Resource)
Expand All @@ -43,7 +43,7 @@ func NewResource(version, kind, terraformResourceType string, opts ...ResourceOp
TerraformResourceType: terraformResourceType,
TerraformIDFieldName: "id",
ExternalName: ExternalName{
SetNameAttributeFn: NopSetNameAttribute,
SetIdentifierArgumentFn: NopSetIdentifierArgument,
},
}
for _, f := range opts {
Expand All @@ -56,9 +56,9 @@ func NewResource(version, kind, terraformResourceType string, opts ...ResourceOp
// such as removal of those fields from spec schema and calling Configure function
// to fill attributes with information given in external name.
type ExternalName struct {
// SetNameAttributeFn sets the name of the resource in Terraform attribute
// SetIdentifierArgumentFn sets the name of the resource in Terraform argument
// map.
SetNameAttributeFn SetNameAttributeFn
SetIdentifierArgumentFn SetIdentifierArgumentFn

// OmittedFields are the ones you'd like to be removed from the schema since
// they are specified via external name. You can omit only the top level fields.
Expand Down
4 changes: 2 additions & 2 deletions pkg/terraform/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func NewFileProducer(ctx context.Context, client resource.SecretClient, dir stri
return nil, errors.Wrap(err, "cannot get sensitive parameters")
}
// TODO(muvaf): Once we have automatic defaulting, remove this if check.
if fp.Config.ExternalName.SetNameAttributeFn != nil {
fp.Config.ExternalName.SetNameAttributeFn(params, meta.GetExternalName(tr))
if fp.Config.ExternalName.SetIdentifierArgumentFn != nil {
fp.Config.ExternalName.SetIdentifierArgumentFn(params, meta.GetExternalName(tr))
}
fp.parameters = params

Expand Down