Skip to content

Commit

Permalink
Pass full state to GetExternalNameFn function to access field other t…
Browse files Browse the repository at this point in the history
…han ID

Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Dec 21, 2023
1 parent 807fb9d commit ac7c6a1
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pkg/controller/external_nofork.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (n *noForkExternal) Observe(ctx context.Context, mg xpresource.Managed) (ma
resource.SetUpToDateCondition(mg, noDiff)
}
// check for an external-name change
if nameChanged, err := n.setExternalName(mg, newState); err != nil {
if nameChanged, err := n.setExternalName(mg, stateValueMap); err != nil {
return managed.ExternalObservation{}, errors.Wrapf(err, "failed to set the external-name of the managed resource during observe")
} else {
specUpdateRequired = specUpdateRequired || nameChanged
Expand All @@ -543,15 +543,14 @@ func (n *noForkExternal) Observe(ctx context.Context, mg xpresource.Managed) (ma

// sets the external-name on the MR. Returns `true`
// if the external-name of the MR has changed.
func (n *noForkExternal) setExternalName(mg xpresource.Managed, newState *tf.InstanceState) (bool, error) {
if newState.ID == "" {
func (n *noForkExternal) setExternalName(mg xpresource.Managed, stateValueMap map[string]interface{}) (bool, error) {
id, ok := stateValueMap["id"]
if !ok {
return false, nil
}
newName, err := n.config.ExternalName.GetExternalNameFn(map[string]any{
"id": newState.ID,
})
newName, err := n.config.ExternalName.GetExternalNameFn(stateValueMap)
if err != nil {
return false, errors.Wrapf(err, "failed to get the external-name from ID: %s", newState.ID)
return false, errors.Wrapf(err, "failed to get the external-name from ID: %s", id)
}
oldName := meta.GetExternalName(mg)
// we have to make sure the newly set external-name is recorded
Expand Down Expand Up @@ -602,10 +601,10 @@ func (n *noForkExternal) Create(ctx context.Context, mg xpresource.Managed) (man
}
n.opTracker.SetTfState(newState)

if _, err := n.setExternalName(mg, newState); err != nil {
stateValueMap, err := n.fromInstanceStateToJSONMap(newState)
if _, err := n.setExternalName(mg, stateValueMap); err != nil {
return managed.ExternalCreation{}, errors.Wrapf(err, "failed to set the external-name of the managed resource during create")
}
stateValueMap, err := n.fromInstanceStateToJSONMap(newState)
if err != nil {
return managed.ExternalCreation{}, err
}
Expand Down

0 comments on commit ac7c6a1

Please sign in to comment.