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

Commit

Permalink
config.externalname: rename usages of name to externalName
Browse files Browse the repository at this point in the history
Signed-off-by: Muvaffak Onus <me@muvaf.com>
  • Loading branch information
muvaf committed Nov 17, 2021
1 parent e684934 commit 924507c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions pkg/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
SetIdentifierArgumentFn: func(base map[string]interface{}, name string) {
base["name"] = name
},
GetNameFn: IDAsName,
GetIDFn: NameAsID,
GetExternalNameFn: IDAsExternalName,
GetIDFn: ExternalNameAsID,
OmittedFields: []string{
"name",
"name_prefix",
Expand All @@ -44,8 +44,8 @@ var (
// vpc-2213das instead of letting user choose a name.
IdentifierFromProvider = ExternalName{
SetIdentifierArgumentFn: NopSetIdentifierArgument,
GetNameFn: IDAsName,
GetIDFn: NameAsID,
GetExternalNameFn: IDAsExternalName,
GetIDFn: ExternalNameAsID,
DisableNameInitializer: true,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestDefaultResource(t *testing.T) {
ignoreUnexported := []cmp.Option{
cmpopts.IgnoreFields(Sensitive{}, "fieldPaths", "AdditionalConnectionDetailsFn"),
cmpopts.IgnoreFields(LateInitializer{}, "ignoredCanonicalFieldPaths"),
cmpopts.IgnoreFields(ExternalName{}, "SetIdentifierArgumentFn", "GetNameFn", "GetIDFn"),
cmpopts.IgnoreFields(ExternalName{}, "SetIdentifierArgumentFn", "GetExternalNameFn", "GetIDFn"),
}

for name, tc := range cases {
Expand Down
22 changes: 11 additions & 11 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ import (

// SetIdentifierArgumentsFn sets the name of the resource in Terraform attributes map,
// i.e. Main HCL file.
type SetIdentifierArgumentsFn func(base map[string]interface{}, name string)
type SetIdentifierArgumentsFn func(base map[string]interface{}, externalName string)

// 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.
var NopSetIdentifierArgument SetIdentifierArgumentsFn = func(_ map[string]interface{}, _ string) {}

// GetIDFn returns the ID to be used in TF State file, i.e. "id" field in
// terraform.tfstate.
type GetIDFn func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string
type GetIDFn func(externalName string, parameters map[string]interface{}, providerConfig map[string]interface{}) string

// NameAsID returns the name to be used as ID in TF State file.
var NameAsID GetIDFn = func(name string, _ map[string]interface{}, _ map[string]interface{}) string {
return name
// ExternalNameAsID returns the name to be used as ID in TF State file.
var ExternalNameAsID GetIDFn = func(externalName string, _ map[string]interface{}, _ map[string]interface{}) string {
return externalName
}

// GetNameFn returns the external name extracted from the TF State.
type GetNameFn func(tfstate map[string]interface{}) string
// GetExternalNameFn returns the external name extracted from the TF State.
type GetExternalNameFn func(tfstate map[string]interface{}) string

// IDAsName returns the TF State ID as external name.
var IDAsName GetNameFn = func(tfstate map[string]interface{}) string {
// IDAsExternalName returns the TF State ID as external name.
var IDAsExternalName GetExternalNameFn = func(tfstate map[string]interface{}) string {
if id, ok := tfstate["id"].(string); ok {
return id
}
Expand Down Expand Up @@ -69,14 +69,14 @@ type ExternalName struct {
// name and assign it to that specific key for that resource type.
SetIdentifierArgumentFn SetIdentifierArgumentsFn

// GetNameFn returns the external name extracted from TF State. In most cases,
// GetExternalNameFn returns the external name extracted from TF State. In most cases,
// "id" field contains all the information you need. You'll need to extract
// the format that is decided for external name annotation to use.
// For example the following is an Azure resource ID:
// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1
// The function should return "mygroup1" so that it can be used to set external
// name if it was not set already.
GetNameFn GetNameFn
GetExternalNameFn GetExternalNameFn

// GetIDFn returns the string that will be used as "id" key in TF state. In
// many cases, external name format is the same as "id" but when it is not
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.
return managed.ExternalObservation{}, errors.Wrap(err, "cannot set observation")
}

lateInitedAnn, err := resource.LateInitializeAnnotations(tr, e.config.ExternalName.GetNameFn(attr), string(res.State.GetPrivateRaw()))
lateInitedAnn, err := resource.LateInitializeAnnotations(tr, e.config.ExternalName.GetExternalNameFn(attr), string(res.State.GetPrivateRaw()))
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot late initialize annotations")
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (e *external) Create(ctx context.Context, mg xpresource.Managed) (managed.E
}

// NOTE(muvaf): Only spec and metadata changes are saved after Create call.
_, err = resource.LateInitializeAnnotations(tr, e.config.ExternalName.GetNameFn(attr), string(res.State.GetPrivateRaw()))
_, err = resource.LateInitializeAnnotations(tr, e.config.ExternalName.GetExternalNameFn(attr), string(res.State.GetPrivateRaw()))
return managed.ExternalCreation{ConnectionDetails: conn}, errors.Wrap(err, "cannot late initialize annotations")
}

Expand Down

0 comments on commit 924507c

Please sign in to comment.