-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass full state to GetExternalNameFn function to access field other than ID #316
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||
|
@@ -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) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||||||||||||||
} | ||||||||||||||||
oldName := meta.GetExternalName(mg) | ||||||||||||||||
// we have to make sure the newly set external-name is recorded | ||||||||||||||||
|
@@ -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 { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make sure we check any errors returned from
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||||||||||||||
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 | ||||||||||||||||
} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block would be moved:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be precise, we also need to make sure that the Terraform ID is not an empty string, i.e., it may be in the map but its value could be the empty string:
, with the assumption that if the
id
key exists, its value is a string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!